4 * locallib.php - moodle tag local library - output functions
7 * @licence http://www.gnu.org/copyleft/gpl.html GNU Public License
15 * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
16 * @param $return if true return html string
18 function tag_print_cloud($nr_of_tags=150, $return=false) {
22 $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM
));
24 if ( !$tagsincloud = get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag '.
25 'FROM '. $CFG->prefix
.'tag_instance ti INNER JOIN '. $CFG->prefix
.'tag tg ON tg.id = ti.tagid '.
26 'WHERE ti.itemtype != \'tag\' '.
27 'GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype '.
28 'ORDER BY count DESC, tg.name ASC', 0, $nr_of_tags) ) {
29 $tagsincloud = array();
32 $tagkeys = array_keys($tagsincloud);
33 if (!empty($tagkeys)) {
34 $firsttagkey = $tagkeys[0];
35 $maxcount = $tagsincloud[$firsttagkey]->count
;
40 foreach ($tagsincloud as $tag) {
41 $size = (int) (( $tag->count
/ $maxcount) * 20);
42 $tag->class = "$tag->tagtype s$size";
46 usort($etags, "tag_cloud_sort");
48 $output .= "\n<ul class='tag_cloud inline-list'>\n";
49 foreach ($etags as $tag) {
50 if ($tag->flag
> 0 && $can_manage_tags) {
51 $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
53 $tagname = tag_display_name($tag);
56 $link = $CFG->wwwroot
.'/tag/index.php?tag='. rawurlencode($tag->name
);
57 $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
58 'title="'. get_string('numberofentries', 'blog', $tag->count
) .'">'.
59 $tagname .'</a></li> ';
61 $output .= "\n</ul>\n";
71 * This function is used by print_tag_cloud, to usort() the tags in the cloud.
72 * See php.net/usort for the parameters documentation. This was originally in
73 * blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
75 function tag_cloud_sort($a, $b) {
78 if (empty($CFG->tagsort
)) {
79 $tagsort = 'name'; // by default, sort by name
81 $tagsort = $CFG->tagsort
;
84 if (is_numeric($a->$tagsort)) {
85 return ($a->$tagsort == $b->$tagsort) ?
0 : ($a->$tagsort > $b->$tagsort) ?
1 : -1;
86 } elseif (is_string($a->$tagsort)) {
87 return strcmp($a->$tagsort, $b->$tagsort);
94 * Prints a box with the description of a tag and its related tags
96 * @param unknown_type $tag_object
97 * @param $return if true return html string
99 function tag_print_description_box($tag_object, $return=false) {
103 $max_tags_displayed = 10; // todo: turn this into a system setting
105 $tagname = tag_display_name($tag_object);
106 $related_tags = tag_get_related_tags($tag_object->id
, TAG_RELATED_ALL
, $max_tags_displayed+
1); // this gets one more than we want
108 $content = !empty($tag_object->description
) ||
$related_tags;
112 $output .= print_box_start('generalbox', 'tag-description', true);
115 if (!empty($tag_object->description
)) {
116 $options = new object();
117 $options->para
= false;
118 $output .= format_text($tag_object->description
, $tag_object->descriptionformat
, $options);
123 if (count($related_tags) > $max_tags_displayed) {
124 array_pop($related_tags);
127 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
134 $output .= print_box_end(true);
145 * Prints a box that contains the management links of a tag
148 * @param $return if true return html string
150 function tag_print_management_box($tag_object, $return=false) {
154 $tagname = tag_display_name($tag_object);
157 if (!isguestuser()) {
158 $output .= print_box_start('box','tag-management-box', true);
159 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
162 // Add a link for users to add/remove this from their interests
163 if (tag_record_tagged_with('user', $USER->id
, $tag_object->name
)) {
164 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=removeinterest&sesskey='. sesskey() .'&tag='. rawurlencode($tag_object->name
) .'">'. get_string('removetagfrommyinterests', 'tag', $tagname) .'</a>';
166 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=addinterest&sesskey='. sesskey() .'&tag='. rawurlencode($tag_object->name
) .'">'. get_string('addtagtomyinterests', 'tag', $tagname) .'</a>';
169 // flag as inappropriate link
170 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=flaginappropriate&sesskey='. sesskey() .'&tag='. rawurlencode($tag_object->name
) .'">'. get_string('flagasinappropriate', 'tag', rawurlencode($tagname)) .'</a>';
172 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
173 if (has_capability('moodle/tag:edit', $systemcontext) ||
174 has_capability('moodle/tag:manage', $systemcontext)) {
175 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/edit.php?tag='. rawurlencode($tag_object->name
) .'">'. get_string('edittag', 'tag') .'</a>';
178 $output .= implode(' | ', $links);
179 $output .= print_box_end(true);
190 * Prints the tag search box
192 * @param bool $return if true return html string
194 function tag_print_search_box($return=false) {
197 $output = print_box_start('','tag-search-box', true);
198 $output .= '<form action="'.$CFG->wwwroot
.'/tag/search.php" style="display:inline">';
200 $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
201 $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
203 $output .= '</form>';
204 $output .= print_box_end(true);
215 * Prints the tag search results
217 * @param string $query text that tag names will be matched against
218 * @param int $page current page
219 * @param int $perpage nr of users displayed per page
220 * @param $return if true return html string
222 function tag_print_search_results($query, $page, $perpage, $return=false) {
226 $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL
));
228 $count = sizeof(tag_find_tags($query, false));
231 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
232 $tags = array_values($found_tags);
235 $baseurl = $CFG->wwwroot
.'/tag/search.php?query='. rawurlencode($query);
238 // link "Add $query to my interests"
240 if( !tag_record_tagged_with('user', $USER->id
, $query) ) {
241 $addtaglink = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=addinterest&sesskey='. sesskey() .'&tag='. rawurlencode($query) .'">';
242 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
245 if ( !empty($tags) ) { // there are results to display!!
246 $output .= print_heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", '', 3, 'main', true);
248 //print a link "Add $query to my interests"
249 if (!empty($addtaglink)) {
250 $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
253 $nr_of_lis_per_ul = 6;
254 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
256 $output .= '<ul id="tag-search-results">';
257 for($i = 0; $i < $nr_of_uls; $i++
) {
259 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
260 $tag_link = ' <a href="'. $CFG->wwwroot
.'/tag/index.php?id='. $tag->id
.'">'. tag_display_name($tag) .'</a>';
261 $output .= '•'. $tag_link .'<br/>';
266 $output .= '<div> </div>'; // <-- small layout hack in order to look good in Firefox
268 $output .= print_paging_bar($count, $page, $perpage, $baseurl .'&', 'page', false, true);
270 else { //no results were found!!
271 $output .= print_heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), '', 3, 'main' , true);
273 //print a link "Add $query to my interests"
274 if (!empty($addtaglink)) {
275 $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
288 * Prints a table of the users tagged with the tag passed as argument
291 * @param int $users_per_row number of users per row to display
292 * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
293 * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
294 * @param $return if true return html string
296 function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
298 //List of users with this tag
299 $userlist = tag_find_records($tag_object->name
, 'user', $limitfrom, $limitnum);
301 $output = tag_print_user_list($userlist, true);
312 * Prints an individual user box
314 * @param $user user object (contains the following fields: id, firstname, lastname and picture)
315 * @param $return if true return html string
317 function tag_print_user_box($user, $return=false) {
320 $textlib = textlib_get_instance();
321 $usercontext = get_context_instance(CONTEXT_USER
, $user->id
);
324 if ( has_capability('moodle/user:viewdetails', $usercontext) ||
isteacherinanycourse($user->id
) ) {
325 $profilelink = $CFG->wwwroot
.'/user/view.php?id='. $user->id
;
328 $output = print_box_start('user-box', 'user'. $user->id
, true);
329 $fullname = fullname($user);
332 if (!empty($profilelink)) {
333 $output .= '<a href="'. $profilelink .'">';
337 //print user image - if image is only content of link it needs ALT text!
338 if ($user->picture
) {
339 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot
.'/user/pix.php/'. $user->id
.'/f1.jpg" />';
341 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot
.'/pix/u/f1.png" />';
346 if (!empty($profilelink)) {
350 //truncate name if it's too big
351 if ($textlib->strlen($fullname) > 26) {
352 $fullname = $textlib->substr($fullname, 0, 26) .'...';
355 $output .= '<strong>'. $fullname .'</strong>';
356 $output .= print_box_end(true);
366 * Prints a list of users
368 * @param array $userlist an array of user objects
369 * @param $return if true return html string
371 function tag_print_user_list($userlist, $return=false) {
373 $output = '<ul class="inline-list">';
375 foreach ($userlist as $user){
376 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
378 $output .= "</ul>\n";