MDL-10956, convert blogs to use the same tags (and convert old tags)
authortoyomoyo <toyomoyo>
Mon, 27 Aug 2007 08:46:00 +0000 (08:46 +0000)
committertoyomoyo <toyomoyo>
Mon, 27 Aug 2007 08:46:00 +0000 (08:46 +0000)
blocks/blog_tags/block_blog_tags.php
blog/edit.php
blog/edit_form.php
blog/header.php
blog/lib.php
blog/tags.php
lib/db/upgrade.php
tag/lib.php
version.php

index 580cb3f1c3ff19f501f0d9ac61faf813c08e78d7..bcc9dd11edfd6b9b4c05d16932cd5221efea741e 100644 (file)
@@ -2,7 +2,7 @@
 
 define('BLOGDEFAULTTIMEWITHIN', 90);
 define('BLOGDEFAULTNUMBEROFTAGS', 20);
-define('BLOGDEFAULTSORT', 'text');
+define('BLOGDEFAULTSORT', 'name');
 
 require_once($CFG->dirroot .'/blog/lib.php');
 
@@ -38,7 +38,6 @@ class block_blog_tags extends block_base {
         }
     }
 
-
     function get_content() {
 
         global $CFG, $SITE, $COURSE, $USER;
@@ -71,19 +70,18 @@ class block_blog_tags extends block_base {
         $this->content->text = '';
         $this->content->footer = '';
 
-
         /// Get a list of tags
 
         $timewithin = $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
 
-        $sql  = 'SELECT t.id, t.type, t.text, COUNT(DISTINCT bt.id) as ct ';
-        $sql .= "FROM {$CFG->prefix}tags t, {$CFG->prefix}blog_tag_instance bt, {$CFG->prefix}post p ";
-        $sql .= 'WHERE t.id = bt.tagid ';
-        $sql .= 'AND p.id = bt.entryid ';
+        $sql  = 'SELECT t.id, t.tagtype, t.name, COUNT(DISTINCT ti.id) as ct ';
+        $sql .= "FROM {$CFG->prefix}tag t, {$CFG->prefix}tag_instance ti, {$CFG->prefix}post p ";
+        $sql .= 'WHERE t.id = ti.tagid ';
+        $sql .= 'AND p.id = ti.itemid ';
         $sql .= 'AND (p.publishstate = \'site\' or p.publishstate=\'public\') ';
-        $sql .= "AND bt.timemodified > {$timewithin} ";
-        $sql .= 'GROUP BY t.id, t.type, t.text ';
-        $sql .= 'ORDER BY ct DESC, t.text ASC';
+        $sql .= "AND ti.timemodified > {$timewithin} ";
+        $sql .= 'GROUP BY t.id, t.tagtype, t.name ';
+        $sql .= 'ORDER BY ct DESC, t.name ASC';
 
         if ($tags = get_records_sql($sql, 0, $this->config->numberoftags)) {
 
@@ -111,7 +109,7 @@ class block_blog_tags extends block_base {
                     $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
                 }
 
-                $tag->class = "$tag->type s$size";
+                $tag->class = "$tag->tagtype s$size";
                 $etags[] = $tag;
 
             }
@@ -159,7 +157,7 @@ class block_blog_tags extends block_base {
                 $this->content->text .= '<li><a href="'.$link.'" '.
                                         'class="'.$tag->class.'" '.
                                         'title="'.get_string('numberofentries','blog',$tag->ct).'">'.
-                                        $tag->text.'</a></li> ';
+                                        $tag->name.'</a></li> ';
             }
             $this->content->text .= "\n</ul>\n";
 
@@ -186,7 +184,7 @@ class block_blog_tags extends block_base {
 
     /// set up sort select field
         $sort = array();
-        $sort['text'] = get_string('tagtext', 'blog');
+        $sort['name'] = get_string('tagtext', 'blog');
         $sort['id']   = get_string('tagdatelastused', 'blog');
 
 
@@ -197,9 +195,7 @@ class block_blog_tags extends block_base {
         } else {
             notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
         }
-
     }
-
 }
 
 function blog_tags_sort($a, $b) {
index 51a45295cb9b1a9c0a0921be5e27808b490b465f..c3ed2799abb5c0a87ba87a488cd9f0f85b6e9d68 100755 (executable)
@@ -2,6 +2,7 @@
 
 require_once('../config.php');
 include_once('lib.php');
+include_once($CFG->dirroot.'/tag/lib.php');
 
 $action   = required_param('action', PARAM_ALPHA);
 $id       = optional_param('id', 0, PARAM_INT);
@@ -118,12 +119,12 @@ switch ($action) {
         $post->action       = $action;
         $strformheading = get_string('updateentrywithid', 'blog');
 
-        if ($ptags = get_records_sql_menu("SELECT t.id, t.text FROM
-                                     {$CFG->prefix}tags t,
-                                     {$CFG->prefix}blog_tag_instance bti
-                                     WHERE t.id = bti.tagid
-                                     AND t.type = 'personal'
-                                     AND bti.entryid = {$post->id}")) {
+        if ($ptags = get_records_sql_menu("SELECT t.id, t.name FROM
+                                     {$CFG->prefix}tag t,
+                                     {$CFG->prefix}tag_instance ti
+                                     WHERE t.id = ti.tagid
+                                     AND t.tagtype = 'default'
+                                     AND ti.itemid = {$post->id}")) {
 
             $post->ptags = implode(', ', $ptags);
         } else {
@@ -131,12 +132,12 @@ switch ($action) {
             //was used but seems redundant.
             $post->ptags = '';
         }
-        if ($otags = get_records_sql_menu("SELECT t.id, t.text FROM
-                                     {$CFG->prefix}tags t,
-                                     {$CFG->prefix}blog_tag_instance bti
-                                     WHERE t.id = bti.tagid
-                                     AND t.type = 'official'
-                                     AND bti.entryid = {$post->id}")){
+        if ($otags = get_records_sql_menu("SELECT t.id, t.name FROM
+                                     {$CFG->prefix}tag t,
+                                     {$CFG->prefix}tag_instance ti
+                                     WHERE t.id = ti.tagid
+                                     AND t.tagtype = 'official'
+                                     AND ti.itemid = {$post->id}")){
             $post->otags = array_keys($otags);
         }
     break;
@@ -185,56 +186,47 @@ function no_submit_button_actions(&$blogeditform, $sitecontext){
 function delete_otags($tagids, $sitecontext){
     foreach ($tagids as $tagid) {
 
-        if (!$tag = get_record('tags', 'id', $tagid)) {
+        if (!$tag = tag_by_id($tagid)) {
             error('Can not delete tag, tag doesn\'t exist');
         }
-
-        if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
-            //can not delete
-            error('Can not delete tag, you don\'t have permission to delete an official tag');
+        if ($tag->tagtype != 'official') {
+            continue; 
         }
-
-        if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
+        if ($tag->tagtype == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
             //can not delete
-            error('Can not delete tag, you don\'t have permission to delete a personal tag');
+            error('Can not delete tag, you don\'t have permission to delete an official tag');
         }
 
         // Delete the tag itself
-        if (!delete_records('tags', 'id', $tagid)) {
+        if (!tag_delete($tagid)) {
             error('Can not delete tag');
         }
-
-        // Deleteing all references to this tag
-        if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
-            error('Can not delete blog tag instances');
-        }
-
-
     }
 }
 
 function add_otag($otag){
     global $USER;
     $error = '';
-    if ($tag = get_record('tags', 'text', $otag)) {
-        if ($tag->type == 'official') {
+
+    // When adding ofical tag, we see if there's already a personal tag
+    // With the same Name, if there is, we just change the type
+    if ($tag = tag_by_name ($otag)) {
+        if ($tag->tagtype == 'official') {
             // official tag already exist
             $error = get_string('tagalready');
+            break;
         } else {
-            $tag->type = 'official';
-            update_record('tags', $tag);
+            // might not want to do this anymore?
+            $tag->tagtype = 'official';
+            update_record('tag', $tag);
             $tagid = $tag->id;
         }
+                
     } else { // Brand new offical tag
-
-        $tag = new object();
-        $tag->userid = $USER->id;
-        $tag->text   = $otag;
-        $tag->type   = 'official';
-
-        if (!$tagid = insert_record('tags', $tag)) {
+        $tagid = tag_create($otag, 'official');
+        if (empty($tagid)) {
             error('Can not create tag!');
-        }
+        }  
     }
     return $error;
 }
@@ -246,8 +238,9 @@ function do_delete($post) {
     global $returnurl;
 
     $status = delete_records('post', 'id', $post->id);
-    $status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
-
+    //$status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
+    untag_an_item('blog', $post->id);
+    
     blog_delete_old_attachments($post);
 
     add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
@@ -305,11 +298,13 @@ function do_edit($post, $blogeditform) {
     // update record
     if (update_record('post', $post)) {
         // delete all tags associated with this entry
-        delete_records('blog_tag_instance', 'entryid', $post->id);
+        
+        //delete_records('blog_tag_instance', 'entryid', $post->id);
+        //delete_records('tag_instance', 'itemid', $post->id, 'itemtype', 'blog');
+        untag_an_item('blog', $post->id);
         // add them back
         add_tags_info($post->id);
 
-
         add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
 
     } else {
@@ -327,16 +322,12 @@ function add_tags_info($postid) {
 
     $post = get_record('post', 'id', $postid);
 
-    $tag = new object();
-    $tag->entryid = $post->id;
-    $tag->userid = $post->userid;
-    $tag->timemodified = time();
-
     /// Attach official tags
     if ($otags = optional_param('otags', '', PARAM_INT)) {
         foreach ($otags as $otag) {
             $tag->tagid = $otag;
-            insert_record('blog_tag_instance', $tag);
+            //insert_record('blog_tag_instance', $tag);
+            tag_an_item('blog', $postid, $otag, 'official'); 
         }
     }
 
@@ -348,17 +339,13 @@ function add_tags_info($postid) {
             // check for existance
             // it does not matter whether it is an offical tag or personal tag
             // we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag)
-            if ($ctag = get_record('tags', 'text', $ptag)) {
-                $tag->tagid = $ctag->id;
-                insert_record('blog_tag_instance', $tag);
+            if ($ctag = tag_by_id($ptag)) {
+                tag_an_item('blog', $postid, $ctag);
             } else { // create a personal tag
-                $ctag = new object;
-                $ctag->userid = $USER->id;
-                $ctag->text = $ptag;
-                $ctag->type = 'personal';
-                if ($tagid = insert_record('tags', $ctag)) {
-                    $tag->tagid = $tagid;
-                    insert_record('blog_tag_instance', $tag);
+                if ($tagid = tag_create($ptag)) {
+                    if ($tagid = array_shift($tagid)) {
+                        tag_an_item('blog', $postid, $tagid);
+                    }
                 }
             }
         }
index b98a46a34102e67af749daeb6c9631c2b63b118b..9160ffbb1bb87429049b8af0c7b4d2e8b4432bf3 100644 (file)
@@ -92,12 +92,14 @@ class blog_edit_form extends moodleform {
     function otags_select_setup(){
         global $CFG;
         $mform =& $this->_form;
-        $otagsselect =& $mform->getElement('otags');
-        $otagsselect->removeOptions();
-        if ($otags = get_records_sql_menu('SELECT id, text from '.$CFG->prefix.'tags WHERE type=\'official\' ORDER by text ASC')){
+        if ($otagsselect =& $mform->getElement('otags')) {
+            $otagsselect->removeOptions();
+        }
+        if ($otags = get_records_sql_menu('SELECT id, name from '.$CFG->prefix.'tag WHERE tagtype=\'official\' ORDER by name ASC')){
             $otagsselect->loadArray($otags);
         } else {
-            $mform->removeElement('otags');
+            // removing this causes errors
+            //$mform->removeElement('otags');
         }
     }
 
index c12f8ed4c4450359b3943660f133634337090432..3e770e66d2bb8cac654b556a9f3bcaf3ed787332 100755 (executable)
@@ -87,13 +87,11 @@ if ($editing) {
 }
 
 if (!empty($tagid)) {
-    $taginstance = get_record('tags', 'id', $tagid);
+    $taginstance = get_record('tag', 'id', $tagid);
 } else {
     $tagid = '';
     if (!empty($tag)) {
-        $tagrec = get_record('tags', 'text', $tag);
-        $tagid = $tagrec->id;
-        $taginstance = get_record('tags', 'id', $tagid);
+        $taginstance = tag_id($tag);
     }
 }
 
@@ -119,7 +117,7 @@ $navlinks = array();
         case 'site':
             if ($tagid || !empty($tag)) {
                 $navlinks[] = array('name' => $blogstring, 'link' => "index.php?filtertype=site", 'type' => 'misc');
-                $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+                $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
                 $navigation = build_navigation($navlinks);
                 print_header("$SITE->shortname: $blogstring", $SITE->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
             } else {
@@ -134,7 +132,7 @@ $navlinks = array();
                 $navlinks[] = array('name' => $blogstring,
                                     'link' => "index.php?filtertype=course&amp;filterselect=$filterselect",
                                     'type' => 'misc');
-                $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+                $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
                 $navigation = build_navigation($navlinks);
                 print_header("$course->shortname: $blogstring", $course->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
             } else {
@@ -154,7 +152,7 @@ $navlinks = array();
                     $navlinks[] = array('name' => $blogstring,
                                         'link' => "index.php?filtertype=group&amp;filterselect=$filterselect",
                                         'type' => 'misc');
-                    $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+                    $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
                     $navigation = build_navigation($navlinks);
                     print_header("$course->shortname: $blogstring", $course->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
                 } else {
@@ -191,7 +189,7 @@ $navlinks = array();
                     $navlinks[] = array('name' => $blogstring,
                                         'link' => "index.php?courseid=$course->id&amp;filtertype=user&amp;filterselect=$filterselect",
                                         'type' => 'misc');
-                    $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+                    $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
                     $navigation = build_navigation($navlinks);
 
                     print_header("$course->shortname: $blogstring", $course->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
@@ -226,7 +224,7 @@ $navlinks = array();
                     $navlinks[] = array('name' => $blogstring,
                                         'link' => "index.php?filtertype=user&amp;filterselect=$filterselect",
                                         'type' => 'misc');
-                    $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+                    $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
                     $navigation = build_navigation($navlinks);
 
                     print_header("$SITE->shortname: $blogstring", $SITE->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
index b77f8c1bd72c0bda843e9777f5fc06346cbf001b..7c5b164fe6705322953ce22622990c833551846b 100755 (executable)
@@ -7,7 +7,7 @@
     require_once($CFG->libdir .'/pagelib.php');
     require_once($CFG->dirroot .'/blog/rsslib.php');
     require_once($CFG->dirroot .'/blog/blogpage.php');
-
+    include_once($CFG->dirroot.'/tag/lib.php');
 
     /**
      * Definition of blogcourse page type (blog page with course id present).
         echo $attachedimages;
     /// Links to tags
 
+        /*
         if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti
                                      WHERE t.id = ti.tagid
                                      AND ti.entryid = '.$blogEntry->id)) {
+        */
+        if ($blogtags = get_item_tags('blog', $blogEntry->id)) {
             echo '<div class="tags">';
             if ($blogtags) {
                 print_string('tags');
                 echo ': ';
                 foreach ($blogtags as $key => $blogtag) {
-                    $taglist[] = '<a href="index.php?filtertype='.$filtertype.'&amp;filterselect='.$filterselect.'&amp;tagid='.$blogtag->id.'">'.$blogtag->text.'</a>';
+                    $taglist[] = '<a href="index.php?filtertype='.$filtertype.'&amp;filterselect='.$filterselect.'&amp;tagid='.$blogtag->id.'">'.$blogtag->name.'</a>';
                 }
                 echo implode(', ', $taglist);
             }
         if ($tagid) {
             $tag = $tagid;
         } else if ($tag) {
-            if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tags WHERE text LIKE "'.$tag.'"')) {
+            if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tag WHERE name LIKE "'.$tag.'"')) {
                 $tag = $tagrec->id;
             } else {
                 $tag = -1;    //no records found
         }
 
         if ($tag) {
-            $tagtablesql = $CFG->prefix.'blog_tag_instance bt, ';
-            $tagquerysql = ' AND bt.entryid = p.id AND bt.tagid = '.$tag.' ';
+            $tagtablesql = $CFG->prefix.'tag_instance ti, ';
+            $tagquerysql = ' AND ti.itemid = p.id AND ti.tagid = '.$tag.' ';
         } else {
             $tagtablesql = '';
             $tagquerysql = '';
index 2f133b6a0181f958b1b0c27492d19e707326a9fd..1583fbbaecc0578ce9d1281a40018425a3634e99 100755 (executable)
@@ -33,25 +33,19 @@ switch ($action) {
             $otag = trim(required_param('otag', PARAM_NOTAGS));
             // When adding ofical tag, we see if there's already a personal tag
             // With the same Name, if there is, we just change the type
-            if ($tag = get_record('tags', 'text', $otag)) {
+            if ($tag = tag_by_name ($otag)) {
                 if ($tag->type == 'official') {
                     // official tag already exist
                     $error = get_string('tagalready');
                     break;
                 } else { 
                     $tag->type = 'official';
-                    update_record('tags', $tag);
+                    update_record('tag', $tag);
                     $tagid = $tag->id;
                 }
                 
             } else { // Brand new offical tag
-
-                $tag = new object();
-                $tag->userid = $USER->id;
-                $tag->text   = $otag;
-                $tag->type   = 'official';
-            
-                if (!$tagid = insert_record('tags', $tag)) {
+                if (!$tagid = tag_create($otag, 'official')) {
                     error('Can not create tag!');
                 }
             }
index 98ace3166ac2dbbb3c95e5167e5f1ab99293e8dd..0a6dd5f03c0249c0f4f22d30eff1b8dec0f33f64 100644 (file)
@@ -1789,7 +1789,50 @@ function xmldb_main_upgrade($oldversion=0) {
     /// Launch add field timemodified
         $result = $result && add_field($table, $field);
     }
+    
+    /// migrate all tags table to tag
+    if ($result && $oldversion < 2007082701) {
+        require_once($CFG->dirroot.'/tag/lib.php');  
+        $tagrefs = array(); // $tagrefs[$oldtagid] = $newtagid
+        if ($tags = get_records('tags')) {
+            foreach ($tags as $oldtag) {
+                // if this tag does not exist in tag table yet
+                if (!$newtag = get_record('tag', 'name', tag_normalize($oldtag->text))) {
+                    $itag->name = tag_normalize($oldtag->text);
+                    $itag->rawname = tag_normalize($oldtag->text, false);
+
+                    if ($oldtag->type == 'official') {
+                        $itag->tagtype = $oldtag->type;
+                    } else {
+                        $itag->tagtype = 'default';
+                    }
+                    $itag->userid = $oldtag->userid;
+                    $itag->timemodified = time();
 
+                    if ($idx = insert_record('tag', $itag)) {
+                        $tagrefs[$oldtag->id] = $idx;
+                    }
+                // if this tag is already used by tag table
+                } else {
+                    $tagrefs[$oldtag->id] = $newtag->id;
+                }
+            }
+        }
+        
+        // fetch all the tag instances and migrate them as well
+        if ($blogtags = get_records('blog_tag_instance')) {
+            foreach ($blogtags as $blogtag) {
+                if (!empty($tagrefs[$blogtag->tagid])) {
+                    tag_an_item('blog', $blogtag->entryid, $tagrefs[$blogtag->tagid]);
+                }
+            }
+        }
+
+        $table = new XMLDBTable('tags');
+        drop_table($table);
+        $table = new XMLDBTable('blog_tag_instance');
+        drop_table($table);
+    }
     return $result;
 }
 
index 9b0e658b093d2b406568a269dee30e7ddff1e631..27d1c0a85579573e89e348648ef578fec4154743 100644 (file)
@@ -74,11 +74,14 @@ function tag_delete($tag_names_or_ids_csv) {
 
     //covert all ids to names
     $tag_names_csv = tag_name_from_string($tag_names_or_ids_csv);
-
+    $tag_ids_csv = tag_id_from_string($tag_names_csv);
     //put apostrophes in names
     $tag_names_csv_with_apos = "'" . str_replace(',', "','", $tag_names_csv) . "'";
+    $tag_ids_csv_with_apos = "'" . str_replace(',', "','", $tag_ids_csv) . "'";
 
-    delete_records_select('tag',"name IN ($tag_names_csv_with_apos)");
+    // tag instances needs to be deleted as well
+    delete_records_select('tag_instance',"tagid IN ($tag_ids_csv_with_apos)");
+    return delete_records_select('tag',"name IN ($tag_names_csv_with_apos)");
 
 }
 
@@ -396,13 +399,14 @@ function tag_an_item($item_type, $item_id, $tag_names_or_ids_csv, $tag_type="def
 
         $tag_instance->tagid = $tag_id;
         $tag_instance->ordering = $ordering[$tag_normalized_name];
-
+        $tag_instance->timemodified = time();
         $tag_instance_exists = get_record('tag_instance', 'tagid', $tag_id, 'itemtype', $item_type, 'itemid', $item_id);
 
         if (!$tag_instance_exists) {
             insert_record('tag_instance',$tag_instance);
         }
         else {
+            $tag_instance_exists->timemodified = time();
             $tag_instance_exists->ordering = $ordering[$tag_normalized_name];
             update_record('tag_instance',$tag_instance_exists);
         }
index 3bd8b3d405c351e9a35a67d541e0c87f7473f615..07b88e304c8c0dc3ab95eeac37a57a42f691b06d 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2007082700;  // YYYYMMDD = date
+    $version = 2007082701;  // YYYYMMDD = date
                             //       XY = increments within a single day
 
     $release = '1.9 Beta +';   // Human-friendly version name