MDL-19823 Converted print_box* to $OUTPUT->box*
[moodle.git] / tag / locallib.php
CommitLineData
d3477228 1<?php // $Id$
2
48d56528 3require_once('lib.php');
4
d3477228 5/**
6 * locallib.php - moodle tag local library - output functions
7 *
8 * @version: $Id$
9 * @licence http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package moodlecore
11 *
12 */
13
14/**
15 * Prints a tag cloud
16 *
17 * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
d3477228 18 * @param $return if true return html string
19 */
cf79f661 20function tag_print_cloud($nr_of_tags=150, $return=false) {
f7ed2687 21 global $CFG, $DB;
456d32fd 22
4e6d0c94 23 $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
d3477228 24
dae7b4f6 25 if ( !$tagsincloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
26 FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
27 WHERE ti.itemtype <> \'tag\'
28 GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype
29 ORDER BY count DESC, tg.name ASC', null, 0, $nr_of_tags) ) {
30 $tagsincloud = array();
420c306e 31 }
d3477228 32
dae7b4f6 33 $tagkeys = array_keys($tagsincloud);
f6d7d5f5 34 if (!empty($tagkeys)) {
35 $firsttagkey = $tagkeys[0];
36 $maxcount = $tagsincloud[$firsttagkey]->count;
37 }
d3477228 38
4e6d0c94 39 $etags = array();
d3477228 40
dae7b4f6 41 foreach ($tagsincloud as $tag) {
42 $size = (int) (( $tag->count / $maxcount) * 20);
4e6d0c94 43 $tag->class = "$tag->tagtype s$size";
44 $etags[] = $tag;
d3477228 45 }
46
dae7b4f6 47 usort($etags, "tag_cloud_sort");
4e6d0c94 48 $output = '';
49 $output .= "\n<ul class='tag_cloud inline-list'>\n";
50 foreach ($etags as $tag) {
d3477228 51 if ($tag->flag > 0 && $can_manage_tags) {
dae7b4f6 52 $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
53 } else {
4e6d0c94 54 $tagname = tag_display_name($tag);
d3477228 55 }
56
5a620a8c 57 $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
4e6d0c94 58 $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
59 'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
60 $tagname .'</a></li> ';
d3477228 61 }
4e6d0c94 62 $output .= "\n</ul>\n";
d3477228 63
64 if ($return) {
65 return $output;
66 } else {
67 echo $output;
68 }
69}
70
4e6d0c94 71/**
456d32fd 72 * This function is used by print_tag_cloud, to usort() the tags in the cloud.
4e6d0c94 73 * See php.net/usort for the parameters documentation. This was originally in
74 * blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
75 */
76function tag_cloud_sort($a, $b) {
77 global $CFG;
78
79 if (empty($CFG->tagsort)) {
47b13a7b 80 $tagsort = 'name'; // by default, sort by name
4e6d0c94 81 } else {
82 $tagsort = $CFG->tagsort;
83 }
84
85 if (is_numeric($a->$tagsort)) {
86 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
87 } elseif (is_string($a->$tagsort)) {
88 return strcmp($a->$tagsort, $b->$tagsort);
89 } else {
90 return 0;
91 }
92}
93
d3477228 94/**
95 * Prints a box with the description of a tag and its related tags
96 *
97 * @param unknown_type $tag_object
98 * @param $return if true return html string
99 */
100function tag_print_description_box($tag_object, $return=false) {
101
456d32fd 102 global $USER, $CFG, $OUTPUT;
d3477228 103
b50d2245 104 $max_tags_displayed = 10; // todo: turn this into a system setting
105
d3477228 106 $tagname = tag_display_name($tag_object);
b50d2245 107 $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
d3477228 108
109 $content = !empty($tag_object->description) || $related_tags;
110 $output = '';
111
112 if ($content) {
456d32fd 113 $output .= $OUTPUT->box_start('generalbox', 'tag-description');
d3477228 114 }
115
116 if (!empty($tag_object->description)) {
117 $options = new object();
118 $options->para = false;
119 $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
120 }
121
122 if ($related_tags) {
b50d2245 123 $more_links = false;
124 if (count($related_tags) > $max_tags_displayed) {
125 array_pop($related_tags);
126 $more_links = true;
127 }
d3477228 128 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
b50d2245 129 if ($more_links) {
130 $output .= ' ...';
131 }
d3477228 132 }
133
134 if ($content) {
456d32fd 135 $output .= $OUTPUT->box_end();
d3477228 136 }
137
138 if ($return) {
139 return $output;
140 } else {
141 echo $output;
142 }
143}
144
145/**
146 * Prints a box that contains the management links of a tag
147 *
148 * @param $tagid
149 * @param $return if true return html string
150 */
151function tag_print_management_box($tag_object, $return=false) {
152
456d32fd 153 global $USER, $CFG, $OUTPUT;
d3477228 154
155 $tagname = tag_display_name($tag_object);
156 $output = '';
157
158 if (!isguestuser()) {
456d32fd 159 $output .= $OUTPUT->box_start('box','tag-management-box');
d3477228 160 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
161 $links = array();
456d32fd 162
3627b0e3 163 // Add a link for users to add/remove this from their interests
0f039283 164 if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
3627b0e3 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>';
d3477228 168 }
169
3627b0e3 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>';
172
173 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
456d32fd 174 if (has_capability('moodle/tag:edit', $systemcontext) ||
3efae234 175 has_capability('moodle/tag:manage', $systemcontext)) {
d3477228 176 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
177 }
178
d3477228 179 $output .= implode(' | ', $links);
456d32fd 180 $output .= $OUTPUT->box_end();
d3477228 181 }
182
183 if ($return) {
184 return $output;
185 } else {
186 echo $output;
187 }
188}
189
190/**
191 * Prints the tag search box
192 *
193 * @param bool $return if true return html string
194 */
195function tag_print_search_box($return=false) {
456d32fd 196 global $CFG, $OUTPUT;
d3477228 197
456d32fd 198 $output = $OUTPUT->box_start('','tag-search-box');
d3477228 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>';
456d32fd 205 $output .= $OUTPUT->box_end();
d3477228 206
207 if ($return) {
208 return $output;
209 }
210 else {
211 echo $output;
212 }
213}
214
215/**
216 * Prints the tag search results
217 *
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
222 */
223function tag_print_search_results($query, $page, $perpage, $return=false) {
224
73aec3a7 225 global $CFG, $USER, $OUTPUT;
d3477228 226
0f039283 227 $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
228
229 $count = sizeof(tag_find_tags($query, false));
d3477228 230 $tags = array();
231
0f039283 232 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
d3477228 233 $tags = array_values($found_tags);
234 }
235
236 $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
237 $output = '';
238
239 // link "Add $query to my interests"
240 $addtaglink = '';
0f039283 241 if( !tag_record_tagged_with('user', $USER->id, $query) ) {
3627b0e3 242 $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
0f039283 243 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
d3477228 244 }
245
246 if ( !empty($tags) ) { // there are results to display!!
73aec3a7 247 $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
d3477228 248
249 //print a link "Add $query to my interests"
250 if (!empty($addtaglink)) {
456d32fd 251 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
d3477228 252 }
253
254 $nr_of_lis_per_ul = 6;
255 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
256
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/>';
263 }
264 $output .= '</li>';
265 }
266 $output .= '</ul>';
267 $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
268
928a93da 269 $output .= $OUTPUT->paging_bar(moodle_paging_bar::make($count, $page, $perpage, $baseurl));
d3477228 270 }
271 else { //no results were found!!
73aec3a7 272 $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
d3477228 273
274 //print a link "Add $query to my interests"
275 if (!empty($addtaglink)) {
456d32fd 276 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
d3477228 277 }
278 }
279
280 if ($return) {
281 return $output;
282 }
283 else {
284 echo $output;
285 }
286}
287
288/**
289 * Prints a table of the users tagged with the tag passed as argument
290 *
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
296 */
297function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
298
299 //List of users with this tag
2dc038ff 300 $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
d3477228 301
302 $output = tag_print_user_list($userlist, true);
303
304 if ($return) {
305 return $output;
306 }
307 else {
308 echo $output;
309 }
310}
311
312/**
313 * Prints an individual user box
314 *
315 * @param $user user object (contains the following fields: id, firstname, lastname and picture)
316 * @param $return if true return html string
317 */
318function tag_print_user_box($user, $return=false) {
456d32fd 319 global $CFG, $OUTPUT;
d3477228 320
321 $textlib = textlib_get_instance();
322 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
323 $profilelink = '';
324
1cb1f985 325 if ( has_capability('moodle/user:viewdetails', $usercontext) || isteacherinanycourse($user->id) ) {
d3477228 326 $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
327 }
328
456d32fd 329 $output = $OUTPUT->box_start('user-box', 'user'. $user->id);
d3477228 330 $fullname = fullname($user);
331 $alt = '';
332
333 if (!empty($profilelink)) {
334 $output .= '<a href="'. $profilelink .'">';
335 $alt = $fullname;
336 }
337
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" />';
343 }
456d32fd 344
d3477228 345 $output .= '<br />';
346
347 if (!empty($profilelink)) {
348 $output .= '</a>';
349 }
350
351 //truncate name if it's too big
352 if ($textlib->strlen($fullname) > 26) {
353 $fullname = $textlib->substr($fullname, 0, 26) .'...';
354 }
355
356 $output .= '<strong>'. $fullname .'</strong>';
456d32fd 357 $output .= $OUTPUT->box_end();
d3477228 358
359 if ($return) {
360 return $output;
361 }
362 else {
363 echo $output;
364 }
365}
366/**
367 * Prints a list of users
368 *
369 * @param array $userlist an array of user objects
370 * @param $return if true return html string
371 */
372function tag_print_user_list($userlist, $return=false) {
373
374 $output = '<ul class="inline-list">';
375
376 foreach ($userlist as $user){
377 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
378 }
379 $output .= "</ul>\n";
380
381 if ($return) {
382 return $output;
383 }
384 else {
385 echo $output;
386 }
387}
388
389
390?>