Moodle release 3.0.3
[moodle.git] / tag / locallib.php
blob55cd31e278a7c9bfb0ccca18023db3e0f3d211ad
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Moodle tag local library - output functions
21 * @package core_tag
22 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 require_once($CFG->dirroot.'/tag/lib.php');
27 require_once($CFG->libdir.'/filelib.php');
29 /**
30 * Prints or returns a HTML tag cloud with varying classes styles depending on the popularity and type of each tag.
32 * @package core_tag
33 * @access public
34 * @category tag
35 * @param array $tagset Array of tags to display
36 * @param int $nr_of_tags Limit for the number of tags to return/display, used if $tagset is null
37 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
38 * @param string $sort (optional) selected sorting, default is alpha sort (name) also timemodified or popularity
39 * @return string|null a HTML string or null if this function does the output
41 function tag_print_cloud($tagset=null, $nr_of_tags=150, $return=false, $sort='') {
42 global $CFG, $DB;
44 $can_manage_tags = has_capability('moodle/tag:manage', context_system::instance());
46 if (is_null($tagset)) {
47 // No tag set received, so fetch tags from database
48 if ( !$tagsincloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
49 FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
50 WHERE ti.itemtype <> \'tag\'
51 GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype
52 ORDER BY count DESC, tg.name ASC', null, 0, $nr_of_tags) ) {
53 $tagsincloud = array();
55 } else {
56 $tagsincloud = $tagset;
59 $tagkeys = array_keys($tagsincloud);
60 if (!empty($tagkeys)) {
61 $firsttagkey = $tagkeys[0];
62 $maxcount = $tagsincloud[$firsttagkey]->count;
65 $etags = array();
67 foreach ($tagsincloud as $tag) {
68 $size = (int) (( $tag->count / $maxcount) * 20);
69 $tag->class = "$tag->tagtype s$size";
70 $etags[] = $tag;
73 // Set up sort global - used to pass sort type into tag_cloud_sort through usort() avoiding multiple sort functions.
74 // TODO make calling functions pass 'count' or 'timemodified' not 'popularity' or 'date'.
75 $oldsort = empty($CFG->tagsort) ? null : $CFG->tagsort;
76 if ($sort == 'popularity') {
77 $CFG->tagsort = 'count';
78 } else if ($sort == 'date') {
79 $CFG->tagsort = 'timemodified';
80 } else {
81 $CFG->tagsort = 'name';
83 usort($etags, "tag_cloud_sort");
84 $CFG->tagsort = $oldsort;
86 $output = '';
87 $output .= "\n<ul class='tag_cloud inline-list'>\n";
88 foreach ($etags as $tag) {
89 if ($tag->flag > 0 && $can_manage_tags) {
90 $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
91 } else {
92 $tagname = tag_display_name($tag);
95 $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
96 $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
97 'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
98 $tagname .'</a></li> ';
100 $output .= "\n</ul>\n";
102 if ($return) {
103 return $output;
104 } else {
105 echo $output;
110 * This function is used by print_tag_cloud, to usort() the tags in the cloud. See php.net/usort for the parameters documentation.
111 * This was originally in blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
113 * @package core_tag
114 * @access private
115 * @param string $a Tag name to compare against $b
116 * @param string $b Tag name to compare against $a
117 * @return int The result of the comparison/validation 1, 0 or -1
119 function tag_cloud_sort($a, $b) {
120 global $CFG;
122 if (empty($CFG->tagsort)) {
123 $tagsort = 'name'; // by default, sort by name
124 } else {
125 $tagsort = $CFG->tagsort;
128 if (is_numeric($a->$tagsort)) {
129 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
130 } elseif (is_string($a->$tagsort)) {
131 return strcmp($a->$tagsort, $b->$tagsort);
132 } else {
133 return 0;
138 * Prints a box with the description of a tag and its related tags
140 * @package core_tag
141 * @access public
142 * @todo MDL-31149 create a system setting for $max_tags_displayed, instead of using an in code literal
143 * @param stdClass $tag_object
144 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
145 * @return string/null a HTML box showing a description of the tag object and it's relationsips or null if output is done directly
146 * in the function.
148 function tag_print_description_box($tag_object, $return=false) {
150 global $USER, $CFG, $OUTPUT;
152 $max_tags_displayed = 10;
154 $tagname = tag_display_name($tag_object);
155 $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
157 $content = !empty($tag_object->description) || $related_tags;
158 $output = '';
160 if ($content) {
161 $output .= $OUTPUT->box_start('generalbox', 'tag-description');
164 if (!empty($tag_object->description)) {
165 $options = new stdClass();
166 $options->para = false;
167 $options->overflowdiv = true;
168 $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', context_system::instance()->id, 'tag', 'description', $tag_object->id);
169 $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
172 if ($related_tags) {
173 $more_links = false;
174 if (count($related_tags) > $max_tags_displayed) {
175 array_pop($related_tags);
176 $more_links = true;
178 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
179 if ($more_links) {
180 $output .= ' ...';
184 if ($content) {
185 $output .= $OUTPUT->box_end();
188 if ($return) {
189 return $output;
190 } else {
191 echo $output;
196 * Prints a box that contains the management links of a tag
198 * @access public
199 * @param stdClass $tag_object
200 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
201 * @return string|null a HTML string or null if this function does the output
203 function tag_print_management_box($tag_object, $return=false) {
205 global $USER, $CFG, $OUTPUT;
207 $tagname = tag_display_name($tag_object);
208 $output = '';
210 if (!isguestuser()) {
211 $output .= $OUTPUT->box_start('box','tag-management-box');
212 $systemcontext = context_system::instance();
213 $links = array();
215 // Add a link for users to add/remove this from their interests
216 if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
217 $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>';
218 } else {
219 $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>';
222 // Flag as inappropriate link. Only people with moodle/tag:flag capability.
223 if (has_capability('moodle/tag:flag', $systemcontext)) {
224 $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>';
227 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
228 if (has_capability('moodle/tag:edit', $systemcontext) ||
229 has_capability('moodle/tag:manage', $systemcontext)) {
230 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
233 $output .= implode(' | ', $links);
234 $output .= $OUTPUT->box_end();
237 if ($return) {
238 return $output;
239 } else {
240 echo $output;
245 * Prints the tag search box
247 * @access public
248 * @param bool $return if true return html string
249 * @return string|null a HTML string or null if this function does the output
251 function tag_print_search_box($return=false) {
252 global $CFG, $OUTPUT;
254 $output = $OUTPUT->box_start('','tag-search-box');
255 $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
256 $output .= '<div>';
257 $output .= '<label class="accesshide" for="searchform_search">'.get_string('searchtags', 'tag').'</label>';
258 $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
259 $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
260 $output .= '</div>';
261 $output .= '</form>';
262 $output .= $OUTPUT->box_end();
264 if ($return) {
265 return $output;
267 else {
268 echo $output;
273 * Prints the tag search results
275 * @access public
276 * @param string $query text that tag names will be matched against
277 * @param int $page current page
278 * @param int $perpage nr of users displayed per page
279 * @param bool $return if true return html string
280 * @return string|null a HTML string or null if this function does the output
282 function tag_print_search_results($query, $page, $perpage, $return=false) {
284 global $CFG, $USER, $OUTPUT;
286 $norm = tag_normalize($query, TAG_CASE_ORIGINAL);
287 $query = array_shift($norm);
289 $count = sizeof(tag_find_tags($query, false));
290 $tags = array();
292 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
293 $tags = array_values($found_tags);
296 $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
297 $output = '';
299 // link "Add $query to my interests"
300 $addtaglink = '';
301 if( !tag_record_tagged_with('user', $USER->id, $query) ) {
302 $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
303 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
306 if ( !empty($tags) ) { // there are results to display!!
307 $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
309 //print a link "Add $query to my interests"
310 if (!empty($addtaglink)) {
311 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
314 $nr_of_lis_per_ul = 6;
315 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
317 $output .= '<ul id="tag-search-results">';
318 for($i = 0; $i < $nr_of_uls; $i++) {
319 $output .= '<li>';
320 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
321 $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
322 $output .= '&#8226;'. $tag_link .'<br/>';
324 $output .= '</li>';
326 $output .= '</ul>';
327 $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
329 $output .= $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
331 else { //no results were found!!
332 $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
334 //print a link "Add $query to my interests"
335 if (!empty($addtaglink)) {
336 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
340 if ($return) {
341 return $output;
343 else {
344 echo $output;
349 * Prints a table of the users tagged with the tag passed as argument
351 * @param int $tag_object the tag we wish to return data for
352 * @param int $limitfrom (optional, required if $limitnum is set) prints users starting at this point.
353 * @param int $limitnum (optional, required if $limitfrom is set) prints this many users.
354 * @param bool $return if true return html string
355 * @return string|null a HTML string or null if this function does the output
357 function tag_print_tagged_users_table($tag_object, $limitfrom='', $limitnum='', $return=false) {
359 //List of users with this tag
360 $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
362 $output = tag_print_user_list($userlist, true);
364 if ($return) {
365 return $output;
367 else {
368 echo $output;
373 * Prints an individual user box
375 * @param user_object $user (contains the following fields: id, firstname, lastname and picture)
376 * @param bool $return if true return html string
377 * @return string|null a HTML string or null if this function does the output
379 function tag_print_user_box($user, $return=false) {
380 global $CFG, $OUTPUT;
382 $usercontext = context_user::instance($user->id);
383 $profilelink = '';
385 if ($usercontext and (has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($user->id))) {
386 $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
389 $output = $OUTPUT->box_start('user-box', 'user'. $user->id);
390 $fullname = fullname($user);
391 $alt = '';
393 if (!empty($profilelink)) {
394 $output .= '<a href="'. $profilelink .'">';
395 $alt = $fullname;
398 $output .= $OUTPUT->user_picture($user, array('size'=>100));
399 $output .= '<br />';
401 if (!empty($profilelink)) {
402 $output .= '</a>';
405 //truncate name if it's too big
406 if (core_text::strlen($fullname) > 26) {
407 $fullname = core_text::substr($fullname, 0, 26) .'...';
410 $output .= '<strong>'. $fullname .'</strong>';
411 $output .= $OUTPUT->box_end();
413 if ($return) {
414 return $output;
416 else {
417 echo $output;
422 * Prints a list of users
424 * @param array $userlist an array of user objects
425 * @param bool $return if true return html string, otherwise output the result
426 * @return string|null a HTML string or null if this function does the output
428 function tag_print_user_list($userlist, $return=false) {
430 $output = '<ul class="inline-list">';
432 foreach ($userlist as $user){
433 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
435 $output .= "</ul>\n";
437 if ($return) {
438 return $output;
440 else {
441 echo $output;