- tag names can be altered now in tags management page
authorluizlaydner <luizlaydner>
Fri, 24 Aug 2007 17:28:54 +0000 (17:28 +0000)
committerluizlaydner <luizlaydner>
Fri, 24 Aug 2007 17:28:54 +0000 (17:28 +0000)
lang/en_utf8/tag.php
tag/lib.php
tag/manage.php

index 7b74327f238e09a26e347ee29bdfb1f73b599dc2..d42960c43ad84ff2863057758b3dfcb49168bdf2 100644 (file)
@@ -2,6 +2,7 @@
 
 $string['addtagtomyinterests'] =  'Add \"$a\" to my interests';
 $string['count'] = 'Count';
+$string['changename'] = 'Change name';
 $string['description'] = 'Description';
 $string['delete'] = 'Delete';
 $string['deleted'] = 'Deleted';
@@ -12,6 +13,8 @@ $string['helprelatedtags'] = 'Comma separated related tags';
 $string['id'] = 'id';
 $string['managetags'] = 'Manage Tags';
 $string['name'] = 'Name';
+$string['namesalreadybeeingused'] = 'Tag names already being used';
+$string['newname'] = 'New Name';
 $string['noresultsfor'] = 'No results for \"$a\"';
 $string['owner'] = 'Owner';
 $string['relatedtags'] = 'Related tags';
@@ -31,6 +34,7 @@ $string['thistaghasnodesc'] = 'This tag currently has no description.';
 $string['timemodified'] = 'Modified';
 $string['userstaggedwith'] = 'Users tagged with \"$a\"';
 $string['updatetag'] = 'Update';
+$string['updated'] = 'Updated';
 $string['withselectedtags'] = 'With selected tags...';
 
 ?>
index da49934a0c820bb46f4b5e3c06330963dc42efe2..9b0e658b093d2b406568a269dee30e7ddff1e631 100644 (file)
@@ -41,7 +41,7 @@ function tag_create($tag_names_csv, $tag_type="default") {
 
         $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);
             }
@@ -323,6 +323,22 @@ function tag_name_from_string($tag_names_or_ids_csv) {
 
 }
 
+
+/**
+ * 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
  * 
@@ -344,7 +360,7 @@ function tag_an_item($item_type, $item_id, $tag_names_or_ids_csv, $tag_type="def
     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);
 
@@ -359,27 +375,28 @@ function tag_an_item($item_type, $item_id, $tag_names_or_ids_csv, $tag_type="def
         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) {
@@ -507,7 +524,7 @@ function get_item_tags($item_type, $item_id, $sort='ti.ordering ASC', $fields=DE
     if ($sort) {
         $sort = ' ORDER BY '. $sort;
     }
-        
+
     $query = "
         SELECT
             {$fields}
@@ -959,7 +976,7 @@ function tag_normalize($tag_names_csv, $lowercase=true) {
         }
 
         //$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);
@@ -1010,6 +1027,44 @@ function tag_flag_reset($tag_names_or_ids_csv){
     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
  *
@@ -1456,13 +1511,14 @@ function print_tag_management_list($perpage='100') {
 
     //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')
     );
 
@@ -1547,6 +1603,7 @@ function print_tag_management_list($perpage='100') {
             $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) {
@@ -1558,7 +1615,7 @@ function print_tag_management_list($perpage='100') {
                 $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);
         }
@@ -1571,6 +1628,7 @@ function print_tag_management_list($perpage='100') {
                     <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>';
index a744f581a8612c1c1cb013d7556d56019665dc82..f9097088ceb764905728a3b628728adeda297180 100644 (file)
@@ -15,6 +15,7 @@ $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
 require_capability('moodle/tag:manage', $systemcontext);
 
 $tagschecked    = optional_param('tagschecked', array());
+$newnames       = optional_param('newname', array());
 $action         = optional_param('action', '', PARAM_ALPHA);
 
 $navlinks = array();
@@ -24,30 +25,66 @@ $navlinks[] = array('name' => get_string('managetags', 'tag'), 'link' => '', 'ty
 $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();