From b876ab8098c2fe7c71e6db2747847b0c6709b3a7 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Mon, 4 Jan 2010 18:32:03 +0000 Subject: [PATCH] MDL-19937 workshop migration - initial versions of upgrade scripts --- mod/workshop/db/upgrade.php | 90 +++++++++++++++ mod/workshop/db/upgradelib.php | 200 +++++++++++++++++++++++++++++++++ 2 files changed, 290 insertions(+) create mode 100644 mod/workshop/db/upgrade.php create mode 100644 mod/workshop/db/upgradelib.php diff --git a/mod/workshop/db/upgrade.php b/mod/workshop/db/upgrade.php new file mode 100644 index 0000000000..9160a9233f --- /dev/null +++ b/mod/workshop/db/upgrade.php @@ -0,0 +1,90 @@ +. + +/** + * Keeps track of upgrades to the workshop module + * + * @package mod-workshop + * @copyright 2009 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Performs upgrade of the database structure and data + * + * Workshop supports upgrades from version 1.9.0 and higher only. During 1.9 > 2.0 upgrade, + * there are significant database changes. + * + * @param int $oldversion the version we are upgrading from + * @return bool result + */ +function xmldb_workshop_upgrade($oldversion) { + global $CFG, $DB, $OUTPUT; + + $dbman = $DB->get_manager(); + $result = true; + + /** + * Upgrading from workshop 1.9.x - big things going to happen now... + * The migration procedure is divided into smaller chunks using incremental + * versions 2009102900, 2009102901, 2009102902 etc. The day zero of the new + * workshop 2.0 is version 2009103000 since when the upgrade code is maintained. + */ + + /** + * Migration from 1.9 - step 1 - rename old tables + */ + if ($result && $oldversion < 2009102901) { + echo $OUTPUT->notification('Renaming old workshop module tables', 'notifysuccess'); + foreach (array('workshop', 'workshop_elements', 'workshop_rubrics', 'workshop_submissions', 'workshop_assessments', + 'workshop_grades', 'workshop_comments', 'workshop_stockcomments') as $tableorig) { + $tablearchive = $tableorig . '_old'; + if ($dbman->table_exists($tableorig)) { + $dbman->rename_table(new XMLDBTable($tableorig), $tablearchive); + } + // append a new field 'newid' in every archived table. null value means the record was not migrated yet + $table = new xmldb_table($tablearchive); + $field = new xmldb_field('newid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + } + upgrade_mod_savepoint($result, 2009102901, 'workshop'); + } + + /** + * Migration from 1.9 - step 2 - create new workshop core tables + */ + if ($result && $oldversion < 2009102902) { + require_once(dirname(__FILE__) . '/upgradelib.php'); + echo $OUTPUT->notification('Preparing new workshop module tables', 'notifysuccess'); + workshop_upgrade_prepare_20_tables(); + upgrade_mod_savepoint($result, 2009102902, 'workshop'); + } + + /** + * Migration from 1.9 - step 3 - migrate workshop instances + */ + if ($result && $oldversion < 2009102903) { + require_once(dirname(__FILE__) . '/upgradelib.php'); + echo $OUTPUT->notification('Copying workshop core data', 'notifysuccess'); + workshop_upgrade_copy_instances(); + upgrade_mod_savepoint($result, 2009102903, 'workshop'); + } + + return $result; +} diff --git a/mod/workshop/db/upgradelib.php b/mod/workshop/db/upgradelib.php new file mode 100644 index 0000000000..9e07e35c80 --- /dev/null +++ b/mod/workshop/db/upgradelib.php @@ -0,0 +1,200 @@ +. + +/** + * Functions used by some stages in workshop db/upgrade.php + * + * @package mod-workshop + * @copyright 2009 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Prepares the inital workshop 2.0 core tables + */ +function workshop_upgrade_prepare_20_tables() { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + if (!$dbman->table_exists('workshop')) { + $table = new xmldb_table('workshop'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); // id in 1.9 table + $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('intro', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('introformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('instructauthors', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('instructauthorsformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('instructreviewers', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('instructreviewersformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('phase', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('useexamples', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('usepeerassessment', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('useselfassessment', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, '80'); + $table->add_field('gradinggrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, '20'); + $table->add_field('strategy', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null); + $table->add_field('gradedecimals', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('nattachments', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('latesubmissions', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '100000'); + $table->add_field('examplesmode', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('submissionstart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('submissionend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('assessmentstart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('assessmentend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('course_fk', XMLDB_KEY_FOREIGN, array('course'), 'course', array('id')); + $dbman->create_table($table); + } + + if (!$dbman->table_exists('workshop_submissions')) { + $table = new xmldb_table('workshop_submissions'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('workshopid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('example', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('authorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('contentformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('contenttrust', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('attachment', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null); + $table->add_field('gradeover', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null); + $table->add_field('gradeoverby', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null); + $table->add_field('feedbackauthor', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('feedbackauthorformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('timegraded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('workshop_fk', XMLDB_KEY_FOREIGN, array('workshopid'), 'workshop', array('id')); + $table->add_key('overriddenby_fk', XMLDB_KEY_FOREIGN, array('gradeoverby'), 'user', array('id')); + $table->add_key('author_fk', XMLDB_KEY_FOREIGN, array('authorid'), 'user', array('id')); + $dbman->create_table($table); + } + + if (!$dbman->table_exists('workshop_assessments')) { + $table = new xmldb_table('workshop_assessments'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('submissionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('reviewerid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null); + $table->add_field('gradinggrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null); + $table->add_field('gradinggradeover', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null); + $table->add_field('gradinggradeoverby', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null); + $table->add_field('feedbackauthor', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('feedbackauthorformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('feedbackreviewer', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('feedbackreviewerformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('submission_fk', XMLDB_KEY_FOREIGN, array('submissionid'), 'workshop_submissions', array('id')); + $table->add_key('overriddenby_fk', XMLDB_KEY_FOREIGN, array('gradinggradeoverby'), 'user', array('id')); + $table->add_key('reviewer_fk', XMLDB_KEY_FOREIGN, array('reviewerid'), 'user', array('id')); + $dbman->create_table($table); + } + + if (!$dbman->table_exists('workshop_grades')) { + $table = new xmldb_table('workshop_grades'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('assessmentid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('strategy', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null); + $table->add_field('dimensionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('peercomment', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('peercommentformat', XMLDB_TYPE_INTEGER, '3', null, null, null, '0'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('assessment_fk', XMLDB_KEY_FOREIGN, array('assessmentid'), 'workshop_assessments', array('id')); + $table->add_key('formfield_uk', XMLDB_KEY_UNIQUE, array('assessmentid', 'strategy', 'dimensionid')); + $dbman->create_table($table); + } + + if (!$dbman->table_exists('workshop_aggregations')) { + $table = new xmldb_table('workshop_aggregations'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('workshopid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('gradinggrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null); + $table->add_field('timegraded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('workshop_fk', XMLDB_KEY_FOREIGN, array('workshopid'), 'workshop', array('id')); + $table->add_key('user_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); + $table->add_key('workshopuser', XMLDB_KEY_UNIQUE, array('workshopid', 'userid')); + $dbman->create_table($table); + } +} + +/** + * Copies the records from workshop_old into workshop table, keeping the original id in oldid column + * + * @return void + */ +function workshop_upgrade_copy_instances() { + global $CFG, $DB, $OUTPUT; + + upgrade_set_timeout(); + $moduleid = $DB->get_field('modules', 'id', array('name' => 'workshop'), MUST_EXIST); + $rs = $DB->get_recordset_select('workshop_old', 'newid IS NULL'); + foreach ($rs as $old) { + $new = new stdClass(); + $new->oldid = $old->id; + $new->course = $old->course; + $new->name = $old->name; + $new->intro = $old->description; + $new->introformat = FORMAT_HTML; + $new->nattachments = $old->nattachments; + $new->maxbytes = $old->maxbytes; + $new->grade = $old->grade; + $new->gradinggrade = $old->gradinggrade; + $new->phase = 50; // defined in locallib.php as const workshop::PHASE_CLOSED + $new->timemodified = time(); + if ($old->ntassessments > 0) { + $new->useexamples = 1; + } else { + $new->useexamples = 0; + } + $new->usepeerassessment = 1; + $new->useselfassessment = $old->includeself; + switch ($old->gradingstrategy) { + case 0: + $new->strategy = 'comments'; // 'notgraded' + break; + case 1: + $new->strategy = 'accumulative'; // 'accumulative' + break; + case 2: + $new->strategy = 'numerrors'; // 'errorbanded' + break; + case 3: + $new->strategy = 'rubric'; // 'criterion' + break; + case 4: + $new->strategy = 'rubric'; // 'rubric' + break; + } + $newid = $DB->insert_record('workshop', $new, true, true); + $DB->set_field('course_modules', 'instance', $newid, array('module' => $moduleid, 'instance' => $old->id)); + $DB->set_field('workshop_old', 'newid', $newid, array('id' => $old->id)); + } + $rs->close(); +} -- 2.39.5