libdir.'/tablelib.php');
require_once('lib.php');
define('SHOW_ALL_PAGE_SIZE', 50000);
define('DEFAULT_PAGE_SIZE', 30);
$tagschecked = optional_param('tagschecked', array(), PARAM_INT);
$newnames = optional_param('newname', array(), PARAM_TAG);
$tagtypes = optional_param('tagtypes', array(), PARAM_ALPHA);
$action = optional_param('action', '', PARAM_ALPHA);
$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
require_login();
if (empty($CFG->usetags)) {
print_error('tagsaredisabled', 'tag');
}
//managing tags requires moodle/tag:manage capability
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
require_capability('moodle/tag:manage', $systemcontext);
$navlinks = array();
$navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
$navlinks[] = array('name' => get_string('managetags', 'tag'), 'link' => '', 'type' => '');
$navigation = build_navigation($navlinks);
print_header_simple(get_string('managetags', 'tag'), '', $navigation);
$err_notice = '';
$notice = '';
// get all the possible tag types from db
$existing_tagtypes = array();
if ($ptypes = get_records_sql("SELECT DISTINCT(tagtype) FROM {$CFG->prefix}tag")) {
foreach ($ptypes as $ptype) {
$existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
}
}
$existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
$existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
switch($action) {
case 'delete':
if (!data_submitted() or !confirm_sesskey()) {
break;
}
$str_tagschecked = implode(', ', tag_get_name($tagschecked));
tag_delete($tagschecked);
$notice = $str_tagschecked.' -- '.get_string('deleted','tag');
break;
case 'reset':
if (!data_submitted() or !confirm_sesskey()) {
break;
}
$str_tagschecked = implode(', ', tag_get_name($tagschecked));
tag_unset_flag($tagschecked);
$notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
break;
case 'changetype':
if (!data_submitted() or !confirm_sesskey()) {
break;
}
$changed = array();
foreach ($tagschecked as $tag_id) {
if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
//can not add new types here!!
continue;
}
// update tag type;
if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
$changed[] = $tag_id;
}
}
if (!empty($changed)) {
$str_changed = implode(', ', tag_get_name($changed));
$notice = $str_changed .' -- '. get_string('typechanged','tag');
}
break;
case 'changename':
if (!data_submitted() or !confirm_sesskey()) {
break;
}
$tags_names_changed = array();
foreach ($tagschecked as $tag_id) {
if ($newnames[$tag_id] != '') {
if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
// if tag already exists, or is not a valid tag name, etc.
$err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'
';
} else {
$tags_names_changed[$tag_id] = $newnames[$tag_id];
}
}
}
//notice to inform what tags had their names effectively updated
if ($tags_names_changed){
$notice = implode($tags_names_changed, ', ');
$notice .= ' -- ' . get_string('updated','tag');
}
break;
case 'addofficialtag':
if (!data_submitted() or !confirm_sesskey()) {
break;
}
$new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
$notice = '';
foreach ( $new_otags as $new_otag ) {
if ( $new_otag_id = tag_get_id($new_otag) ) {
// tag exists, change the type
tag_type_set($new_otag_id, 'official');
} else {
tag_add($new_otag, 'official');
}
$notice .= get_string('addedotag', 'tag', $new_otag) .' ';
}
break;
}
echo '
';
if ($err_notice) {
notify($err_notice, 'red');
}
if ($notice) {
notify($notice, 'green');
}
// small form to add an official tag
print('