From c8730ff028466080e3bf820160816bb5032461e4 Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Tue, 20 Jul 2010 10:36:37 +0000 Subject: [PATCH] MDL-21432 backup - complete standard/course outcomes backup & restore --- backup/moodle2/backup_stepslib.php | 3 + backup/moodle2/restore_root_task.class.php | 3 + backup/moodle2/restore_stepslib.php | 68 +++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index 3daf020548c..ab537fa996a 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -539,6 +539,9 @@ class backup_final_outcomes_structure_step extends backup_structure_step { WHERE bi.backupid = ? AND bi.itemname = 'outcomefinal'", array(backup::VAR_BACKUPID)); + // Annotate outcome files (they store files in system context, so pass it instead of default one) + $outcome->annotate_files('grade', 'outcome', 'id', get_context_instance(CONTEXT_SYSTEM)->id); + // Return main element (outcomesdef) return $outcomesdef; } diff --git a/backup/moodle2/restore_root_task.class.php b/backup/moodle2/restore_root_task.class.php index c2d0f7d60ff..98128fea5f8 100644 --- a/backup/moodle2/restore_root_task.class.php +++ b/backup/moodle2/restore_root_task.class.php @@ -63,6 +63,9 @@ class restore_root_task extends restore_task { // Unconditionally, load create all the needed scales $this->add_step(new restore_scales_structure_step('create_scales', 'scales.xml')); + // Unconditionally, load create all the needed outcomes + $this->add_step(new restore_outcomes_structure_step('create_scales', 'outcomes.xml')); + // Unconditionally, load create all the needed outcomes. // TODO: restore outcomes // $this->add_step(new restore_outcomes_structure_step('create_outcomes', 'outcomes.xml')); diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 04d761bcbb2..2494a6432f0 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -323,7 +323,6 @@ class restore_groups_structure_step extends restore_structure_step { /** * Structure step that will create all the needed scales * by loading them from the scales.xml - * Note group members only will be added if restoring user info */ class restore_scales_structure_step extends restore_structure_step { @@ -383,6 +382,73 @@ class restore_scales_structure_step extends restore_structure_step { } +/** + * Structure step that will create all the needed outocomes + * by loading them from the outcomes.xml + */ +class restore_outcomes_structure_step extends restore_structure_step { + + protected function define_structure() { + + $paths = array(); // Add paths here + $paths[] = new restore_path_element('outcome', '/outcomes_definition/outcome'); + return $paths; + } + + protected function process_outcome($data) { + global $DB; + + $data = (object)$data; + + $restorefiles = false; // Only if we end creating the group + + $oldid = $data->id; // need this saved for later + + // Look for outcome (by shortname both in standard (courseid=null) and current course + // with priority to standard outcomes (ORDER clause) + // outcome is not course unique, use get_record_sql to suppress warning + $params = array('courseid' => $this->get_courseid(), 'shortname' => $data->shortname); + if (!$outdb = $DB->get_record_sql('SELECT * + FROM {grade_outcomes} + WHERE shortname = :shortname + AND (courseid = :courseid OR courseid IS NULL) + ORDER BY COALESCE(courseid, 0)', $params, IGNORE_MULTIPLE)) { + // Remap the user + $userid = $this->get_mappingid('user', $data->usermodified); + $data->usermodified = $userid ? $userid : $this->get_userid(); + // Remap the course if course outcome + $data->courseid = $data->courseid ? $this->get_courseid() : null; + // If global outcome (course=null), check the user has perms to create it + // falling to course outcome if not + $systemctx = get_context_instance(CONTEXT_SYSTEM); + if (is_null($data->courseid) && !has_capability('moodle/grade:manageoutcomes', $systemctx , $data->userid)) { + $data->courseid = $this->get_courseid(); + } + // outcome doesn't exist, create + $newitemid = $DB->insert_record('grade_outcomes', $data); + $restorefiles = true; // We'll restore the files + } else { + // scale exists, use it + $newitemid = $outdb->id; + } + // Set the corresponding grade_outcomes_courses record + $outcourserec = new stdclass(); + $outcourserec->courseid = $this->get_courseid(); + $outcourserec->outcomeid = $newitemid; + if (!$DB->record_exists('grade_outcomes_courses', (array)$outcourserec)) { + $DB->insert_record('grade_outcomes_courses', $outcourserec); + } + // Save the id mapping (with files support at system context) + $this->set_mapping('outcome', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); + } + + protected function after_execute() { + // Add outcomes related files, matching with "outcome" mappings + $this->add_related_files('grade', 'outcome', 'outcome', $this->task->get_old_system_contextid()); + } +} + + /* * Structure step that will read the course.xml file, loading it and performing * various actions depending of the site/restore settings -- 2.43.0