MDL-50919 behat: Step for keypress event in behat
[moodle.git] / tag / manage.php
CommitLineData
6bfe7aac 1<?php
c933a060 2
000ebd5c
DM
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.
14//
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/>.
17
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
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
PS
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);
70e9b59d 36$action = optional_param('action', '', PARAM_ALPHA);
37$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
c933a060 38
39require_login();
40
70e9b59d 41if (empty($CFG->usetags)) {
f7ed2687 42 print_error('tagsaredisabled', 'tag');
c933a060 43}
44
0edd6535 45$params = array();
46if ($perpage != DEFAULT_PAGE_SIZE) {
47 $params['perpage'] = $perpage;
48}
823c593f
RT
49admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'standard'));
50
0edd6535 51$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
823c593f 52
968583af 53echo $OUTPUT->header();
c933a060 54
2d7e3f9b 55$err_notice = '';
70e9b59d 56$notice = '';
57
58// get all the possible tag types from db
59$existing_tagtypes = array();
f7ed2687 60if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
70e9b59d 61 foreach ($ptypes as $ptype) {
62 $existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
63 }
64}
65$existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
66$existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
2d7e3f9b 67
c933a060 68switch($action) {
3a597ace 69
c933a060 70 case 'delete':
292ad3bf 71 if (!data_submitted() or !confirm_sesskey()) {
70e9b59d 72 break;
73 }
6bfe7aac 74
3d535996 75 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
76 tag_delete($tagschecked);
70e9b59d 77 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
c933a060 78 break;
70e9b59d 79
3a597ace 80 case 'reset':
292ad3bf 81 if (!data_submitted() or !confirm_sesskey()) {
70e9b59d 82 break;
83 }
3d535996 84 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
85 tag_unset_flag($tagschecked);
86 $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
3a597ace 87 break;
70e9b59d 88
79081e87 89 case 'changetype':
292ad3bf 90 if (!data_submitted() or !confirm_sesskey()) {
70e9b59d 91 break;
92 }
93
94 $changed = array();
70e9b59d 95 foreach ($tagschecked as $tag_id) {
990027e1 96 if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
70e9b59d 97 //can not add new types here!!
98 continue;
99 }
100
79081e87 101 // update tag type;
3d535996 102 if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
70e9b59d 103 $changed[] = $tag_id;
79081e87 104 }
105 }
70e9b59d 106
3d535996 107 if (!empty($changed)) {
108 $str_changed = implode(', ', tag_get_name($changed));
109 $notice = $str_changed .' -- '. get_string('typechanged','tag');
70e9b59d 110 }
79081e87 111 break;
70e9b59d 112
3a597ace 113 case 'changename':
292ad3bf 114 if (!data_submitted() or !confirm_sesskey()) {
70e9b59d 115 break;
3a597ace 116 }
6bfe7aac 117
3a597ace 118 $tags_names_changed = array();
70e9b59d 119 foreach ($tagschecked as $tag_id) {
120 if ($newnames[$tag_id] != '') {
3d535996 121 if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
122 // if tag already exists, or is not a valid tag name, etc.
70e9b59d 123 $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
124 } else {
125 $tags_names_changed[$tag_id] = $newnames[$tag_id];
126 }
127 }
3a597ace 128 }
129
3a597ace 130 //notice to inform what tags had their names effectively updated
3d535996 131 if ($tags_names_changed){
132 $notice = implode($tags_names_changed, ', ');
3a597ace 133 $notice .= ' -- ' . get_string('updated','tag');
134 }
c933a060 135 break;
3d535996 136 case 'addofficialtag':
e35139b7
PS
137 if (!data_submitted() or !confirm_sesskey()) {
138 break;
139 }
140
3d535996 141 $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
142 $notice = '';
143 foreach ( $new_otags as $new_otag ) {
144 if ( $new_otag_id = tag_get_id($new_otag) ) {
145 // tag exists, change the type
2ce9a1f5 146 tag_type_set($new_otag_id, 'official');
3d535996 147 } else {
43731030 148 require_capability('moodle/tag:create', context_system::instance());
3d535996 149 tag_add($new_otag, 'official');
150 }
151 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
152 }
153 break;
c933a060 154}
155
2d7e3f9b 156if ($err_notice) {
aba6e0bf 157 echo $OUTPUT->notification($err_notice, 'notifyproblem');
2d7e3f9b 158}
159if ($notice) {
aba6e0bf 160 echo $OUTPUT->notification($notice, 'notifysuccess');
2d7e3f9b 161}
3a597ace 162
3d535996 163// small form to add an official tag
164print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
ec6a0e70 165print('<input type="hidden" name="action" value="addofficialtag" />');
3d535996 166print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
ec6a0e70 167 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
e35139b7 168 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
ec6a0e70 169 '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
3d535996 170 '</div>');
171print('</form>');
172
70e9b59d 173//setup table
174
3d535996 175$tablecolumns = array('id', 'name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
176$tableheaders = array(get_string('id', 'tag'),
177 get_string('name', 'tag'),
178 get_string('owner', 'tag'),
179 get_string('count', 'tag'),
180 get_string('flag', 'tag'),
181 get_string('timemodified', 'tag'),
70e9b59d 182 get_string('newname', 'tag'),
183 get_string('tagtype', 'tag'),
184 get_string('select', 'tag'));
185
186$table = new flexible_table('tag-management-list-'.$USER->id);
187
188$baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
189
190$table->define_columns($tablecolumns);
191$table->define_headers($tableheaders);
192$table->define_baseurl($baseurl);
193
194$table->sortable(true, 'flag', SORT_DESC);
195
196$table->set_attribute('cellspacing', '0');
197$table->set_attribute('id', 'tag-management-list');
8be244b2 198$table->set_attribute('class', 'admintable generaltable');
70e9b59d 199
200$table->set_control_variables(array(
201TABLE_VAR_SORT => 'ssort',
202TABLE_VAR_HIDE => 'shide',
203TABLE_VAR_SHOW => 'sshow',
204TABLE_VAR_IFIRST => 'sifirst',
205TABLE_VAR_ILAST => 'silast',
206TABLE_VAR_PAGE => 'spage'
207));
208
209$table->setup();
210
211if ($table->get_sql_sort()) {
3d535996 212 $sort = 'ORDER BY '. $table->get_sql_sort();
70e9b59d 213} else {
214 $sort = '';
215}
216
0f21a964
PS
217list($where, $params) = $table->get_sql_where();
218if ($where) {
219 $where = 'WHERE '. $where;
70e9b59d 220}
221
506c8d59 222$allusernames = get_all_user_name_fields(true, 'u');
64d05f13
DM
223$query = "
224 SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
506c8d59 225 u.id AS owner, $allusernames,
64d05f13
DM
226 COUNT(ti.id) AS count
227 FROM {tag} tg
228 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
229 LEFT JOIN {user} u ON u.id = tg.userid
230 $where
231 GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
0ee4ccb6 232 u.id, $allusernames
64d05f13
DM
233 $sort";
234
235$totalcount = $DB->count_records_sql("
236 SELECT COUNT(DISTINCT(tg.id))
237 FROM {tag} tg
238 LEFT JOIN {user} u ON u.id = tg.userid
239 $where", $params);
70e9b59d 240
241$table->initialbars(true); // always initial bars
242$table->pagesize($perpage, $totalcount);
243
ca2d7327 244//@todo MDL-35474 convert to mform
3d535996 245echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
70e9b59d 246
247//retrieve tags from DB
f7ed2687 248if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {
70e9b59d 249
70e9b59d 250 //populate table with data
64d05f13 251 foreach ($tagrecords as $tag) {
ca2d7327
RW
252 $id = $tag->id;
253 $params = array('id' => $tag->id);
254 $taglink = new moodle_url($CFG->wwwroot . '/tag/index.php', $params);
255 $name = html_writer::link($taglink, tag_display_name($tag));
256 $params = array('id' => $tag->owner);
257 $ownerlink = new moodle_url($CFG->wwwroot . '/user/view.php', $params);
258 $owner = html_writer::link($ownerlink, fullname($tag));
259 $count = $tag->count;
260 $flag = $tag->flag;
261 $timemodified = format_time(time() - $tag->timemodified);
262 $checkbox = html_writer::tag('input', '', array('type' => 'checkbox', 'name' => 'tagschecked[]', 'value' => $tag->id));
04a530c9 263 $attrs = array('type' => 'text', 'id' => 'newname_' . $tag->id, 'name' => 'newname['.$tag->id.']');
ca2d7327
RW
264 $text = html_writer::label(get_string('newname', 'tag'), 'newname_' . $tag->id, false, array('class' => 'accesshide'));
265 $text .= html_writer::empty_tag('input', $attrs);
266 $tagtype = html_writer::label(get_string('tagtype', 'tag'), 'menutagtypes'. $tag->id, false, array('class' => 'accesshide'));
267 $tagtype .= html_writer::select($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false, array('id' => 'menutagtypes'. $tag->id));
70e9b59d 268
269 //if the tag if flagged, highlight it
270 if ($tag->flag > 0) {
ca2d7327
RW
271 $id = html_writer::tag('span', $id, array('class' => 'flagged-tag'));
272 $name = html_writer::tag('span', $name, array('class' => 'flagged-tag'));
273 $owner = html_writer::tag('span', $owner, array('class' => 'flagged-tag'));
274 $count = html_writer::tag('span', $count, array('class' => 'flagged-tag'));
275 $flag = html_writer::tag('span', $flag, array('class' => 'flagged-tag'));
276 $timemodified = html_writer::tag('span', $timemodified, array('class' => 'flagged-tag'));
277 $tagtype = html_writer::tag('span', $tagtype, array('class' => 'flagged-tag'));
70e9b59d 278 }
279
280 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
281
282 $table->add_data($data);
283 }
284
ca2d7327
RW
285 echo html_writer::empty_tag('input', array('type' => 'button', 'onclick' => 'checkall()', 'value' => get_string('selectall')));
286 echo html_writer::empty_tag('input', array('type' => 'button', 'onclick' => 'checknone()', 'value' => get_string('deselectall')));
287 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
288 echo html_writer::empty_tag('br');
289 echo html_writer::empty_tag('br');
290
bbfae1eb 291 echo html_writer::label(get_string('withselectedtags', 'tag'), 'menuformaction', false, array('class' => 'accesshide'));
ca2d7327
RW
292 $options = array('' => get_string('withselectedtags', 'tag'),
293 'reset' => get_string('resetflag', 'tag'),
294 'delete' => get_string('delete', 'tag'),
295 'changetype' => get_string('changetype', 'tag'),
296 'changename' => get_string('changename', 'tag'));
297 echo html_writer::select($options, 'action', '', array(), array('id' => 'menuformaction'));
298
299 echo html_writer::tag('button', get_string('ok'), array('id' => 'tag-management-submit', 'type' => 'submit'));
70e9b59d 300}
301
302$table->print_html();
ca2d7327
RW
303
304//@todo MDL-35474 convert to mform
70e9b59d 305echo '</div></form>';
c933a060 306
70e9b59d 307if ($perpage == SHOW_ALL_PAGE_SIZE) {
ca2d7327
RW
308 echo html_writer::start_tag('div', array('id' => 'showall'));
309 $params = array('perpage' => DEFAULT_PAGE_SIZE);
310 $url = new moodle_url($baseurl, $params);
311 echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
312 echo html_writer::end_tag('div');
70e9b59d 313
314} else if ($totalcount > 0 and $perpage < $totalcount) {
ca2d7327
RW
315 echo html_writer::start_tag('div', array('id' => 'showall'));
316 $params = array('perpage' => SHOW_ALL_PAGE_SIZE);
317 $url = new moodle_url($baseurl, $params);
318 echo html_writer::link($url, get_string('showall', '', $totalcount));
319 echo html_writer::end_tag('div');
70e9b59d 320}
e3ff328b 321
ca2d7327 322echo html_writer::empty_tag('br');
c933a060 323
1dd2ba54 324echo $OUTPUT->footer();