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