Moodle release 3.6.9
[moodle.git] / tag / manage.php
blob3f757628c6f9e98b099b5f53b57381545009f69c
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 print_error('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 switch($action) {
81 case 'colladd':
82 require_sesskey();
83 $name = required_param('name', PARAM_NOTAGS);
84 $searchable = optional_param('searchable', false, PARAM_BOOL);
85 core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
86 redirect($manageurl);
87 break;
89 case 'colldelete':
90 if ($tagcoll && !$tagcoll->component) {
91 require_sesskey();
92 core_tag_collection::delete($tagcoll);
93 \core\notification::success(get_string('changessaved', 'core_tag'));
95 redirect($manageurl);
96 break;
98 case 'collmoveup':
99 if ($tagcoll) {
100 require_sesskey();
101 core_tag_collection::change_sortorder($tagcoll, -1);
102 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
104 redirect($manageurl);
105 break;
107 case 'collmovedown':
108 if ($tagcoll) {
109 require_sesskey();
110 core_tag_collection::change_sortorder($tagcoll, 1);
111 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
113 redirect($manageurl);
114 break;
116 case 'delete':
117 if ($tagid) {
118 require_sesskey();
119 core_tag_tag::delete_tags(array($tagid));
120 \core\notification::success(get_string('deleted', 'core_tag'));
122 redirect($PAGE->url);
123 break;
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'));
132 redirect($PAGE->url);
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);
142 $tag->combine_tags($tags);
143 \core\notification::success(get_string('combined', 'core_tag'));
145 redirect($PAGE->url);
147 break;
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'));
163 redirect($PAGE->url);
164 break;
166 case 'addstandardtag':
167 require_sesskey();
168 $tagobjects = array();
169 if ($tagcoll) {
170 $tagslist = optional_param('tagslist', '', PARAM_RAW);
171 $newtags = preg_split('/\s*,\s*/', trim($tagslist), -1, PREG_SPLIT_NO_EMPTY);
172 $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
174 foreach ($tagobjects as $tagobject) {
175 if (!$tagobject->isstandard) {
176 $tagobject->update(array('isstandard' => 1));
179 redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
180 null, \core\output\notification::NOTIFY_SUCCESS);
181 break;
184 echo $OUTPUT->header();
186 if (!$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);
191 echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
192 echo html_writer::table($colltable);
193 $url = new moodle_url($manageurl, array('action' => 'colladd'));
194 echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
195 'mdl-right addtagcoll');
197 echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
198 echo html_writer::table($tagareastable);
200 $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
202 echo $OUTPUT->footer();
203 exit;
206 // Tag collection is specified. Manage tags in this collection.
207 echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
209 // Form to filter tags.
210 print('<form class="tag-filter-form" method="get" action="'.$CFG->wwwroot.'/tag/manage.php">');
211 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_tagfilter">'. get_string('search') .'</label>'.
212 '<input type="hidden" name="tc" value="'.$tagcollid.'" />'.
213 '<input type="hidden" name="perpage" value="'.$perpage.'" />'.
214 '<input id="id_tagfilter" name="filter" type="text" value=' . s($filter) . '>'.
215 '<input value="'. s(get_string('search')) .'" type="submit" class="btn btn-secondary"> '.
216 ($filter !== '' ? html_writer::link(new moodle_url($PAGE->url, array('filter' => null)),
217 get_string('resetfilter', 'tag'), array('class' => 'resetfilterlink')) : '').
218 '</div>');
219 print('</form>');
221 // Link to add an standard tags.
222 $img = $OUTPUT->pix_icon('t/add', '');
223 echo '<div class="addstandardtags visibleifjs">' .
224 html_writer::link('#', $img . get_string('addotags', 'tag'), array('data-action' => 'addstandardtag')) .
225 '</div>';
227 $table = new core_tag_manage_table($tagcollid);
228 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
229 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
230 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
231 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
232 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
233 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
234 echo $table->out($perpage, true);
236 if ($table->rawdata) {
237 echo html_writer::start_tag('p');
238 echo html_writer::tag('button', get_string('deleteselected', 'tag'),
239 array('id' => 'tag-management-delete', 'type' => 'submit',
240 'class' => 'tagdeleteselected btn btn-secondary', 'name' => 'bulkdelete'));
241 echo html_writer::tag('button', get_string('combineselected', 'tag'),
242 array('id' => 'tag-management-combine', 'type' => 'submit',
243 'class' => 'tagcombineselected btn btn-secondary', 'name' => 'bulkcombine'));
244 echo html_writer::end_tag('p');
246 echo '</form>';
248 $totalcount = $table->totalcount;
249 if ($perpage == SHOW_ALL_PAGE_SIZE) {
250 echo html_writer::start_tag('div', array('id' => 'showall'));
251 $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
252 $url = new moodle_url($PAGE->url, $params);
253 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
254 echo html_writer::end_tag('div');
255 } else if ($totalcount > 0 and $perpage < $totalcount) {
256 echo html_writer::start_tag('div', array('id' => 'showall'));
257 $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
258 $url = new moodle_url($PAGE->url, $params);
259 echo html_writer::link($url, get_string('showall', '', $totalcount));
260 echo html_writer::end_tag('div');
263 echo $OUTPUT->footer();