MDL-51283 core_tag: Allow each tag area to set 'showstandard'
[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
141 case 'areaenable':
142 case 'areadisable':
143 if ($tagarea) {
144 require_sesskey();
145 $data = array('enabled' => ($action === 'areaenable') ? 1 : 0);
146 core_tag_area::update($tagarea, $data);
147 redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
148 }
149 redirect($manageurl);
150 break;
151
152 case 'areasetcoll':
153 if ($tagarea) {
154 require_sesskey();
155 if ($newtagcollid = optional_param('areacollid', null, PARAM_INT)) {
156 core_tag_area::update($tagarea, array('tagcollid' => $newtagcollid));
157 redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
158 }
159 }
160 redirect($manageurl);
161 break;
162
4be9c7ad
MG
163 case 'areasetshowstandard':
164 if ($tagarea) {
165 require_sesskey();
166 if (($showstandard = optional_param('showstandard', null, PARAM_INT)) !== null) {
167 core_tag_area::update($tagarea, array('showstandard' => $showstandard));
168 redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
169 }
170 }
171 redirect($manageurl);
172 break;
173
c933a060 174 case 'delete':
8e355853
MG
175 require_sesskey();
176 if (!$tagschecked && $tagid) {
177 $tagschecked = array($tagid);
70e9b59d 178 }
c026a28d
MG
179 core_tag_tag::delete_tags($tagschecked);
180 redirect(new moodle_url($PAGE->url, $tagschecked ? array('notice' => 'deleted') : null));
c933a060 181 break;
70e9b59d 182
8e355853
MG
183 case 'setflag':
184 require_sesskey();
c026a28d
MG
185 if ($tagid) {
186 $tagobject->flag();
187 redirect(new moodle_url($PAGE->url, array('notice' => 'flagged')));
188 }
189 redirect($PAGE->url);
3a597ace 190 break;
70e9b59d 191
8e355853
MG
192 case 'resetflag':
193 require_sesskey();
c026a28d
MG
194 if ($tagid) {
195 $tagobject->reset_flag();
196 redirect(new moodle_url($PAGE->url, array('notice' => 'resetflag')));
197 }
198 redirect($PAGE->url);
79081e87 199 break;
70e9b59d 200
8e355853
MG
201 case 'changetype':
202 require_sesskey();
e11d7380
MG
203 if ($tagid && $tagobject->update(
204 array('isstandard' => $isstandard ? 1 : 0))) {
c026a28d 205 redirect(new moodle_url($PAGE->url, array('notice' => 'typechanged')));
3a597ace 206 }
8e355853 207 redirect($PAGE->url);
c933a060 208 break;
8e355853 209
e11d7380 210 case 'addstandardtag':
8e355853 211 require_sesskey();
c026a28d
MG
212 $tagobjects = null;
213 if ($tagcoll) {
214 $otagsadd = optional_param('otagsadd', '', PARAM_RAW);
215 $newtags = preg_split('/\s*,\s*/', trim($otagsadd), -1, PREG_SPLIT_NO_EMPTY);
216 $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
e35139b7 217 }
c026a28d 218 foreach ($tagobjects as $tagobject) {
e11d7380
MG
219 if (!$tagobject->isstandard) {
220 $tagobject->update(array('isstandard' => 1));
3d535996 221 }
3d535996 222 }
c026a28d 223 redirect(new moodle_url($PAGE->url, $tagobjects ? array('notice' => 'added') : null));
3d535996 224 break;
c933a060 225}
226
8e355853
MG
227echo $OUTPUT->header();
228
229if ($notice && get_string_manager()->string_exists($notice, 'tag')) {
230 echo $OUTPUT->notification(get_string($notice, 'tag'), 'notifysuccess');
2d7e3f9b 231}
3a597ace 232
c026a28d
MG
233if (!$tagcoll) {
234 // Tag collection is not specified. Display the overview of tag collections and tag areas.
235 $tagareastable = new core_tag_areas_table($manageurl);
236 $colltable = new core_tag_collections_table($manageurl);
237
4be9c7ad 238 echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
c026a28d
MG
239 echo html_writer::table($colltable);
240 $url = new moodle_url($manageurl, array('action' => 'colladd'));
241 echo html_writer::div(html_writer::link($url, get_string('addtagcoll', 'tag')), 'mdl-right addtagcoll');
242
243 echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
244 echo html_writer::table($tagareastable);
245
246 echo $OUTPUT->footer();
247 exit;
248}
249
250// Tag collection is specified. Manage tags in this collection.
4be9c7ad 251echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
c026a28d 252
e11d7380 253// Small form to add an standard tag.
8e355853 254print('<form class="tag-addtags-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
c026a28d 255print('<input type="hidden" name="tc" value="'.$tagcollid.'" />');
e11d7380 256print('<input type="hidden" name="action" value="addstandardtag" />');
8e355853
MG
257print('<input type="hidden" name="perpage" value="'.$perpage.'" />');
258print('<input type="hidden" name="page" value="'.$page.'" />');
259print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">' .
260 get_string('addotags', 'tag') .'</label>'.
ec6a0e70 261 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
e35139b7 262 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
8e355853
MG
263 '<input name="addotags" value="'. get_string('addotags', 'tag') .
264 '" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
3d535996 265 '</div>');
266print('</form>');
267
c026a28d 268$table = new core_tag_manage_table($tagcollid);
8e355853 269echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
c026a28d 270echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
8e355853
MG
271echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
272echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'delete'));
273echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
274echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
275echo $table->out($perpage, true);
276
c026a28d
MG
277if ($table->rawdata) {
278 echo html_writer::start_tag('p');
279 echo html_writer::tag('button', get_string('deleteselected', 'tag'),
280 array('id' => 'tag-management-delete', 'type' => 'submit', 'class' => 'tagdeleteselected'));
281 echo html_writer::end_tag('p');
282}
8e355853
MG
283echo '</form>';
284
285$totalcount = $table->totalcount;
70e9b59d 286if ($perpage == SHOW_ALL_PAGE_SIZE) {
ca2d7327 287 echo html_writer::start_tag('div', array('id' => 'showall'));
8e355853
MG
288 $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
289 $url = new moodle_url($PAGE->url, $params);
ca2d7327
RW
290 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
291 echo html_writer::end_tag('div');
70e9b59d 292} else if ($totalcount > 0 and $perpage < $totalcount) {
ca2d7327 293 echo html_writer::start_tag('div', array('id' => 'showall'));
8e355853
MG
294 $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
295 $url = new moodle_url($PAGE->url, $params);
ca2d7327
RW
296 echo html_writer::link($url, get_string('showall', '', $totalcount));
297 echo html_writer::end_tag('div');
70e9b59d 298}
e3ff328b 299
1dd2ba54 300echo $OUTPUT->footer();