MDL-52707 core_tag: allow to combine tags
[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);
c933a060 41
0edd6535 42$params = array();
43if ($perpage != DEFAULT_PAGE_SIZE) {
44 $params['perpage'] = $perpage;
45}
8e355853
MG
46if ($page > 0) {
47 $params['page'] = $page;
48}
0d202789
MG
49if ($tagcollid) {
50 $params['tc'] = $tagcollid;
51}
c026a28d 52
8e355853 53admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
823c593f 54
c026a28d
MG
55if (empty($CFG->usetags)) {
56 print_error('tagsaredisabled', 'tag');
57}
58
59$tagobject = null;
60if ($tagid) {
61 $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
62 $tagcollid = $tagobject->tagcollid;
63}
64$tagcoll = core_tag_collection::get_by_id($tagcollid);
65$tagarea = core_tag_area::get_by_id($tagareaid);
66$manageurl = new moodle_url('/tag/manage.php');
67if ($tagcoll) {
0d202789 68 // We are inside a tag collection - add it to the breadcrumb.
c026a28d
MG
69 $PAGE->navbar->add(core_tag_collection::display_name($tagcoll),
70 new moodle_url($manageurl, array('tc' => $tagcoll->id)));
71}
72
0edd6535 73$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
823c593f 74
c933a060 75switch($action) {
3a597ace 76
c026a28d 77 case 'colladd':
352bfbf5
MG
78 require_sesskey();
79 $name = required_param('name', PARAM_NOTAGS);
80 $searchable = required_param('searchable', PARAM_BOOL);
81 core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
21d9596c 82 redirect($manageurl);
c026a28d
MG
83 break;
84
85 case 'colldelete':
c026a28d
MG
86 if ($tagcoll && !$tagcoll->component) {
87 require_sesskey();
88 core_tag_collection::delete($tagcoll);
352bfbf5 89 \core\notification::success(get_string('changessaved', 'core_tag'));
c026a28d
MG
90 }
91 redirect($manageurl);
92 break;
93
94 case 'collmoveup':
95 if ($tagcoll) {
96 require_sesskey();
97 core_tag_collection::change_sortorder($tagcoll, -1);
352bfbf5 98 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
c026a28d
MG
99 }
100 redirect($manageurl);
101 break;
102
103 case 'collmovedown':
104 if ($tagcoll) {
105 require_sesskey();
106 core_tag_collection::change_sortorder($tagcoll, 1);
352bfbf5 107 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
c026a28d
MG
108 }
109 redirect($manageurl);
110 break;
111
c933a060 112 case 'delete':
0d202789
MG
113 if ($tagid) {
114 require_sesskey();
115 core_tag_tag::delete_tags(array($tagid));
116 \core\notification::success(get_string('deleted', 'core_tag'));
70e9b59d 117 }
0d202789
MG
118 redirect($PAGE->url);
119 break;
120
121 case 'bulk':
122 if (optional_param('bulkdelete', null, PARAM_RAW) !== null) {
123 if ($tagschecked) {
124 require_sesskey();
125 core_tag_tag::delete_tags($tagschecked);
126 \core\notification::success(get_string('deleted', 'core_tag'));
127 }
352bfbf5 128 redirect($PAGE->url);
0d202789
MG
129 } else if (optional_param('bulkcombine', null, PARAM_RAW) !== null) {
130 $tags = core_tag_tag::get_bulk($tagschecked, '*');
131 if (count($tags) > 1) {
132 require_sesskey();
133 if (($maintag = optional_param('maintag', 0, PARAM_INT)) && array_key_exists($maintag, $tags)) {
134 $tag = $tags[$maintag];
135 } else {
136 $tag = array_shift($tags);
137 }
138 $tag->combine_tags($tags);
139 \core\notification::success(get_string('combined', 'core_tag'));
140 }
141 redirect($PAGE->url);
142 }
143 break;
144
145 case 'renamecombine':
146 // Allows to rename the tag and if the tag with the new name already exists these tags will be combined.
147 if ($tagid && ($newname = required_param('newname', PARAM_TAG))) {
148 require_sesskey();
149 $tag = core_tag_tag::get($tagid, '*', MUST_EXIST);
150 $targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*');
151 if ($targettag) {
152 $targettag->combine_tags(array($tag));
153 \core\notification::success(get_string('combined', 'core_tag'));
154 } else {
155 $tag->update(array('rawname' => $newname));
156 \core\notification::success(get_string('changessaved', 'core_tag'));
157 }
352bfbf5 158 }
0d202789 159 redirect($PAGE->url);
c933a060 160 break;
70e9b59d 161
e11d7380 162 case 'addstandardtag':
8e355853 163 require_sesskey();
21d9596c 164 $tagobjects = array();
c026a28d
MG
165 if ($tagcoll) {
166 $otagsadd = optional_param('otagsadd', '', PARAM_RAW);
167 $newtags = preg_split('/\s*,\s*/', trim($otagsadd), -1, PREG_SPLIT_NO_EMPTY);
168 $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
e35139b7 169 }
c026a28d 170 foreach ($tagobjects as $tagobject) {
e11d7380
MG
171 if (!$tagobject->isstandard) {
172 $tagobject->update(array('isstandard' => 1));
3d535996 173 }
3d535996 174 }
352bfbf5
MG
175 redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
176 null, \core\output\notification::NOTIFY_SUCCESS);
3d535996 177 break;
c933a060 178}
179
8e355853
MG
180echo $OUTPUT->header();
181
c026a28d
MG
182if (!$tagcoll) {
183 // Tag collection is not specified. Display the overview of tag collections and tag areas.
184 $tagareastable = new core_tag_areas_table($manageurl);
185 $colltable = new core_tag_collections_table($manageurl);
186
4be9c7ad 187 echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
c026a28d
MG
188 echo html_writer::table($colltable);
189 $url = new moodle_url($manageurl, array('action' => 'colladd'));
352bfbf5
MG
190 echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
191 'mdl-right addtagcoll');
c026a28d
MG
192
193 echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
194 echo html_writer::table($tagareastable);
195
131b78b9
MG
196 $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
197
c026a28d
MG
198 echo $OUTPUT->footer();
199 exit;
200}
201
202// Tag collection is specified. Manage tags in this collection.
4be9c7ad 203echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
c026a28d 204
e11d7380 205// Small form to add an standard tag.
8e355853 206print('<form class="tag-addtags-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
c026a28d 207print('<input type="hidden" name="tc" value="'.$tagcollid.'" />');
e11d7380 208print('<input type="hidden" name="action" value="addstandardtag" />');
8e355853
MG
209print('<input type="hidden" name="perpage" value="'.$perpage.'" />');
210print('<input type="hidden" name="page" value="'.$page.'" />');
211print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">' .
212 get_string('addotags', 'tag') .'</label>'.
ec6a0e70 213 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
e35139b7 214 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
8e355853
MG
215 '<input name="addotags" value="'. get_string('addotags', 'tag') .
216 '" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
3d535996 217 '</div>');
218print('</form>');
219
c026a28d 220$table = new core_tag_manage_table($tagcollid);
8e355853 221echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
c026a28d 222echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
8e355853 223echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
0d202789 224echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
8e355853
MG
225echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
226echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
227echo $table->out($perpage, true);
228
c026a28d
MG
229if ($table->rawdata) {
230 echo html_writer::start_tag('p');
231 echo html_writer::tag('button', get_string('deleteselected', 'tag'),
0d202789
MG
232 array('id' => 'tag-management-delete', 'type' => 'submit', 'class' => 'tagdeleteselected', 'name' => 'bulkdelete'));
233 echo html_writer::tag('button', get_string('combineselected', 'tag'),
234 array('id' => 'tag-management-combine', 'type' => 'submit', 'class' => 'tagcombineselected', 'name' => 'bulkcombine'));
c026a28d
MG
235 echo html_writer::end_tag('p');
236}
8e355853
MG
237echo '</form>';
238
239$totalcount = $table->totalcount;
70e9b59d 240if ($perpage == SHOW_ALL_PAGE_SIZE) {
ca2d7327 241 echo html_writer::start_tag('div', array('id' => 'showall'));
8e355853
MG
242 $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
243 $url = new moodle_url($PAGE->url, $params);
ca2d7327
RW
244 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
245 echo html_writer::end_tag('div');
70e9b59d 246} else if ($totalcount > 0 and $perpage < $totalcount) {
ca2d7327 247 echo html_writer::start_tag('div', array('id' => 'showall'));
8e355853
MG
248 $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
249 $url = new moodle_url($PAGE->url, $params);
ca2d7327
RW
250 echo html_writer::link($url, get_string('showall', '', $totalcount));
251 echo html_writer::end_tag('div');
70e9b59d 252}
e3ff328b 253
1dd2ba54 254echo $OUTPUT->footer();