PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tag/locallib.php

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