calendar/lib: calendar_set_filters() use pre-fetched context and course recs
[moodle-pu.git] / tag / manage.php
blobb8250775a7a0442be2ad936743aac9ac463d04c3
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->libdir.'/tablelib.php');
5 require_once('lib.php');
7 define('SHOW_ALL_PAGE_SIZE', 50000);
8 define('DEFAULT_PAGE_SIZE', 30);
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);
16 require_login();
18 if (empty($CFG->usetags)) {
19 error(get_string('tagsaredisabled', 'tag'));
22 //managing tags requires moodle/tag:manage capability
23 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
24 require_capability('moodle/tag:manage', $systemcontext);
26 $navlinks = array();
27 $navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
28 $navlinks[] = array('name' => get_string('managetags', 'tag'), 'link' => '', 'type' => '');
30 $navigation = build_navigation($navlinks);
31 print_header_simple(get_string('managetags', 'tag'), '', $navigation);
33 $err_notice = '';
34 $notice = '';
36 // get all the possible tag types from db
37 $existing_tagtypes = array();
38 if ($ptypes = get_records_sql("SELECT DISTINCT(tagtype), id FROM {$CFG->prefix}tag")) {
39 foreach ($ptypes as $ptype) {
40 $existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
43 $existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
44 $existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
46 switch($action) {
48 case 'delete':
49 if (!data_submitted or !confirm_sesskey()) {
50 break;
52 $str_tagschecked = tag_name_from_string(implode($tagschecked, ','));
53 $str_tagschecked = str_replace(',', ', ', $str_tagschecked);
55 tag_delete(implode($tagschecked, ','));
57 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
58 break;
60 case 'reset':
61 if (!data_submitted or !confirm_sesskey()) {
62 break;
64 $str_tagschecked = tag_name_from_string(implode($tagschecked, ','));
65 $str_tagschecked = str_replace(',', ', ', $str_tagschecked);
67 tag_flag_reset(implode($tagschecked, ','));
69 $notice = $str_tagschecked.' -- '.get_string('reset','tag');
70 break;
72 case 'changetype':
73 if (!data_submitted or !confirm_sesskey()) {
74 break;
77 $changed = array();
79 foreach ($tagschecked as $tag_id) {
80 if (!in_array($tagtypes[$tag_id], $existing_tagtypes)) {
81 //can not add new types here!!
82 continue;
85 // update tag type;
86 $tag = tag_by_id($tag_id);
87 $tag->tagtype = $tagtypes[$tag_id];
89 if (update_record('tag', $tag)) {
90 $changed[] = $tag_id;
94 if ($changed) {
95 $str_changed = tag_name_from_string(implode($changed, ','));
96 $str_changed = str_replace(',', ', ', $str_changed);
97 $notice = $str_changed.' -- '.get_string('typechanged','tag');
99 break;
101 case 'changename':
102 if (!data_submitted or !confirm_sesskey()) {
103 break;
106 $tags_names_changed = array();
108 foreach ($tagschecked as $tag_id) {
109 if ($newnames[$tag_id] != '') {
110 if (tag_exists($newnames[$tag_id])) {
111 $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
112 } else {
113 $tags_names_changed[$tag_id] = $newnames[$tag_id];
118 $tags_names_updated = tag_update_name($tags_names_changed);
120 //notice to inform what tags had their names effectively updated
121 if ($tags_names_updated){
122 $notice = implode($tags_names_updated, ', ');
123 $notice .= ' -- ' . get_string('updated','tag');
125 break;
128 echo '<br/>';
130 if ($err_notice) {
131 notify($err_notice, 'red');
133 if ($notice) {
134 notify($notice , 'green');
137 //setup table
139 $tablecolumns = array('id','name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
140 $tableheaders = array(get_string('id' , 'tag'),
141 get_string('name' , 'tag'),
142 get_string('owner','tag'),
143 get_string('count','tag'),
144 get_string('flag','tag'),
145 get_string('timemodified','tag'),
146 get_string('newname', 'tag'),
147 get_string('tagtype', 'tag'),
148 get_string('select', 'tag'));
150 $table = new flexible_table('tag-management-list-'.$USER->id);
152 $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
154 $table->define_columns($tablecolumns);
155 $table->define_headers($tableheaders);
156 $table->define_baseurl($baseurl);
158 $table->sortable(true, 'flag', SORT_DESC);
160 $table->set_attribute('cellspacing', '0');
161 $table->set_attribute('id', 'tag-management-list');
162 $table->set_attribute('class', 'generaltable generalbox');
164 $table->set_control_variables(array(
165 TABLE_VAR_SORT => 'ssort',
166 TABLE_VAR_HIDE => 'shide',
167 TABLE_VAR_SHOW => 'sshow',
168 TABLE_VAR_IFIRST => 'sifirst',
169 TABLE_VAR_ILAST => 'silast',
170 TABLE_VAR_PAGE => 'spage'
173 $table->setup();
175 if ($table->get_sql_sort()) {
176 $sort = ' ORDER BY '.$table->get_sql_sort();
177 } else {
178 $sort = '';
181 if ($table->get_sql_where()) {
182 $where = 'WHERE '.$table->get_sql_where();
183 } else {
184 $where = '';
187 $query = "
188 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
189 FROM {$CFG->prefix}tag_instance ti
190 RIGHT JOIN {$CFG->prefix}tag tg ON tg.id = ti.tagid
191 LEFT JOIN {$CFG->prefix}user u ON tg.userid = u.id
192 {$where}
193 GROUP BY tg.id
194 {$sort}";
197 $totalcount = count_records_sql("SELECT COUNT(DISTINCT(tg.id))
198 FROM {$CFG->prefix}tag tg
199 LEFT JOIN {$CFG->prefix}user u ON u.id = tg.userid
200 $where");
202 $table->initialbars(true); // always initial bars
203 $table->pagesize($perpage, $totalcount);
205 echo '<form id="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
207 //retrieve tags from DB
208 if ($tagrecords = get_records_sql($query, $table->get_page_start(), $table->get_page_size())) {
210 $taglist = array_values($tagrecords);
212 //print_tag_cloud(array_values(get_records_sql($query)), false);
214 //populate table with data
215 foreach ($taglist as $tag ){
217 $id = $tag->id;
218 $name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
219 $owner = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . fullname($tag) . '</a>';
220 $count = $tag->count;
221 $flag = $tag->flag;
222 $timemodified = format_time(time() - $tag->timemodified);
223 $checkbox = '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
224 $text = '<input type="text" name="newname['.$tag->id.']" />';
226 $tagtype = choose_from_menu($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, '', '', '0', true);
228 //if the tag if flagged, highlight it
229 if ($tag->flag > 0) {
230 $id = '<span class="flagged-tag">' . $id . '</span>';
231 $name = '<span class="flagged-tag">' . $name . '</span>';
232 $owner = '<span class="flagged-tag">' . $owner . '</span>';
233 $count = '<span class="flagged-tag">' . $count . '</span>';
234 $flag = '<span class="flagged-tag">' . $flag . '</span>';
235 $timemodified = '<span class="flagged-tag">' . $timemodified . '</span>';
236 $tagtype = '<span class="flagged-tag">'. $tagtype. '</span>';
239 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
241 $table->add_data($data);
245 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
246 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
247 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" /> ';
248 echo '<br/><br/>';
249 echo '<select id="menuformaction" name="action">
250 <option value="" selected="selected">'. get_string('withselectedtags', 'tag') .'</option>
251 <option value="reset">'. get_string('resetflag', 'tag') .'</option>
252 <option value="delete">'. get_string('delete', 'tag') .'</option>
253 <option value="changetype">'. get_string('changetype', 'tag') .'</option>
254 <option value="changename">'. get_string('changename', 'tag') .'</option>
255 </select>';
257 echo '<button id="tag-management-submit" type="submit">'. get_string('ok') .'</button>';
261 $table->print_html();
263 echo '</div></form>';
265 if ($perpage == SHOW_ALL_PAGE_SIZE) {
266 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>';
268 } else if ($totalcount > 0 and $perpage < $totalcount) {
269 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $totalcount).'</a></div>';
272 echo '<br/>';
274 print_footer();