Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/tag/coursetagslib.php
Go to the documentation of this file.
00001 <?php
00007 require_once $CFG->dirroot.'/tag/lib.php';
00008 
00021 function coursetag_get_tags($courseid, $userid=0, $tagtype='', $numtags=0, $sort='name') {
00022 
00023     global $CFG, $DB;
00024 
00025     // get visible course ids
00026     $courselist = array();
00027     if ($courseid === 0) {
00028         if ($courses = $DB->get_records_select('course', 'visible=1 AND category>0', null, '', 'id')) {
00029             foreach ($courses as $key => $value) {
00030                 $courselist[] = $key;
00031             }
00032         }
00033     }
00034 
00035     // get tags from the db ordered by highest count first
00036     $params = array();
00037     $sql = "SELECT id as tkey, name, id, tagtype, rawname, f.timemodified, flag, count
00038               FROM {tag} t,
00039                  (SELECT tagid, MAX(timemodified) as timemodified, COUNT(id) as count
00040                     FROM {tag_instance}
00041                    WHERE itemtype = 'course' ";
00042 
00043     if ($courseid > 0) {
00044         $sql .= "    AND itemid = :courseid ";
00045         $params['courseid'] = $courseid;
00046     } else {
00047         if (!empty($courselist)) {
00048             list($usql, $uparams) = $DB->get_in_or_equal($courselist, SQL_PARAMS_NAMED);
00049             $sql .= "AND itemid $usql ";
00050             $params = $params + $uparams;
00051         }
00052     }
00053 
00054     if ($userid > 0) {
00055         $sql .= "    AND tiuserid = :userid ";
00056         $params['userid'] = $userid;
00057     }
00058 
00059     $sql .= "   GROUP BY tagid) f
00060              WHERE t.id = f.tagid ";
00061     if ($tagtype != '') {
00062         $sql .= "AND tagtype = :tagtype ";
00063         $params['tagtype'] = $tagtype;
00064     }
00065     $sql .= "ORDER BY count DESC, name ASC";
00066 
00067     // limit the number of tags for output
00068     if ($numtags == 0) {
00069         $tags = $DB->get_records_sql($sql, $params);
00070     } else {
00071         $tags = $DB->get_records_sql($sql, $params, 0, $numtags);
00072     }
00073 
00074     // prepare the return
00075     $return = array();
00076     if ($tags) {
00077         // sort the tag display order
00078         if ($sort != 'popularity') {
00079             $CFG->tagsort = $sort;
00080             usort($tags, "coursetag_sort");
00081         }
00082         // avoid print_tag_cloud()'s ksort upsetting ordering by setting the key here
00083         foreach ($tags as $value) {
00084             $return[] = $value;
00085         }
00086     }
00087 
00088     return $return;
00089 
00090 }
00091 
00101 function coursetag_get_all_tags($sort='name', $numtags=0) {
00102 
00103     global $CFG, $DB;
00104 
00105     // note that this selects all tags except for courses that are not visible
00106     $sql = "SELECT id, name, id, tagtype, rawname, f.timemodified, flag, count
00107         FROM {tag} t,
00108         (SELECT tagid, MAX(timemodified) as timemodified, COUNT(id) as count
00109             FROM {tag_instance} WHERE tagid NOT IN
00110                 (SELECT tagid FROM {tag_instance} ti, {course} c
00111                 WHERE c.visible = 0
00112                 AND ti.itemtype = 'course'
00113                 AND ti.itemid = c.id)
00114         GROUP BY tagid) f
00115         WHERE t.id = f.tagid
00116         ORDER BY count DESC, name ASC";
00117     if ($numtags == 0) {
00118         $tags = $DB->get_records_sql($sql);
00119     } else {
00120         $tags = $DB->get_records_sql($sql, null, 0, $numtags);
00121     }
00122 
00123     $return = array();
00124     if ($tags) {
00125         if ($sort != 'popularity') {
00126             $CFG->tagsort = $sort;
00127             usort($tags, "coursetag_sort");
00128         }
00129         foreach ($tags as $value) {
00130             $return[] = $value;
00131         }
00132     }
00133 
00134     return $return;
00135 }
00136 
00141 function coursetag_sort($a, $b) {
00142     // originally from block_blog_tags
00143     global $CFG;
00144 
00145     // set up the variable $tagsort as either 'name' or 'timemodified' only, 'popularity' does not need sorting
00146     if (empty($CFG->tagsort)) {
00147         $tagsort = 'name';
00148     } else {
00149         $tagsort = $CFG->tagsort;
00150     }
00151 
00152     if (is_numeric($a->$tagsort)) {
00153         return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
00154     } elseif (is_string($a->$tagsort)) {
00155         return strcmp($a->$tagsort, $b->$tagsort);
00156     } else {
00157         return 0;
00158     }
00159 }
00160 
00169 function coursetag_print_cloud($tagcloud, $return=false, $max_size=180, $min_size=80) {
00170 
00171     global $CFG;
00172 
00173     if (empty($tagcloud)) {
00174         return;
00175     }
00176 
00177     ksort($tagcloud);
00178 
00179     $count = array();
00180     foreach ($tagcloud as $key => $value) {
00181         if(!empty($value->count)) {
00182             $count[$key] = log10($value->count);
00183         } else {
00184             $count[$key] = 0;
00185         }
00186     }
00187 
00188     $max = max($count);
00189     $min = min($count);
00190 
00191     $spread = $max - $min;
00192     if (0 == $spread) { // we don't want to divide by zero
00193         $spread = 1;
00194     }
00195 
00196     $step = ($max_size - $min_size)/($spread);
00197 
00198     $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
00199     $can_manage_tags = has_capability('moodle/tag:manage', $systemcontext);
00200 
00201     //prints the tag cloud
00202     $output = '<ul class="tag-cloud inline-list">';
00203     foreach ($tagcloud as $key => $tag) {
00204 
00205         $size = $min_size + ((log10($tag->count) - $min) * $step);
00206         $size = ceil($size);
00207 
00208         $style = 'style="font-size: '.$size.'%"';
00209 
00210         if ($tag->count > 1) {
00211             $title = 'title="'.s(get_string('thingstaggedwith','tag', $tag)).'"';
00212         } else {
00213             $title = 'title="'.s(get_string('thingtaggedwith','tag', $tag)).'"';
00214         }
00215 
00216         $href = 'href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'"';
00217 
00218         //highlight tags that have been flagged as inappropriate for those who can manage them
00219         $tagname = tag_display_name($tag);
00220         if ($tag->flag > 0 && $can_manage_tags) {
00221             $tagname =  '<span class="flagged-tag">' . tag_display_name($tag) . '</span>';
00222         }
00223 
00224         $tag_link = '<li><a '.$href.' '.$title.' '. $style .'>'.$tagname.'</a></li> ';
00225 
00226         $output .= $tag_link;
00227 
00228     }
00229     $output .= '</ul>'."\n";
00230 
00231     if ($return) {
00232         return $output;
00233     } else {
00234         echo $output;
00235     }
00236 }
00237 
00243 function coursetag_get_jscript($coursetagdivs = '') {
00244     global $CFG, $DB, $PAGE;
00245 
00246     $PAGE->requires->js('/tag/tag.js');
00247     $PAGE->requires->strings_for_js(array('jserror1', 'jserror2'), 'block_tags');
00248 
00249     if ($coursetagdivs) {
00250         $PAGE->requires->js_function_call('set_course_tag_divs', $coursetagdivs);
00251     }
00252 
00253     if ($coursetags = $DB->get_records('tag', null, 'name ASC', 'name, id')) {
00254         foreach ($coursetags as $key => $value) {
00255             $PAGE->requires->js_function_call('set_course_tag', array($key));
00256         }
00257     }
00258 
00259     $PAGE->requires->js('/blocks/tags/coursetags.js');
00260 
00261     return '';
00262 }
00263 
00267 function coursetag_get_jscript_links($elementid, $coursetagslinks) {
00268     global $PAGE;
00269 
00270     if (!empty($coursetagslinks)) {
00271         foreach ($coursetagslinks as $a) {
00272             $PAGE->requires->js_function_call('add_tag_footer_link', array($elementid, $a['title'], $a['onclick'], $a['text']), true);
00273         }
00274     }
00275 
00276     return '';
00277 }
00278 
00286 function coursetag_get_records($courseid, $userid) {
00287 
00288     global $CFG, $DB;
00289 
00290     $sql = "SELECT t.id, name, rawname
00291               FROM {tag} t, {tag_instance} ti
00292              WHERE t.id = ti.tagid
00293                  AND ti.tiuserid = :userid
00294                  AND ti.itemid = :courseid
00295           ORDER BY name ASC";
00296 
00297     return $DB->get_records_sql($sql, array('userid'=>$userid, 'courseid'=>$courseid));
00298 }
00299 
00310 function coursetag_store_keywords($tags, $courseid, $userid=0, $tagtype='official', $myurl='') {
00311 
00312     global $CFG;
00313 
00314     if (is_array($tags) and !empty($tags)) {
00315         foreach($tags as $tag) {
00316             $tag = trim($tag);
00317             if (strlen($tag) > 0) {
00318                 //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags
00319 
00320                 //add tag if does not exist
00321                 if (!$tagid = tag_get_id($tag)) {
00322                     $tag_id_array = tag_add(array($tag), $tagtype);
00323                     $tagid = $tag_id_array[moodle_strtolower($tag)];
00324                 }
00325                 //ordering
00326                 $ordering = 0;
00327                 if ($current_ids = tag_get_tags_ids('course', $courseid)) {
00328                     end($current_ids);
00329                     $ordering = key($current_ids) + 1;
00330                 }
00331                 //set type
00332                 tag_type_set($tagid, $tagtype);
00333 
00334                 //tag_instance entry
00335                 tag_assign('course', $courseid, $tagid, $ordering, $userid);
00336 
00337                 //logging - note only for user added tags
00338                 if ($tagtype == 'default' and $myurl != '') {
00339                     $url = $myurl.'?query='.urlencode($tag);
00340                     add_to_log($courseid, 'coursetags', 'add', $url, 'Course tagged');
00341                 }
00342             }
00343         }
00344     }
00345 
00346 }
00347 
00356 function coursetag_delete_keyword($tagid, $userid, $courseid) {
00357 
00358     global $CFG, $DB;
00359 
00360     $sql = "SELECT COUNT(*)
00361         FROM {tag_instance}
00362         WHERE tagid = $tagid
00363         AND tiuserid = $userid
00364         AND itemtype = 'course'
00365         AND itemid = $courseid";
00366     if ($DB->count_records_sql($sql) == 1) {
00367         $sql = "tagid = $tagid
00368             AND tiuserid = $userid
00369             AND itemtype = 'course'
00370             AND itemid = $courseid";
00371         $DB->delete_records_select('tag_instance', $sql);
00372         // if there are no other instances of the tag then consider deleting the tag as well
00373         if (!$DB->record_exists('tag_instance', array('tagid' => $tagid))) {
00374             // if the tag is a personal tag then delete it - don't do official tags
00375             if ($DB->record_exists('tag', array('id' => $tagid, 'tagtype' => 'default'))) {
00376                 $DB->delete_records('tag', array('id' => $tagid, 'tagtype' => 'default'));
00377             }
00378         }
00379     } else {
00380         print_error("errordeleting", 'tag', '', $tagid);
00381     }
00382 
00383 }
00384 
00391 function coursetag_get_tagged_courses($tagid) {
00392 
00393     global $DB;
00394 
00395     $courses = array();
00396     if ($crs = $DB->get_records_select('tag_instance', "tagid=:tagid AND itemtype='course'", array('tagid'=>$tagid))) {
00397         foreach ($crs as $c) {
00398             //this capability check was introduced to stop display of courses that a student could not
00399             //view, but arguably it is best that when clicking on a tag, the tagged course summary should
00400             //be seen and then if the student clicks on that they will be given the opportunity to join
00401             //note courses not visible should not have their tagid sent to this function
00402             // $context = get_context_instance(CONTEXT_COURSE, $c->itemid);
00403             //if (is_enrolled($context) oe is_viewing($context)) {
00404                 $course = $DB->get_record('course', array('id'=>$c->itemid));
00405                 $courses[$c->itemid] = $course;
00406             //}
00407         }
00408     }
00409     return $courses;
00410 
00411 }
00412 
00420 function coursetag_delete_course_tags($courseid, $showfeedback=false) {
00421 
00422     global $DB, $OUTPUT;
00423 
00424     if ($tags = $DB->get_records_select('tag_instance', "itemtype='course' AND itemid=:courseid", array('courseid'=>$courseid))) {
00425         foreach ($tags as $tag) {
00426             //delete the course tag instance record
00427             $DB->delete_records('tag_instance', array('tagid'=>$tag->tagid, 'itemtype'=>'course', 'itemid'=> $courseid));
00428             // delete tag if there are no other tag_instance entries now
00429             if (!($DB->record_exists('tag_instance', array('tagid'=>$tag->tagid)))) {
00430                 $DB->delete_records('tag', array('id'=>$tag->tagid));
00431                 // Delete files
00432                 $fs = get_file_storage();
00433                 $fs->delete_area_files(get_system_context()->id, 'tag', 'description', $tag->tagid);
00434             }
00435         }
00436     }
00437 
00438     if ($showfeedback) {
00439         echo $OUTPUT->notification(get_string('deletedcoursetags', 'tag'), 'notifysuccess');
00440     }
00441 }
00442 
00452 /*
00453 function coursetag_rss_feeds() {
00454 
00455     global $CFG, $DB;
00456     require_once($CFG->dirroot.'/lib/dmllib.php');
00457     require_once($CFG->dirroot.'/lib/rsslib.php');
00458 
00459     $status = true;
00460     mtrace('    Preparing to update all user unit tags RSS feeds');
00461     if (empty($CFG->enablerssfeeds)) {
00462         mtrace('      RSS DISABLED (admin variables - enablerssfeeds)');
00463     } else {
00464 
00465         //  Load all the categories for use later on
00466         $categories = $DB->get_records('course_categories');
00467 
00468         // get list of users who have tagged a unit
00469         $sql = "
00470             SELECT DISTINCT u.id as userid, u.username, u.firstname, u.lastname, u.email
00471             FROM {user} u, {course} c, {tag_instance} cti, {tag} t
00472             WHERE c.id = cti.itemid
00473             AND u.id = cti.tiuserid
00474             AND t.id = cti.tagid
00475             AND t.tagtype = 'personal'
00476             AND u.confirmed = 1
00477             AND u.deleted = 0
00478             ORDER BY userid";
00479         if ($users = $DB->get_records_sql($sql)) {
00480 
00481             $items = array(); //contains rss data items for each user
00482             foreach ($users as $user) {
00483 
00484                 // loop through each user, getting the data (tags for courses)
00485                 $sql = "
00486                     SELECT cti.id, c.id as courseid, c.fullname, c.shortname, c.category, t.rawname, cti.timemodified
00487                     FROM {course} c, {tag_instance} cti, {tag} t
00488                     WHERE c.id = cti.itemid
00489                     AND cti.tiuserid = :userid{$user->userid}
00490                     AND cti.tagid = t.id
00491                     AND t.tagtype = 'personal'
00492                     ORDER BY courseid";
00493                 if ($usertags = $DB->get_records_sql($sql, array('userid' => $user->userid))) {
00494                     $latest_date = 0; //latest date any tag was created by a user
00495                     $c = 0; //course identifier
00496 
00497                     foreach ($usertags as $usertag) {
00498                         if ($usertag->courseid != $c) {
00499                             $c = $usertag->courseid;
00500                             $items[$c] = new stdClass();
00501                             $items[$c]->title = $usertag->fullname . '(' . $usertag->shortname . ')';
00502                             $items[$c]->link = $CFG->wwwroot . '/course/view.php?name=' . $usertag->shortname;
00503                             $items[$c]->description = ''; //needs to be blank
00504                             $items[$c]->category = $categories[$usertag->category]->name;
00505                             $items[$c]->subject[] = $usertag->rawname;
00506                             $items[$c]->pubdate = $usertag->timemodified;
00507                             $items[$c]->tag = true;
00508                         } else {
00509                             $items[$c]->subject[] .= $usertag->rawname;
00510                         }
00511                         //  Check and set the latest modified date.
00512                         $latest_date = $usertag->timemodified > $latest_date ? $usertag->timemodified : $latest_date;
00513                     }
00514 
00515                     //  Setup some vars for use while creating the file
00516                     $path = $CFG->dataroot.'/1/usertagsrss/'.$user->userid;
00517                     $file_name = 'user_unit_tags_rss.xml';
00518                     $title = get_string('rsstitle', 'tag', ucwords(strtolower($user->firstname.' '.$user->lastname)));
00519                     $desc = get_string('rssdesc', 'tag');
00520                     // check that the path exists
00521                     if (!file_exists($path)) {
00522                         mtrace('  Creating folder '.$path);
00523                         check_dir_exists($path, TRUE, TRUE);
00524                     }
00525 
00526                     // create or update the feed for the user
00527                     // this functionality can be copied into seperate lib as in next two lines
00528                     //require_once($CFG->dirroot.'/local/ocilib.php');
00529                     //oci_create_rss_feed( $path, $file_name, $latest_date, $items, $title, $desc, $dc=true, $cc=false);
00530 
00531                     //  Set path to RSS file
00532                     $full_path = "$save_path/$file_name";
00533 
00534                     mtrace("    Preparing to update RSS feed for $file_name");
00535 
00536                     //  First let's make sure there is work to do by checking the time the file was last modified,
00537                     //  if a course was update after the file was mofified
00538                     if (file_exists($full_path)) {
00539                         if ($lastmodified = filemtime($full_path)) {
00540                             mtrace("        XML File $file_name Created on ".date( "D, j M Y G:i:s T", $lastmodified ));
00541                             mtrace('        Lastest course modification on '.date( "D, j M Y G:i:s T", $latest_date ));
00542                             if ($latest_date > $lastmodified) {
00543                                 mtrace("        XML File $file_name needs updating");
00544                                 $changes = true;
00545                             } else {
00546                                 mtrace("        XML File $file_name doesn't need updating");
00547                                 $changes = false;
00548                             }
00549                         }
00550                     } else {
00551                         mtrace("        XML File $file_name needs updating");
00552                         $changes = true;
00553                     }
00554 
00555                     if ($changes) {
00556                         //  Now we know something has changed, write the new file
00557 
00558                         if (!empty($items)) {
00559                             //  First set rss feeds common headers
00560                             $header = rss_standard_header(strip_tags(format_string($title,true)),
00561                                                           $CFG->wwwroot,
00562                                                           $desc,
00563                                                           true, true);
00564                             //  Now all the rss items
00565                             if (!empty($header)) {
00566                                 $articles = rss_add_items($items,$dc,$cc);
00567                             }
00568                             //  Now all rss feeds common footers
00569                             if (!empty($header) && !empty($articles)) {
00570                                 $footer = rss_standard_footer();
00571                             }
00572                             //  Now, if everything is ok, concatenate it
00573                             if (!empty($header) && !empty($articles) && !empty($footer)) {
00574                                 $result = $header.$articles.$footer;
00575                             } else {
00576                                 $result = false;
00577                             }
00578                         } else {
00579                             $result = false;
00580                         }
00581 
00582                         //  Save the XML contents to file
00583                         if (!empty($result)) {
00584                             $rss_file = fopen($full_path, "w");
00585                             if ($rss_file) {
00586                                 $status = fwrite ($rss_file, $result);
00587                                 fclose($rss_file);
00588                             } else {
00589                                 $status = false;
00590                             }
00591                         }
00592 
00593                         //  Output result
00594                         if (empty($result)) {
00595                             // There was nothing to put into the XML file. Delete it!
00596                             if( is_file($full_path) ) {
00597                                 mtrace("        There were no items for XML File $file_name. Deleting XML File");
00598                                 unlink($full_path);
00599                                 mtrace("        $full_path -> (deleted)");
00600                             } else {
00601                                 mtrace("        There were no items for the XML File $file_name and no file to delete. Ignore.");
00602                             }
00603                         } else {
00604                             if (!empty($status)) {
00605                                 mtrace("        $full_path -> OK");
00606                             } else {
00607                                 mtrace("        $full_path -> FAILED");
00608                             }
00609                         }
00610                     }
00611                     //end of oci_create_rss_feed()
00612                 }
00613             }
00614         }
00615     }
00616 
00617     return $status;
00618 }
00619  */
00620 
00634 /*
00635 function coursetag_get_official_keywords($courseid, $asarray=false) {
00636     global $CFG;
00637     $returnstr = '';
00638     $sql = "SELECT t.id, name, rawname
00639         FROM {tag} t, {tag_instance} ti
00640         WHERE ti.itemid = :courseid
00641         AND ti.itemtype = 'course'
00642         AND t.tagtype = 'official'
00643         AND ti.tagid = t.id
00644         ORDER BY name ASC";
00645     if ($tags = $DB->get_records_sql($sql, array('courseid' => $courseid))) {
00646         if ($asarray) {
00647             return $tags;
00648         }
00649         foreach ($tags as $tag) {
00650             if( empty($CFG->keeptagnamecase) ) {
00651                 $textlib = textlib_get_instance();
00652                 $name = $textlib->strtotitle($tag->name);
00653             } else {
00654                 $name = $tag->rawname;
00655             }
00656             $returnstr .= $name.', ';
00657         }
00658         $returnstr = rtrim($returnstr, ', ');
00659     }
00660     return $returnstr;
00661 }
00662 */
 All Data Structures Namespaces Files Functions Variables Enumerations