Updated the 19 build version to 20080928
[moodle.git] / tag / manage.php
blob3fe1fd4a37654bc6ed27562bc6c8eeebaf0ec660
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 $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
116 $notice = '';
117 foreach ( $new_otags as $new_otag ) {
118 if ( $new_otag_id = tag_get_id($new_otag) ) {
119 // tag exists, change the type
120 tag_type_set($new_otag_id, 'official');
121 } else {
122 tag_add($new_otag, 'official');
124 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
126 break;
129 echo '<br/>';
131 if ($err_notice) {
132 notify($err_notice, 'red');
134 if ($notice) {
135 notify($notice, 'green');
138 // small form to add an official tag
139 print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
140 print('<input type="hidden" name="action" value="addofficialtag">');
141 print('<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>');
145 print('</form>');
147 //setup table
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'),
156 get_string('newname', 'tag'),
157 get_string('tagtype', 'tag'),
158 get_string('select', 'tag'));
160 $table = new flexible_table('tag-management-list-'.$USER->id);
162 $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
164 $table->define_columns($tablecolumns);
165 $table->define_headers($tableheaders);
166 $table->define_baseurl($baseurl);
168 $table->sortable(true, 'flag', SORT_DESC);
170 $table->set_attribute('cellspacing', '0');
171 $table->set_attribute('id', 'tag-management-list');
172 $table->set_attribute('class', 'generaltable generalbox');
174 $table->set_control_variables(array(
175 TABLE_VAR_SORT => 'ssort',
176 TABLE_VAR_HIDE => 'shide',
177 TABLE_VAR_SHOW => 'sshow',
178 TABLE_VAR_IFIRST => 'sifirst',
179 TABLE_VAR_ILAST => 'silast',
180 TABLE_VAR_PAGE => 'spage'
183 $table->setup();
185 if ($table->get_sql_sort()) {
186 $sort = 'ORDER BY '. $table->get_sql_sort();
187 } else {
188 $sort = '';
191 if ($table->get_sql_where()) {
192 $where = 'WHERE '. $table->get_sql_where();
193 } else {
194 $where = '';
197 $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 '.
198 '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 '.
199 $where .' '.
200 'GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, u.id, tg.flag, tg.timemodified, u.firstname, u.lastname '.
201 $sort;
203 $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);
205 $table->initialbars(true); // always initial bars
206 $table->pagesize($perpage, $totalcount);
208 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
210 //retrieve tags from DB
211 if ($tagrecords = get_records_sql($query, $table->get_page_start(), $table->get_page_size())) {
213 $taglist = array_values($tagrecords);
215 //print_tag_cloud(array_values(get_records_sql($query)), false);
216 //populate table with data
217 foreach ($taglist as $tag ){
218 $id = $tag->id;
219 $name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
220 $owner = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . fullname($tag) . '</a>';
221 $count = $tag->count;
222 $flag = $tag->flag;
223 $timemodified = format_time(time() - $tag->timemodified);
224 $checkbox = '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
225 $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);
244 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
245 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
246 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" /> ';
247 echo '<br/><br/>';
248 echo '<select id="menuformaction" name="action">
249 <option value="" selected="selected">'. get_string('withselectedtags', 'tag') .'</option>
250 <option value="reset">'. get_string('resetflag', 'tag') .'</option>
251 <option value="delete">'. get_string('delete', 'tag') .'</option>
252 <option value="changetype">'. get_string('changetype', 'tag') .'</option>
253 <option value="changename">'. get_string('changename', 'tag') .'</option>
254 </select>';
256 echo '<button id="tag-management-submit" type="submit">'. get_string('ok') .'</button>';
259 $table->print_html();
260 echo '</div></form>';
262 if ($perpage == SHOW_ALL_PAGE_SIZE) {
263 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. DEFAULT_PAGE_SIZE .'">'. get_string('showperpage', '', DEFAULT_PAGE_SIZE) .'</a></div>';
265 } else if ($totalcount > 0 and $perpage < $totalcount) {
266 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. SHOW_ALL_PAGE_SIZE .'">'. get_string('showall', '', $totalcount) .'</a></div>';
269 echo '<br/>';
271 print_footer();