PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tag/locallib.php

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