diff --git a/lib/db/install.xml b/lib/db/install.xml
index e5ab4c80799..5f186240f3f 100644
--- a/lib/db/install.xml
+++ b/lib/db/install.xml
@@ -2057,15 +2057,19 @@
+
+
+
+
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php
index a81720d2791..0ccbbc907f5 100644
--- a/lib/db/upgrade.php
+++ b/lib/db/upgrade.php
@@ -3183,5 +3183,102 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2014031400.04);
}
+ if ($oldversion < 2014032000.01) {
+ // Add new fields to the 'tag_instance' table.
+ $table = new xmldb_table('tag_instance');
+ $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, false, null, null, 'tagid');
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', null, false, null, '0', 'itemid');
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'ordering');
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ $sql = "UPDATE {tag_instance}
+ SET timecreated = timemodified";
+ $DB->execute($sql);
+
+ // Update all the course tags.
+ $sql = "UPDATE {tag_instance}
+ SET component = 'core',
+ contextid = (SELECT ctx.id
+ FROM {context} ctx
+ WHERE ctx.contextlevel = :contextlevel
+ AND ctx.instanceid = {tag_instance}.itemid)
+ WHERE itemtype = 'course'";
+ $DB->execute($sql, array('contextlevel' => CONTEXT_COURSE));
+
+ // Update all the user tags.
+ $sql = "UPDATE {tag_instance}
+ SET component = 'core',
+ contextid = (SELECT ctx.id
+ FROM {context} ctx
+ WHERE ctx.contextlevel = :contextlevel
+ AND ctx.instanceid = {tag_instance}.itemid)
+ WHERE itemtype = 'user'";
+ $DB->execute($sql, array('contextlevel' => CONTEXT_USER));
+
+ // Update all the blog post tags.
+ $sql = "UPDATE {tag_instance}
+ SET component = 'core',
+ contextid = (SELECT ctx.id
+ FROM {context} ctx
+ JOIN {post} p
+ ON p.userid = ctx.instanceid
+ WHERE ctx.contextlevel = :contextlevel
+ AND p.id = {tag_instance}.itemid)
+ WHERE itemtype = 'post'";
+ $DB->execute($sql, array('contextlevel' => CONTEXT_USER));
+
+ // Update all the wiki page tags.
+ $sql = "UPDATE {tag_instance}
+ SET component = 'mod_wiki',
+ contextid = (SELECT ctx.id
+ FROM {context} ctx
+ JOIN {course_modules} cm
+ ON cm.id = ctx.instanceid
+ JOIN {modules} m
+ ON m.id = cm.module
+ JOIN {wiki} w
+ ON w.id = cm.instance
+ JOIN {wiki_subwikis} sw
+ ON sw.wikiid = w.id
+ JOIN {wiki_pages} wp
+ ON wp.subwikiid = sw.id
+ WHERE m.name = 'wiki'
+ AND ctx.contextlevel = :contextlevel
+ AND wp.id = {tag_instance}.itemid)
+ WHERE itemtype = 'wiki_pages'";
+ $DB->execute($sql, array('contextlevel' => CONTEXT_MODULE));
+
+ // Update all the question tags.
+ $sql = "UPDATE {tag_instance}
+ SET component = 'core_question',
+ contextid = (SELECT qc.contextid
+ FROM {question} q
+ JOIN {question_categories} qc
+ ON q.category = qc.id
+ WHERE q.id = {tag_instance}.itemid)
+ WHERE itemtype = 'question'";
+ $DB->execute($sql);
+
+ // Update all the tag tags.
+ $sql = "UPDATE {tag_instance}
+ SET component = 'core',
+ contextid = :systemcontext
+ WHERE itemtype = 'tag'";
+ $DB->execute($sql, array('systemcontext' => context_system::instance()->id));
+
+ // Main savepoint reached.
+ upgrade_main_savepoint(true, 2014032000.01);
+ }
+
return true;
}
diff --git a/version.php b/version.php
index 8404826b6ac..8477a5dc23c 100644
--- a/version.php
+++ b/version.php
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2014032000.00; // YYYYMMDD = weekly release date of this DEV branch.
+$version = 2014032000.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.