"MDL-14668, fix course full name in activity reports, see tracker"
[moodle-linuxchix.git] / tag / locallib.php
blob635e75ffd2971bf8dd8f55edaf236f0f98a6ed18
1 <?php // $Id$
3 /**
4 * locallib.php - moodle tag local library - output functions
6 * @version: $Id$
7 * @licence http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package moodlecore
12 /**
13 * Prints a tag cloud
15 * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
16 * @param $return if true return html string
18 function tag_print_cloud($nr_of_tags=150, $return=false) {
20 global $CFG;
22 $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
24 if ( !$tagcloud = get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag '.
25 'FROM '. $CFG->prefix .'tag_instance ti INNER JOIN '. $CFG->prefix .'tag tg ON tg.id = ti.tagid '.
26 'WHERE ti.itemtype != \'tag\' '.
27 'GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype '.
28 'ORDER BY count DESC, tg.name ASC', 0, $nr_of_tags) ) {
29 $tagcloud = array();
32 $totaltags = count($tagcloud);
33 $currenttag = 0;
34 $size = 20;
35 $lasttagct = -1;
37 $etags = array();
38 foreach ($tagcloud as $tag) {
40 $currenttag++;
42 if ($currenttag == 1) {
43 $lasttagct = $tag->count;
44 $size = 20;
45 } else if ($tag->count != $lasttagct) {
46 $lasttagct = $tag->count;
47 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
50 $tag->class = "$tag->tagtype s$size";
51 $etags[] = $tag;
54 usort($etags, "tag_cloud_sort");
55 $output = '';
56 $output .= "\n<ul class='tag_cloud inline-list'>\n";
57 foreach ($etags as $tag) {
58 if ($tag->flag > 0 && $can_manage_tags) {
59 $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
60 } else {
61 $tagname = tag_display_name($tag);
64 $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
65 $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
66 'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
67 $tagname .'</a></li> ';
69 $output .= "\n</ul>\n";
71 if ($return) {
72 return $output;
73 } else {
74 echo $output;
78 /**
79 * This function is used by print_tag_cloud, to usort() the tags in the cloud.
80 * See php.net/usort for the parameters documentation. This was originally in
81 * blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
83 function tag_cloud_sort($a, $b) {
84 global $CFG;
86 if (empty($CFG->tagsort)) {
87 $tagsort = 'name'; // by default, sort by name
88 } else {
89 $tagsort = $CFG->tagsort;
92 if (is_numeric($a->$tagsort)) {
93 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
94 } elseif (is_string($a->$tagsort)) {
95 return strcmp($a->$tagsort, $b->$tagsort);
96 } else {
97 return 0;
102 * Prints a box with the description of a tag and its related tags
104 * @param unknown_type $tag_object
105 * @param $return if true return html string
107 function tag_print_description_box($tag_object, $return=false) {
109 global $USER, $CFG;
111 $max_tags_displayed = 10; // todo: turn this into a system setting
113 $tagname = tag_display_name($tag_object);
114 $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
116 $content = !empty($tag_object->description) || $related_tags;
117 $output = '';
119 if ($content) {
120 $output .= print_box_start('generalbox', 'tag-description', true);
123 if (!empty($tag_object->description)) {
124 $options = new object();
125 $options->para = false;
126 $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
129 if ($related_tags) {
130 $more_links = false;
131 if (count($related_tags) > $max_tags_displayed) {
132 array_pop($related_tags);
133 $more_links = true;
135 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
136 if ($more_links) {
137 $output .= ' ...';
141 if ($content) {
142 $output .= print_box_end(true);
145 if ($return) {
146 return $output;
147 } else {
148 echo $output;
153 * Prints a box that contains the management links of a tag
155 * @param $tagid
156 * @param $return if true return html string
158 function tag_print_management_box($tag_object, $return=false) {
160 global $USER, $CFG;
162 $tagname = tag_display_name($tag_object);
163 $output = '';
165 if (!isguestuser()) {
166 $output .= print_box_start('box','tag-management-box', true);
167 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
168 $links = array();
170 // Add a link for users to add/remove this from their interests
171 if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
172 $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>';
173 } else {
174 $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>';
177 // flag as inappropriate link
178 $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 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
181 if (has_capability('moodle/tag:edit', $systemcontext) ||
182 has_capability('moodle/tag:manage', $systemcontext)) {
183 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
186 $output .= implode(' | ', $links);
187 $output .= print_box_end(true);
190 if ($return) {
191 return $output;
192 } else {
193 echo $output;
198 * Prints the tag search box
200 * @param bool $return if true return html string
202 function tag_print_search_box($return=false) {
203 global $CFG;
205 $output = print_box_start('','tag-search-box', true);
206 $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
207 $output .= '<div>';
208 $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
209 $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
210 $output .= '</div>';
211 $output .= '</form>';
212 $output .= print_box_end(true);
214 if ($return) {
215 return $output;
217 else {
218 echo $output;
223 * Prints the tag search results
225 * @param string $query text that tag names will be matched against
226 * @param int $page current page
227 * @param int $perpage nr of users displayed per page
228 * @param $return if true return html string
230 function tag_print_search_results($query, $page, $perpage, $return=false) {
232 global $CFG, $USER;
234 $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
236 $count = sizeof(tag_find_tags($query, false));
237 $tags = array();
239 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
240 $tags = array_values($found_tags);
243 $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
244 $output = '';
246 // link "Add $query to my interests"
247 $addtaglink = '';
248 if( !tag_record_tagged_with('user', $USER->id, $query) ) {
249 $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
250 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
253 if ( !empty($tags) ) { // there are results to display!!
254 $output .= print_heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", '', 3, 'main', true);
256 //print a link "Add $query to my interests"
257 if (!empty($addtaglink)) {
258 $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
261 $nr_of_lis_per_ul = 6;
262 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
264 $output .= '<ul id="tag-search-results">';
265 for($i = 0; $i < $nr_of_uls; $i++) {
266 $output .= '<li>';
267 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
268 $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
269 $output .= '&#8226;'. $tag_link .'<br/>';
271 $output .= '</li>';
273 $output .= '</ul>';
274 $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
276 $output .= print_paging_bar($count, $page, $perpage, $baseurl .'&amp;', 'page', false, true);
278 else { //no results were found!!
279 $output .= print_heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), '', 3, 'main' , true);
281 //print a link "Add $query to my interests"
282 if (!empty($addtaglink)) {
283 $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
287 if ($return) {
288 return $output;
290 else {
291 echo $output;
296 * Prints a table of the users tagged with the tag passed as argument
298 * @param $tag_object
299 * @param int $users_per_row number of users per row to display
300 * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
301 * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
302 * @param $return if true return html string
304 function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
306 //List of users with this tag
307 $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
309 $output = tag_print_user_list($userlist, true);
311 if ($return) {
312 return $output;
314 else {
315 echo $output;
320 * Prints an individual user box
322 * @param $user user object (contains the following fields: id, firstname, lastname and picture)
323 * @param $return if true return html string
325 function tag_print_user_box($user, $return=false) {
326 global $CFG;
328 $textlib = textlib_get_instance();
329 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
330 $profilelink = '';
332 if ( has_capability('moodle/user:viewdetails', $usercontext) ) {
333 $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
336 $output = print_box_start('user-box', 'user'. $user->id, true);
337 $fullname = fullname($user);
338 $alt = '';
340 if (!empty($profilelink)) {
341 $output .= '<a href="'. $profilelink .'">';
342 $alt = $fullname;
345 //print user image - if image is only content of link it needs ALT text!
346 if ($user->picture) {
347 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/user/pix.php/'. $user->id .'/f1.jpg" />';
348 } else {
349 $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/pix/u/f1.png" />';
352 $output .= '<br />';
354 if (!empty($profilelink)) {
355 $output .= '</a>';
358 //truncate name if it's too big
359 if ($textlib->strlen($fullname) > 26) {
360 $fullname = $textlib->substr($fullname, 0, 26) .'...';
363 $output .= '<strong>'. $fullname .'</strong>';
364 $output .= print_box_end(true);
366 if ($return) {
367 return $output;
369 else {
370 echo $output;
374 * Prints a list of users
376 * @param array $userlist an array of user objects
377 * @param $return if true return html string
379 function tag_print_user_list($userlist, $return=false) {
381 $output = '<ul class="inline-list">';
383 foreach ($userlist as $user){
384 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
386 $output .= "</ul>\n";
388 if ($return) {
389 return $output;
391 else {
392 echo $output;