Exemple #1
0
/**
 * 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_accumulative_upgrade_legacy() {
    global $CFG, $DB, $OUTPUT;
    require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php');

    if (!workshopform_accumulative_upgrade_legacy_needed()) {
        return;
    }

    // get the list of all legacy workshops using this grading strategy
    if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 1), '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);
        // prepare system (global) scales to replace the legacy in-built ones
        $newscaleids = workshopform_accumulative_upgrade_scales();
        foreach ($rs as $old) {
            $new = workshopform_accumulative_upgrade_element($old, $newscaleids, $old->workshopid);
            $newid = $DB->insert_record('workshopform_accumulative', $new);
            $DB->set_field('workshop_elements_old', 'newplugin', 'accumulative', 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 ids. Although we have them in $newelements after the first run, we must
        // refetch them from DB so that this function can be called during recovery
        $newelementids = workshop_upgrade_element_id_mappings('accumulative');

        // migrate all grades for these elements (it est the values that reviewers put into forms)
        echo $OUTPUT->notification('Copying assessment form grades', '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($newassessmentids[$old->assessmentid])) {
                // orphaned grade - the assessment was removed but the grade remained
                continue;
            }
            if (!isset($newelementids[$old->workshopid]) or !isset($newelementids[$old->workshopid][$old->elementno])) {
                // orphaned grade - the assessment form element has been removed after the grade was recorded
                continue;
            }
            $new = workshopform_accumulative_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', 'accumulative', array('id' => $old->id));
            $DB->set_field('workshop_grades_old', 'newid', $newid, array('id' => $old->id));
        }
        $rs->close();
    }
}
 /**
  * Converts <ELEMENT> into <workshopform_accumulative_dimension>
  */
 public function process_legacy_element(array $data, array $raw)
 {
     // prepare a fake record and re-use the upgrade logic
     $fakerecord = (object) $data;
     $newscaleid = $this->get_new_scaleid($data['scale']);
     $converted = (array) workshopform_accumulative_upgrade_element($fakerecord, $newscaleid, 12345678);
     unset($converted['workshopid']);
     $converted['id'] = $data['id'];
     $this->write_xml('workshopform_accumulative_dimension', $converted, array('/workshopform_accumulative_dimension/id'));
     return $converted;
 }