Permalink
Browse files

MDL-33133 Conditional section: restore to existing course fix

  • Loading branch information...
1 parent 4f7f2a8 commit 52e9dfc58fb642102df5d2600b15c9d5cea336d3 @sammarshallou sammarshallou committed May 21, 2012
Showing with 21 additions and 8 deletions.
  1. +21 −8 backup/moodle2/restore_stepslib.php
@@ -1110,10 +1110,19 @@ public function process_section($data) {
public function process_availability($data) {
global $DB;
$data = (object)$data;
+
$data->coursesectionid = $this->task->get_sectionid();
+
// NOTE: Other values in $data need updating, but these (cm,
- // grade items) have not yet been restored.
- $DB->insert_record('course_sections_availability', $data);
+ // grade items) have not yet been restored, so are done later.
+
+ $newid = $DB->insert_record('course_sections_availability', $data);
+
+ // We do not need to map between old and new id but storing a mapping
+ // means it gets added to the backup_ids table to record which ones
+ // need updating. The mapping is stored with $newid => $newid for
+ // convenience.
+ $this->set_mapping('course_sections_availability', $newid, $newid);
}
protected function after_execute() {
@@ -1126,16 +1135,20 @@ public function after_restore() {
$sectionid = $this->get_task()->get_sectionid();
- // Get data object for current section availability (if any)
- // TODO: This can be processing already existing records, we need to be able to know which ones
- // are the just restored ones, perhaps creating 'course_sections_availability' mappings for them.
- // TODO: Also, this must avoid duplicates, so if one course module or one grade item already is being
- // used for some availability rule... we need to handle that carefully.
+ // Get data object for current section availability (if any).
$data = $DB->get_record('course_sections_availability',
array('coursesectionid' => $sectionid), 'id, sourcecmid, gradeitemid', IGNORE_MISSING);
- // Update mappings
+ // If it exists, update mappings.
if ($data) {
+ // Only update mappings for entries which are created by this restore.
+ // Otherwise, when you restore to an existing course, it will mess up
+ // existing section availability entries.
+ if (!$this->get_mappingid('course_sections_availability', $data->id, false)) {
+ return;
+ }
+
+ // Update source cmid / grade id to new value.
$data->sourcecmid = $this->get_mappingid('course_module', $data->sourcecmid);
if (!$data->sourcecmid) {
$data->sourcecmid = null;

0 comments on commit 52e9dfc

Please sign in to comment.