MDL-21432 backup - complete standard/course outcomes backup & restore

This commit is contained in:
Eloy Lafuente 2010-07-20 10:36:37 +00:00
parent 6969b70aa5
commit c8730ff028
3 changed files with 73 additions and 1 deletions

View file

@ -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;
}

View file

@ -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'));

View file

@ -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