PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tag/locallib.php

https://github.com/plymouthstate/moodle
PHP | 383 lines | 244 code | 67 blank | 72 comment | 49 complexity | 956860177f2d123839e27c037ef76831 MD5 | raw file
  1. <?php
  2. require_once($CFG->dirroot.'/tag/lib.php');
  3. require_once($CFG->libdir.'/filelib.php');
  4. /**
  5. * locallib.php - moodle tag local library - output functions
  6. *
  7. * @licence http://www.gnu.org/copyleft/gpl.html GNU Public License
  8. * @package moodlecore
  9. *
  10. */
  11. /**
  12. * Prints a tag cloud
  13. *
  14. * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
  15. * @param $return if true return html string
  16. */
  17. function tag_print_cloud($nr_of_tags=150, $return=false) {
  18. global $CFG, $DB;
  19. $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
  20. if ( !$tagsincloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
  21. FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
  22. WHERE ti.itemtype <> \'tag\'
  23. GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype
  24. ORDER BY count DESC, tg.name ASC', null, 0, $nr_of_tags) ) {
  25. $tagsincloud = array();
  26. }
  27. $tagkeys = array_keys($tagsincloud);
  28. if (!empty($tagkeys)) {
  29. $firsttagkey = $tagkeys[0];
  30. $maxcount = $tagsincloud[$firsttagkey]->count;
  31. }
  32. $etags = array();
  33. foreach ($tagsincloud as $tag) {
  34. $size = (int) (( $tag->count / $maxcount) * 20);
  35. $tag->class = "$tag->tagtype s$size";
  36. $etags[] = $tag;
  37. }
  38. usort($etags, "tag_cloud_sort");
  39. $output = '';
  40. $output .= "\n<ul class='tag_cloud inline-list'>\n";
  41. foreach ($etags as $tag) {
  42. if ($tag->flag > 0 && $can_manage_tags) {
  43. $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
  44. } else {
  45. $tagname = tag_display_name($tag);
  46. }
  47. $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
  48. $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
  49. 'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
  50. $tagname .'</a></li> ';
  51. }
  52. $output .= "\n</ul>\n";
  53. if ($return) {
  54. return $output;
  55. } else {
  56. echo $output;
  57. }
  58. }
  59. /**
  60. * This function is used by print_tag_cloud, to usort() the tags in the cloud.
  61. * See php.net/usort for the parameters documentation. This was originally in
  62. * blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
  63. */
  64. function tag_cloud_sort($a, $b) {
  65. global $CFG;
  66. if (empty($CFG->tagsort)) {
  67. $tagsort = 'name'; // by default, sort by name
  68. } else {
  69. $tagsort = $CFG->tagsort;
  70. }
  71. if (is_numeric($a->$tagsort)) {
  72. return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
  73. } elseif (is_string($a->$tagsort)) {
  74. return strcmp($a->$tagsort, $b->$tagsort);
  75. } else {
  76. return 0;
  77. }
  78. }
  79. /**
  80. * Prints a box with the description of a tag and its related tags
  81. *
  82. * @param stdClass $tag_object
  83. * @param $return if true return html string
  84. */
  85. function tag_print_description_box($tag_object, $return=false) {
  86. global $USER, $CFG, $OUTPUT;
  87. $max_tags_displayed = 10; // todo: turn this into a system setting
  88. $tagname = tag_display_name($tag_object);
  89. $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
  90. $content = !empty($tag_object->description) || $related_tags;
  91. $output = '';
  92. if ($content) {
  93. $output .= $OUTPUT->box_start('generalbox', 'tag-description');
  94. }
  95. if (!empty($tag_object->description)) {
  96. $options = new stdClass();
  97. $options->para = false;
  98. $options->overflowdiv = true;
  99. $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', get_context_instance(CONTEXT_SYSTEM)->id, 'tag', 'description', $tag_object->id);
  100. $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
  101. }
  102. if ($related_tags) {
  103. $more_links = false;
  104. if (count($related_tags) > $max_tags_displayed) {
  105. array_pop($related_tags);
  106. $more_links = true;
  107. }
  108. $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
  109. if ($more_links) {
  110. $output .= ' ...';
  111. }
  112. }
  113. if ($content) {
  114. $output .= $OUTPUT->box_end();
  115. }
  116. if ($return) {
  117. return $output;
  118. } else {
  119. echo $output;
  120. }
  121. }
  122. /**
  123. * Prints a box that contains the management links of a tag
  124. *
  125. * @param $tagid
  126. * @param $return if true return html string
  127. */
  128. function tag_print_management_box($tag_object, $return=false) {
  129. global $USER, $CFG, $OUTPUT;
  130. $tagname = tag_display_name($tag_object);
  131. $output = '';
  132. if (!isguestuser()) {
  133. $output .= $OUTPUT->box_start('box','tag-management-box');
  134. $systemcontext = get_context_instance(CONTEXT_SYSTEM);
  135. $links = array();
  136. // Add a link for users to add/remove this from their interests
  137. if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
  138. $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>';
  139. } else {
  140. $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>';
  141. }
  142. // flag as inappropriate link
  143. $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>';
  144. // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
  145. if (has_capability('moodle/tag:edit', $systemcontext) ||
  146. has_capability('moodle/tag:manage', $systemcontext)) {
  147. $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
  148. }
  149. $output .= implode(' | ', $links);
  150. $output .= $OUTPUT->box_end();
  151. }
  152. if ($return) {
  153. return $output;
  154. } else {
  155. echo $output;
  156. }
  157. }
  158. /**
  159. * Prints the tag search box
  160. *
  161. * @param bool $return if true return html string
  162. */
  163. function tag_print_search_box($return=false) {
  164. global $CFG, $OUTPUT;
  165. $output = $OUTPUT->box_start('','tag-search-box');
  166. $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
  167. $output .= '<div>';
  168. $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
  169. $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
  170. $output .= '</div>';
  171. $output .= '</form>';
  172. $output .= $OUTPUT->box_end();
  173. if ($return) {
  174. return $output;
  175. }
  176. else {
  177. echo $output;
  178. }
  179. }
  180. /**
  181. * Prints the tag search results
  182. *
  183. * @param string $query text that tag names will be matched against
  184. * @param int $page current page
  185. * @param int $perpage nr of users displayed per page
  186. * @param $return if true return html string
  187. */
  188. function tag_print_search_results($query, $page, $perpage, $return=false) {
  189. global $CFG, $USER, $OUTPUT;
  190. $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
  191. $count = sizeof(tag_find_tags($query, false));
  192. $tags = array();
  193. if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
  194. $tags = array_values($found_tags);
  195. }
  196. $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
  197. $output = '';
  198. // link "Add $query to my interests"
  199. $addtaglink = '';
  200. if( !tag_record_tagged_with('user', $USER->id, $query) ) {
  201. $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
  202. $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
  203. }
  204. if ( !empty($tags) ) { // there are results to display!!
  205. $output .= $OUTPUT->heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", 3, 'main');
  206. //print a link "Add $query to my interests"
  207. if (!empty($addtaglink)) {
  208. $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
  209. }
  210. $nr_of_lis_per_ul = 6;
  211. $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
  212. $output .= '<ul id="tag-search-results">';
  213. for($i = 0; $i < $nr_of_uls; $i++) {
  214. $output .= '<li>';
  215. foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
  216. $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
  217. $output .= '&#8226;'. $tag_link .'<br/>';
  218. }
  219. $output .= '</li>';
  220. }
  221. $output .= '</ul>';
  222. $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
  223. $output .= $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
  224. }
  225. else { //no results were found!!
  226. $output .= $OUTPUT->heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), 3, 'main');
  227. //print a link "Add $query to my interests"
  228. if (!empty($addtaglink)) {
  229. $output .= $OUTPUT->box($addtaglink, 'box', 'tag-management-box');
  230. }
  231. }
  232. if ($return) {
  233. return $output;
  234. }
  235. else {
  236. echo $output;
  237. }
  238. }
  239. /**
  240. * Prints a table of the users tagged with the tag passed as argument
  241. *
  242. * @param $tag_object
  243. * @param int $users_per_row number of users per row to display
  244. * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
  245. * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
  246. * @param $return if true return html string
  247. */
  248. function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
  249. //List of users with this tag
  250. $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
  251. $output = tag_print_user_list($userlist, true);
  252. if ($return) {
  253. return $output;
  254. }
  255. else {
  256. echo $output;
  257. }
  258. }
  259. /**
  260. * Prints an individual user box
  261. *
  262. * @param $user user object (contains the following fields: id, firstname, lastname and picture)
  263. * @param $return if true return html string
  264. */
  265. function tag_print_user_box($user, $return=false) {
  266. global $CFG, $OUTPUT;
  267. $textlib = textlib_get_instance();
  268. $usercontext = get_context_instance(CONTEXT_USER, $user->id);
  269. $profilelink = '';
  270. if ($usercontext and (has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($user->id))) {
  271. $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
  272. }
  273. $output = $OUTPUT->box_start('user-box', 'user'. $user->id);
  274. $fullname = fullname($user);
  275. $alt = '';
  276. if (!empty($profilelink)) {
  277. $output .= '<a href="'. $profilelink .'">';
  278. $alt = $fullname;
  279. }
  280. $output .= $OUTPUT->user_picture($user, array('size'=>100));
  281. $output .= '<br />';
  282. if (!empty($profilelink)) {
  283. $output .= '</a>';
  284. }
  285. //truncate name if it's too big
  286. if ($textlib->strlen($fullname) > 26) {
  287. $fullname = $textlib->substr($fullname, 0, 26) .'...';
  288. }
  289. $output .= '<strong>'. $fullname .'</strong>';
  290. $output .= $OUTPUT->box_end();
  291. if ($return) {
  292. return $output;
  293. }
  294. else {
  295. echo $output;
  296. }
  297. }
  298. /**
  299. * Prints a list of users
  300. *
  301. * @param array $userlist an array of user objects
  302. * @param $return if true return html string
  303. */
  304. function tag_print_user_list($userlist, $return=false) {
  305. $output = '<ul class="inline-list">';
  306. foreach ($userlist as $user){
  307. $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
  308. }
  309. $output .= "</ul>\n";
  310. if ($return) {
  311. return $output;
  312. }
  313. else {
  314. echo $output;
  315. }
  316. }