2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Managing tags, tag areas and tags collections
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
);
44 if ($perpage != DEFAULT_PAGE_SIZE
) {
45 $params['perpage'] = $perpage;
48 $params['page'] = $page;
51 $params['tc'] = $tagcollid;
54 $params['filter'] = $filter;
57 admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
59 if (empty($CFG->usetags
)) {
60 print_error('tagsaredisabled', 'tag');
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');
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');
83 $name = required_param('name', PARAM_NOTAGS
);
84 $searchable = required_param('searchable', PARAM_BOOL
);
85 core_tag_collection
::create(array('name' => $name, 'searchable' => $searchable));
90 if ($tagcoll && !$tagcoll->component
) {
92 core_tag_collection
::delete($tagcoll);
93 \core\notification
::success(get_string('changessaved', 'core_tag'));
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);
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);
119 core_tag_tag
::delete_tags(array($tagid));
120 \core\notification
::success(get_string('deleted', 'core_tag'));
122 redirect($PAGE->url
);
126 if (optional_param('bulkdelete', null, PARAM_RAW
) !== null) {
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) {
137 if (($maintag = optional_param('maintag', 0, PARAM_INT
)) && array_key_exists($maintag, $tags)) {
138 $tag = $tags[$maintag];
140 $tag = array_shift($tags);
142 $tag->combine_tags($tags);
143 \core\notification
::success(get_string('combined', 'core_tag'));
145 redirect($PAGE->url
);
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
))) {
153 $tag = core_tag_tag
::get($tagid, '*', MUST_EXIST
);
154 $targettag = core_tag_tag
::get_by_name($tag->tagcollid
, $newname, '*');
156 $targettag->combine_tags(array($tag));
157 \core\notification
::success(get_string('combined', 'core_tag'));
159 $tag->update(array('rawname' => $newname));
160 \core\notification
::success(get_string('changessaved', 'core_tag'));
163 redirect($PAGE->url
);
166 case 'addstandardtag':
168 $tagobjects = array();
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
);
184 echo $OUTPUT->header();
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();
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"> '.
216 ($filter !== '' ? html_writer
::link(new moodle_url($PAGE->url
, array('filter' => null)),
217 get_string('resetfilter', 'tag'), array('class' => 'resetfilterlink')) : '').
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')) .
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', 'class' => 'tagdeleteselected', 'name' => 'bulkdelete'));
240 echo html_writer
::tag('button', get_string('combineselected', 'tag'),
241 array('id' => 'tag-management-combine', 'type' => 'submit', 'class' => 'tagcombineselected', 'name' => 'bulkcombine'));
242 echo html_writer
::end_tag('p');
246 $totalcount = $table->totalcount
;
247 if ($perpage == SHOW_ALL_PAGE_SIZE
) {
248 echo html_writer
::start_tag('div', array('id' => 'showall'));
249 $params = array('perpage' => DEFAULT_PAGE_SIZE
, 'page' => 0);
250 $url = new moodle_url($PAGE->url
, $params);
251 echo html_writer
::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE
));
252 echo html_writer
::end_tag('div');
253 } else if ($totalcount > 0 and $perpage < $totalcount) {
254 echo html_writer
::start_tag('div', array('id' => 'showall'));
255 $params = array('perpage' => SHOW_ALL_PAGE_SIZE
, 'page' => 0);
256 $url = new moodle_url($PAGE->url
, $params);
257 echo html_writer
::link($url, get_string('showall', '', $totalcount));
258 echo html_writer
::end_tag('div');
261 echo $OUTPUT->footer();