Skip to content

Commit

Permalink
MDL-61614 Quiz: DB schema changes
Browse files Browse the repository at this point in the history
Created a new DB table to store tag data for qiuz slots.
  • Loading branch information
rezaies committed Apr 18, 2018
1 parent 6d4bc5b commit 28e392a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
16 changes: 14 additions & 2 deletions mod/quiz/db/install.xml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/quiz/db" VERSION="20180207" COMMENT="XMLDB file for Moodle mod/quiz"
<XMLDB PATH="mod/quiz/db" VERSION="20180407" COMMENT="XMLDB file for Moodle mod/quiz"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -65,7 +65,6 @@
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Foreign key references question.id."/>
<FIELD NAME="questioncategoryid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The question category that the random question will be picked from. Will be null if and only if the question is not a random question."/>
<FIELD NAME="includingsubcategories" TYPE="int" LENGTH="4" NOTNULL="false" SEQUENCE="false" COMMENT="Whether the random question can be picked from sub categories or not. Will be null if questioncategoryid is null."/>
<FIELD NAME="tags" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Contains data about the tags that a question must have so that it can be selected for this slot. This field is an array in the form of [tagid,tagname] which is stored as JSON. Will be null if questioncategoryid is null."/>
<FIELD NAME="maxmark" TYPE="number" LENGTH="12" NOTNULL="true" DEFAULT="0" SEQUENCE="false" DECIMALS="7" COMMENT="How many marks this question contributes to quiz.sumgrades."/>
</FIELDS>
<KEYS>
Expand Down Expand Up @@ -186,5 +185,18 @@
<INDEX NAME="name" UNIQUE="true" FIELDS="name"/>
</INDEXES>
</TABLE>
<TABLE NAME="quiz_slot_tags" COMMENT="Stores data about the tags that a question must have so that it can be selected for a quiz slot (when having a random question by tags on that slot).">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="slotid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The quiz slot that this tag belong to"/>
<FIELD NAME="tagid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="tagname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="The tag name is to be stored as well, so we won't lose data if the tag is removed from Moodle (A tag with the same name might be added in future)."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="slotid" TYPE="foreign" FIELDS="slotid" REFTABLE="quiz_slots" REFFIELDS="id"/>
<KEY NAME="tagid" TYPE="foreign" FIELDS="tagid" REFTABLE="tag" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
47 changes: 40 additions & 7 deletions mod/quiz/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ function xmldb_quiz_upgrade($oldversion) {
$dbman->add_field($table, $field);
}

// Define field tags to be added to quiz_slots.
$field = new xmldb_field('tags', XMLDB_TYPE_TEXT, null, null, null, null, null, 'includingsubcategories');
// Conditionally launch add field tags.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Quiz savepoint reached.
upgrade_mod_savepoint(true, 2018020700, 'quiz');
}
Expand Down Expand Up @@ -163,5 +156,45 @@ function xmldb_quiz_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2018020701, 'quiz');
}

if ($oldversion < 2018040700) {

// Define field tags to be dropped from quiz_slots. This field was added earlier to master only.
$table = new xmldb_table('quiz_slots');
$field = new xmldb_field('tags');

// Conditionally launch drop field quizid.
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}

// Quiz savepoint reached.
upgrade_mod_savepoint(true, 2018040700, 'quiz');
}

if ($oldversion < 2018040800) {

// Define table quiz_slot_tags to be created.
$table = new xmldb_table('quiz_slot_tags');

// Adding fields to table quiz_slot_tags.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('slotid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('tagid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('tagname', XMLDB_TYPE_CHAR, '255', null, null, null, null);

// Adding keys to table quiz_slot_tags.
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('slotid', XMLDB_KEY_FOREIGN, array('slotid'), 'quiz_slots', array('id'));
$table->add_key('tagid', XMLDB_KEY_FOREIGN, array('tagid'), 'tag', array('id'));

// Conditionally launch create table for quiz_slot_tags.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Quiz savepoint reached.
upgrade_mod_savepoint(true, 2018040800, 'quiz');
}

return true;
}
2 changes: 1 addition & 1 deletion mod/quiz/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2018020701;
$plugin->version = 2018040800;
$plugin->requires = 2017110800;
$plugin->component = 'mod_quiz';
$plugin->cron = 60;

0 comments on commit 28e392a

Please sign in to comment.