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