2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Moodle tag local library - output functions
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');
30 * Prints or returns a HTML tag cloud with varying classes styles depending on the popularity and type of each 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) {
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();
55 $tagsincloud = $tagset;
58 $tagkeys = array_keys($tagsincloud);
59 if (!empty($tagkeys)) {
60 $firsttagkey = $tagkeys[0];
61 $maxcount = $tagsincloud[$firsttagkey]->count
;
66 foreach ($tagsincloud as $tag) {
67 $size = (int) (( $tag->count
/ $maxcount) * 20);
68 $tag->class = "$tag->tagtype s$size";
72 usort($etags, "tag_cloud_sort");
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>';
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";
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().
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) {
109 if (empty($CFG->tagsort
)) {
110 $tagsort = 'name'; // by default, sort by name
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);
125 * Prints a box with the description of a tag and its related tags
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
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;
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);
161 if (count($related_tags) > $max_tags_displayed) {
162 array_pop($related_tags);
165 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
172 $output .= $OUTPUT->box_end();
183 * Prints a box that contains the management links of a tag
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);
197 if (!isguestuser()) {
198 $output .= $OUTPUT->box_start('box','tag-management-box');
199 $systemcontext = context_system
::instance();
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&sesskey='. sesskey() .'&tag='. rawurlencode($tag_object->name
) .'">'. get_string('removetagfrommyinterests', 'tag', $tagname) .'</a>';
206 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=addinterest&sesskey='. sesskey() .'&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&sesskey='. sesskey() .'&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();
232 * Prints the tag search box
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">';
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 />';
248 $output .= '</form>';
249 $output .= $OUTPUT->box_end();
260 * Prints the tag search results
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));
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);
286 // link "Add $query to my interests"
288 if( !tag_record_tagged_with('user', $USER->id
, $query) ) {
289 $addtaglink = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=addinterest&sesskey='. sesskey() .'&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++
) {
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 .= '•'. $tag_link .'<br/>';
314 $output .= '<div> </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');
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);
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
);
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);
380 if (!empty($profilelink)) {
381 $output .= '<a href="'. $profilelink .'">';
385 $output .= $OUTPUT->user_picture($user, array('size'=>100));
388 if (!empty($profilelink)) {
392 //truncate name if it's too big
393 if (textlib
::strlen($fullname) > 26) {
394 $fullname = textlib
::substr($fullname, 0, 26) .'...';
397 $output .= '<strong>'. $fullname .'</strong>';
398 $output .= $OUTPUT->box_end();
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";