Updated the 19 build version to 20100906
[moodle.git] / tag / manage.php
blob4c1e3ea097e659bd20a8efe47cda6bbb00f76490
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 print_error('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) 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;
53 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
54 tag_delete($tagschecked);
55 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
56 break;
58 case 'reset':
59 if (!data_submitted() or !confirm_sesskey()) {
60 break;
62 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
63 tag_unset_flag($tagschecked);
64 $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
65 break;
67 case 'changetype':
68 if (!data_submitted() or !confirm_sesskey()) {
69 break;
72 $changed = array();
73 foreach ($tagschecked as $tag_id) {
74 if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
75 //can not add new types here!!
76 continue;
79 // update tag type;
80 if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
81 $changed[] = $tag_id;
85 if (!empty($changed)) {
86 $str_changed = implode(', ', tag_get_name($changed));
87 $notice = $str_changed .' -- '. get_string('typechanged','tag');
89 break;
91 case 'changename':
92 if (!data_submitted() or !confirm_sesskey()) {
93 break;
96 $tags_names_changed = array();
97 foreach ($tagschecked as $tag_id) {
98 if ($newnames[$tag_id] != '') {
99 if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
100 // if tag already exists, or is not a valid tag name, etc.
101 $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
102 } else {
103 $tags_names_changed[$tag_id] = $newnames[$tag_id];
108 //notice to inform what tags had their names effectively updated
109 if ($tags_names_changed){
110 $notice = implode($tags_names_changed, ', ');
111 $notice .= ' -- ' . get_string('updated','tag');
113 break;
114 case 'addofficialtag':
115 if (!data_submitted() or !confirm_sesskey()) {
116 break;
119 $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
120 $notice = '';
121 foreach ( $new_otags as $new_otag ) {
122 if ( $new_otag_id = tag_get_id($new_otag) ) {
123 // tag exists, change the type
124 tag_type_set($new_otag_id, 'official');
125 } else {
126 tag_add($new_otag, 'official');
128 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
130 break;
133 echo '<br/>';
135 if ($err_notice) {
136 notify($err_notice, 'red');
138 if ($notice) {
139 notify($notice, 'green');
142 // small form to add an official tag
143 print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
144 print('<input type="hidden" name="action" value="addofficialtag">');
145 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
146 '<input name="otagsadd" id="id_otagsadd" type="text">'.
147 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
148 '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit">'.
149 '</div>');
150 print('</form>');
152 //setup table
154 $tablecolumns = array('id', 'name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
155 $tableheaders = array(get_string('id', 'tag'),
156 get_string('name', 'tag'),
157 get_string('owner', 'tag'),
158 get_string('count', 'tag'),
159 get_string('flag', 'tag'),
160 get_string('timemodified', 'tag'),
161 get_string('newname', 'tag'),
162 get_string('tagtype', 'tag'),
163 get_string('select', 'tag'));
165 $table = new flexible_table('tag-management-list-'.$USER->id);
167 $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
169 $table->define_columns($tablecolumns);
170 $table->define_headers($tableheaders);
171 $table->define_baseurl($baseurl);
173 $table->sortable(true, 'flag', SORT_DESC);
175 $table->set_attribute('cellspacing', '0');
176 $table->set_attribute('id', 'tag-management-list');
177 $table->set_attribute('class', 'generaltable generalbox');
179 $table->set_control_variables(array(
180 TABLE_VAR_SORT => 'ssort',
181 TABLE_VAR_HIDE => 'shide',
182 TABLE_VAR_SHOW => 'sshow',
183 TABLE_VAR_IFIRST => 'sifirst',
184 TABLE_VAR_ILAST => 'silast',
185 TABLE_VAR_PAGE => 'spage'
188 $table->setup();
190 if ($table->get_sql_sort()) {
191 $sort = 'ORDER BY '. $table->get_sql_sort();
192 } else {
193 $sort = '';
196 if ($table->get_sql_where()) {
197 $where = 'WHERE '. $table->get_sql_where();
198 } else {
199 $where = '';
202 $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 '.
203 'FROM '. $CFG->prefix .'tag_instance ti RIGHT JOIN '. $CFG->prefix .'tag tg ON tg.id = ti.tagid LEFT JOIN '. $CFG->prefix .'user u ON tg.userid = u.id '.
204 $where .' '.
205 'GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, u.id, tg.flag, tg.timemodified, u.firstname, u.lastname '.
206 $sort;
208 $totalcount = count_records_sql('SELECT COUNT(DISTINCT(tg.id)) FROM '. $CFG->prefix .'tag tg LEFT JOIN '. $CFG->prefix .'user u ON u.id = tg.userid '. $where);
210 $table->initialbars(true); // always initial bars
211 $table->pagesize($perpage, $totalcount);
213 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
215 //retrieve tags from DB
216 if ($tagrecords = get_records_sql($query, $table->get_page_start(), $table->get_page_size())) {
218 $taglist = array_values($tagrecords);
220 //print_tag_cloud(array_values(get_records_sql($query)), false);
221 //populate table with data
222 foreach ($taglist as $tag ){
223 $id = $tag->id;
224 $name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
225 $owner = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . fullname($tag) . '</a>';
226 $count = $tag->count;
227 $flag = $tag->flag;
228 $timemodified = format_time(time() - $tag->timemodified);
229 $checkbox = '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
230 $text = '<input type="text" name="newname['.$tag->id.']" />';
231 $tagtype = choose_from_menu($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, '', '', '0', true);
233 //if the tag if flagged, highlight it
234 if ($tag->flag > 0) {
235 $id = '<span class="flagged-tag">' . $id . '</span>';
236 $name = '<span class="flagged-tag">' . $name . '</span>';
237 $owner = '<span class="flagged-tag">' . $owner . '</span>';
238 $count = '<span class="flagged-tag">' . $count . '</span>';
239 $flag = '<span class="flagged-tag">' . $flag . '</span>';
240 $timemodified = '<span class="flagged-tag">' . $timemodified . '</span>';
241 $tagtype = '<span class="flagged-tag">'. $tagtype. '</span>';
244 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
246 $table->add_data($data);
249 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
250 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
251 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" /> ';
252 echo '<br/><br/>';
253 echo '<select id="menuformaction" name="action">
254 <option value="" selected="selected">'. get_string('withselectedtags', 'tag') .'</option>
255 <option value="reset">'. get_string('resetflag', 'tag') .'</option>
256 <option value="delete">'. get_string('delete', 'tag') .'</option>
257 <option value="changetype">'. get_string('changetype', 'tag') .'</option>
258 <option value="changename">'. get_string('changename', 'tag') .'</option>
259 </select>';
261 echo '<button id="tag-management-submit" type="submit">'. get_string('ok') .'</button>';
264 $table->print_html();
265 echo '</div></form>';
267 if ($perpage == SHOW_ALL_PAGE_SIZE) {
268 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. DEFAULT_PAGE_SIZE .'">'. get_string('showperpage', '', DEFAULT_PAGE_SIZE) .'</a></div>';
270 } else if ($totalcount > 0 and $perpage < $totalcount) {
271 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. SHOW_ALL_PAGE_SIZE .'">'. get_string('showall', '', $totalcount) .'</a></div>';
274 echo '<br/>';
276 print_footer();