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');
28 require_once($CFG->libdir
.'/adminlib.php');
30 define('SHOW_ALL_PAGE_SIZE', 50000);
31 define('DEFAULT_PAGE_SIZE', 30);
33 $tagschecked = optional_param_array('tagschecked', array(), PARAM_INT
);
34 $newnames = optional_param_array('newname', array(), PARAM_TAG
);
35 $tagtypes = optional_param_array('tagtypes', array(), PARAM_ALPHA
);
36 $action = optional_param('action', '', PARAM_ALPHA
);
37 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE
, PARAM_INT
);
41 if (empty($CFG->usetags
)) {
42 print_error('tagsaredisabled', 'tag');
46 if ($perpage != DEFAULT_PAGE_SIZE
) {
47 $params['perpage'] = $perpage;
49 admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'standard'));
51 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
53 echo $OUTPUT->header();
58 // get all the possible tag types from db
59 $existing_tagtypes = array();
60 if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
61 foreach ($ptypes as $ptype) {
62 $existing_tagtypes[$ptype->tagtype
] = $ptype->tagtype
;
65 $existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
66 $existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
71 if (!data_submitted() or !confirm_sesskey()) {
75 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
76 tag_delete($tagschecked);
77 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
81 if (!data_submitted() or !confirm_sesskey()) {
84 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
85 tag_unset_flag($tagschecked);
86 $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
90 if (!data_submitted() or !confirm_sesskey()) {
95 foreach ($tagschecked as $tag_id) {
96 if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
97 //can not add new types here!!
102 if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
103 $changed[] = $tag_id;
107 if (!empty($changed)) {
108 $str_changed = implode(', ', tag_get_name($changed));
109 $notice = $str_changed .' -- '. get_string('typechanged','tag');
114 if (!data_submitted() or !confirm_sesskey()) {
118 $tags_names_changed = array();
119 foreach ($tagschecked as $tag_id) {
120 if ($newnames[$tag_id] != '') {
121 if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
122 // if tag already exists, or is not a valid tag name, etc.
123 $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
125 $tags_names_changed[$tag_id] = $newnames[$tag_id];
130 //notice to inform what tags had their names effectively updated
131 if ($tags_names_changed){
132 $notice = implode($tags_names_changed, ', ');
133 $notice .= ' -- ' . get_string('updated','tag');
136 case 'addofficialtag':
137 if (!data_submitted() or !confirm_sesskey()) {
141 $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG
));
143 foreach ( $new_otags as $new_otag ) {
144 if ( $new_otag_id = tag_get_id($new_otag) ) {
145 // tag exists, change the type
146 tag_type_set($new_otag_id, 'official');
148 require_capability('moodle/tag:create', context_system
::instance());
149 tag_add($new_otag, 'official');
151 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
157 echo $OUTPUT->notification($err_notice, 'notifyproblem');
160 echo $OUTPUT->notification($notice, 'notifysuccess');
163 // small form to add an official tag
164 print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot
.'/tag/manage.php">');
165 print('<input type="hidden" name="action" value="addofficialtag" />');
166 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
167 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
168 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
169 '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
175 $tablecolumns = array('id', 'name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
176 $tableheaders = array(get_string('id', 'tag'),
177 get_string('name', 'tag'),
178 get_string('owner', 'tag'),
179 get_string('count', 'tag'),
180 get_string('flag', 'tag'),
181 get_string('timemodified', 'tag'),
182 get_string('newname', 'tag'),
183 get_string('tagtype', 'tag'),
184 get_string('select', 'tag'));
186 $table = new flexible_table('tag-management-list-'.$USER->id
);
188 $baseurl = $CFG->wwwroot
.'/tag/manage.php?perpage='.$perpage;
190 $table->define_columns($tablecolumns);
191 $table->define_headers($tableheaders);
192 $table->define_baseurl($baseurl);
194 $table->sortable(true, 'flag', SORT_DESC
);
196 $table->set_attribute('cellspacing', '0');
197 $table->set_attribute('id', 'tag-management-list');
198 $table->set_attribute('class', 'admintable generaltable');
200 $table->set_control_variables(array(
201 TABLE_VAR_SORT
=> 'ssort',
202 TABLE_VAR_HIDE
=> 'shide',
203 TABLE_VAR_SHOW
=> 'sshow',
204 TABLE_VAR_IFIRST
=> 'sifirst',
205 TABLE_VAR_ILAST
=> 'silast',
206 TABLE_VAR_PAGE
=> 'spage'
211 if ($table->get_sql_sort()) {
212 $sort = 'ORDER BY '. $table->get_sql_sort();
217 list($where, $params) = $table->get_sql_where();
219 $where = 'WHERE '. $where;
222 $allusernames = get_all_user_name_fields(true, 'u');
224 SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
225 u.id AS owner, $allusernames,
226 COUNT(ti.id) AS count
228 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
229 LEFT JOIN {user} u ON u.id = tg.userid
231 GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
235 $totalcount = $DB->count_records_sql("
236 SELECT COUNT(DISTINCT(tg.id))
238 LEFT JOIN {user} u ON u.id = tg.userid
241 $table->initialbars(true); // always initial bars
242 $table->pagesize($perpage, $totalcount);
244 //@todo MDL-35474 convert to mform
245 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot
.'/tag/manage.php"><div>';
247 //retrieve tags from DB
248 if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {
250 //populate table with data
251 foreach ($tagrecords as $tag) {
253 $params = array('id' => $tag->id
);
254 $taglink = new moodle_url($CFG->wwwroot
. '/tag/index.php', $params);
255 $name = html_writer
::link($taglink, tag_display_name($tag));
256 $params = array('id' => $tag->owner
);
257 $ownerlink = new moodle_url($CFG->wwwroot
. '/user/view.php', $params);
258 $owner = html_writer
::link($ownerlink, fullname($tag));
259 $count = $tag->count
;
261 $timemodified = format_time(time() - $tag->timemodified
);
262 $checkbox = html_writer
::tag('input', '', array('type' => 'checkbox', 'name' => 'tagschecked[]', 'value' => $tag->id
));
263 $attrs = array('type' => 'text', 'id' => 'newname_' . $tag->id
, 'name' => 'newname['.$tag->id
.']');
264 $text = html_writer
::label(get_string('newname', 'tag'), 'newname_' . $tag->id
, false, array('class' => 'accesshide'));
265 $text .= html_writer
::empty_tag('input', $attrs);
266 $tagtype = html_writer
::label(get_string('tagtype', 'tag'), 'menutagtypes'. $tag->id
, false, array('class' => 'accesshide'));
267 $tagtype .= html_writer
::select($existing_tagtypes, 'tagtypes['.$tag->id
.']', $tag->tagtype
, false, array('id' => 'menutagtypes'. $tag->id
));
269 //if the tag if flagged, highlight it
270 if ($tag->flag
> 0) {
271 $id = html_writer
::tag('span', $id, array('class' => 'flagged-tag'));
272 $name = html_writer
::tag('span', $name, array('class' => 'flagged-tag'));
273 $owner = html_writer
::tag('span', $owner, array('class' => 'flagged-tag'));
274 $count = html_writer
::tag('span', $count, array('class' => 'flagged-tag'));
275 $flag = html_writer
::tag('span', $flag, array('class' => 'flagged-tag'));
276 $timemodified = html_writer
::tag('span', $timemodified, array('class' => 'flagged-tag'));
277 $tagtype = html_writer
::tag('span', $tagtype, array('class' => 'flagged-tag'));
280 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
282 $table->add_data($data);
285 echo html_writer
::empty_tag('input', array('type' => 'button', 'onclick' => 'checkall()', 'value' => get_string('selectall')));
286 echo html_writer
::empty_tag('input', array('type' => 'button', 'onclick' => 'checknone()', 'value' => get_string('deselectall')));
287 echo html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
288 echo html_writer
::empty_tag('br');
289 echo html_writer
::empty_tag('br');
291 echo html_writer
::label(get_string('withselectedtags', 'tag'), 'menuformaction', false, array('class' => 'accesshide'));
292 $options = array('' => get_string('withselectedtags', 'tag'),
293 'reset' => get_string('resetflag', 'tag'),
294 'delete' => get_string('delete', 'tag'),
295 'changetype' => get_string('changetype', 'tag'),
296 'changename' => get_string('changename', 'tag'));
297 echo html_writer
::select($options, 'action', '', array(), array('id' => 'menuformaction'));
299 echo html_writer
::tag('button', get_string('ok'), array('id' => 'tag-management-submit', 'type' => 'submit'));
302 $table->print_html();
304 //@todo MDL-35474 convert to mform
305 echo '</div></form>';
307 if ($perpage == SHOW_ALL_PAGE_SIZE
) {
308 echo html_writer
::start_tag('div', array('id' => 'showall'));
309 $params = array('perpage' => DEFAULT_PAGE_SIZE
);
310 $url = new moodle_url($baseurl, $params);
311 echo html_writer
::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE
));
312 echo html_writer
::end_tag('div');
314 } else if ($totalcount > 0 and $perpage < $totalcount) {
315 echo html_writer
::start_tag('div', array('id' => 'showall'));
316 $params = array('perpage' => SHOW_ALL_PAGE_SIZE
);
317 $url = new moodle_url($baseurl, $params);
318 echo html_writer
::link($url, get_string('showall', '', $totalcount));
319 echo html_writer
::end_tag('div');
322 echo html_writer
::empty_tag('br');
324 echo $OUTPUT->footer();