MDL-21432 backup - allow arbitrary contexts restoring files. original sysctxid + scales

This commit is contained in:
Eloy Lafuente
2010-07-18 18:11:20 +00:00
parent 3a1cccc6d4
commit c0440b3f0e
4 changed files with 146 additions and 66 deletions
@@ -57,6 +57,9 @@ class restore_root_task extends restore_task {
// Unconditionally, load create all the needed groups and groupings
$this->add_step(new restore_groups_structure_step('create_groups_and_groupings', 'groups.xml'));
// Unconditionally, load create all the needed scales
$this->add_step(new restore_scales_structure_step('create_scales', 'scales.xml'));
// At the end, mark it as built
$this->built = true;
}
+129 -62
View File
@@ -43,6 +43,10 @@ class restore_create_and_clean_temp_stuff extends restore_execution_step {
$itemid = $this->task->get_old_contextid();
$newitemid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid);
// Create the old-system-ctxid to new-system-ctxid mapping, we need that available since the beginning
$itemid = $this->task->get_old_system_contextid();
$newitemid = get_context_instance(CONTEXT_SYSTEM)->id;
restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid);
}
}
@@ -174,86 +178,149 @@ class restore_create_included_users extends restore_execution_step {
*/
class restore_groups_structure_step extends restore_structure_step {
protected function define_structure() {
protected function define_structure() {
$paths = array(); // Add paths here
$paths = array(); // Add paths here
$paths[] = new restore_path_element('group', '/groups/group');
if ($this->get_setting_value('users')) {
$paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
}
$paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
$paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
$paths[] = new restore_path_element('group', '/groups/group');
if ($this->get_setting_value('users')) {
$paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
}
$paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
$paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
return $paths;
}
return $paths;
}
// Processing functions go here
public function process_group($data) {
global $DB;
// Processing functions go here
public function process_group($data) {
global $DB;
$data = (object)$data; // handy
$data->courseid = $this->get_courseid();
$data = (object)$data; // handy
$data->courseid = $this->get_courseid();
$oldid = $data->id; // need this saved for later
$oldid = $data->id; // need this saved for later
$restorefiles = false; // Only if we end creating the group
$restorefiles = false; // Only if we end creating the group
// Search if the group already exists (by name & description) in the target course
$description_clause = '';
$params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
if (!empty($data->description)) {
$description_clause = ' AND ' .
$DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc');
$params['desc'] = $data->description;
}
if (!$groupdb = $DB->get_record_sql("SELECT *
FROM {groups}
WHERE courseid = :courseid
AND name = :grname $description_clause", $params)) {
// group doesn't exist, create
$newitemid = $DB->insert_record('groups', $data);
$restorefiles = true; // We'll restore the files
} else {
// group exists, use it
$newitemid = $groupdb->id;
}
// Save the id mapping
$this->set_mapping('group', $oldid, $newitemid, $restorefiles);
}
// Search if the group already exists (by name & description) in the target course
$description_clause = '';
$params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
if (!empty($data->description)) {
$description_clause = ' AND ' .
$DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc');
$params['desc'] = $data->description;
}
if (!$groupdb = $DB->get_record_sql("SELECT *
FROM {groups}
WHERE courseid = :courseid
AND name = :grname $description_clause", $params)) {
// group doesn't exist, create
$newitemid = $DB->insert_record('groups', $data);
$restorefiles = true; // We'll restore the files
} else {
// group exists, use it
$newitemid = $groupdb->id;
}
// Save the id mapping
$this->set_mapping('group', $oldid, $newitemid, $restorefiles);
}
public function process_member($data) {
global $DB;
public function process_member($data) {
global $DB;
$data = (object)$data; // handy
$data = (object)$data; // handy
// get parent group->id
$data->groupid = $this->get_new_parentid('group');
// get parent group->id
$data->groupid = $this->get_new_parentid('group');
// map user newitemid and insert if not member already
if ($data->userid = $this->get_mappingid('user', $data->userid)) {
if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
$DB->insert_record('groups_members', $data);
}
}
}
// map user newitemid and insert if not member already
if ($data->userid = $this->get_mappingid('user', $data->userid)) {
if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
$DB->insert_record('groups_members', $data);
}
}
}
public function process_grouping($data) {
debugging('TODO: Grouping restore not implemented. Detected grouping', DEBUG_DEVELOPER);
}
public function process_grouping($data) {
debugging('TODO: Grouping restore not implemented. Detected grouping', DEBUG_DEVELOPER);
}
public function process_grouping_group($data) {
debugging('TODO: Grouping restore not implemented. Detected grouping group', DEBUG_DEVELOPER);
}
public function process_grouping_group($data) {
debugging('TODO: Grouping restore not implemented. Detected grouping group', DEBUG_DEVELOPER);
}
protected function after_execute() {
// Add group related files, matching with "group" mappings
$this->add_related_files('group', 'icon', 'group');
$this->add_related_files('group', 'description', 'group');
}
protected function after_execute() {
// Add group related files, matching with "group" mappings
$this->add_related_files('group', 'icon', 'group');
$this->add_related_files('group', 'description', 'group');
}
}
/**
* 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 {
protected function define_structure() {
$paths = array(); // Add paths here
$paths[] = new restore_path_element('scale', '/scales_definition/scale');
return $paths;
}
protected function process_scale($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 scale (by 'scale' both in standard (course=0) and current course
// with priority to standard scales (ORDER clause)
// scale is not course unique, use get_record_sql to suppress warning
// Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides
$compare_scale_clause = $DB->sql_compare_text('scale') . ' = ' . $DB->sql_compare_text(':scaledesc');
$params = array('courseid' => $this->get_courseid(), 'scaledesc' => $data->scale);
if (!$scadb = $DB->get_record_sql("SELECT *
FROM {scale}
WHERE courseid IN (0, :courseid)
AND $compare_scale_clause
ORDER BY courseid", $params, IGNORE_MULTIPLE)) {
// Remap the user if possible, defaut to user performing the restore if not
$userid = $this->get_mappingid('user', $data->userid);
$data->userid = $userid ? $userid : $this->get_userid();
// Remap the course if course scale
$data->courseid = $data->courseid ? $this->get_courseid() : 0;
// If global scale (course=0), check the user has perms to create it
// falling to course scale if not
$systemctx = get_context_instance(CONTEXT_SYSTEM);
if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $data->userid)) {
$data->courseid = $this->get_courseid();
}
// scale doesn't exist, create
$newitemid = $DB->insert_record('scale', $data);
$restorefiles = true; // We'll restore the files
} else {
// scale exists, use it
$newitemid = $scadb->id;
}
// Save the id mapping (with files support at system context)
$this->set_mapping('scale', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid());
}
protected function after_execute() {
// Add scales related files, matching with "scale" mappings
$this->add_related_files('grade', 'scale', 'scale', $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
@@ -200,8 +200,13 @@ abstract class restore_structure_step extends restore_step {
* by children. Also will inject the known old context id for the task
* in case it's going to be used for restoring files later
*/
protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false) {
$parentitemid = $restorefiles ? $this->task->get_old_contextid() : null;
protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null) {
// If we haven't specified one context for the files, use the task one
if ($filesctxid == null) {
$parentitemid = $restorefiles ? $this->task->get_old_contextid() : null;
} else { // Use the specified one
$parentitemid = $restorefiles ? $filesctxid : null;
}
// Let's call the low level one
restore_dbops::set_backup_ids_record($this->get_restoreid(), $itemname, $oldid, $newid, $parentitemid);
// Now, if the itemname matches any pathelement->name, store the latest $newid
@@ -244,9 +249,10 @@ abstract class restore_structure_step extends restore_step {
/**
* Add all the existing file, given their component and filearea and one backup_ids itemname to match with
*/
protected function add_related_files($component, $filearea, $mappingitemname) {
protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null) {
$filesctxid = is_null($filesctxid) ? $this->task->get_old_contextid() : $filesctxid;
restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), $component,
$filearea, $this->task->get_old_contextid(), $mappingitemname);
$filearea, $filesctxid, $mappingitemname);
}
/**
+4
View File
@@ -70,6 +70,10 @@ abstract class restore_task extends base_task {
public function get_old_contextid() {
return $this->plan->get_info()->original_course_contextid;
}
public function get_old_system_contextid() {
return $this->plan->get_info()->original_system_contextid;
}
}
/*