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