$exists = record_exists('tag', 'name', $tag_object->name);
- if ( !$exists && !empty($tag_object->name) && !is_numeric($tag_object->name) ) {
+ if ( !$exists && is_tag_name_valid($tag_object->name) ) {
if ($can_create_tags) {
insert_record('tag', $tag_object);
}
}
+
+/**
+ * Determines if a tag name is valid
+ *
+ * @param string $name
+ * @return boolean
+ */
+function is_tag_name_valid($name){
+
+ $normalized = tag_normalize($name);
+
+ return !strcmp($normalized, $name) && !empty($name) && !is_numeric($name);
+
+}
+
+
/**
* Associates a tag with an item
*
global $CFG;
//convert any tag ids passed to their corresponding tag names
$tag_names_csv = tag_name_from_string($tag_names_or_ids_csv);
-
+
//create the tags
$tags_created_ids = tag_create($tag_names_csv,$tag_type);
AND
ti.itemid = '{$item_id}'
";
-
- $max_order = get_field_sql($query);
+
+ $max_order = get_field_sql($query);
+
$tag_names = explode(',', tag_normalize($tag_names_csv));
-
+
$ordering = array();
foreach($tag_names as $tag_name){
$ordering[$tag_name] = ++$max_order;
}
-
+
//setup tag_instance object
$tag_instance = new StdClass;
$tag_instance->itemtype = $item_type;
$tag_instance->itemid = $item_id;
-
-
+
+
//create tag instances
foreach ($tags_created_ids as $tag_normalized_name => $tag_id) {
$tag_instance->tagid = $tag_id;
$tag_instance->ordering = $ordering[$tag_normalized_name];
-
+
$tag_instance_exists = get_record('tag_instance', 'tagid', $tag_id, 'itemtype', $item_type, 'itemid', $item_id);
if (!$tag_instance_exists) {
if ($sort) {
$sort = ' ORDER BY '. $sort;
}
-
+
$query = "
SELECT
{$fields}
}
//$value = preg_replace('|[^\w ]|i', '', strtolower(trim($tag_names_csv)));
- $value = preg_replace('|[\!\@\#\$\%\^\&\*\(\)\-\+\=\~\`\\"\'\_.\[\]\{\}\:\;\?\´\^\\\/\<\>\|]|i', '', trim($value));
+ $value = preg_replace('|[\,\!\@\#\$\%\^\&\*\(\)\-\+\=\~\`\\"\'\_.\[\]\{\}\:\;\?\´\^\\\/\<\>\|]|i', '', trim($value));
//removes excess white spaces
$value = preg_replace('/\s\s+/', ' ', $value);
execute_sql($query, false);
}
+/**
+ * Function that updates tags names.
+ * Updates only if the new name suggested for a tag doesn´t exist already.
+ *
+ * @param Array $tags_names_changed array of new tag names indexed by tag ids.
+ * @return Array array of tags names that were effectively updated, indexed by tag ids.
+ */
+function tag_update_name($tags_names_changed){
+
+ $tags_names_updated = array();
+
+ foreach ($tags_names_changed as $id => $newname){
+
+ $norm_newname = tag_normalize($newname);
+
+ if( !tag_exists($norm_newname) && is_tag_name_valid($norm_newname) ) {
+
+ $tag = tag_by_id($id);
+
+ $tags_names_updated[$id] = $tag->name;
+
+ // rawname keeps the original casing of the string
+ $tag->rawname = tag_normalize($newname, false);
+
+ // name lowercases the string
+ $tag->name = $norm_newname;
+
+ $tag->timemodified = time();
+
+ update_record('tag',$tag);
+
+ }
+ }
+
+ return $tags_names_updated;
+
+}
+
/**
* Function that returns comma separated HTML links to the tag pages of the tags passed
*
//setup table
- $tablecolumns = array('id','name', 'owner', 'count', 'flag', 'timemodified', '');
+ $tablecolumns = array('id','name', 'owner', 'count', 'flag', 'timemodified', 'rawname', '');
$tableheaders = array( get_string('id' , 'tag'),
get_string('name' , 'tag'),
get_string('owner','tag'),
get_string('count','tag'),
get_string('flag','tag'),
get_string('timemodified','tag'),
+ get_string('newname', 'tag'),
get_string('select', 'tag')
);
$flag = $tag->flag;
$timemodified = format_time(time() - $tag->timemodified);
$checkbox = '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
+ $text = '<input type="text" name="newname['.$tag->id.']" />';
//if the tag if flagged, highlight it
if ($tag->flag > 0) {
$timemodified = '<span class="flagged-tag">' . $timemodified . '</span>';
}
- $data = array($id, $name , $owner ,$count ,$flag, $timemodified, $checkbox);
+ $data = array($id, $name , $owner ,$count ,$flag, $timemodified, $text, $checkbox);
$table->add_data($data);
}
<option value="" selected="selected">'. get_string('withselectedtags', 'tag') .'</option>
<option value="reset">'. get_string('resetflag', 'tag') .'</option>
<option value="delete">'. get_string('delete', 'tag') .'</option>
+ <option value="changename">'. get_string('changename', 'tag') .'</option>
</select>';
echo '<button id="tag-management-submit" type="submit">'. get_string('ok') .'</button>';
require_capability('moodle/tag:manage', $systemcontext);
$tagschecked = optional_param('tagschecked', array());
+$newnames = optional_param('newname', array());
$action = optional_param('action', '', PARAM_ALPHA);
$navlinks = array();
$navigation = build_navigation($navlinks);
print_header_simple(get_string('managetags', 'tag'), '', $navigation);
+$notice = tag_name_from_string(implode($tagschecked, ', '));
+$notice = str_replace(',', ', ', $notice);
+
switch($action) {
-
+
case 'delete':
-
- $notice = tag_name_from_string(implode($tagschecked, ', '));
- $notice = str_replace(',', ', ', $notice);
+
$notice .= ' -- ' . get_string('deleted','tag');
- notify($notice , 'green');
-
+
tag_delete(implode($tagschecked, ','));
+
break;
- case 'reset':
- $notice = tag_name_from_string(implode($tagschecked, ', '));
- $notice = str_replace(',', ', ', $notice);
+ case 'reset':
+
$notice .= ' -- ' . get_string('reset','tag');
- notify($notice , 'green');
tag_flag_reset(implode($tagschecked, ','));
+
+ break;
+
+ case 'changename':
+
+ $normalized_new_names_csv = tag_normalize( str_replace(',,','',implode($newnames, ',')) );
+
+ //tag names entered might already exist
+ $existing_tags = tags_id( $normalized_new_names_csv );
+
+ //notice to warn that names already exist
+ $err_notice = '';
+ foreach ($existing_tags as $name => $tag){
+ $err_notice .= $name . ', ';
+ }
+ if(!empty($err_notice)){
+ $err_notice .= '-- ' . get_string('namesalreadybeeingused','tag');
+ }
+
+
+ //update tag names with the names passed in $newnames
+ $tags_names_changed = array();
+ foreach ($tagschecked as $tag_id){
+ $tags_names_changed[$tag_id] = str_replace(',','',$newnames[$tag_id]) ;
+ }
+
+ $tags_names_updated = tag_update_name($tags_names_changed);
+
+ //notice to inform what tags had their names effectively updated
+ $notice = implode($tags_names_updated, ', ');
+ if(!empty($notice)){
+ $notice .= ' -- ' . get_string('updated','tag');
+ }
+
break;
}
echo '<br/>';
+notify($err_notice, 'red');
+notify($notice , 'green');
+
print_tag_management_list();