From f9b827d86ed1719bdd8364741ea79d0ca03c7b63 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Fri, 15 Jan 2010 17:28:14 +0000 Subject: [PATCH] MDL-19937 workshop: migration of comments (no grading) strategy --- mod/workshop/form/comments/db/install.php | 47 +++++++ mod/workshop/form/comments/db/upgradelib.php | 134 +++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 mod/workshop/form/comments/db/install.php create mode 100644 mod/workshop/form/comments/db/upgradelib.php diff --git a/mod/workshop/form/comments/db/install.php b/mod/workshop/form/comments/db/install.php new file mode 100644 index 00000000000..fa3f09df29b --- /dev/null +++ b/mod/workshop/form/comments/db/install.php @@ -0,0 +1,47 @@ +. + +/** + * This file replaces the legacy STATEMENTS section in db/install.xml, + * lib.php/modulename_install() post installation hook and partially defaults.php + * + * @package mod-workshop + * @copyright 2010 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Post installation procedure + */ +function xmldb_workshopform_comments_install() { + global $CFG, $DB; + require_once(dirname(__FILE__) . '/upgradelib.php'); + + // upgrade from old workshop 1.x if needed + workshopform_comments_upgrade_legacy(); +} + +/** + * Post installation procedure recovery + */ +function xmldb_workshopform_comments_install_recovery() { + global $CFG, $DB; + require_once(dirname(__FILE__) . '/upgradelib.php'); + + // continue upgrading from old workshop 1.x if needed + workshopform_comments_upgrade_legacy(); +} diff --git a/mod/workshop/form/comments/db/upgradelib.php b/mod/workshop/form/comments/db/upgradelib.php new file mode 100644 index 00000000000..8939b37dff5 --- /dev/null +++ b/mod/workshop/form/comments/db/upgradelib.php @@ -0,0 +1,134 @@ +. + +/** + * Functions used by some stages of comments-only grading upgrade + * + * @package workshopform_comments + * @copyright 2010 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Check if there are some legacy workshop 1.x data to be migrated and upgrade them + * + * This must be called after workshop core migration has finished so that + * all assessments are already upgraded and tables are correctly renamed. + */ +function workshopform_comments_upgrade_legacy() { + global $CFG, $DB, $OUTPUT; + require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php'); + + if (!workshopform_comments_upgrade_legacy_needed()) { + return; + } + + // get the list of all legacy workshops using this grading strategy + if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 0), 'course,id', 'id')) { + echo $OUTPUT->notification('Copying assessment forms elements', 'notifysuccess'); + $legacyworkshops = array_keys($legacyworkshops); + // get the list of all form elements + list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED); + $sql = "SELECT * + FROM {workshop_elements_old} + WHERE workshopid $workshopids + AND newid IS NULL"; + $rs = $DB->get_recordset_sql($sql, $params); + $newworkshopids = workshop_upgrade_workshop_id_mappings(); + foreach ($rs as $old) { + $new = workshopform_comments_upgrade_element($old, $newworkshopids[$old->workshopid]); + $newid = $DB->insert_record('workshopform_comments', $new); + $DB->set_field('workshop_elements_old', 'newplugin', 'comments', array('id' => $old->id)); + $DB->set_field('workshop_elements_old', 'newid', $newid, array('id' => $old->id)); + } + $rs->close(); + + // now we need to reload the legacy element ids + $newelementids = workshop_upgrade_element_id_mappings('comments'); + + // migrate all comments for these elements (it est the values that reviewers put into forms) + echo $OUTPUT->notification('Copying assessment form comments', 'notifysuccess'); + $sql = "SELECT * + FROM {workshop_grades_old} + WHERE workshopid $workshopids + AND newid IS NULL"; + $rs = $DB->get_recordset_sql($sql, $params); + $newassessmentids = workshop_upgrade_assessment_id_mappings(); + foreach ($rs as $old) { + if (!isset($newelementids[$old->workshopid]) or !isset($newelementids[$old->workshopid][$old->elementno])) { + // orphaned comment - the assessment form element has been removed after the grade was recorded + continue; + } + $new = workshopform_comments_upgrade_grade($old, $newassessmentids[$old->assessmentid], + $newelementids[$old->workshopid][$old->elementno]); + $newid = $DB->insert_record('workshop_grades', $new); + $DB->set_field('workshop_grades_old', 'newplugin', 'comments', array('id' => $old->id)); + $DB->set_field('workshop_grades_old', 'newid', $newid, array('id' => $old->id)); + } + $rs->close(); + } +} + +/** + * Transforms a given record from workshop_elements_old into an object to be saved into workshopform_comments + * + * @param stdclass $old legacy record from workshop_elements_old + * @param int $newworkshopid id of the new workshop instance that replaced the previous one + * @return stdclass to be saved in workshopform_comments + */ +function workshopform_comments_upgrade_element(stdclass $old, $newworkshopid) { + $new = new stdclass(); + $new->workshopid = $newworkshopid; + $new->sort = $old->elementno; + $new->description = $old->description; + $new->descriptionformat = FORMAT_HTML; + return $new; +} + +/** + * Transforms given grade record + * + * @param stdclass $old + * @param int $newassessmentid + * @param stdclass $newdimensioninfo + * @return stdclass + */ +function workshopform_comments_upgrade_grade(stdclass $old, $newassessmentid, stdclass $newdimensioninfo) { + $new = new stdclass(); + $new->assessmentid = $newassessmentid; + $new->strategy = 'comments'; + $new->dimensionid = $newdimensioninfo->newid; + $new->grade = 100.00000; + $new->peercomment = $old->feedback; + $new->peercommentformat = FORMAT_HTML; + return $new; +} + +/** + * Check if the the migration from legacy workshop 1.9 is needed + * + * @return bool + */ +function workshopform_comments_upgrade_legacy_needed() { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + if (!($dbman->table_exists('workshop_elements_old') and $dbman->table_exists('workshop_grades_old'))) { + return false; + } + return true; +}