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);
43 if ($perpage != DEFAULT_PAGE_SIZE) {
44 $params['perpage'] = $perpage;
47 $params['page'] = $page;
50 admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
52 if (empty($CFG->usetags)) {
53 print_error('tagsaredisabled', 'tag');
58 $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
59 $tagcollid = $tagobject->tagcollid;
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');
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)));
71 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
77 $name = required_param('name', PARAM_NOTAGS);
78 $searchable = required_param('searchable', PARAM_BOOL);
79 core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
84 if ($tagcoll && !$tagcoll->component) {
86 core_tag_collection::delete($tagcoll);
87 \core\notification::success(get_string('changessaved', 'core_tag'));
95 core_tag_collection::change_sortorder($tagcoll, -1);
96 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
104 core_tag_collection::change_sortorder($tagcoll, 1);
105 redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
107 redirect($manageurl);
112 if (!$tagschecked && $tagid) {
113 $tagschecked = array($tagid);
115 core_tag_tag::delete_tags($tagschecked);
117 redirect($PAGE->url, get_string('deleted', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
119 redirect($PAGE->url);
123 case 'addstandardtag':
125 $tagobjects = array();
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);
131 foreach ($tagobjects as $tagobject) {
132 if (!$tagobject->isstandard) {
133 $tagobject->update(array('isstandard' => 1));
136 redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
137 null, \core\output\notification::NOTIFY_SUCCESS);
141 echo $OUTPUT->header();
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);
148 echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
149 echo html_writer::table($colltable);
150 $url = new moodle_url($manageurl, array('action' => 'colladd'));
151 echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
152 'mdl-right addtagcoll');
154 echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
155 echo html_writer::table($tagareastable);
157 $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
159 echo $OUTPUT->footer();
163 // Tag collection is specified. Manage tags in this collection.
164 echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
166 // Small form to add an standard tag.
167 print('<form class="tag-addtags-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
168 print('<input type="hidden" name="tc" value="'.$tagcollid.'" />');
169 print('<input type="hidden" name="action" value="addstandardtag" />');
170 print('<input type="hidden" name="perpage" value="'.$perpage.'" />');
171 print('<input type="hidden" name="page" value="'.$page.'" />');
172 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">' .
173 get_string('addotags', 'tag') .'</label>'.
174 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
175 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
176 '<input name="addotags" value="'. get_string('addotags', 'tag') .
177 '" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
181 $table = new core_tag_manage_table($tagcollid);
182 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
183 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
184 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
185 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'delete'));
186 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
187 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
188 echo $table->out($perpage, true);
190 if ($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');
198 $totalcount = $table->totalcount;
199 if ($perpage == SHOW_ALL_PAGE_SIZE) {
200 echo html_writer::start_tag('div', array('id' => 'showall'));
201 $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
202 $url = new moodle_url($PAGE->url, $params);
203 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
204 echo html_writer::end_tag('div');
205 } else if ($totalcount > 0 and $perpage < $totalcount) {
206 echo html_writer::start_tag('div', array('id' => 'showall'));
207 $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
208 $url = new moodle_url($PAGE->url, $params);
209 echo html_writer::link($url, get_string('showall', '', $totalcount));
210 echo html_writer::end_tag('div');
213 echo $OUTPUT->footer();