MDL-21695 adding help string
[moodle.git] / tag / locallib.php
blob6a6e82403321fcc4a8ba39fe9a0d4a1328b358bd
1 <?php
3 require_once('lib.php');
5 /**
6 * locallib.php - moodle tag local library - output functions
8 * @licence http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package moodlecore
13 /**
14 * Prints a tag cloud
16 * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
17 * @param $return if true return html string
19 function tag_print_cloud($nr_of_tags=150, $return=false) {
20 global $CFG, $DB;
22 $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
24 if ( !$tagsincloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
25 FROM {tag_instance} ti JOIN {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', null, 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;
38 $etags = array();
40 foreach ($tagsincloud as $tag) {
41 $size = (int) (( $tag->count / $maxcount) * 20);
42 $tag->class = "$tag->tagtype s$size";
43 $etags[] = $tag;
46 usort($etags, "tag_cloud_sort");
47 $output = '';
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>';
52 } else {
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";
63 if ($return) {
64 return $output;
65 } else {
66 echo $output;
70 /**
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) {
76 global $CFG;
78 if (empty($CFG->tagsort)) {
79 $tagsort = 'name'; // by default, sort by name
80 } else {
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);
88 } else {
89 return 0;
93 /**
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) {
101 global $USER, $CFG, $OUTPUT;
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;
109 $output = '';
111 if ($content) {
112 $output .= $OUTPUT->box_start('generalbox', 'tag-description');
115 if (!empty($tag_object->description)) {
116 $options = new object();
117 $options->para = false;
118 $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', get_context_instance(CONTEXT_SYSTEM)->id, 'tag_description', $tag_object->id);
119 $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
122 if ($related_tags) {
123 $more_links = false;
124 if (count($related_tags) > $max_tags_displayed) {
125 array_pop($related_tags);
126 $more_links = true;
128 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
129 if ($more_links) {
130 $output .= ' ...';
134 if ($content) {
135 $output .= $OUTPUT->box_end();
138 if ($return) {
139 return $output;
140 } else {
141 echo $output;
146 * Prints a box that contains the management links of a tag
148 * @param $tagid
149 * @param $return if true return html string
151 function tag_print_management_box($tag_object, $return=false) {
153 global $USER, $CFG, $OUTPUT;
155 $tagname = tag_display_name($tag_object);
156 $output = '';
158 if (!isguestuser()) {
159 $output .= $OUTPUT->box_start('box','tag-management-box');
160 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
161 $links = array();
163 // Add a link for users to add/remove this from their interests
164 if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
165 $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>';
166 } else {
167 $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>';
170 // flag as inappropriate link
171 $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>';
173 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
174 if (has_capability('moodle/tag:edit', $systemcontext) ||
175 has_capability('moodle/tag:manage', $systemcontext)) {
176 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
179 $output .= implode(' | ', $links);
180 $output .= $OUTPUT->box_end();
183 if ($return) {
184 return $output;
185 } else {
186 echo $output;
191 * Prints the tag search box
193 * @param bool $return if true return html string
195 function tag_print_search_box($return=false) {
196 global $CFG, $OUTPUT;
198 $output = $OUTPUT->box_start('','tag-search-box');
199 $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
200 $output .= '<div>';
201 $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
202 $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
203 $output .= '</div>';
204 $output .= '</form>';
205 $output .= $OUTPUT->box_end();
207 if ($return) {
208 return $output;
210 else {
211 echo $output;
216 * Prints the tag search results
218 * @param string $query text that tag names will be matched against
219 * @param int $page current page
220 * @param int $perpage nr of users displayed per page
221 * @param $return if true return html string
223 function tag_print_search_results($query, $page, $perpage, $return=false) {
225 global $CFG, $USER, $OUTPUT;
227 $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
229 $count = sizeof(tag_find_tags($query, false));
230 $tags = array();
232 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
233 $tags = array_values($found_tags);
236 $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
237 $output = '';
239 // link "Add $query to my interests"
240 $addtaglink = '';
241 if( !tag_record_tagged_with('user', $USER->id, $query) ) {
242 $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
243 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
246 if ( !empty($tags) ) { // there are results to display!!
247 $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
249 //print a link "Add $query to my interests"
250 if (!empty($addtaglink)) {
251 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
254 $nr_of_lis_per_ul = 6;
255 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
257 $output .= '<ul id="tag-search-results">';
258 for($i = 0; $i < $nr_of_uls; $i++) {
259 $output .= '<li>';
260 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
261 $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
262 $output .= '&#8226;'. $tag_link .'<br/>';
264 $output .= '</li>';
266 $output .= '</ul>';
267 $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
269 $output .= $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
271 else { //no results were found!!
272 $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
274 //print a link "Add $query to my interests"
275 if (!empty($addtaglink)) {
276 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
280 if ($return) {
281 return $output;
283 else {
284 echo $output;
289 * Prints a table of the users tagged with the tag passed as argument
291 * @param $tag_object
292 * @param int $users_per_row number of users per row to display
293 * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
294 * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
295 * @param $return if true return html string
297 function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
299 //List of users with this tag
300 $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
302 $output = tag_print_user_list($userlist, true);
304 if ($return) {
305 return $output;
307 else {
308 echo $output;
313 * Prints an individual user box
315 * @param $user user object (contains the following fields: id, firstname, lastname and picture)
316 * @param $return if true return html string
318 function tag_print_user_box($user, $return=false) {
319 global $CFG, $OUTPUT;
321 $textlib = textlib_get_instance();
322 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
323 $profilelink = '';
325 if ( has_capability('moodle/user:viewdetails', $usercontext) || has_coursemanager_role($user->id) ) {
326 $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
329 $output = $OUTPUT->box_start('user-box', 'user'. $user->id);
330 $fullname = fullname($user);
331 $alt = '';
333 if (!empty($profilelink)) {
334 $output .= '<a href="'. $profilelink .'">';
335 $alt = $fullname;
338 //print user image - if image is only content of link it needs ALT text!
339 if ($user->picture) {
340 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/user/pix.php/'. $user->id .'/f1.jpg" />';
341 } else {
342 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/pix/u/f1.png" />';
345 $output .= '<br />';
347 if (!empty($profilelink)) {
348 $output .= '</a>';
351 //truncate name if it's too big
352 if ($textlib->strlen($fullname) > 26) {
353 $fullname = $textlib->substr($fullname, 0, 26) .'...';
356 $output .= '<strong>'. $fullname .'</strong>';
357 $output .= $OUTPUT->box_end();
359 if ($return) {
360 return $output;
362 else {
363 echo $output;
367 * Prints a list of users
369 * @param array $userlist an array of user objects
370 * @param $return if true return html string
372 function tag_print_user_list($userlist, $return=false) {
374 $output = '<ul class="inline-list">';
376 foreach ($userlist as $user){
377 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
379 $output .= "</ul>\n";
381 if ($return) {
382 return $output;
384 else {
385 echo $output;