Merge branch '48304-28' of git://github.com/samhemelryk/moodle into MOODLE_28_STABLE
[moodle.git] / tag / manage.php
blob8f2636997e81deaf5cd160328048d448ac1eb88e
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * @package core
20 * @subpackage tag
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 $newnames = optional_param_array('newname', array(), PARAM_TAG);
35 $tagtypes = optional_param_array('tagtypes', array(), PARAM_ALPHA);
36 $action = optional_param('action', '', PARAM_ALPHA);
37 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
39 require_login();
41 if (empty($CFG->usetags)) {
42 print_error('tagsaredisabled', 'tag');
45 $params = array();
46 if ($perpage != DEFAULT_PAGE_SIZE) {
47 $params['perpage'] = $perpage;
49 admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'standard'));
51 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
52 $PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'));
53 $PAGE->navbar->add(get_string('managetags', 'tag'));
55 echo $OUTPUT->header();
57 $err_notice = '';
58 $notice = '';
60 // get all the possible tag types from db
61 $existing_tagtypes = array();
62 if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
63 foreach ($ptypes as $ptype) {
64 $existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
67 $existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
68 $existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
70 switch($action) {
72 case 'delete':
73 if (!data_submitted() or !confirm_sesskey()) {
74 break;
77 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
78 tag_delete($tagschecked);
79 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
80 break;
82 case 'reset':
83 if (!data_submitted() or !confirm_sesskey()) {
84 break;
86 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
87 tag_unset_flag($tagschecked);
88 $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
89 break;
91 case 'changetype':
92 if (!data_submitted() or !confirm_sesskey()) {
93 break;
96 $changed = array();
97 foreach ($tagschecked as $tag_id) {
98 if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
99 //can not add new types here!!
100 continue;
103 // update tag type;
104 if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
105 $changed[] = $tag_id;
109 if (!empty($changed)) {
110 $str_changed = implode(', ', tag_get_name($changed));
111 $notice = $str_changed .' -- '. get_string('typechanged','tag');
113 break;
115 case 'changename':
116 if (!data_submitted() or !confirm_sesskey()) {
117 break;
120 $tags_names_changed = array();
121 foreach ($tagschecked as $tag_id) {
122 if ($newnames[$tag_id] != '') {
123 if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
124 // if tag already exists, or is not a valid tag name, etc.
125 $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
126 } else {
127 $tags_names_changed[$tag_id] = $newnames[$tag_id];
132 //notice to inform what tags had their names effectively updated
133 if ($tags_names_changed){
134 $notice = implode($tags_names_changed, ', ');
135 $notice .= ' -- ' . get_string('updated','tag');
137 break;
138 case 'addofficialtag':
139 if (!data_submitted() or !confirm_sesskey()) {
140 break;
143 $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
144 $notice = '';
145 foreach ( $new_otags as $new_otag ) {
146 if ( $new_otag_id = tag_get_id($new_otag) ) {
147 // tag exists, change the type
148 tag_type_set($new_otag_id, 'official');
149 } else {
150 require_capability('moodle/tag:create', context_system::instance());
151 tag_add($new_otag, 'official');
153 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
155 break;
158 if ($err_notice) {
159 echo $OUTPUT->notification($err_notice, 'red');
161 if ($notice) {
162 echo $OUTPUT->notification($notice, 'green');
165 // small form to add an official tag
166 print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
167 print('<input type="hidden" name="action" value="addofficialtag" />');
168 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
169 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
170 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
171 '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
172 '</div>');
173 print('</form>');
175 //setup table
177 $tablecolumns = array('id', 'name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
178 $tableheaders = array(get_string('id', 'tag'),
179 get_string('name', 'tag'),
180 get_string('owner', 'tag'),
181 get_string('count', 'tag'),
182 get_string('flag', 'tag'),
183 get_string('timemodified', 'tag'),
184 get_string('newname', 'tag'),
185 get_string('tagtype', 'tag'),
186 get_string('select', 'tag'));
188 $table = new flexible_table('tag-management-list-'.$USER->id);
190 $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
192 $table->define_columns($tablecolumns);
193 $table->define_headers($tableheaders);
194 $table->define_baseurl($baseurl);
196 $table->sortable(true, 'flag', SORT_DESC);
198 $table->set_attribute('cellspacing', '0');
199 $table->set_attribute('id', 'tag-management-list');
200 $table->set_attribute('class', 'admintable generaltable');
202 $table->set_control_variables(array(
203 TABLE_VAR_SORT => 'ssort',
204 TABLE_VAR_HIDE => 'shide',
205 TABLE_VAR_SHOW => 'sshow',
206 TABLE_VAR_IFIRST => 'sifirst',
207 TABLE_VAR_ILAST => 'silast',
208 TABLE_VAR_PAGE => 'spage'
211 $table->setup();
213 if ($table->get_sql_sort()) {
214 $sort = 'ORDER BY '. $table->get_sql_sort();
215 } else {
216 $sort = '';
219 list($where, $params) = $table->get_sql_where();
220 if ($where) {
221 $where = 'WHERE '. $where;
224 $allusernames = get_all_user_name_fields(true, 'u');
225 $query = "
226 SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
227 u.id AS owner, $allusernames,
228 COUNT(ti.id) AS count
229 FROM {tag} tg
230 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
231 LEFT JOIN {user} u ON u.id = tg.userid
232 $where
233 GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
234 u.id, $allusernames
235 $sort";
237 $totalcount = $DB->count_records_sql("
238 SELECT COUNT(DISTINCT(tg.id))
239 FROM {tag} tg
240 LEFT JOIN {user} u ON u.id = tg.userid
241 $where", $params);
243 $table->initialbars(true); // always initial bars
244 $table->pagesize($perpage, $totalcount);
246 //@todo MDL-35474 convert to mform
247 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
249 //retrieve tags from DB
250 if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {
252 //populate table with data
253 foreach ($tagrecords as $tag) {
254 $id = $tag->id;
255 $params = array('id' => $tag->id);
256 $taglink = new moodle_url($CFG->wwwroot . '/tag/index.php', $params);
257 $name = html_writer::link($taglink, tag_display_name($tag));
258 $params = array('id' => $tag->owner);
259 $ownerlink = new moodle_url($CFG->wwwroot . '/user/view.php', $params);
260 $owner = html_writer::link($ownerlink, fullname($tag));
261 $count = $tag->count;
262 $flag = $tag->flag;
263 $timemodified = format_time(time() - $tag->timemodified);
264 $checkbox = html_writer::tag('input', '', array('type' => 'checkbox', 'name' => 'tagschecked[]', 'value' => $tag->id));
265 $attrs = array('type' => 'text', 'id' => 'newname_' . $tag->id, 'name' => 'newname['.$tag->id.']');
266 $text = html_writer::label(get_string('newname', 'tag'), 'newname_' . $tag->id, false, array('class' => 'accesshide'));
267 $text .= html_writer::empty_tag('input', $attrs);
268 $tagtype = html_writer::label(get_string('tagtype', 'tag'), 'menutagtypes'. $tag->id, false, array('class' => 'accesshide'));
269 $tagtype .= html_writer::select($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false, array('id' => 'menutagtypes'. $tag->id));
271 //if the tag if flagged, highlight it
272 if ($tag->flag > 0) {
273 $id = html_writer::tag('span', $id, array('class' => 'flagged-tag'));
274 $name = html_writer::tag('span', $name, array('class' => 'flagged-tag'));
275 $owner = html_writer::tag('span', $owner, array('class' => 'flagged-tag'));
276 $count = html_writer::tag('span', $count, array('class' => 'flagged-tag'));
277 $flag = html_writer::tag('span', $flag, array('class' => 'flagged-tag'));
278 $timemodified = html_writer::tag('span', $timemodified, array('class' => 'flagged-tag'));
279 $tagtype = html_writer::tag('span', $tagtype, array('class' => 'flagged-tag'));
282 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
284 $table->add_data($data);
287 echo html_writer::empty_tag('input', array('type' => 'button', 'onclick' => 'checkall()', 'value' => get_string('selectall')));
288 echo html_writer::empty_tag('input', array('type' => 'button', 'onclick' => 'checknone()', 'value' => get_string('deselectall')));
289 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
290 echo html_writer::empty_tag('br');
291 echo html_writer::empty_tag('br');
293 echo html_writer::label(get_string('withselectedtags', 'tag'), 'menuformaction', false, array('class' => 'accesshide'));
294 $options = array('' => get_string('withselectedtags', 'tag'),
295 'reset' => get_string('resetflag', 'tag'),
296 'delete' => get_string('delete', 'tag'),
297 'changetype' => get_string('changetype', 'tag'),
298 'changename' => get_string('changename', 'tag'));
299 echo html_writer::select($options, 'action', '', array(), array('id' => 'menuformaction'));
301 echo html_writer::tag('button', get_string('ok'), array('id' => 'tag-management-submit', 'type' => 'submit'));
304 $table->print_html();
306 //@todo MDL-35474 convert to mform
307 echo '</div></form>';
309 if ($perpage == SHOW_ALL_PAGE_SIZE) {
310 echo html_writer::start_tag('div', array('id' => 'showall'));
311 $params = array('perpage' => DEFAULT_PAGE_SIZE);
312 $url = new moodle_url($baseurl, $params);
313 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
314 echo html_writer::end_tag('div');
316 } else if ($totalcount > 0 and $perpage < $totalcount) {
317 echo html_writer::start_tag('div', array('id' => 'showall'));
318 $params = array('perpage' => SHOW_ALL_PAGE_SIZE);
319 $url = new moodle_url($baseurl, $params);
320 echo html_writer::link($url, get_string('showall', '', $totalcount));
321 echo html_writer::end_tag('div');
324 echo html_writer::empty_tag('br');
326 echo $OUTPUT->footer();