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