3 require_once($CFG->dirroot
.'/tag/lib.php');
4 require_once($CFG->libdir
.'/filelib.php');
7 * locallib.php - moodle tag local library - output functions
9 * @licence http://www.gnu.org/copyleft/gpl.html GNU Public License
17 * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
18 * @param $return if true return html string
20 function tag_print_cloud($nr_of_tags=150, $return=false) {
23 $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM
));
25 if ( !$tagsincloud = $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) ) {
30 $tagsincloud = array();
33 $tagkeys = array_keys($tagsincloud);
34 if (!empty($tagkeys)) {
35 $firsttagkey = $tagkeys[0];
36 $maxcount = $tagsincloud[$firsttagkey]->count
;
41 foreach ($tagsincloud as $tag) {
42 $size = (int) (( $tag->count
/ $maxcount) * 20);
43 $tag->class = "$tag->tagtype s$size";
47 usort($etags, "tag_cloud_sort");
49 $output .= "\n<ul class='tag_cloud inline-list'>\n";
50 foreach ($etags as $tag) {
51 if ($tag->flag
> 0 && $can_manage_tags) {
52 $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
54 $tagname = tag_display_name($tag);
57 $link = $CFG->wwwroot
.'/tag/index.php?tag='. rawurlencode($tag->name
);
58 $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
59 'title="'. get_string('numberofentries', 'blog', $tag->count
) .'">'.
60 $tagname .'</a></li> ';
62 $output .= "\n</ul>\n";
72 * This function is used by print_tag_cloud, to usort() the tags in the cloud.
73 * See php.net/usort for the parameters documentation. This was originally in
74 * blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
76 function tag_cloud_sort($a, $b) {
79 if (empty($CFG->tagsort
)) {
80 $tagsort = 'name'; // by default, sort by name
82 $tagsort = $CFG->tagsort
;
85 if (is_numeric($a->$tagsort)) {
86 return ($a->$tagsort == $b->$tagsort) ?
0 : ($a->$tagsort > $b->$tagsort) ?
1 : -1;
87 } elseif (is_string($a->$tagsort)) {
88 return strcmp($a->$tagsort, $b->$tagsort);
95 * Prints a box with the description of a tag and its related tags
97 * @param stdClass $tag_object
98 * @param $return if true return html string
100 function tag_print_description_box($tag_object, $return=false) {
102 global $USER, $CFG, $OUTPUT;
104 $max_tags_displayed = 10; // todo: turn this into a system setting
106 $tagname = tag_display_name($tag_object);
107 $related_tags = tag_get_related_tags($tag_object->id
, TAG_RELATED_ALL
, $max_tags_displayed+
1); // this gets one more than we want
109 $content = !empty($tag_object->description
) ||
$related_tags;
113 $output .= $OUTPUT->box_start('generalbox', 'tag-description');
116 if (!empty($tag_object->description
)) {
117 $options = new stdClass();
118 $options->para
= false;
119 $options->overflowdiv
= true;
120 $tag_object->description
= file_rewrite_pluginfile_urls($tag_object->description
, 'pluginfile.php', get_context_instance(CONTEXT_SYSTEM
)->id
, 'tag', 'description', $tag_object->id
);
121 $output .= format_text($tag_object->description
, $tag_object->descriptionformat
, $options);
126 if (count($related_tags) > $max_tags_displayed) {
127 array_pop($related_tags);
130 $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
137 $output .= $OUTPUT->box_end();
148 * Prints a box that contains the management links of a tag
151 * @param $return if true return html string
153 function tag_print_management_box($tag_object, $return=false) {
155 global $USER, $CFG, $OUTPUT;
157 $tagname = tag_display_name($tag_object);
160 if (!isguestuser()) {
161 $output .= $OUTPUT->box_start('box','tag-management-box');
162 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
165 // Add a link for users to add/remove this from their interests
166 if (tag_record_tagged_with('user', $USER->id
, $tag_object->name
)) {
167 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=removeinterest&sesskey='. sesskey() .'&tag='. rawurlencode($tag_object->name
) .'">'. get_string('removetagfrommyinterests', 'tag', $tagname) .'</a>';
169 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=addinterest&sesskey='. sesskey() .'&tag='. rawurlencode($tag_object->name
) .'">'. get_string('addtagtomyinterests', 'tag', $tagname) .'</a>';
172 // flag as inappropriate link
173 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=flaginappropriate&sesskey='. sesskey() .'&tag='. rawurlencode($tag_object->name
) .'">'. get_string('flagasinappropriate', 'tag', rawurlencode($tagname)) .'</a>';
175 // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
176 if (has_capability('moodle/tag:edit', $systemcontext) ||
177 has_capability('moodle/tag:manage', $systemcontext)) {
178 $links[] = '<a href="'. $CFG->wwwroot
.'/tag/edit.php?tag='. rawurlencode($tag_object->name
) .'">'. get_string('edittag', 'tag') .'</a>';
181 $output .= implode(' | ', $links);
182 $output .= $OUTPUT->box_end();
193 * Prints the tag search box
195 * @param bool $return if true return html string
197 function tag_print_search_box($return=false) {
198 global $CFG, $OUTPUT;
200 $output = $OUTPUT->box_start('','tag-search-box');
201 $output .= '<form action="'.$CFG->wwwroot
.'/tag/search.php" style="display:inline">';
203 $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
204 $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
206 $output .= '</form>';
207 $output .= $OUTPUT->box_end();
218 * Prints the tag search results
220 * @param string $query text that tag names will be matched against
221 * @param int $page current page
222 * @param int $perpage nr of users displayed per page
223 * @param $return if true return html string
225 function tag_print_search_results($query, $page, $perpage, $return=false) {
227 global $CFG, $USER, $OUTPUT;
229 $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL
));
231 $count = sizeof(tag_find_tags($query, false));
234 if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
235 $tags = array_values($found_tags);
238 $baseurl = $CFG->wwwroot
.'/tag/search.php?query='. rawurlencode($query);
241 // link "Add $query to my interests"
243 if( !tag_record_tagged_with('user', $USER->id
, $query) ) {
244 $addtaglink = '<a href="'. $CFG->wwwroot
.'/tag/user.php?action=addinterest&sesskey='. sesskey() .'&tag='. rawurlencode($query) .'">';
245 $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
248 if ( !empty($tags) ) { // there are results to display!!
249 $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
251 //print a link "Add $query to my interests"
252 if (!empty($addtaglink)) {
253 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
256 $nr_of_lis_per_ul = 6;
257 $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
259 $output .= '<ul id="tag-search-results">';
260 for($i = 0; $i < $nr_of_uls; $i++
) {
262 foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
263 $tag_link = ' <a href="'. $CFG->wwwroot
.'/tag/index.php?id='. $tag->id
.'">'. tag_display_name($tag) .'</a>';
264 $output .= '•'. $tag_link .'<br/>';
269 $output .= '<div> </div>'; // <-- small layout hack in order to look good in Firefox
271 $output .= $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
273 else { //no results were found!!
274 $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
276 //print a link "Add $query to my interests"
277 if (!empty($addtaglink)) {
278 $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
291 * Prints a table of the users tagged with the tag passed as argument
294 * @param int $users_per_row number of users per row to display
295 * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
296 * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
297 * @param $return if true return html string
299 function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
301 //List of users with this tag
302 $userlist = tag_find_records($tag_object->name
, 'user', $limitfrom, $limitnum);
304 $output = tag_print_user_list($userlist, true);
315 * Prints an individual user box
317 * @param $user user object (contains the following fields: id, firstname, lastname and picture)
318 * @param $return if true return html string
320 function tag_print_user_box($user, $return=false) {
321 global $CFG, $OUTPUT;
323 $textlib = textlib_get_instance();
324 $usercontext = get_context_instance(CONTEXT_USER
, $user->id
);
327 if ( has_capability('moodle/user:viewdetails', $usercontext) ||
has_coursecontact_role($user->id
) ) {
328 $profilelink = $CFG->wwwroot
.'/user/view.php?id='. $user->id
;
331 $output = $OUTPUT->box_start('user-box', 'user'. $user->id
);
332 $fullname = fullname($user);
335 if (!empty($profilelink)) {
336 $output .= '<a href="'. $profilelink .'">';
340 $output .= $OUTPUT->user_picture($user, array('size'=>100));
343 if (!empty($profilelink)) {
347 //truncate name if it's too big
348 if ($textlib->strlen($fullname) > 26) {
349 $fullname = $textlib->substr($fullname, 0, 26) .'...';
352 $output .= '<strong>'. $fullname .'</strong>';
353 $output .= $OUTPUT->box_end();
363 * Prints a list of users
365 * @param array $userlist an array of user objects
366 * @param $return if true return html string
368 function tag_print_user_list($userlist, $return=false) {
370 $output = '<ul class="inline-list">';
372 foreach ($userlist as $user){
373 $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
375 $output .= "</ul>\n";