Merge branch 'MDL-79108-master' of https://github.com/sarjona/moodle
[moodle.git] / tag / manage.php
blobe4165a8095590d33d07145ff6ee7a1359d771ea4
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.
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/>.
17 /**
18 * Managing tags, tag areas and tags collections
20 * @package core_tag
21 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once($CFG->libdir.'/tablelib.php');
27 require_once('lib.php');
28 require_once($CFG->libdir.'/adminlib.php');
30 define('SHOW_ALL_PAGE_SIZE', 50000);
31 define('DEFAULT_PAGE_SIZE', 30);
33 $tagschecked = optional_param_array('tagschecked', array(), PARAM_INT);
34 $tagid = optional_param('tagid', null, PARAM_INT);
35 $isstandard = optional_param('isstandard', null, PARAM_INT);
36 $action = optional_param('action', '', PARAM_ALPHA);
37 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
38 $page = optional_param('page', 0, PARAM_INT);
39 $tagcollid = optional_param('tc', 0, PARAM_INT);
40 $tagareaid = optional_param('ta', null, PARAM_INT);
41 $filter = optional_param('filter', '', PARAM_NOTAGS);
43 $params = array();
44 if ($perpage != DEFAULT_PAGE_SIZE) {
45 $params['perpage'] = $perpage;
47 if ($page > 0) {
48 $params['page'] = $page;
50 if ($tagcollid) {
51 $params['tc'] = $tagcollid;
53 if ($filter !== '') {
54 $params['filter'] = $filter;
57 admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
59 if (empty($CFG->usetags)) {
60 throw new \moodle_exception('tagsaredisabled', 'tag');
63 $tagobject = null;
64 if ($tagid) {
65 $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
66 $tagcollid = $tagobject->tagcollid;
68 $tagcoll = core_tag_collection::get_by_id($tagcollid);
69 $tagarea = core_tag_area::get_by_id($tagareaid);
70 $manageurl = new moodle_url('/tag/manage.php');
71 if ($tagcoll) {
72 // We are inside a tag collection - add it to the breadcrumb.
73 $PAGE->navbar->add(core_tag_collection::display_name($tagcoll),
74 new moodle_url($manageurl, array('tc' => $tagcoll->id)));
77 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
79 $PAGE->set_primary_active_tab('siteadminnode');
81 switch($action) {
83 case 'colladd':
84 require_sesskey();
85 $name = required_param('name', PARAM_NOTAGS);
86 $searchable = optional_param('searchable', false, PARAM_BOOL);
87 core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
88 redirect($manageurl);
89 break;
91 case 'colldelete':
92 if ($tagcoll && !$tagcoll->component) {
93 require_sesskey();
94 core_tag_collection::delete($tagcoll);
95 \core\notification::success(get_string('changessaved', 'core_tag'));
97 redirect($manageurl);
98 break;
100 case 'collmoveup':
101 if ($tagcoll) {
102 require_sesskey();
103 core_tag_collection::change_sortorder($tagcoll, -1);
104 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
106 redirect($manageurl);
107 break;
109 case 'collmovedown':
110 if ($tagcoll) {
111 require_sesskey();
112 core_tag_collection::change_sortorder($tagcoll, 1);
113 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
115 redirect($manageurl);
116 break;
118 case 'delete':
119 if ($tagid) {
120 require_sesskey();
121 core_tag_tag::delete_tags(array($tagid));
122 \core\notification::success(get_string('deleted', 'core_tag'));
124 redirect($PAGE->url);
125 break;
127 case 'bulk':
128 if (optional_param('bulkdelete', null, PARAM_RAW) !== null) {
129 if ($tagschecked) {
130 require_sesskey();
131 core_tag_tag::delete_tags($tagschecked);
132 \core\notification::success(get_string('deleted', 'core_tag'));
134 redirect($PAGE->url);
135 } else if (optional_param('bulkcombine', null, PARAM_RAW) !== null) {
136 $tags = core_tag_tag::get_bulk($tagschecked, '*');
137 if (count($tags) > 1) {
138 require_sesskey();
139 if (($maintag = optional_param('maintag', 0, PARAM_INT)) && array_key_exists($maintag, $tags)) {
140 $tag = $tags[$maintag];
141 } else {
142 $tag = array_shift($tags);
144 $tag->combine_tags($tags);
145 \core\notification::success(get_string('combined', 'core_tag'));
147 redirect($PAGE->url);
149 break;
151 case 'renamecombine':
152 // Allows to rename the tag and if the tag with the new name already exists these tags will be combined.
153 if ($tagid && ($newname = required_param('newname', PARAM_TAG))) {
154 require_sesskey();
155 $tag = core_tag_tag::get($tagid, '*', MUST_EXIST);
156 $targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*');
157 if ($targettag) {
158 $targettag->combine_tags(array($tag));
159 \core\notification::success(get_string('combined', 'core_tag'));
160 } else {
161 $tag->update(array('rawname' => $newname));
162 \core\notification::success(get_string('changessaved', 'core_tag'));
165 redirect($PAGE->url);
166 break;
168 case 'addstandardtag':
169 require_sesskey();
170 $tagobjects = array();
171 if ($tagcoll) {
172 $tagslist = optional_param('tagslist', '', PARAM_RAW);
173 $newtags = preg_split('/\s*,\s*/', trim($tagslist), -1, PREG_SPLIT_NO_EMPTY);
174 $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
176 foreach ($tagobjects as $tagobject) {
177 if (!$tagobject->isstandard) {
178 $tagobject->update(array('isstandard' => 1));
181 redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
182 null, \core\output\notification::NOTIFY_SUCCESS);
183 break;
186 echo $OUTPUT->header();
188 if (!$tagcoll) {
189 // Tag collection is not specified. Display the overview of tag collections and tag areas.
190 $tagareastable = new core_tag_areas_table($manageurl);
191 $colltable = new core_tag_collections_table($manageurl);
193 echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
194 echo html_writer::table($colltable);
195 $url = new moodle_url($manageurl, array('action' => 'colladd'));
196 echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
197 'mdl-right addtagcoll');
199 echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
200 echo html_writer::table($tagareastable);
202 $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
204 echo $OUTPUT->footer();
205 exit;
208 // Tag collection is specified. Manage tags in this collection.
209 echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
211 $hiddenfields = [
212 (object) ['type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid],
213 (object) ['type' => 'hidden', 'name' => 'perpage', 'value' => $perpage]
216 $otherfields = '';
217 if ($filter !== '') {
218 $otherfields = html_writer::link(new moodle_url($PAGE->url, ['filter' => null]),
219 get_string('resetfilter', 'tag'));
222 $data = [
223 'action' => new moodle_url('/tag/manage.php'),
224 'hiddenfields' => $hiddenfields,
225 'inputname' => 'filter',
226 'searchstring' => get_string('search'),
227 'query' => s($filter),
228 'extraclasses' => 'mb-2',
229 'otherfields' => $otherfields
231 echo $OUTPUT->render_from_template('core/search_input', $data);
233 // Link to add an standard tags.
234 $img = $OUTPUT->pix_icon('t/add', '');
235 echo '<div class="addstandardtags visibleifjs">' .
236 html_writer::link('#', $img . get_string('addotags', 'tag'), array('data-action' => 'addstandardtag')) .
237 '</div>';
239 $table = new core_tag_manage_table($tagcollid);
240 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
241 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
242 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
243 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
244 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
245 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
246 echo $table->out($perpage, true);
248 if ($table->rawdata) {
249 echo html_writer::start_tag('p');
250 echo html_writer::tag('button', get_string('deleteselected', 'tag'),
251 array('id' => 'tag-management-delete', 'type' => 'submit',
252 'class' => 'tagdeleteselected btn btn-secondary', 'name' => 'bulkdelete'));
253 echo html_writer::tag('button', get_string('combineselected', 'tag'),
254 array('id' => 'tag-management-combine', 'type' => 'submit',
255 'class' => 'tagcombineselected btn btn-secondary', 'name' => 'bulkcombine'));
256 echo html_writer::end_tag('p');
258 echo '</form>';
260 $totalcount = $table->totalcount;
261 if ($perpage == SHOW_ALL_PAGE_SIZE) {
262 echo html_writer::start_tag('div', array('id' => 'showall'));
263 $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
264 $url = new moodle_url($PAGE->url, $params);
265 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
266 echo html_writer::end_tag('div');
267 } else if ($totalcount > 0 and $perpage < $totalcount) {
268 echo html_writer::start_tag('div', array('id' => 'showall'));
269 $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
270 $url = new moodle_url($PAGE->url, $params);
271 echo html_writer::link($url, get_string('showall', '', $totalcount));
272 echo html_writer::end_tag('div');
275 echo $OUTPUT->footer();