MDL-53254 tags: tag areas inplace editable
[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
MG
38$page = optional_param('page', 0, PARAM_INT);
39$notice = optional_param('notice', '', PARAM_ALPHA);
c026a28d
MG
40$tagcollid = optional_param('tc', 0, PARAM_INT);
41$tagareaid = optional_param('ta', null, PARAM_INT);
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}
c026a28d 50
8e355853 51admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
823c593f 52
c026a28d
MG
53if (empty($CFG->usetags)) {
54 print_error('tagsaredisabled', 'tag');
55}
56
57$tagobject = null;
58if ($tagid) {
59 $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
60 $tagcollid = $tagobject->tagcollid;
61}
62$tagcoll = core_tag_collection::get_by_id($tagcollid);
63$tagarea = core_tag_area::get_by_id($tagareaid);
64$manageurl = new moodle_url('/tag/manage.php');
65if ($tagcoll) {
66 // We are inside a tag collection - add it to the page url and the breadcrumb.
67 $PAGE->set_url(new moodle_url($PAGE->url, array('tc' => $tagcoll->id)));
68 $PAGE->navbar->add(core_tag_collection::display_name($tagcoll),
69 new moodle_url($manageurl, array('tc' => $tagcoll->id)));
70}
71
0edd6535 72$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
823c593f 73
c933a060 74switch($action) {
3a597ace 75
c026a28d
MG
76 case 'colladd':
77 case 'colledit':
78 if ($action === 'colladd' || ($action === 'colledit' && $tagcoll && empty($tagcoll->component))) {
79 $form = new core_tag_collection_form($manageurl, $tagcoll);
80 if ($form->is_cancelled()) {
81 redirect($manageurl);
82 } else if ($data = $form->get_data()) {
83 if ($action === 'colladd') {
84 core_tag_collection::create($data);
85 } else {
86 core_tag_collection::update($tagcoll, $data);
87 }
88 redirect($manageurl);
89 } else {
90 $title = ($action === 'colladd') ?
91 get_string('addtagcoll', 'tag') :
92 get_string('edittagcoll', 'tag', core_tag_collection::display_name($tagcoll));
93 $PAGE->navbar->add($title);
94 echo $OUTPUT->header();
95 echo $OUTPUT->heading($title, 2);
96 $form->display();
97 echo $OUTPUT->footer();
98 exit;
99 }
100 }
101 break;
102
103 case 'colldelete':
104 $confirm = optional_param('confirm', false, PARAM_BOOL);
105 if (!$confirm) {
106 echo $OUTPUT->header();
107 $strconfirm = get_string('suredeletecoll', 'tag', core_tag_collection::display_name($tagcoll));
108 $params = array('tc' => $tagcoll->id, 'confirm' => 1, 'sesskey' => sesskey(), 'action' => 'colldelete');
109 $formcontinue = new single_button(new moodle_url($manageurl, $params), get_string('yes'));
110 $formcancel = new single_button($manageurl, get_string('no'), 'get');
111 echo $OUTPUT->confirm($strconfirm, $formcontinue, $formcancel);
112 echo $OUTPUT->footer();
113 die;
114 }
115 if ($tagcoll && !$tagcoll->component) {
116 require_sesskey();
117 core_tag_collection::delete($tagcoll);
118 redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
119 }
120 redirect($manageurl);
121 break;
122
123 case 'collmoveup':
124 if ($tagcoll) {
125 require_sesskey();
126 core_tag_collection::change_sortorder($tagcoll, -1);
127 redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
128 }
129 redirect($manageurl);
130 break;
131
132 case 'collmovedown':
133 if ($tagcoll) {
134 require_sesskey();
135 core_tag_collection::change_sortorder($tagcoll, 1);
136 redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
137 }
138 redirect($manageurl);
139 break;
140
c933a060 141 case 'delete':
8e355853
MG
142 require_sesskey();
143 if (!$tagschecked && $tagid) {
144 $tagschecked = array($tagid);
70e9b59d 145 }
c026a28d
MG
146 core_tag_tag::delete_tags($tagschecked);
147 redirect(new moodle_url($PAGE->url, $tagschecked ? array('notice' => 'deleted') : null));
c933a060 148 break;
70e9b59d 149
8e355853
MG
150 case 'setflag':
151 require_sesskey();
c026a28d
MG
152 if ($tagid) {
153 $tagobject->flag();
154 redirect(new moodle_url($PAGE->url, array('notice' => 'flagged')));
155 }
156 redirect($PAGE->url);
3a597ace 157 break;
70e9b59d 158
8e355853
MG
159 case 'resetflag':
160 require_sesskey();
c026a28d
MG
161 if ($tagid) {
162 $tagobject->reset_flag();
163 redirect(new moodle_url($PAGE->url, array('notice' => 'resetflag')));
164 }
165 redirect($PAGE->url);
79081e87 166 break;
70e9b59d 167
8e355853
MG
168 case 'changetype':
169 require_sesskey();
e11d7380
MG
170 if ($tagid && $tagobject->update(
171 array('isstandard' => $isstandard ? 1 : 0))) {
c026a28d 172 redirect(new moodle_url($PAGE->url, array('notice' => 'typechanged')));
3a597ace 173 }
8e355853 174 redirect($PAGE->url);
c933a060 175 break;
8e355853 176
e11d7380 177 case 'addstandardtag':
8e355853 178 require_sesskey();
c026a28d
MG
179 $tagobjects = null;
180 if ($tagcoll) {
181 $otagsadd = optional_param('otagsadd', '', PARAM_RAW);
182 $newtags = preg_split('/\s*,\s*/', trim($otagsadd), -1, PREG_SPLIT_NO_EMPTY);
183 $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
e35139b7 184 }
c026a28d 185 foreach ($tagobjects as $tagobject) {
e11d7380
MG
186 if (!$tagobject->isstandard) {
187 $tagobject->update(array('isstandard' => 1));
3d535996 188 }
3d535996 189 }
c026a28d 190 redirect(new moodle_url($PAGE->url, $tagobjects ? array('notice' => 'added') : null));
3d535996 191 break;
c933a060 192}
193
8e355853
MG
194echo $OUTPUT->header();
195
196if ($notice && get_string_manager()->string_exists($notice, 'tag')) {
197 echo $OUTPUT->notification(get_string($notice, 'tag'), 'notifysuccess');
2d7e3f9b 198}
3a597ace 199
c026a28d
MG
200if (!$tagcoll) {
201 // Tag collection is not specified. Display the overview of tag collections and tag areas.
202 $tagareastable = new core_tag_areas_table($manageurl);
203 $colltable = new core_tag_collections_table($manageurl);
204
4be9c7ad 205 echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
c026a28d
MG
206 echo html_writer::table($colltable);
207 $url = new moodle_url($manageurl, array('action' => 'colladd'));
208 echo html_writer::div(html_writer::link($url, get_string('addtagcoll', 'tag')), 'mdl-right addtagcoll');
209
210 echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
211 echo html_writer::table($tagareastable);
212
131b78b9
MG
213 $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
214
c026a28d
MG
215 echo $OUTPUT->footer();
216 exit;
217}
218
219// Tag collection is specified. Manage tags in this collection.
4be9c7ad 220echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
c026a28d 221
e11d7380 222// Small form to add an standard tag.
8e355853 223print('<form class="tag-addtags-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
c026a28d 224print('<input type="hidden" name="tc" value="'.$tagcollid.'" />');
e11d7380 225print('<input type="hidden" name="action" value="addstandardtag" />');
8e355853
MG
226print('<input type="hidden" name="perpage" value="'.$perpage.'" />');
227print('<input type="hidden" name="page" value="'.$page.'" />');
228print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">' .
229 get_string('addotags', 'tag') .'</label>'.
ec6a0e70 230 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
e35139b7 231 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
8e355853
MG
232 '<input name="addotags" value="'. get_string('addotags', 'tag') .
233 '" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
3d535996 234 '</div>');
235print('</form>');
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
MG
240echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
241echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'delete'));
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'),
249 array('id' => 'tag-management-delete', 'type' => 'submit', 'class' => 'tagdeleteselected'));
250 echo html_writer::end_tag('p');
251}
8e355853
MG
252echo '</form>';
253
254$totalcount = $table->totalcount;
70e9b59d 255if ($perpage == SHOW_ALL_PAGE_SIZE) {
ca2d7327 256 echo html_writer::start_tag('div', array('id' => 'showall'));
8e355853
MG
257 $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
258 $url = new moodle_url($PAGE->url, $params);
ca2d7327
RW
259 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
260 echo html_writer::end_tag('div');
70e9b59d 261} else if ($totalcount > 0 and $perpage < $totalcount) {
ca2d7327 262 echo html_writer::start_tag('div', array('id' => 'showall'));
8e355853
MG
263 $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
264 $url = new moodle_url($PAGE->url, $params);
ca2d7327
RW
265 echo html_writer::link($url, get_string('showall', '', $totalcount));
266 echo html_writer::end_tag('div');
70e9b59d 267}
e3ff328b 268
1dd2ba54 269echo $OUTPUT->footer();