[moodle] / moodle / tag / locallib.php Repository:

Annotation of /moodle/tag/locallib.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.2.13 - (view) (download)

1 : scyrma 1.1.2.13 <?php // $Id: locallib.php,v 1.1.2.12 2008/03/05 01:44:33 scyrma Exp $
2 : scyrma 1.1.2.2
3 :     /**
4 :     * locallib.php - moodle tag local library - output functions
5 :     *
6 : scyrma 1.1.2.13 * @version: $Id: locallib.php,v 1.1.2.12 2008/03/05 01:44:33 scyrma Exp $
7 : scyrma 1.1.2.2 * @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 $return if true return html string
17 :     */
18 : scyrma 1.1.2.9 function tag_print_cloud($nr_of_tags=150, $return=false) {
19 : scyrma 1.1.2.2
20 :     global $CFG;
21 : scyrma 1.1.2.8
22 :     $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
23 : scyrma 1.1.2.2
24 : scyrma 1.1.2.13 if ( !$tagcloud = get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag '.
25 : scyrma 1.1.2.2 'FROM '. $CFG->prefix .'tag_instance ti INNER JOIN '. $CFG->prefix .'tag tg ON tg.id = ti.tagid '.
26 : scyrma 1.1.2.12 'GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype '.
27 : scyrma 1.1.2.13 'ORDER BY count DESC, tg.name ASC', 0, $nr_of_tags) ) {
28 :     $tagcloud = array();
29 :     }
30 : scyrma 1.1.2.2
31 : scyrma 1.1.2.8 $totaltags = count($tagcloud);
32 :     $currenttag = 0;
33 :     $size = 20;
34 :     $lasttagct = -1;
35 :    
36 :     $etags = array();
37 :     foreach ($tagcloud as $tag) {
38 :    
39 :     $currenttag++;
40 :    
41 :     if ($currenttag == 1) {
42 :     $lasttagct = $tag->count;
43 :     $size = 20;
44 :     } else if ($tag->count != $lasttagct) {
45 :     $lasttagct = $tag->count;
46 :     $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
47 : scyrma 1.1.2.2 }
48 :    
49 : scyrma 1.1.2.8 $tag->class = "$tag->tagtype s$size";
50 :     $etags[] = $tag;
51 : scyrma 1.1.2.2 }
52 :    
53 : scyrma 1.1.2.8 usort($etags, "tag_cloud_sort");
54 :     $output = '';
55 :     $output .= "\n<ul class='tag_cloud inline-list'>\n";
56 :     foreach ($etags as $tag) {
57 : scyrma 1.1.2.2 if ($tag->flag > 0 && $can_manage_tags) {
58 :     $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
59 : scyrma 1.1.2.8 } else {
60 :     $tagname = tag_display_name($tag);
61 : scyrma 1.1.2.2 }
62 :    
63 : scyrma 1.1.2.8 $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name) .'"';
64 :     $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
65 :     'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
66 :     $tagname .'</a></li> ';
67 : scyrma 1.1.2.2 }
68 : scyrma 1.1.2.8 $output .= "\n</ul>\n";
69 : scyrma 1.1.2.2
70 :     if ($return) {
71 :     return $output;
72 :     } else {
73 :     echo $output;
74 :     }
75 :     }
76 :    
77 :     /**
78 : scyrma 1.1.2.8 * This function is used by print_tag_cloud, to usort() the tags in the cloud.
79 :     * See php.net/usort for the parameters documentation. This was originally in
80 :     * blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
81 :     */
82 :     function tag_cloud_sort($a, $b) {
83 :     global $CFG;
84 :    
85 :     if (empty($CFG->tagsort)) {
86 : scyrma 1.1.2.10 $tagsort = 'name'; // by default, sort by name
87 : scyrma 1.1.2.8 } else {
88 :     $tagsort = $CFG->tagsort;
89 :     }
90 :    
91 :     if (is_numeric($a->$tagsort)) {
92 :     return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
93 :     } elseif (is_string($a->$tagsort)) {
94 :     return strcmp($a->$tagsort, $b->$tagsort);
95 :     } else {
96 :     return 0;
97 :     }
98 :     }
99 :    
100 :     /**
101 : scyrma 1.1.2.2 * Prints a box with the description of a tag and its related tags
102 :     *
103 :     * @param unknown_type $tag_object
104 :     * @param $return if true return html string
105 :     */
106 :     function tag_print_description_box($tag_object, $return=false) {
107 :    
108 :     global $USER, $CFG;
109 :    
110 : scyrma 1.1.2.11 $max_tags_displayed = 10; // todo: turn this into a system setting
111 :    
112 : scyrma 1.1.2.2 $tagname = tag_display_name($tag_object);
113 : scyrma 1.1.2.11 $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
114 : scyrma 1.1.2.2
115 :     $content = !empty($tag_object->description) || $related_tags;
116 :     $output = '';
117 :    
118 :     if ($content) {
119 :     $output .= print_box_start('generalbox', 'tag-description', true);
120 :     }
121 :    
122 :     if (!empty($tag_object->description)) {
123 :     $options = new object();
124 :     $options->para = false;
125 :     $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
126 :     }
127 :    
128 :     if ($related_tags) {
129 : scyrma 1.1.2.11 $more_links = false;
130 :     if (count($related_tags) > $max_tags_displayed) {
131 :     array_pop($related_tags);
132 :     $more_links = true;
133 :     }
134 : scyrma 1.1.2.2 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
135 : scyrma 1.1.2.11 if ($more_links) {
136 :     $output .= ' ...';
137 :     }
138 : scyrma 1.1.2.2 }
139 :    
140 :     if ($content) {
141 :     $output .= print_box_end(true);
142 :     }
143 :    
144 :     if ($return) {
145 :     return $output;
146 :     } else {
147 :     echo $output;
148 :     }
149 :     }
150 :    
151 :     /**
152 :     * Prints a box that contains the management links of a tag
153 :     *
154 :     * @param $tagid
155 :     * @param $return if true return html string
156 :     */
157 :     function tag_print_management_box($tag_object, $return=false) {
158 :    
159 :     global $USER, $CFG;
160 :    
161 :     $tagname = tag_display_name($tag_object);
162 :     $output = '';
163 :    
164 :     if (!isguestuser()) {
165 :     $output .= print_box_start('box','tag-management-box', true);
166 :     $systemcontext = get_context_instance(CONTEXT_SYSTEM);
167 :     $links = array();
168 :    
169 : moodler 1.1.2.5 // Add a link for users to add/remove this from their interests
170 : scyrma 1.1.2.6 if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
171 : moodler 1.1.2.5 $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>';
172 :     } else {
173 :     $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>';
174 : scyrma 1.1.2.2 }
175 :    
176 : moodler 1.1.2.5 // flag as inappropriate link
177 :     $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>';
178 :    
179 :     // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
180 :     if (has_capability('moodle/tag:edit', $systemcontext) &&
181 : scyrma 1.1.2.6 (tag_record_tagged_with('user', $USER->id, $tag_object->name) ||
182 : moodler 1.1.2.5 has_capability('moodle/tag:manage', $systemcontext))) {
183 : scyrma 1.1.2.2 $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
184 :     }
185 :    
186 :    
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 :     */
203 :     function 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 :     */
231 :     function tag_print_search_results($query, $page, $perpage, $return=false) {
232 :    
233 :     global $CFG, $USER;
234 :    
235 : scyrma 1.1.2.6 $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
236 :    
237 :     $count = sizeof(tag_find_tags($query, false));
238 : scyrma 1.1.2.2 $tags = array();
239 :    
240 : scyrma 1.1.2.6 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
241 : scyrma 1.1.2.2 $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 = '';
249 : scyrma 1.1.2.6 if( !tag_record_tagged_with('user', $USER->id, $query) ) {
250 : moodler 1.1.2.5 $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
251 : scyrma 1.1.2.6 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
252 : scyrma 1.1.2.2 }
253 :    
254 :     if ( !empty($tags) ) { // there are results to display!!
255 : scyrma 1.1.2.6 $output .= print_heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", '', 3, 'main', true);
256 : scyrma 1.1.2.2
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!!
280 : scyrma 1.1.2.6 $output .= print_heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), '', 3, 'main' , true);
281 : scyrma 1.1.2.2
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 :     */
305 :     function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
306 :    
307 :     //List of users with this tag
308 : scyrma 1.1.2.7 $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
309 : scyrma 1.1.2.2
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 :     */
326 :     function 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 :    
333 :     if ( has_capability('moodle/user:viewdetails', $usercontext) ) {
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 :     */
380 :     function 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 :     ?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7