MDL-13590 - adding locallib.php for HEAD
[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
150 // if the user is not tagged with the $tag_object tag, a link "add blahblah to my interests" will appear
151 if( !tag_record_tagged_with(array('type'=>'user', 'id'=>$USER->id), $tag_object->name )) {
152 $links[] = '<a href="'. $CFG->wwwroot .'/user/tag.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('addtagtomyinterests', 'tag', $tagname) .'</a>';
153 }
154
155 // only people with moodle/tag:edit capability may edit the tag description
156 if ( has_capability('moodle/tag:edit', $systemcontext) && tag_record_tagged_with(array('type'=>'user', 'id'=>$USER->id), $tag_object->name ) ) {
157 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
158 }
159
160 // flag as inappropriate link
161 $links[] = '<a href="'. $CFG->wwwroot .'/user/tag.php?action=flaginappropriate&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('flagasinappropriate', 'tag', rawurlencode($tagname)) .'</a>';
162
163 // Manage all tags links
164 if ( has_capability('moodle/tag:manage', $systemcontext) ) {
165 $links[] = '<a href="'. $CFG->wwwroot .'/tag/manage.php">'. get_string('managetags', 'tag') .'</a>' ;
166 }
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
216 $count = sizeof(tag_search($query, false));
217 $tags = array();
218
219 if ( $found_tags = tag_search($query, true, $page * $perpage, $perpage) ) {
220 $tags = array_values($found_tags);
221 }
222
223 $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
224 $output = '';
225
226 // link "Add $query to my interests"
227 $addtaglink = '';
228 if( !is_item_tagged_with('user', $USER->id, $query) ) {
229 $addtaglink = '<a href="'. $CFG->wwwroot .'/user/tag.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
230 $addtaglink .= get_string('addtagtomyinterests', 'tag', rawurlencode($query)) .'</a>';
231 }
232
233 if ( !empty($tags) ) { // there are results to display!!
234 $output .= print_heading(get_string('searchresultsfor', 'tag', rawurlencode($query)) ." : {$count}", '', 3, 'main', true);
235
236 //print a link "Add $query to my interests"
237 if (!empty($addtaglink)) {
238 $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
239 }
240
241 $nr_of_lis_per_ul = 6;
242 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
243
244 $output .= '<ul id="tag-search-results">';
245 for($i = 0; $i < $nr_of_uls; $i++) {
246 $output .= '<li>';
247 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
248 $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
249 $output .= '&#8226;'. $tag_link .'<br/>';
250 }
251 $output .= '</li>';
252 }
253 $output .= '</ul>';
254 $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
255
256 $output .= print_paging_bar($count, $page, $perpage, $baseurl .'&amp;', 'page', false, true);
257 }
258 else { //no results were found!!
259 $output .= print_heading(get_string('noresultsfor', 'tag', rawurlencode($query)), '', 3, 'main' , true);
260
261 //print a link "Add $query to my interests"
262 if (!empty($addtaglink)) {
263 $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
264 }
265 }
266
267 if ($return) {
268 return $output;
269 }
270 else {
271 echo $output;
272 }
273}
274
275/**
276 * Prints a table of the users tagged with the tag passed as argument
277 *
278 * @param $tag_object
279 * @param int $users_per_row number of users per row to display
280 * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
281 * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
282 * @param $return if true return html string
283 */
284function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
285
286 //List of users with this tag
287 $userlist = tag_find_records($tag_object->name, 'user');
288
289 $output = tag_print_user_list($userlist, true);
290
291 if ($return) {
292 return $output;
293 }
294 else {
295 echo $output;
296 }
297}
298
299/**
300 * Prints an individual user box
301 *
302 * @param $user user object (contains the following fields: id, firstname, lastname and picture)
303 * @param $return if true return html string
304 */
305function tag_print_user_box($user, $return=false) {
306 global $CFG;
307
308 $textlib = textlib_get_instance();
309 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
310 $profilelink = '';
311
312 if ( has_capability('moodle/user:viewdetails', $usercontext) ) {
313 $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
314 }
315
316 $output = print_box_start('user-box', 'user'. $user->id, true);
317 $fullname = fullname($user);
318 $alt = '';
319
320 if (!empty($profilelink)) {
321 $output .= '<a href="'. $profilelink .'">';
322 $alt = $fullname;
323 }
324
325 //print user image - if image is only content of link it needs ALT text!
326 if ($user->picture) {
327 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/user/pix.php/'. $user->id .'/f1.jpg" />';
328 } else {
329 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/pix/u/f1.png" />';
330 }
331
332 $output .= '<br />';
333
334 if (!empty($profilelink)) {
335 $output .= '</a>';
336 }
337
338 //truncate name if it's too big
339 if ($textlib->strlen($fullname) > 26) {
340 $fullname = $textlib->substr($fullname, 0, 26) .'...';
341 }
342
343 $output .= '<strong>'. $fullname .'</strong>';
344 $output .= print_box_end(true);
345
346 if ($return) {
347 return $output;
348 }
349 else {
350 echo $output;
351 }
352}
353/**
354 * Prints a list of users
355 *
356 * @param array $userlist an array of user objects
357 * @param $return if true return html string
358 */
359function tag_print_user_list($userlist, $return=false) {
360
361 $output = '<ul class="inline-list">';
362
363 foreach ($userlist as $user){
364 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
365 }
366 $output .= "</ul>\n";
367
368 if ($return) {
369 return $output;
370 }
371 else {
372 echo $output;
373 }
374}
375
376
377?>