PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tag/manage.php

https://github.com/mspall/moodle
PHP | 275 lines | 215 code | 34 blank | 26 comment | 34 complexity | e572b21273e3c2852ccede758580fa74 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. $PAGE->set_primary_active_tab('siteadminnode');
  70. switch($action) {
  71. case 'colladd':
  72. require_sesskey();
  73. $name = required_param('name', PARAM_NOTAGS);
  74. $searchable = optional_param('searchable', false, PARAM_BOOL);
  75. core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
  76. redirect($manageurl);
  77. break;
  78. case 'colldelete':
  79. if ($tagcoll && !$tagcoll->component) {
  80. require_sesskey();
  81. core_tag_collection::delete($tagcoll);
  82. \core\notification::success(get_string('changessaved', 'core_tag'));
  83. }
  84. redirect($manageurl);
  85. break;
  86. case 'collmoveup':
  87. if ($tagcoll) {
  88. require_sesskey();
  89. core_tag_collection::change_sortorder($tagcoll, -1);
  90. redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
  91. }
  92. redirect($manageurl);
  93. break;
  94. case 'collmovedown':
  95. if ($tagcoll) {
  96. require_sesskey();
  97. core_tag_collection::change_sortorder($tagcoll, 1);
  98. redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
  99. }
  100. redirect($manageurl);
  101. break;
  102. case 'delete':
  103. if ($tagid) {
  104. require_sesskey();
  105. core_tag_tag::delete_tags(array($tagid));
  106. \core\notification::success(get_string('deleted', 'core_tag'));
  107. }
  108. redirect($PAGE->url);
  109. break;
  110. case 'bulk':
  111. if (optional_param('bulkdelete', null, PARAM_RAW) !== null) {
  112. if ($tagschecked) {
  113. require_sesskey();
  114. core_tag_tag::delete_tags($tagschecked);
  115. \core\notification::success(get_string('deleted', 'core_tag'));
  116. }
  117. redirect($PAGE->url);
  118. } else if (optional_param('bulkcombine', null, PARAM_RAW) !== null) {
  119. $tags = core_tag_tag::get_bulk($tagschecked, '*');
  120. if (count($tags) > 1) {
  121. require_sesskey();
  122. if (($maintag = optional_param('maintag', 0, PARAM_INT)) && array_key_exists($maintag, $tags)) {
  123. $tag = $tags[$maintag];
  124. } else {
  125. $tag = array_shift($tags);
  126. }
  127. $tag->combine_tags($tags);
  128. \core\notification::success(get_string('combined', 'core_tag'));
  129. }
  130. redirect($PAGE->url);
  131. }
  132. break;
  133. case 'renamecombine':
  134. // Allows to rename the tag and if the tag with the new name already exists these tags will be combined.
  135. if ($tagid && ($newname = required_param('newname', PARAM_TAG))) {
  136. require_sesskey();
  137. $tag = core_tag_tag::get($tagid, '*', MUST_EXIST);
  138. $targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*');
  139. if ($targettag) {
  140. $targettag->combine_tags(array($tag));
  141. \core\notification::success(get_string('combined', 'core_tag'));
  142. } else {
  143. $tag->update(array('rawname' => $newname));
  144. \core\notification::success(get_string('changessaved', 'core_tag'));
  145. }
  146. }
  147. redirect($PAGE->url);
  148. break;
  149. case 'addstandardtag':
  150. require_sesskey();
  151. $tagobjects = array();
  152. if ($tagcoll) {
  153. $tagslist = optional_param('tagslist', '', PARAM_RAW);
  154. $newtags = preg_split('/\s*,\s*/', trim($tagslist), -1, PREG_SPLIT_NO_EMPTY);
  155. $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
  156. }
  157. foreach ($tagobjects as $tagobject) {
  158. if (!$tagobject->isstandard) {
  159. $tagobject->update(array('isstandard' => 1));
  160. }
  161. }
  162. redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
  163. null, \core\output\notification::NOTIFY_SUCCESS);
  164. break;
  165. }
  166. echo $OUTPUT->header();
  167. if (!$tagcoll) {
  168. // Tag collection is not specified. Display the overview of tag collections and tag areas.
  169. $tagareastable = new core_tag_areas_table($manageurl);
  170. $colltable = new core_tag_collections_table($manageurl);
  171. echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
  172. echo html_writer::table($colltable);
  173. $url = new moodle_url($manageurl, array('action' => 'colladd'));
  174. echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
  175. 'mdl-right addtagcoll');
  176. echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
  177. echo html_writer::table($tagareastable);
  178. $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
  179. echo $OUTPUT->footer();
  180. exit;
  181. }
  182. // Tag collection is specified. Manage tags in this collection.
  183. echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
  184. $hiddenfields = [
  185. (object) ['type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid],
  186. (object) ['type' => 'hidden', 'name' => 'perpage', 'value' => $perpage]
  187. ];
  188. $otherfields = '';
  189. if ($filter !== '') {
  190. $otherfields = html_writer::link(new moodle_url($PAGE->url, ['filter' => null]),
  191. get_string('resetfilter', 'tag'));
  192. }
  193. $data = [
  194. 'action' => new moodle_url('/tag/manage.php'),
  195. 'hiddenfields' => $hiddenfields,
  196. 'inputname' => 'filter',
  197. 'searchstring' => get_string('search'),
  198. 'query' => s($filter),
  199. 'extraclasses' => 'mb-2',
  200. 'otherfields' => $otherfields
  201. ];
  202. echo $OUTPUT->render_from_template('core/search_input', $data);
  203. // Link to add an standard tags.
  204. $img = $OUTPUT->pix_icon('t/add', '');
  205. echo '<div class="addstandardtags visibleifjs">' .
  206. html_writer::link('#', $img . get_string('addotags', 'tag'), array('data-action' => 'addstandardtag')) .
  207. '</div>';
  208. $table = new core_tag_manage_table($tagcollid);
  209. echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
  210. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
  211. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
  212. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
  213. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
  214. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
  215. echo $table->out($perpage, true);
  216. if ($table->rawdata) {
  217. echo html_writer::start_tag('p');
  218. echo html_writer::tag('button', get_string('deleteselected', 'tag'),
  219. array('id' => 'tag-management-delete', 'type' => 'submit',
  220. 'class' => 'tagdeleteselected btn btn-secondary', 'name' => 'bulkdelete'));
  221. echo html_writer::tag('button', get_string('combineselected', 'tag'),
  222. array('id' => 'tag-management-combine', 'type' => 'submit',
  223. 'class' => 'tagcombineselected btn btn-secondary', 'name' => 'bulkcombine'));
  224. echo html_writer::end_tag('p');
  225. }
  226. echo '</form>';
  227. $totalcount = $table->totalcount;
  228. if ($perpage == SHOW_ALL_PAGE_SIZE) {
  229. echo html_writer::start_tag('div', array('id' => 'showall'));
  230. $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
  231. $url = new moodle_url($PAGE->url, $params);
  232. echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
  233. echo html_writer::end_tag('div');
  234. } else if ($totalcount > 0 and $perpage < $totalcount) {
  235. echo html_writer::start_tag('div', array('id' => 'showall'));
  236. $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
  237. $url = new moodle_url($PAGE->url, $params);
  238. echo html_writer::link($url, get_string('showall', '', $totalcount));
  239. echo html_writer::end_tag('div');
  240. }
  241. echo $OUTPUT->footer();