Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/tag/locallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once($CFG->dirroot.'/tag/lib.php');
00004 require_once($CFG->libdir.'/filelib.php');
00005 
00020 function tag_print_cloud($nr_of_tags=150, $return=false) {
00021     global $CFG, $DB;
00022 
00023     $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
00024 
00025     if ( !$tagsincloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
00026                                                  FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
00027                                                 WHERE ti.itemtype <> \'tag\'
00028                                              GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype
00029                                              ORDER BY count DESC, tg.name ASC', null, 0, $nr_of_tags) ) {
00030         $tagsincloud = array();
00031     }
00032 
00033     $tagkeys = array_keys($tagsincloud);
00034     if (!empty($tagkeys)) {
00035         $firsttagkey = $tagkeys[0];
00036         $maxcount = $tagsincloud[$firsttagkey]->count;
00037     }
00038 
00039     $etags = array();
00040 
00041     foreach ($tagsincloud as $tag) {
00042         $size = (int) (( $tag->count / $maxcount) * 20);
00043         $tag->class = "$tag->tagtype s$size";
00044         $etags[] = $tag;
00045     }
00046 
00047     usort($etags, "tag_cloud_sort");
00048     $output = '';
00049     $output .= "\n<ul class='tag_cloud inline-list'>\n";
00050     foreach ($etags as $tag) {
00051         if ($tag->flag > 0 && $can_manage_tags) {
00052             $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
00053         } else {
00054             $tagname = tag_display_name($tag);
00055         }
00056 
00057         $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
00058         $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
00059             'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
00060             $tagname .'</a></li> ';
00061     }
00062     $output .= "\n</ul>\n";
00063 
00064     if ($return) {
00065         return $output;
00066     } else {
00067         echo $output;
00068     }
00069 }
00070 
00076 function tag_cloud_sort($a, $b) {
00077     global $CFG;
00078 
00079     if (empty($CFG->tagsort)) {
00080         $tagsort = 'name'; // by default, sort by name
00081     } else {
00082         $tagsort = $CFG->tagsort;
00083     }
00084 
00085     if (is_numeric($a->$tagsort)) {
00086         return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
00087     } elseif (is_string($a->$tagsort)) {
00088         return strcmp($a->$tagsort, $b->$tagsort);
00089     } else {
00090         return 0;
00091     }
00092 }
00093 
00100 function tag_print_description_box($tag_object, $return=false) {
00101 
00102     global $USER, $CFG, $OUTPUT;
00103 
00104     $max_tags_displayed = 10; // todo: turn this into a system setting
00105 
00106     $tagname  = tag_display_name($tag_object);
00107     $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
00108 
00109     $content = !empty($tag_object->description) || $related_tags;
00110     $output = '';
00111 
00112     if ($content) {
00113         $output .= $OUTPUT->box_start('generalbox', 'tag-description');
00114     }
00115 
00116     if (!empty($tag_object->description)) {
00117         $options = new stdClass();
00118         $options->para = false;
00119         $options->overflowdiv = true;
00120         $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', get_context_instance(CONTEXT_SYSTEM)->id, 'tag', 'description', $tag_object->id);
00121         $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
00122     }
00123 
00124     if ($related_tags) {
00125         $more_links = false;
00126         if (count($related_tags) > $max_tags_displayed) {
00127             array_pop($related_tags);
00128             $more_links = true;
00129         }
00130         $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
00131         if ($more_links) {
00132             $output .= ' ...';
00133         }
00134     }
00135 
00136     if ($content) {
00137         $output .= $OUTPUT->box_end();
00138     }
00139 
00140     if ($return) {
00141         return $output;
00142     } else {
00143         echo $output;
00144     }
00145 }
00146 
00153 function tag_print_management_box($tag_object, $return=false) {
00154 
00155     global $USER, $CFG, $OUTPUT;
00156 
00157     $tagname  = tag_display_name($tag_object);
00158     $output = '';
00159 
00160     if (!isguestuser()) {
00161         $output .= $OUTPUT->box_start('box','tag-management-box');
00162         $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
00163         $links = array();
00164 
00165         // Add a link for users to add/remove this from their interests
00166         if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
00167             $links[] = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=removeinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('removetagfrommyinterests', 'tag', $tagname) .'</a>';
00168         } else {
00169             $links[] = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('addtagtomyinterests', 'tag', $tagname) .'</a>';
00170         }
00171 
00172         // flag as inappropriate link
00173         $links[] = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=flaginappropriate&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('flagasinappropriate', 'tag', rawurlencode($tagname)) .'</a>';
00174 
00175         // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
00176         if (has_capability('moodle/tag:edit', $systemcontext) ||
00177             has_capability('moodle/tag:manage', $systemcontext)) {
00178             $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
00179         }
00180 
00181         $output .= implode(' | ', $links);
00182         $output .= $OUTPUT->box_end();
00183     }
00184 
00185     if ($return) {
00186         return $output;
00187     } else {
00188         echo $output;
00189     }
00190 }
00191 
00197 function tag_print_search_box($return=false) {
00198     global $CFG, $OUTPUT;
00199 
00200     $output = $OUTPUT->box_start('','tag-search-box');
00201     $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
00202     $output .= '<div>';
00203     $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
00204     $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
00205     $output .= '</div>';
00206     $output .= '</form>';
00207     $output .= $OUTPUT->box_end();
00208 
00209     if ($return) {
00210         return $output;
00211     }
00212     else {
00213         echo $output;
00214     }
00215 }
00216 
00225 function tag_print_search_results($query,  $page, $perpage, $return=false) {
00226 
00227     global $CFG, $USER, $OUTPUT;
00228 
00229     $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
00230 
00231     $count = sizeof(tag_find_tags($query, false));
00232     $tags = array();
00233 
00234     if ( $found_tags = tag_find_tags($query, true,  $page * $perpage, $perpage) ) {
00235         $tags = array_values($found_tags);
00236     }
00237 
00238     $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
00239     $output = '';
00240 
00241     // link "Add $query to my interests"
00242     $addtaglink = '';
00243     if( !tag_record_tagged_with('user', $USER->id, $query) ) {
00244         $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
00245         $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
00246     }
00247 
00248     if ( !empty($tags) ) { // there are results to display!!
00249         $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
00250 
00251         //print a link "Add $query to my interests"
00252         if (!empty($addtaglink)) {
00253             $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
00254         }
00255 
00256         $nr_of_lis_per_ul = 6;
00257         $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
00258 
00259         $output .= '<ul id="tag-search-results">';
00260         for($i = 0; $i < $nr_of_uls; $i++) {
00261             $output .= '<li>';
00262             foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
00263                 $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
00264                 $output .= '&#8226;'. $tag_link .'<br/>';
00265             }
00266             $output .= '</li>';
00267         }
00268         $output .= '</ul>';
00269         $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
00270 
00271         $output .= $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
00272     }
00273     else { //no results were found!!
00274         $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
00275 
00276         //print a link "Add $query to my interests"
00277         if (!empty($addtaglink)) {
00278             $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
00279         }
00280     }
00281 
00282     if ($return) {
00283         return $output;
00284     }
00285     else {
00286         echo $output;
00287     }
00288 }
00289 
00299 function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
00300 
00301     //List of users with this tag
00302     $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
00303 
00304     $output = tag_print_user_list($userlist, true);
00305 
00306     if ($return) {
00307         return $output;
00308     }
00309     else {
00310         echo $output;
00311     }
00312 }
00313 
00320 function tag_print_user_box($user, $return=false) {
00321     global $CFG, $OUTPUT;
00322 
00323     $textlib = textlib_get_instance();
00324     $usercontext = get_context_instance(CONTEXT_USER, $user->id);
00325     $profilelink = '';
00326 
00327     if ($usercontext and (has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($user->id))) {
00328         $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
00329     }
00330 
00331     $output = $OUTPUT->box_start('user-box', 'user'. $user->id);
00332     $fullname = fullname($user);
00333     $alt = '';
00334 
00335     if (!empty($profilelink)) {
00336         $output .= '<a href="'. $profilelink .'">';
00337         $alt = $fullname;
00338     }
00339 
00340     $output .= $OUTPUT->user_picture($user, array('size'=>100));
00341     $output .= '<br />';
00342 
00343     if (!empty($profilelink)) {
00344         $output .= '</a>';
00345     }
00346 
00347     //truncate name if it's too big
00348     if ($textlib->strlen($fullname) > 26) {
00349         $fullname = $textlib->substr($fullname, 0, 26) .'...';
00350     }
00351 
00352     $output .= '<strong>'. $fullname .'</strong>';
00353     $output .= $OUTPUT->box_end();
00354 
00355     if ($return) {
00356         return $output;
00357     }
00358     else {
00359         echo $output;
00360     }
00361 }
00368 function tag_print_user_list($userlist, $return=false) {
00369 
00370     $output = '<ul class="inline-list">';
00371 
00372     foreach ($userlist as $user){
00373         $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
00374     }
00375     $output .= "</ul>\n";
00376 
00377     if ($return) {
00378         return $output;
00379     }
00380     else {
00381         echo $output;
00382     }
00383 }
 All Data Structures Namespaces Files Functions Variables Enumerations