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

/tag/manage.php

https://github.com/CLAMP-IT/moodle
PHP | 273 lines | 214 code | 33 blank | 26 comment | 34 complexity | d81ba3aa9343543aa456b23fcd78c8ba MD5 | raw file
  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. * Managing tags, tag areas and tags collections
  18. *
  19. * @package core_tag
  20. * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once('../config.php');
  24. require_once($CFG->libdir.'/tablelib.php');
  25. require_once('lib.php');
  26. require_once($CFG->libdir.'/adminlib.php');
  27. define('SHOW_ALL_PAGE_SIZE', 50000);
  28. define('DEFAULT_PAGE_SIZE', 30);
  29. $tagschecked = optional_param_array('tagschecked', array(), PARAM_INT);
  30. $tagid = optional_param('tagid', null, PARAM_INT);
  31. $isstandard = optional_param('isstandard', null, PARAM_INT);
  32. $action = optional_param('action', '', PARAM_ALPHA);
  33. $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
  34. $page = optional_param('page', 0, PARAM_INT);
  35. $tagcollid = optional_param('tc', 0, PARAM_INT);
  36. $tagareaid = optional_param('ta', null, PARAM_INT);
  37. $filter = optional_param('filter', '', PARAM_NOTAGS);
  38. $params = array();
  39. if ($perpage != DEFAULT_PAGE_SIZE) {
  40. $params['perpage'] = $perpage;
  41. }
  42. if ($page > 0) {
  43. $params['page'] = $page;
  44. }
  45. if ($tagcollid) {
  46. $params['tc'] = $tagcollid;
  47. }
  48. if ($filter !== '') {
  49. $params['filter'] = $filter;
  50. }
  51. admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
  52. if (empty($CFG->usetags)) {
  53. print_error('tagsaredisabled', 'tag');
  54. }
  55. $tagobject = null;
  56. if ($tagid) {
  57. $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
  58. $tagcollid = $tagobject->tagcollid;
  59. }
  60. $tagcoll = core_tag_collection::get_by_id($tagcollid);
  61. $tagarea = core_tag_area::get_by_id($tagareaid);
  62. $manageurl = new moodle_url('/tag/manage.php');
  63. if ($tagcoll) {
  64. // We are inside a tag collection - add it to the breadcrumb.
  65. $PAGE->navbar->add(core_tag_collection::display_name($tagcoll),
  66. new moodle_url($manageurl, array('tc' => $tagcoll->id)));
  67. }
  68. $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
  69. switch($action) {
  70. case 'colladd':
  71. require_sesskey();
  72. $name = required_param('name', PARAM_NOTAGS);
  73. $searchable = optional_param('searchable', false, PARAM_BOOL);
  74. core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
  75. redirect($manageurl);
  76. break;
  77. case 'colldelete':
  78. if ($tagcoll && !$tagcoll->component) {
  79. require_sesskey();
  80. core_tag_collection::delete($tagcoll);
  81. \core\notification::success(get_string('changessaved', 'core_tag'));
  82. }
  83. redirect($manageurl);
  84. break;
  85. case 'collmoveup':
  86. if ($tagcoll) {
  87. require_sesskey();
  88. core_tag_collection::change_sortorder($tagcoll, -1);
  89. redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
  90. }
  91. redirect($manageurl);
  92. break;
  93. case 'collmovedown':
  94. if ($tagcoll) {
  95. require_sesskey();
  96. core_tag_collection::change_sortorder($tagcoll, 1);
  97. redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
  98. }
  99. redirect($manageurl);
  100. break;
  101. case 'delete':
  102. if ($tagid) {
  103. require_sesskey();
  104. core_tag_tag::delete_tags(array($tagid));
  105. \core\notification::success(get_string('deleted', 'core_tag'));
  106. }
  107. redirect($PAGE->url);
  108. break;
  109. case 'bulk':
  110. if (optional_param('bulkdelete', null, PARAM_RAW) !== null) {
  111. if ($tagschecked) {
  112. require_sesskey();
  113. core_tag_tag::delete_tags($tagschecked);
  114. \core\notification::success(get_string('deleted', 'core_tag'));
  115. }
  116. redirect($PAGE->url);
  117. } else if (optional_param('bulkcombine', null, PARAM_RAW) !== null) {
  118. $tags = core_tag_tag::get_bulk($tagschecked, '*');
  119. if (count($tags) > 1) {
  120. require_sesskey();
  121. if (($maintag = optional_param('maintag', 0, PARAM_INT)) && array_key_exists($maintag, $tags)) {
  122. $tag = $tags[$maintag];
  123. } else {
  124. $tag = array_shift($tags);
  125. }
  126. $tag->combine_tags($tags);
  127. \core\notification::success(get_string('combined', 'core_tag'));
  128. }
  129. redirect($PAGE->url);
  130. }
  131. break;
  132. case 'renamecombine':
  133. // Allows to rename the tag and if the tag with the new name already exists these tags will be combined.
  134. if ($tagid && ($newname = required_param('newname', PARAM_TAG))) {
  135. require_sesskey();
  136. $tag = core_tag_tag::get($tagid, '*', MUST_EXIST);
  137. $targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*');
  138. if ($targettag) {
  139. $targettag->combine_tags(array($tag));
  140. \core\notification::success(get_string('combined', 'core_tag'));
  141. } else {
  142. $tag->update(array('rawname' => $newname));
  143. \core\notification::success(get_string('changessaved', 'core_tag'));
  144. }
  145. }
  146. redirect($PAGE->url);
  147. break;
  148. case 'addstandardtag':
  149. require_sesskey();
  150. $tagobjects = array();
  151. if ($tagcoll) {
  152. $tagslist = optional_param('tagslist', '', PARAM_RAW);
  153. $newtags = preg_split('/\s*,\s*/', trim($tagslist), -1, PREG_SPLIT_NO_EMPTY);
  154. $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
  155. }
  156. foreach ($tagobjects as $tagobject) {
  157. if (!$tagobject->isstandard) {
  158. $tagobject->update(array('isstandard' => 1));
  159. }
  160. }
  161. redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
  162. null, \core\output\notification::NOTIFY_SUCCESS);
  163. break;
  164. }
  165. echo $OUTPUT->header();
  166. if (!$tagcoll) {
  167. // Tag collection is not specified. Display the overview of tag collections and tag areas.
  168. $tagareastable = new core_tag_areas_table($manageurl);
  169. $colltable = new core_tag_collections_table($manageurl);
  170. echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
  171. echo html_writer::table($colltable);
  172. $url = new moodle_url($manageurl, array('action' => 'colladd'));
  173. echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
  174. 'mdl-right addtagcoll');
  175. echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
  176. echo html_writer::table($tagareastable);
  177. $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
  178. echo $OUTPUT->footer();
  179. exit;
  180. }
  181. // Tag collection is specified. Manage tags in this collection.
  182. echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
  183. $hiddenfields = [
  184. (object) ['type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid],
  185. (object) ['type' => 'hidden', 'name' => 'perpage', 'value' => $perpage]
  186. ];
  187. $otherfields = '';
  188. if ($filter !== '') {
  189. $otherfields = html_writer::link(new moodle_url($PAGE->url, ['filter' => null]),
  190. get_string('resetfilter', 'tag'));
  191. }
  192. $data = [
  193. 'action' => new moodle_url('/tag/manage.php'),
  194. 'hiddenfields' => $hiddenfields,
  195. 'inputname' => 'filter',
  196. 'searchstring' => get_string('search'),
  197. 'query' => s($filter),
  198. 'extraclasses' => 'mb-2',
  199. 'otherfields' => $otherfields
  200. ];
  201. echo $OUTPUT->render_from_template('core/search_input', $data);
  202. // Link to add an standard tags.
  203. $img = $OUTPUT->pix_icon('t/add', '');
  204. echo '<div class="addstandardtags visibleifjs">' .
  205. html_writer::link('#', $img . get_string('addotags', 'tag'), array('data-action' => 'addstandardtag')) .
  206. '</div>';
  207. $table = new core_tag_manage_table($tagcollid);
  208. echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
  209. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
  210. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
  211. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
  212. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
  213. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
  214. echo $table->out($perpage, true);
  215. if ($table->rawdata) {
  216. echo html_writer::start_tag('p');
  217. echo html_writer::tag('button', get_string('deleteselected', 'tag'),
  218. array('id' => 'tag-management-delete', 'type' => 'submit',
  219. 'class' => 'tagdeleteselected btn btn-secondary', 'name' => 'bulkdelete'));
  220. echo html_writer::tag('button', get_string('combineselected', 'tag'),
  221. array('id' => 'tag-management-combine', 'type' => 'submit',
  222. 'class' => 'tagcombineselected btn btn-secondary', 'name' => 'bulkcombine'));
  223. echo html_writer::end_tag('p');
  224. }
  225. echo '</form>';
  226. $totalcount = $table->totalcount;
  227. if ($perpage == SHOW_ALL_PAGE_SIZE) {
  228. echo html_writer::start_tag('div', array('id' => 'showall'));
  229. $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
  230. $url = new moodle_url($PAGE->url, $params);
  231. echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
  232. echo html_writer::end_tag('div');
  233. } else if ($totalcount > 0 and $perpage < $totalcount) {
  234. echo html_writer::start_tag('div', array('id' => 'showall'));
  235. $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
  236. $url = new moodle_url($PAGE->url, $params);
  237. echo html_writer::link($url, get_string('showall', '', $totalcount));
  238. echo html_writer::end_tag('div');
  239. }
  240. echo $OUTPUT->footer();