MDL-44316 core: updated the 'tag_instance' table to store the component, contextid and timecreated

This commit is contained in:
Mark Nelson 2014-02-20 20:15:00 -08:00
parent e0c8619823
commit 8dfc4d9c58
3 changed files with 102 additions and 1 deletions

View File

@ -2057,15 +2057,19 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="tagid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="Defines the Moodle component which the tag was added to"/>
<FIELD NAME="itemtype" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The context id of the item that was tagged"/>
<FIELD NAME="tiuserid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="ordering" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Maintains the order of the tag instances of an item"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="timemodified"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="tagid" TYPE="foreign" FIELDS="tagid" REFTABLE="tag" REFFIELDS="id"/>
<KEY NAME="contextid" TYPE="foreign" FIELDS="contextid" REFTABLE="context" REFFIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="itemtype-itemid-tagid-tiuserid" UNIQUE="true" FIELDS="itemtype, itemid, tagid, tiuserid"/>

View File

@ -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;
}

View File

@ -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.