PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tag/locallib.php

https://github.com/nicolasconnault/moodle2.0
PHP | 390 lines | 246 code | 70 blank | 74 comment | 52 complexity | 7bd4ce027c53d96564797601c9722bee MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php // $Id: locallib.php,v 1.20 2009/03/02 21:51:25 mudrd8mz Exp $
  2. require_once('lib.php');
  3. /**
  4. * locallib.php - moodle tag local library - output functions
  5. *
  6. * @version: $Id: locallib.php,v 1.20 2009/03/02 21:51:25 mudrd8mz Exp $
  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 unknown_type $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;
  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 .= print_box_start('generalbox', 'tag-description', true);
  94. }
  95. if (!empty($tag_object->description)) {
  96. $options = new object();
  97. $options->para = false;
  98. $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
  99. }
  100. if ($related_tags) {
  101. $more_links = false;
  102. if (count($related_tags) > $max_tags_displayed) {
  103. array_pop($related_tags);
  104. $more_links = true;
  105. }
  106. $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
  107. if ($more_links) {
  108. $output .= ' ...';
  109. }
  110. }
  111. if ($content) {
  112. $output .= print_box_end(true);
  113. }
  114. if ($return) {
  115. return $output;
  116. } else {
  117. echo $output;
  118. }
  119. }
  120. /**
  121. * Prints a box that contains the management links of a tag
  122. *
  123. * @param $tagid
  124. * @param $return if true return html string
  125. */
  126. function tag_print_management_box($tag_object, $return=false) {
  127. global $USER, $CFG;
  128. $tagname = tag_display_name($tag_object);
  129. $output = '';
  130. if (!isguestuser()) {
  131. $output .= print_box_start('box','tag-management-box', true);
  132. $systemcontext = get_context_instance(CONTEXT_SYSTEM);
  133. $links = array();
  134. // Add a link for users to add/remove this from their interests
  135. if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
  136. $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>';
  137. } else {
  138. $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>';
  139. }
  140. // flag as inappropriate link
  141. $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>';
  142. // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
  143. if (has_capability('moodle/tag:edit', $systemcontext) ||
  144. has_capability('moodle/tag:manage', $systemcontext)) {
  145. $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
  146. }
  147. $output .= implode(' | ', $links);
  148. $output .= print_box_end(true);
  149. }
  150. if ($return) {
  151. return $output;
  152. } else {
  153. echo $output;
  154. }
  155. }
  156. /**
  157. * Prints the tag search box
  158. *
  159. * @param bool $return if true return html string
  160. */
  161. function tag_print_search_box($return=false) {
  162. global $CFG;
  163. $output = print_box_start('','tag-search-box', true);
  164. $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
  165. $output .= '<div>';
  166. $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
  167. $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
  168. $output .= '</div>';
  169. $output .= '</form>';
  170. $output .= print_box_end(true);
  171. if ($return) {
  172. return $output;
  173. }
  174. else {
  175. echo $output;
  176. }
  177. }
  178. /**
  179. * Prints the tag search results
  180. *
  181. * @param string $query text that tag names will be matched against
  182. * @param int $page current page
  183. * @param int $perpage nr of users displayed per page
  184. * @param $return if true return html string
  185. */
  186. function tag_print_search_results($query, $page, $perpage, $return=false) {
  187. global $CFG, $USER;
  188. $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
  189. $count = sizeof(tag_find_tags($query, false));
  190. $tags = array();
  191. if ( $found_tags = tag_find_tags($query, true, $page * $perpage, $perpage) ) {
  192. $tags = array_values($found_tags);
  193. }
  194. $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
  195. $output = '';
  196. // link "Add $query to my interests"
  197. $addtaglink = '';
  198. if( !tag_record_tagged_with('user', $USER->id, $query) ) {
  199. $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
  200. $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
  201. }
  202. if ( !empty($tags) ) { // there are results to display!!
  203. $output .= print_heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", '', 3, 'main', true);
  204. //print a link "Add $query to my interests"
  205. if (!empty($addtaglink)) {
  206. $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
  207. }
  208. $nr_of_lis_per_ul = 6;
  209. $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
  210. $output .= '<ul id="tag-search-results">';
  211. for($i = 0; $i < $nr_of_uls; $i++) {
  212. $output .= '<li>';
  213. foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
  214. $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
  215. $output .= '&#8226;'. $tag_link .'<br/>';
  216. }
  217. $output .= '</li>';
  218. }
  219. $output .= '</ul>';
  220. $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
  221. $output .= print_paging_bar($count, $page, $perpage, $baseurl .'&amp;', 'page', false, true);
  222. }
  223. else { //no results were found!!
  224. $output .= print_heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), '', 3, 'main' , true);
  225. //print a link "Add $query to my interests"
  226. if (!empty($addtaglink)) {
  227. $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
  228. }
  229. }
  230. if ($return) {
  231. return $output;
  232. }
  233. else {
  234. echo $output;
  235. }
  236. }
  237. /**
  238. * Prints a table of the users tagged with the tag passed as argument
  239. *
  240. * @param $tag_object
  241. * @param int $users_per_row number of users per row to display
  242. * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
  243. * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
  244. * @param $return if true return html string
  245. */
  246. function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
  247. //List of users with this tag
  248. $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
  249. $output = tag_print_user_list($userlist, true);
  250. if ($return) {
  251. return $output;
  252. }
  253. else {
  254. echo $output;
  255. }
  256. }
  257. /**
  258. * Prints an individual user box
  259. *
  260. * @param $user user object (contains the following fields: id, firstname, lastname and picture)
  261. * @param $return if true return html string
  262. */
  263. function tag_print_user_box($user, $return=false) {
  264. global $CFG;
  265. $textlib = textlib_get_instance();
  266. $usercontext = get_context_instance(CONTEXT_USER, $user->id);
  267. $profilelink = '';
  268. if ( has_capability('moodle/user:viewdetails', $usercontext) || isteacherinanycourse($user->id) ) {
  269. $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
  270. }
  271. $output = print_box_start('user-box', 'user'. $user->id, true);
  272. $fullname = fullname($user);
  273. $alt = '';
  274. if (!empty($profilelink)) {
  275. $output .= '<a href="'. $profilelink .'">';
  276. $alt = $fullname;
  277. }
  278. //print user image - if image is only content of link it needs ALT text!
  279. if ($user->picture) {
  280. $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/user/pix.php/'. $user->id .'/f1.jpg" />';
  281. } else {
  282. $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/pix/u/f1.png" />';
  283. }
  284. $output .= '<br />';
  285. if (!empty($profilelink)) {
  286. $output .= '</a>';
  287. }
  288. //truncate name if it's too big
  289. if ($textlib->strlen($fullname) > 26) {
  290. $fullname = $textlib->substr($fullname, 0, 26) .'...';
  291. }
  292. $output .= '<strong>'. $fullname .'</strong>';
  293. $output .= print_box_end(true);
  294. if ($return) {
  295. return $output;
  296. }
  297. else {
  298. echo $output;
  299. }
  300. }
  301. /**
  302. * Prints a list of users
  303. *
  304. * @param array $userlist an array of user objects
  305. * @param $return if true return html string
  306. */
  307. function tag_print_user_list($userlist, $return=false) {
  308. $output = '<ul class="inline-list">';
  309. foreach ($userlist as $user){
  310. $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
  311. }
  312. $output .= "</ul>\n";
  313. if ($return) {
  314. return $output;
  315. }
  316. else {
  317. echo $output;
  318. }
  319. }
  320. ?>