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