PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tag/manage.php

https://github.com/pauln/moodle
PHP | 261 lines | 203 code | 31 blank | 27 comment | 33 complexity | 744e1e2a7b69ee7d22087d40862b4e69 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 = required_param('searchable', 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. // Form to filter tags.
  184. print('<form class="tag-filter-form" method="get" action="'.$CFG->wwwroot.'/tag/manage.php">');
  185. print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_tagfilter">'. get_string('search') .'</label>'.
  186. '<input type="hidden" name="tc" value="'.$tagcollid.'" />'.
  187. '<input type="hidden" name="perpage" value="'.$perpage.'" />'.
  188. '<input id="id_tagfilter" name="filter" type="text" value=' . s($filter) . '>'.
  189. '<input value="'. s(get_string('search')) .'" type="submit"> '.
  190. ($filter !== '' ? html_writer::link(new moodle_url($PAGE->url, array('filter' => null)),
  191. get_string('resetfilter', 'tag'), array('class' => 'resetfilterlink')) : '').
  192. '</div>');
  193. print('</form>');
  194. // Link to add an standard tags.
  195. $img = $OUTPUT->pix_icon('t/add', '');
  196. echo '<div class="addstandardtags visibleifjs">' .
  197. html_writer::link('#', $img . get_string('addotags', 'tag'), array('data-action' => 'addstandardtag')) .
  198. '</div>';
  199. $table = new core_tag_manage_table($tagcollid);
  200. echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
  201. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
  202. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
  203. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
  204. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
  205. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
  206. echo $table->out($perpage, true);
  207. if ($table->rawdata) {
  208. echo html_writer::start_tag('p');
  209. echo html_writer::tag('button', get_string('deleteselected', 'tag'),
  210. array('id' => 'tag-management-delete', 'type' => 'submit', 'class' => 'tagdeleteselected', 'name' => 'bulkdelete'));
  211. echo html_writer::tag('button', get_string('combineselected', 'tag'),
  212. array('id' => 'tag-management-combine', 'type' => 'submit', 'class' => 'tagcombineselected', 'name' => 'bulkcombine'));
  213. echo html_writer::end_tag('p');
  214. }
  215. echo '</form>';
  216. $totalcount = $table->totalcount;
  217. if ($perpage == SHOW_ALL_PAGE_SIZE) {
  218. echo html_writer::start_tag('div', array('id' => 'showall'));
  219. $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
  220. $url = new moodle_url($PAGE->url, $params);
  221. echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
  222. echo html_writer::end_tag('div');
  223. } else if ($totalcount > 0 and $perpage < $totalcount) {
  224. echo html_writer::start_tag('div', array('id' => 'showall'));
  225. $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
  226. $url = new moodle_url($PAGE->url, $params);
  227. echo html_writer::link($url, get_string('showall', '', $totalcount));
  228. echo html_writer::end_tag('div');
  229. }
  230. echo $OUTPUT->footer();