Merge branch 'MDL-81713-main' of https://github.com/junpataleta/moodle
[moodle.git] / tag / manage.php
blob144160f44f721860d36e494086dd4c1004e6c347
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('lib.php');
27 require_once($CFG->libdir.'/adminlib.php');
29 use core\context\system;
30 use core_reportbuilder\system_report_factory;
31 use core_tag\reportbuilder\local\systemreports\tags;
33 $tagid = optional_param('tagid', null, PARAM_INT);
34 $isstandard = optional_param('isstandard', null, PARAM_INT);
35 $action = optional_param('action', '', PARAM_ALPHA);
36 $tagcollid = optional_param('tc', 0, PARAM_INT);
38 $params = array();
39 if ($tagcollid) {
40 $params['tc'] = $tagcollid;
43 admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
45 if (empty($CFG->usetags)) {
46 throw new \moodle_exception('tagsaredisabled', 'tag');
49 $tagobject = null;
50 if ($tagid) {
51 $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
52 $tagcollid = $tagobject->tagcollid;
54 $tagcoll = core_tag_collection::get_by_id($tagcollid);
55 $manageurl = new moodle_url('/tag/manage.php');
56 if ($tagcoll) {
57 // We are inside a tag collection - add it to the breadcrumb.
58 $PAGE->navbar->add(core_tag_collection::display_name($tagcoll),
59 new moodle_url($manageurl, array('tc' => $tagcoll->id)));
62 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
64 $PAGE->set_primary_active_tab('siteadminnode');
66 switch($action) {
68 case 'colladd':
69 require_sesskey();
70 $name = required_param('name', PARAM_NOTAGS);
71 $searchable = optional_param('searchable', false, PARAM_BOOL);
72 core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
73 redirect($manageurl);
74 break;
76 case 'colldelete':
77 if ($tagcoll && !$tagcoll->component) {
78 require_sesskey();
79 core_tag_collection::delete($tagcoll);
80 \core\notification::success(get_string('changessaved', 'core_tag'));
82 redirect($manageurl);
83 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);
91 redirect($manageurl);
92 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);
100 redirect($manageurl);
101 break;
103 case 'delete':
104 if ($tagid) {
105 require_sesskey();
106 core_tag_tag::delete_tags(array($tagid));
107 \core\notification::success(get_string('deleted', 'core_tag'));
109 redirect($PAGE->url);
110 break;
112 case 'bulk':
113 $tagschecked = explode(',', optional_param('tagschecked', '', PARAM_SEQUENCE));
114 if (optional_param('bulkdelete', null, PARAM_RAW) !== null) {
115 if ($tagschecked) {
116 require_sesskey();
117 core_tag_tag::delete_tags($tagschecked);
118 \core\notification::success(get_string('deleted', 'core_tag'));
120 redirect($PAGE->url);
121 } else if (optional_param('bulkcombine', null, PARAM_RAW) !== null) {
122 $tags = core_tag_tag::get_bulk($tagschecked, '*');
123 if (count($tags) > 1) {
124 require_sesskey();
125 if (($maintag = optional_param('maintag', 0, PARAM_INT)) && array_key_exists($maintag, $tags)) {
126 $tag = $tags[$maintag];
127 } else {
128 $tag = array_shift($tags);
130 $tag->combine_tags($tags);
131 \core\notification::success(get_string('combined', 'core_tag'));
133 redirect($PAGE->url);
135 break;
137 case 'renamecombine':
138 // Allows to rename the tag and if the tag with the new name already exists these tags will be combined.
139 if ($tagid && ($newname = required_param('newname', PARAM_TAG))) {
140 require_sesskey();
141 $tag = core_tag_tag::get($tagid, '*', MUST_EXIST);
142 $targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*');
143 if ($targettag) {
144 $targettag->combine_tags(array($tag));
145 \core\notification::success(get_string('combined', 'core_tag'));
146 } else {
147 $tag->update(array('rawname' => $newname));
148 \core\notification::success(get_string('changessaved', 'core_tag'));
151 redirect($PAGE->url);
152 break;
154 case 'addstandardtag':
155 require_sesskey();
156 $tagobjects = array();
157 if ($tagcoll) {
158 $tagslist = optional_param('tagslist', '', PARAM_RAW);
159 $newtags = preg_split('/\s*,\s*/', trim($tagslist), -1, PREG_SPLIT_NO_EMPTY);
160 $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
162 foreach ($tagobjects as $tagobject) {
163 if (!$tagobject->isstandard) {
164 $tagobject->update(array('isstandard' => 1));
167 redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
168 null, \core\output\notification::NOTIFY_SUCCESS);
169 break;
172 echo $OUTPUT->header();
174 if (!$tagcoll) {
175 // Tag collection is not specified. Display the overview of tag collections and tag areas.
176 $tagareastable = new core_tag_areas_table($manageurl);
177 $colltable = new core_tag_collections_table($manageurl);
179 echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
180 echo html_writer::table($colltable);
181 $url = new moodle_url($manageurl, array('action' => 'colladd'));
182 echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
183 'mdl-right addtagcoll');
185 echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
186 echo html_writer::table($tagareastable);
188 $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
190 echo $OUTPUT->footer();
191 exit;
194 // Tag collection is specified. Manage tags in this collection.
195 echo html_writer::div(
196 $OUTPUT->heading(core_tag_collection::display_name($tagcoll)) .
197 html_writer::tag(
198 'button',
199 $OUTPUT->pix_icon('t/add', '') . get_string('addotags', 'core_tag'),
201 'type' => 'button',
202 'class' => 'btn btn-primary my-auto',
203 'data-action' => 'addstandardtag',
206 'd-flex justify-content-between mb-2',
209 // Render the report.
210 $report = system_report_factory::create(tags::class, system::instance(), '', '', 0, ['collection' => $tagcoll->id]);
211 echo $report->output();
213 // Render bulk actions.
214 if ($DB->record_exists('tag', [])) {
215 echo '<form class="tag-management-form" method="post" action="' . $PAGE->url->out_omit_querystring() . '">';
216 echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'tc', 'value' => $tagcoll->id]);
217 echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
218 echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'action', 'value' => 'bulk']);
219 echo html_writer::tag('button', get_string('deleteselected', 'tag'),
220 array('id' => 'tag-management-delete', 'type' => 'submit',
221 'class' => 'tagdeleteselected btn btn-secondary', 'name' => 'bulkdelete'));
222 echo html_writer::tag('button', get_string('combineselected', 'tag'),
223 array('id' => 'tag-management-combine', 'type' => 'submit',
224 'class' => 'tagcombineselected btn btn-secondary', 'name' => 'bulkcombine'));
225 echo '</form>';
228 $PAGE->requires->js_call_amd('core/tag', 'initManagePage');
230 echo $OUTPUT->footer();