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