Merge branch 'install_21_STABLE' of git://github.com/amosbot/moodle into MOODLE_21_STABLE
[moodle.git] / tag / manage.php
blobc4fae4c13e0d613a03308ef9e5822f291a8b0367
1 <?php
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.
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/>.
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
25 require_once('../config.php');
26 require_once($CFG->libdir.'/tablelib.php');
27 require_once('lib.php');
29 define('SHOW_ALL_PAGE_SIZE', 50000);
30 define('DEFAULT_PAGE_SIZE', 30);
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);
38 require_login();
40 if (empty($CFG->usetags)) {
41 print_error('tagsaredisabled', 'tag');
44 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
45 require_capability('moodle/tag:manage', $systemcontext);
47 $params = array();
48 if ($perpage != DEFAULT_PAGE_SIZE) {
49 $params['perpage'] = $perpage;
51 $PAGE->set_url('/tag/manage.php', $params);
52 $PAGE->set_context($systemcontext);
53 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
54 $PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'));
55 $PAGE->navbar->add(get_string('managetags', 'tag'));
56 $PAGE->set_title(get_string('managetags', 'tag'));
57 $PAGE->set_heading($COURSE->fullname);
58 $PAGE->set_pagelayout('standard');
59 echo $OUTPUT->header();
61 $err_notice = '';
62 $notice = '';
64 // get all the possible tag types from db
65 $existing_tagtypes = array();
66 if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
67 foreach ($ptypes as $ptype) {
68 $existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
71 $existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
72 $existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
74 switch($action) {
76 case 'delete':
77 if (!data_submitted() or !confirm_sesskey()) {
78 break;
81 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
82 tag_delete($tagschecked);
83 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
84 break;
86 case 'reset':
87 if (!data_submitted() or !confirm_sesskey()) {
88 break;
90 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
91 tag_unset_flag($tagschecked);
92 $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
93 break;
95 case 'changetype':
96 if (!data_submitted() or !confirm_sesskey()) {
97 break;
100 $changed = array();
101 foreach ($tagschecked as $tag_id) {
102 if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
103 //can not add new types here!!
104 continue;
107 // update tag type;
108 if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
109 $changed[] = $tag_id;
113 if (!empty($changed)) {
114 $str_changed = implode(', ', tag_get_name($changed));
115 $notice = $str_changed .' -- '. get_string('typechanged','tag');
117 break;
119 case 'changename':
120 if (!data_submitted() or !confirm_sesskey()) {
121 break;
124 $tags_names_changed = array();
125 foreach ($tagschecked as $tag_id) {
126 if ($newnames[$tag_id] != '') {
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.
129 $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
130 } else {
131 $tags_names_changed[$tag_id] = $newnames[$tag_id];
136 //notice to inform what tags had their names effectively updated
137 if ($tags_names_changed){
138 $notice = implode($tags_names_changed, ', ');
139 $notice .= ' -- ' . get_string('updated','tag');
141 break;
142 case 'addofficialtag':
143 if (!data_submitted() or !confirm_sesskey()) {
144 break;
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
152 tag_type_set($new_otag_id, 'official');
153 } else {
154 require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
155 tag_add($new_otag, 'official');
157 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
159 break;
162 if ($err_notice) {
163 echo $OUTPUT->notification($err_notice, 'red');
165 if ($notice) {
166 echo $OUTPUT->notification($notice, 'green');
169 // small form to add an official tag
170 print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
171 print('<input type="hidden" name="action" value="addofficialtag" />');
172 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
173 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
174 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
175 '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
176 '</div>');
177 print('</form>');
179 //setup table
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'),
188 get_string('newname', 'tag'),
189 get_string('tagtype', 'tag'),
190 get_string('select', 'tag'));
192 $table = new flexible_table('tag-management-list-'.$USER->id);
194 $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
196 $table->define_columns($tablecolumns);
197 $table->define_headers($tableheaders);
198 $table->define_baseurl($baseurl);
200 $table->sortable(true, 'flag', SORT_DESC);
202 $table->set_attribute('cellspacing', '0');
203 $table->set_attribute('id', 'tag-management-list');
204 $table->set_attribute('class', 'generaltable generalbox');
206 $table->set_control_variables(array(
207 TABLE_VAR_SORT => 'ssort',
208 TABLE_VAR_HIDE => 'shide',
209 TABLE_VAR_SHOW => 'sshow',
210 TABLE_VAR_IFIRST => 'sifirst',
211 TABLE_VAR_ILAST => 'silast',
212 TABLE_VAR_PAGE => 'spage'
215 $table->setup();
217 if ($table->get_sql_sort()) {
218 $sort = 'ORDER BY '. $table->get_sql_sort();
219 } else {
220 $sort = '';
223 list($where, $params) = $table->get_sql_where();
224 if ($where) {
225 $where = 'WHERE '. $where;
228 $query = "
229 SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
230 u.id AS owner, u.firstname, u.lastname,
231 COUNT(ti.id) AS count
232 FROM {tag} tg
233 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
234 LEFT JOIN {user} u ON u.id = tg.userid
235 $where
236 GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
237 u.id, u.firstname, u.lastname
238 $sort";
240 $totalcount = $DB->count_records_sql("
241 SELECT COUNT(DISTINCT(tg.id))
242 FROM {tag} tg
243 LEFT JOIN {user} u ON u.id = tg.userid
244 $where", $params);
246 $table->initialbars(true); // always initial bars
247 $table->pagesize($perpage, $totalcount);
249 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
251 //retrieve tags from DB
252 if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {
254 //populate table with data
255 foreach ($tagrecords as $tag) {
256 $id = $tag->id;
257 $name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
258 $owner = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . fullname($tag) . '</a>';
259 $count = $tag->count;
260 $flag = $tag->flag;
261 $timemodified = format_time(time() - $tag->timemodified);
262 $checkbox = '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
263 $text = '<input type="text" name="newname['.$tag->id.']" />';
264 $tagtype = html_writer::select($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false);
266 //if the tag if flagged, highlight it
267 if ($tag->flag > 0) {
268 $id = '<span class="flagged-tag">' . $id . '</span>';
269 $name = '<span class="flagged-tag">' . $name . '</span>';
270 $owner = '<span class="flagged-tag">' . $owner . '</span>';
271 $count = '<span class="flagged-tag">' . $count . '</span>';
272 $flag = '<span class="flagged-tag">' . $flag . '</span>';
273 $timemodified = '<span class="flagged-tag">' . $timemodified . '</span>';
274 $tagtype = '<span class="flagged-tag">'. $tagtype. '</span>';
277 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
279 $table->add_data($data);
282 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
283 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
284 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" /> ';
285 echo '<br/><br/>';
286 echo '<select id="menuformaction" name="action">
287 <option value="" selected="selected">'. get_string('withselectedtags', 'tag') .'</option>
288 <option value="reset">'. get_string('resetflag', 'tag') .'</option>
289 <option value="delete">'. get_string('delete', 'tag') .'</option>
290 <option value="changetype">'. get_string('changetype', 'tag') .'</option>
291 <option value="changename">'. get_string('changename', 'tag') .'</option>
292 </select>';
294 echo '<button id="tag-management-submit" type="submit">'. get_string('ok') .'</button>';
297 $table->print_html();
298 echo '</div></form>';
300 if ($perpage == SHOW_ALL_PAGE_SIZE) {
301 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. DEFAULT_PAGE_SIZE .'">'. get_string('showperpage', '', DEFAULT_PAGE_SIZE) .'</a></div>';
303 } else if ($totalcount > 0 and $perpage < $totalcount) {
304 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. SHOW_ALL_PAGE_SIZE .'">'. get_string('showall', '', $totalcount) .'</a></div>';
307 echo '<br/>';
309 echo $OUTPUT->footer();