MDL-4908 Forum: Basic maildigest setting tests
[moodle.git] / tag / locallib.php
blobdb00484df09412cd15ae618adaa3f2fed8df9368
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 * @return string|null a HTML string or null if this function does the output
40 function tag_print_cloud($tagset=null, $nr_of_tags=150, $return=false) {
41 global $CFG, $DB;
43 $can_manage_tags = has_capability('moodle/tag:manage', context_system::instance());
45 if (is_null($tagset)) {
46 // No tag set received, so fetch tags from database
47 if ( !$tagsincloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
48 FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
49 WHERE ti.itemtype <> \'tag\'
50 GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype
51 ORDER BY count DESC, tg.name ASC', null, 0, $nr_of_tags) ) {
52 $tagsincloud = array();
54 } else {
55 $tagsincloud = $tagset;
58 $tagkeys = array_keys($tagsincloud);
59 if (!empty($tagkeys)) {
60 $firsttagkey = $tagkeys[0];
61 $maxcount = $tagsincloud[$firsttagkey]->count;
64 $etags = array();
66 foreach ($tagsincloud as $tag) {
67 $size = (int) (( $tag->count / $maxcount) * 20);
68 $tag->class = "$tag->tagtype s$size";
69 $etags[] = $tag;
72 usort($etags, "tag_cloud_sort");
73 $output = '';
74 $output .= "\n<ul class='tag_cloud inline-list'>\n";
75 foreach ($etags as $tag) {
76 if ($tag->flag > 0 && $can_manage_tags) {
77 $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
78 } else {
79 $tagname = tag_display_name($tag);
82 $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
83 $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
84 'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
85 $tagname .'</a></li> ';
87 $output .= "\n</ul>\n";
89 if ($return) {
90 return $output;
91 } else {
92 echo $output;
96 /**
97 * This function is used by print_tag_cloud, to usort() the tags in the cloud. See php.net/usort for the parameters documentation.
98 * This was originally in blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
100 * @package core_tag
101 * @access private
102 * @param string $a Tag name to compare against $b
103 * @param string $b Tag name to compare against $a
104 * @return int The result of the comparison/validation 1, 0 or -1
106 function tag_cloud_sort($a, $b) {
107 global $CFG;
109 if (empty($CFG->tagsort)) {
110 $tagsort = 'name'; // by default, sort by name
111 } else {
112 $tagsort = $CFG->tagsort;
115 if (is_numeric($a->$tagsort)) {
116 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
117 } elseif (is_string($a->$tagsort)) {
118 return strcmp($a->$tagsort, $b->$tagsort);
119 } else {
120 return 0;
125 * Prints a box with the description of a tag and its related tags
127 * @package core_tag
128 * @access public
129 * @todo MDL-31149 create a system setting for $max_tags_displayed, instead of using an in code literal
130 * @param stdClass $tag_object
131 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
132 * @return string/null a HTML box showing a description of the tag object and it's relationsips or null if output is done directly
133 * in the function.
135 function tag_print_description_box($tag_object, $return=false) {
137 global $USER, $CFG, $OUTPUT;
139 $max_tags_displayed = 10;
141 $tagname = tag_display_name($tag_object);
142 $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
144 $content = !empty($tag_object->description) || $related_tags;
145 $output = '';
147 if ($content) {
148 $output .= $OUTPUT->box_start('generalbox', 'tag-description');
151 if (!empty($tag_object->description)) {
152 $options = new stdClass();
153 $options->para = false;
154 $options->overflowdiv = true;
155 $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', context_system::instance()->id, 'tag', 'description', $tag_object->id);
156 $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
159 if ($related_tags) {
160 $more_links = false;
161 if (count($related_tags) > $max_tags_displayed) {
162 array_pop($related_tags);
163 $more_links = true;
165 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
166 if ($more_links) {
167 $output .= ' ...';
171 if ($content) {
172 $output .= $OUTPUT->box_end();
175 if ($return) {
176 return $output;
177 } else {
178 echo $output;
183 * Prints a box that contains the management links of a tag
185 * @access public
186 * @param stdClass $tag_object
187 * @param bool $return if true the function will return the generated tag cloud instead of displaying it.
188 * @return string|null a HTML string or null if this function does the output
190 function tag_print_management_box($tag_object, $return=false) {
192 global $USER, $CFG, $OUTPUT;
194 $tagname = tag_display_name($tag_object);
195 $output = '';
197 if (!isguestuser()) {
198 $output .= $OUTPUT->box_start('box','tag-management-box');
199 $systemcontext = context_system::instance();
200 $links = array();
202 // Add a link for users to add/remove this from their interests
203 if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
204 $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>';
205 } else {
206 $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>';
209 // Flag as inappropriate link. Only people with moodle/tag:flag capability.
210 if (has_capability('moodle/tag:flag', $systemcontext)) {
211 $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>';
214 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
215 if (has_capability('moodle/tag:edit', $systemcontext) ||
216 has_capability('moodle/tag:manage', $systemcontext)) {
217 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
220 $output .= implode(' | ', $links);
221 $output .= $OUTPUT->box_end();
224 if ($return) {
225 return $output;
226 } else {
227 echo $output;
232 * Prints the tag search box
234 * @access public
235 * @param bool $return if true return html string
236 * @return string|null a HTML string or null if this function does the output
238 function tag_print_search_box($return=false) {
239 global $CFG, $OUTPUT;
241 $output = $OUTPUT->box_start('','tag-search-box');
242 $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
243 $output .= '<div>';
244 $output .= '<label class="accesshide" for="searchform_search">'.get_string('searchtags', 'tag').'</label>';
245 $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
246 $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
247 $output .= '</div>';
248 $output .= '</form>';
249 $output .= $OUTPUT->box_end();
251 if ($return) {
252 return $output;
254 else {
255 echo $output;
260 * Prints the tag search results
262 * @access public
263 * @param string $query text that tag names will be matched against
264 * @param int $page current page
265 * @param int $perpage nr of users displayed per page
266 * @param bool $return if true return html string
267 * @return string|null a HTML string or null if this function does the output
269 function tag_print_search_results($query, $page, $perpage, $return=false) {
271 global $CFG, $USER, $OUTPUT;
273 $norm = tag_normalize($query, TAG_CASE_ORIGINAL);
274 $query = array_shift($norm);
276 $count = sizeof(tag_find_tags($query, false));
277 $tags = array();
279 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
280 $tags = array_values($found_tags);
283 $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
284 $output = '';
286 // link "Add $query to my interests"
287 $addtaglink = '';
288 if( !tag_record_tagged_with('user', $USER->id, $query) ) {
289 $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
290 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
293 if ( !empty($tags) ) { // there are results to display!!
294 $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
296 //print a link "Add $query to my interests"
297 if (!empty($addtaglink)) {
298 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
301 $nr_of_lis_per_ul = 6;
302 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
304 $output .= '<ul id="tag-search-results">';
305 for($i = 0; $i < $nr_of_uls; $i++) {
306 $output .= '<li>';
307 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
308 $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
309 $output .= '&#8226;'. $tag_link .'<br/>';
311 $output .= '</li>';
313 $output .= '</ul>';
314 $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
316 $output .= $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
318 else { //no results were found!!
319 $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
321 //print a link "Add $query to my interests"
322 if (!empty($addtaglink)) {
323 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
327 if ($return) {
328 return $output;
330 else {
331 echo $output;
336 * Prints a table of the users tagged with the tag passed as argument
338 * @param int $tag_object the tag we wish to return data for
339 * @param int $limitfrom (optional, required if $limitnum is set) prints users starting at this point.
340 * @param int $limitnum (optional, required if $limitfrom is set) prints this many users.
341 * @param bool $return if true return html string
342 * @return string|null a HTML string or null if this function does the output
344 function tag_print_tagged_users_table($tag_object, $limitfrom='', $limitnum='', $return=false) {
346 //List of users with this tag
347 $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
349 $output = tag_print_user_list($userlist, true);
351 if ($return) {
352 return $output;
354 else {
355 echo $output;
360 * Prints an individual user box
362 * @param user_object $user (contains the following fields: id, firstname, lastname and picture)
363 * @param bool $return if true return html string
364 * @return string|null a HTML string or null if this function does the output
366 function tag_print_user_box($user, $return=false) {
367 global $CFG, $OUTPUT;
369 $usercontext = context_user::instance($user->id);
370 $profilelink = '';
372 if ($usercontext and (has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($user->id))) {
373 $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
376 $output = $OUTPUT->box_start('user-box', 'user'. $user->id);
377 $fullname = fullname($user);
378 $alt = '';
380 if (!empty($profilelink)) {
381 $output .= '<a href="'. $profilelink .'">';
382 $alt = $fullname;
385 $output .= $OUTPUT->user_picture($user, array('size'=>100));
386 $output .= '<br />';
388 if (!empty($profilelink)) {
389 $output .= '</a>';
392 //truncate name if it's too big
393 if (core_text::strlen($fullname) > 26) {
394 $fullname = core_text::substr($fullname, 0, 26) .'...';
397 $output .= '<strong>'. $fullname .'</strong>';
398 $output .= $OUTPUT->box_end();
400 if ($return) {
401 return $output;
403 else {
404 echo $output;
409 * Prints a list of users
411 * @param array $userlist an array of user objects
412 * @param bool $return if true return html string, otherwise output the result
413 * @return string|null a HTML string or null if this function does the output
415 function tag_print_user_list($userlist, $return=false) {
417 $output = '<ul class="inline-list">';
419 foreach ($userlist as $user){
420 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
422 $output .= "</ul>\n";
424 if ($return) {
425 return $output;
427 else {
428 echo $output;