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