From bd1f4559f7e4c0ca41c204004c867553e2a40e42 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Mon, 27 Aug 2007 08:46:00 +0000 Subject: [PATCH] MDL-10956, convert blogs to use the same tags (and convert old tags) --- blocks/blog_tags/block_blog_tags.php | 26 +++---- blog/edit.php | 107 ++++++++++++--------------- blog/edit_form.php | 10 ++- blog/header.php | 16 ++-- blog/lib.php | 13 ++-- blog/tags.php | 12 +-- lib/db/upgrade.php | 43 +++++++++++ tag/lib.php | 10 ++- version.php | 2 +- 9 files changed, 133 insertions(+), 106 deletions(-) diff --git a/blocks/blog_tags/block_blog_tags.php b/blocks/blog_tags/block_blog_tags.php index 580cb3f1c3..bcc9dd11ed 100644 --- a/blocks/blog_tags/block_blog_tags.php +++ b/blocks/blog_tags/block_blog_tags.php @@ -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 .= '
  • '. - $tag->text.'
  • '; + $tag->name.' '; } $this->content->text .= "\n\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) { diff --git a/blog/edit.php b/blog/edit.php index 51a45295cb..c3ed2799ab 100755 --- a/blog/edit.php +++ b/blog/edit.php @@ -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); + } } } } diff --git a/blog/edit_form.php b/blog/edit_form.php index b98a46a341..9160ffbb1b 100644 --- a/blog/edit_form.php +++ b/blog/edit_form.php @@ -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'); } } diff --git a/blog/header.php b/blog/header.php index c12f8ed4c4..3e770e66d2 100755 --- a/blog/header.php +++ b/blog/header.php @@ -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&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&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&filtertype=user&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&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()); diff --git a/blog/lib.php b/blog/lib.php index b77f8c1bd7..7c5b164fe6 100755 --- a/blog/lib.php +++ b/blog/lib.php @@ -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). @@ -222,15 +222,18 @@ 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 '
    '; if ($blogtags) { print_string('tags'); echo ': '; foreach ($blogtags as $key => $blogtag) { - $taglist[] = ''.$blogtag->text.''; + $taglist[] = ''.$blogtag->name.''; } echo implode(', ', $taglist); } @@ -488,7 +491,7 @@ 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 @@ -521,8 +524,8 @@ } 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 = ''; diff --git a/blog/tags.php b/blog/tags.php index 2f133b6a01..1583fbbaec 100755 --- a/blog/tags.php +++ b/blog/tags.php @@ -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!'); } } diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 98ace3166a..0a6dd5f03c 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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; } diff --git a/tag/lib.php b/tag/lib.php index 9b0e658b09..27d1c0a855 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -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); } diff --git a/version.php b/version.php index 3bd8b3d405..07b88e304c 100644 --- a/version.php +++ b/version.php @@ -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 -- 2.39.5