3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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_array('tagschecked', array(), PARAM_INT
);
33 $newnames = optional_param_array('newname', array(), PARAM_TAG
);
34 $tagtypes = optional_param_array('tagtypes', array(), PARAM_ALPHA
);
35 $action = optional_param('action', '', PARAM_ALPHA
);
36 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE
, PARAM_INT
);
40 if (empty($CFG->usetags
)) {
41 print_error('tagsaredisabled', 'tag');
44 $systemcontext = context_system
::instance();
45 require_capability('moodle/tag:manage', $systemcontext);
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();
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');
77 if (!data_submitted() or !confirm_sesskey()) {
81 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
82 tag_delete($tagschecked);
83 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
87 if (!data_submitted() or !confirm_sesskey()) {
90 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
91 tag_unset_flag($tagschecked);
92 $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
96 if (!data_submitted() or !confirm_sesskey()) {
101 foreach ($tagschecked as $tag_id) {
102 if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
103 //can not add new types here!!
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');
120 if (!data_submitted() or !confirm_sesskey()) {
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 />';
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');
142 case 'addofficialtag':
143 if (!data_submitted() or !confirm_sesskey()) {
147 $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG
));
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');
154 require_capability('moodle/tag:create', context_system
::instance());
155 tag_add($new_otag, 'official');
157 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
163 echo $OUTPUT->notification($err_notice, 'red');
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" />'.
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', 'admintable generaltable');
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'
217 if ($table->get_sql_sort()) {
218 $sort = 'ORDER BY '. $table->get_sql_sort();
223 list($where, $params) = $table->get_sql_where();
225 $where = 'WHERE '. $where;
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
233 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
234 LEFT JOIN {user} u ON u.id = tg.userid
236 GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
237 u.id, u.firstname, u.lastname
240 $totalcount = $DB->count_records_sql("
241 SELECT COUNT(DISTINCT(tg.id))
243 LEFT JOIN {user} u ON u.id = tg.userid
246 $table->initialbars(true); // always initial bars
247 $table->pagesize($perpage, $totalcount);
249 //@todo MDL-35474 convert to mform
250 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot
.'/tag/manage.php"><div>';
252 //retrieve tags from DB
253 if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {
255 //populate table with data
256 foreach ($tagrecords as $tag) {
258 $params = array('id' => $tag->id
);
259 $taglink = new moodle_url($CFG->wwwroot
. '/tag/index.php', $params);
260 $name = html_writer
::link($taglink, tag_display_name($tag));
261 $params = array('id' => $tag->owner
);
262 $ownerlink = new moodle_url($CFG->wwwroot
. '/user/view.php', $params);
263 $owner = html_writer
::link($ownerlink, fullname($tag));
264 $count = $tag->count
;
266 $timemodified = format_time(time() - $tag->timemodified
);
267 $checkbox = html_writer
::tag('input', '', array('type' => 'checkbox', 'name' => 'tagschecked[]', 'value' => $tag->id
));
268 $attrs = array('type' => 'text', 'id' => 'newname_' . $tag->id
, 'name' => 'newname['.$tag->id
.']');
269 $text = html_writer
::label(get_string('newname', 'tag'), 'newname_' . $tag->id
, false, array('class' => 'accesshide'));
270 $text .= html_writer
::empty_tag('input', $attrs);
271 $tagtype = html_writer
::label(get_string('tagtype', 'tag'), 'menutagtypes'. $tag->id
, false, array('class' => 'accesshide'));
272 $tagtype .= html_writer
::select($existing_tagtypes, 'tagtypes['.$tag->id
.']', $tag->tagtype
, false, array('id' => 'menutagtypes'. $tag->id
));
274 //if the tag if flagged, highlight it
275 if ($tag->flag
> 0) {
276 $id = html_writer
::tag('span', $id, array('class' => 'flagged-tag'));
277 $name = html_writer
::tag('span', $name, array('class' => 'flagged-tag'));
278 $owner = html_writer
::tag('span', $owner, array('class' => 'flagged-tag'));
279 $count = html_writer
::tag('span', $count, array('class' => 'flagged-tag'));
280 $flag = html_writer
::tag('span', $flag, array('class' => 'flagged-tag'));
281 $timemodified = html_writer
::tag('span', $timemodified, array('class' => 'flagged-tag'));
282 $tagtype = html_writer
::tag('span', $tagtype, array('class' => 'flagged-tag'));
285 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
287 $table->add_data($data);
290 echo html_writer
::empty_tag('input', array('type' => 'button', 'onclick' => 'checkall()', 'value' => get_string('selectall')));
291 echo html_writer
::empty_tag('input', array('type' => 'button', 'onclick' => 'checknone()', 'value' => get_string('deselectall')));
292 echo html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
293 echo html_writer
::empty_tag('br');
294 echo html_writer
::empty_tag('br');
296 echo html_writer
::label(get_string('withselectedtags', 'tag'), 'menuformaction', false, array('class' => 'accesshide'));
297 $options = array('' => get_string('withselectedtags', 'tag'),
298 'reset' => get_string('resetflag', 'tag'),
299 'delete' => get_string('delete', 'tag'),
300 'changetype' => get_string('changetype', 'tag'),
301 'changename' => get_string('changename', 'tag'));
302 echo html_writer
::select($options, 'action', '', array(), array('id' => 'menuformaction'));
304 echo html_writer
::tag('button', get_string('ok'), array('id' => 'tag-management-submit', 'type' => 'submit'));
307 $table->print_html();
309 //@todo MDL-35474 convert to mform
310 echo '</div></form>';
312 if ($perpage == SHOW_ALL_PAGE_SIZE
) {
313 echo html_writer
::start_tag('div', array('id' => 'showall'));
314 $params = array('perpage' => DEFAULT_PAGE_SIZE
);
315 $url = new moodle_url($baseurl, $params);
316 echo html_writer
::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE
));
317 echo html_writer
::end_tag('div');
319 } else if ($totalcount > 0 and $perpage < $totalcount) {
320 echo html_writer
::start_tag('div', array('id' => 'showall'));
321 $params = array('perpage' => SHOW_ALL_PAGE_SIZE
);
322 $url = new moodle_url($baseurl, $params);
323 echo html_writer
::link($url, get_string('showall', '', $totalcount));
324 echo html_writer
::end_tag('div');
327 echo html_writer
::empty_tag('br');
329 echo $OUTPUT->footer();