forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.php
273 lines (240 loc) · 10.4 KB
/
manage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Managing tags, tag areas and tags collections
*
* @package core_tag
* @copyright 2007 Luiz Cruz <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../config.php');
require_once($CFG->libdir.'/tablelib.php');
require_once('lib.php');
require_once($CFG->libdir.'/adminlib.php');
define('SHOW_ALL_PAGE_SIZE', 50000);
define('DEFAULT_PAGE_SIZE', 30);
$tagschecked = optional_param_array('tagschecked', array(), PARAM_INT);
$tagid = optional_param('tagid', null, PARAM_INT);
$isstandard = optional_param('isstandard', null, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
$tagcollid = optional_param('tc', 0, PARAM_INT);
$tagareaid = optional_param('ta', null, PARAM_INT);
$filter = optional_param('filter', '', PARAM_NOTAGS);
$params = array();
if ($perpage != DEFAULT_PAGE_SIZE) {
$params['perpage'] = $perpage;
}
if ($page > 0) {
$params['page'] = $page;
}
if ($tagcollid) {
$params['tc'] = $tagcollid;
}
if ($filter !== '') {
$params['filter'] = $filter;
}
admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
if (empty($CFG->usetags)) {
print_error('tagsaredisabled', 'tag');
}
$tagobject = null;
if ($tagid) {
$tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
$tagcollid = $tagobject->tagcollid;
}
$tagcoll = core_tag_collection::get_by_id($tagcollid);
$tagarea = core_tag_area::get_by_id($tagareaid);
$manageurl = new moodle_url('/tag/manage.php');
if ($tagcoll) {
// We are inside a tag collection - add it to the breadcrumb.
$PAGE->navbar->add(core_tag_collection::display_name($tagcoll),
new moodle_url($manageurl, array('tc' => $tagcoll->id)));
}
$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
switch($action) {
case 'colladd':
require_sesskey();
$name = required_param('name', PARAM_NOTAGS);
$searchable = optional_param('searchable', false, PARAM_BOOL);
core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
redirect($manageurl);
break;
case 'colldelete':
if ($tagcoll && !$tagcoll->component) {
require_sesskey();
core_tag_collection::delete($tagcoll);
\core\notification::success(get_string('changessaved', 'core_tag'));
}
redirect($manageurl);
break;
case 'collmoveup':
if ($tagcoll) {
require_sesskey();
core_tag_collection::change_sortorder($tagcoll, -1);
redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
}
redirect($manageurl);
break;
case 'collmovedown':
if ($tagcoll) {
require_sesskey();
core_tag_collection::change_sortorder($tagcoll, 1);
redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
}
redirect($manageurl);
break;
case 'delete':
if ($tagid) {
require_sesskey();
core_tag_tag::delete_tags(array($tagid));
\core\notification::success(get_string('deleted', 'core_tag'));
}
redirect($PAGE->url);
break;
case 'bulk':
if (optional_param('bulkdelete', null, PARAM_RAW) !== null) {
if ($tagschecked) {
require_sesskey();
core_tag_tag::delete_tags($tagschecked);
\core\notification::success(get_string('deleted', 'core_tag'));
}
redirect($PAGE->url);
} else if (optional_param('bulkcombine', null, PARAM_RAW) !== null) {
$tags = core_tag_tag::get_bulk($tagschecked, '*');
if (count($tags) > 1) {
require_sesskey();
if (($maintag = optional_param('maintag', 0, PARAM_INT)) && array_key_exists($maintag, $tags)) {
$tag = $tags[$maintag];
} else {
$tag = array_shift($tags);
}
$tag->combine_tags($tags);
\core\notification::success(get_string('combined', 'core_tag'));
}
redirect($PAGE->url);
}
break;
case 'renamecombine':
// Allows to rename the tag and if the tag with the new name already exists these tags will be combined.
if ($tagid && ($newname = required_param('newname', PARAM_TAG))) {
require_sesskey();
$tag = core_tag_tag::get($tagid, '*', MUST_EXIST);
$targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*');
if ($targettag) {
$targettag->combine_tags(array($tag));
\core\notification::success(get_string('combined', 'core_tag'));
} else {
$tag->update(array('rawname' => $newname));
\core\notification::success(get_string('changessaved', 'core_tag'));
}
}
redirect($PAGE->url);
break;
case 'addstandardtag':
require_sesskey();
$tagobjects = array();
if ($tagcoll) {
$tagslist = optional_param('tagslist', '', PARAM_RAW);
$newtags = preg_split('/\s*,\s*/', trim($tagslist), -1, PREG_SPLIT_NO_EMPTY);
$tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
}
foreach ($tagobjects as $tagobject) {
if (!$tagobject->isstandard) {
$tagobject->update(array('isstandard' => 1));
}
}
redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
null, \core\output\notification::NOTIFY_SUCCESS);
break;
}
echo $OUTPUT->header();
if (!$tagcoll) {
// Tag collection is not specified. Display the overview of tag collections and tag areas.
$tagareastable = new core_tag_areas_table($manageurl);
$colltable = new core_tag_collections_table($manageurl);
echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
echo html_writer::table($colltable);
$url = new moodle_url($manageurl, array('action' => 'colladd'));
echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
'mdl-right addtagcoll');
echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
echo html_writer::table($tagareastable);
$PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
echo $OUTPUT->footer();
exit;
}
// Tag collection is specified. Manage tags in this collection.
echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
$hiddenfields = [
(object) ['type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid],
(object) ['type' => 'hidden', 'name' => 'perpage', 'value' => $perpage]
];
$otherfields = '';
if ($filter !== '') {
$otherfields = html_writer::link(new moodle_url($PAGE->url, ['filter' => null]),
get_string('resetfilter', 'tag'));
}
$data = [
'action' => new moodle_url('/tag/manage.php'),
'hiddenfields' => $hiddenfields,
'inputname' => 'filter',
'searchstring' => get_string('search'),
'query' => s($filter),
'extraclasses' => 'mb-2',
'otherfields' => $otherfields
];
echo $OUTPUT->render_from_template('core/search_input', $data);
// Link to add an standard tags.
$img = $OUTPUT->pix_icon('t/add', '');
echo '<div class="addstandardtags visibleifjs">' .
html_writer::link('#', $img . get_string('addotags', 'tag'), array('data-action' => 'addstandardtag')) .
'</div>';
$table = new core_tag_manage_table($tagcollid);
echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
echo $table->out($perpage, true);
if ($table->rawdata) {
echo html_writer::start_tag('p');
echo html_writer::tag('button', get_string('deleteselected', 'tag'),
array('id' => 'tag-management-delete', 'type' => 'submit',
'class' => 'tagdeleteselected btn btn-secondary', 'name' => 'bulkdelete'));
echo html_writer::tag('button', get_string('combineselected', 'tag'),
array('id' => 'tag-management-combine', 'type' => 'submit',
'class' => 'tagcombineselected btn btn-secondary', 'name' => 'bulkcombine'));
echo html_writer::end_tag('p');
}
echo '</form>';
$totalcount = $table->totalcount;
if ($perpage == SHOW_ALL_PAGE_SIZE) {
echo html_writer::start_tag('div', array('id' => 'showall'));
$params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
$url = new moodle_url($PAGE->url, $params);
echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
echo html_writer::end_tag('div');
} else if ($totalcount > 0 and $perpage < $totalcount) {
echo html_writer::start_tag('div', array('id' => 'showall'));
$params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
$url = new moodle_url($PAGE->url, $params);
echo html_writer::link($url, get_string('showall', '', $totalcount));
echo html_writer::end_tag('div');
}
echo $OUTPUT->footer();