MDL-27508 fixing the description of groups related behaviour of some capabilities
[moodle.git] / tag / locallib.php
blobfb7f9a9f7300a21aae4d3c964f25bda10c2493dd
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 int $nr_of_tags Limit for the number of tags to return/display
36 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
37 * @return string|null a HTML string or null if this function does the output
39 function tag_print_cloud($nr_of_tags=150, $return=false) {
40 global $CFG, $DB;
42 $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
44 if ( !$tagsincloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
45 FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
46 WHERE ti.itemtype <> \'tag\'
47 GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype
48 ORDER BY count DESC, tg.name ASC', null, 0, $nr_of_tags) ) {
49 $tagsincloud = array();
52 $tagkeys = array_keys($tagsincloud);
53 if (!empty($tagkeys)) {
54 $firsttagkey = $tagkeys[0];
55 $maxcount = $tagsincloud[$firsttagkey]->count;
58 $etags = array();
60 foreach ($tagsincloud as $tag) {
61 $size = (int) (( $tag->count / $maxcount) * 20);
62 $tag->class = "$tag->tagtype s$size";
63 $etags[] = $tag;
66 usort($etags, "tag_cloud_sort");
67 $output = '';
68 $output .= "\n<ul class='tag_cloud inline-list'>\n";
69 foreach ($etags as $tag) {
70 if ($tag->flag > 0 && $can_manage_tags) {
71 $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
72 } else {
73 $tagname = tag_display_name($tag);
76 $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
77 $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
78 'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
79 $tagname .'</a></li> ';
81 $output .= "\n</ul>\n";
83 if ($return) {
84 return $output;
85 } else {
86 echo $output;
90 /**
91 * This function is used by print_tag_cloud, to usort() the tags in the cloud. See php.net/usort for the parameters documentation.
92 * This was originally in blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
94 * @package core_tag
95 * @access private
96 * @param string $a Tag name to compare against $b
97 * @param string $b Tag name to compare against $a
98 * @return int The result of the comparison/validation 1, 0 or -1
100 function tag_cloud_sort($a, $b) {
101 global $CFG;
103 if (empty($CFG->tagsort)) {
104 $tagsort = 'name'; // by default, sort by name
105 } else {
106 $tagsort = $CFG->tagsort;
109 if (is_numeric($a->$tagsort)) {
110 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
111 } elseif (is_string($a->$tagsort)) {
112 return strcmp($a->$tagsort, $b->$tagsort);
113 } else {
114 return 0;
119 * Prints a box with the description of a tag and its related tags
121 * @package core_tag
122 * @access public
123 * @todo MDL-31149 create a system setting for $max_tags_displayed, instead of using an in code literal
124 * @param stdClass $tag_object
125 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
126 * @return string/null a HTML box showing a description of the tag object and it's relationsips or null if output is done directly
127 * in the function.
129 function tag_print_description_box($tag_object, $return=false) {
131 global $USER, $CFG, $OUTPUT;
133 $max_tags_displayed = 10;
135 $tagname = tag_display_name($tag_object);
136 $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
138 $content = !empty($tag_object->description) || $related_tags;
139 $output = '';
141 if ($content) {
142 $output .= $OUTPUT->box_start('generalbox', 'tag-description');
145 if (!empty($tag_object->description)) {
146 $options = new stdClass();
147 $options->para = false;
148 $options->overflowdiv = true;
149 $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', get_context_instance(CONTEXT_SYSTEM)->id, 'tag', 'description', $tag_object->id);
150 $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
153 if ($related_tags) {
154 $more_links = false;
155 if (count($related_tags) > $max_tags_displayed) {
156 array_pop($related_tags);
157 $more_links = true;
159 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
160 if ($more_links) {
161 $output .= ' ...';
165 if ($content) {
166 $output .= $OUTPUT->box_end();
169 if ($return) {
170 return $output;
171 } else {
172 echo $output;
177 * Prints a box that contains the management links of a tag
179 * @access public
180 * @param stdClass $tag_object
181 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
182 * @return string|null a HTML string or null if this function does the output
184 function tag_print_management_box($tag_object, $return=false) {
186 global $USER, $CFG, $OUTPUT;
188 $tagname = tag_display_name($tag_object);
189 $output = '';
191 if (!isguestuser()) {
192 $output .= $OUTPUT->box_start('box','tag-management-box');
193 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
194 $links = array();
196 // Add a link for users to add/remove this from their interests
197 if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
198 $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>';
199 } else {
200 $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>';
203 // flag as inappropriate link
204 $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>';
206 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
207 if (has_capability('moodle/tag:edit', $systemcontext) ||
208 has_capability('moodle/tag:manage', $systemcontext)) {
209 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
212 $output .= implode(' | ', $links);
213 $output .= $OUTPUT->box_end();
216 if ($return) {
217 return $output;
218 } else {
219 echo $output;
224 * Prints the tag search box
226 * @access public
227 * @param bool $return if true return html string
228 * @return string|null a HTML string or null if this function does the output
230 function tag_print_search_box($return=false) {
231 global $CFG, $OUTPUT;
233 $output = $OUTPUT->box_start('','tag-search-box');
234 $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
235 $output .= '<div>';
236 $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
237 $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
238 $output .= '</div>';
239 $output .= '</form>';
240 $output .= $OUTPUT->box_end();
242 if ($return) {
243 return $output;
245 else {
246 echo $output;
251 * Prints the tag search results
253 * @access public
254 * @param string $query text that tag names will be matched against
255 * @param int $page current page
256 * @param int $perpage nr of users displayed per page
257 * @param bool $return if true return html string
258 * @return string|null a HTML string or null if this function does the output
260 function tag_print_search_results($query, $page, $perpage, $return=false) {
262 global $CFG, $USER, $OUTPUT;
264 $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
266 $count = sizeof(tag_find_tags($query, false));
267 $tags = array();
269 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
270 $tags = array_values($found_tags);
273 $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
274 $output = '';
276 // link "Add $query to my interests"
277 $addtaglink = '';
278 if( !tag_record_tagged_with('user', $USER->id, $query) ) {
279 $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
280 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
283 if ( !empty($tags) ) { // there are results to display!!
284 $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
286 //print a link "Add $query to my interests"
287 if (!empty($addtaglink)) {
288 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
291 $nr_of_lis_per_ul = 6;
292 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
294 $output .= '<ul id="tag-search-results">';
295 for($i = 0; $i < $nr_of_uls; $i++) {
296 $output .= '<li>';
297 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
298 $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
299 $output .= '&#8226;'. $tag_link .'<br/>';
301 $output .= '</li>';
303 $output .= '</ul>';
304 $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
306 $output .= $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
308 else { //no results were found!!
309 $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
311 //print a link "Add $query to my interests"
312 if (!empty($addtaglink)) {
313 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
317 if ($return) {
318 return $output;
320 else {
321 echo $output;
326 * Prints a table of the users tagged with the tag passed as argument
328 * @param int $tag_object the tag we wish to return data for
329 * @param int $limitfrom (optional, required if $limitnum is set) prints users starting at this point.
330 * @param int $limitnum (optional, required if $limitfrom is set) prints this many users.
331 * @param bool $return if true return html string
332 * @return string|null a HTML string or null if this function does the output
334 function tag_print_tagged_users_table($tag_object, $limitfrom='', $limitnum='', $return=false) {
336 //List of users with this tag
337 $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
339 $output = tag_print_user_list($userlist, true);
341 if ($return) {
342 return $output;
344 else {
345 echo $output;
350 * Prints an individual user box
352 * @param user_object $user (contains the following fields: id, firstname, lastname and picture)
353 * @param bool $return if true return html string
354 * @return string|null a HTML string or null if this function does the output
356 function tag_print_user_box($user, $return=false) {
357 global $CFG, $OUTPUT;
359 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
360 $profilelink = '';
362 if ($usercontext and (has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($user->id))) {
363 $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
366 $output = $OUTPUT->box_start('user-box', 'user'. $user->id);
367 $fullname = fullname($user);
368 $alt = '';
370 if (!empty($profilelink)) {
371 $output .= '<a href="'. $profilelink .'">';
372 $alt = $fullname;
375 $output .= $OUTPUT->user_picture($user, array('size'=>100));
376 $output .= '<br />';
378 if (!empty($profilelink)) {
379 $output .= '</a>';
382 //truncate name if it's too big
383 if (textlib::strlen($fullname) > 26) {
384 $fullname = textlib::substr($fullname, 0, 26) .'...';
387 $output .= '<strong>'. $fullname .'</strong>';
388 $output .= $OUTPUT->box_end();
390 if ($return) {
391 return $output;
393 else {
394 echo $output;
399 * Prints a list of users
401 * @param array $userlist an array of user objects
402 * @param bool $return if true return html string, otherwise output the result
403 * @return string|null a HTML string or null if this function does the output
405 function tag_print_user_list($userlist, $return=false) {
407 $output = '<ul class="inline-list">';
409 foreach ($userlist as $user){
410 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
412 $output .= "</ul>\n";
414 if ($return) {
415 return $output;
417 else {
418 echo $output;