From 3223cc95c6cc0caee87046f9afd14def52fc220a Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Thu, 22 Jul 2010 00:23:52 +0000 Subject: [PATCH] MDL-21432 backup - restore sections --- backup/moodle2/restore_final_task.class.php | 2 + backup/moodle2/restore_section_task.class.php | 13 +++- backup/moodle2/restore_stepslib.php | 65 ++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/backup/moodle2/restore_final_task.class.php b/backup/moodle2/restore_final_task.class.php index d0a958c3a0e..71fa38a682d 100644 --- a/backup/moodle2/restore_final_task.class.php +++ b/backup/moodle2/restore_final_task.class.php @@ -35,6 +35,8 @@ class restore_final_task extends restore_task { */ public function build() { + // TODO: Gradebook + // TODO: interlinks // TODO: Apply all the block_positions accumulated along the process // Clean the temp dir (conditionally) and drop temp table diff --git a/backup/moodle2/restore_section_task.class.php b/backup/moodle2/restore_section_task.class.php index 3249a716b87..962f17112db 100644 --- a/backup/moodle2/restore_section_task.class.php +++ b/backup/moodle2/restore_section_task.class.php @@ -31,6 +31,7 @@ class restore_section_task extends restore_task { protected $info; // info related to section gathered from backup file + protected $contextid; // course context id /** * Constructor - instantiates one object of this class @@ -48,12 +49,22 @@ class restore_section_task extends restore_task { return $this->get_basepath() . '/sections/section_' . $this->info->sectionid; } + public function get_contextid() { + return $this->contextid; + } + /** * Create all the steps that will be part of this task */ public function build() { - // TODO: Link all the section steps here + // Define the task contextid (the course one) + $this->contextid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; + + // Executed conditionally if restoring to new course or deleting or if overwrite_conf setting is enabled + if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) { + $this->add_step(new restore_section_structure_step('course_info', 'section.xml')); + } // At the end, mark it as built $this->built = true; diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 137861030fa..e8b5eebb38d 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -453,8 +453,71 @@ class restore_outcomes_structure_step extends restore_structure_step { } } +/** + * Structure step that will read the section.xml creating/updating sections + * as needed, rebuilding course cache and other friends + */ +class restore_section_structure_step extends restore_structure_step { -/* + protected function define_structure() { + return array(new restore_path_element('section', '/section')); + } + + public function process_section($data) { + global $DB; + $data = (object)$data; + $oldid = $data->id; // We'll need this later + + $restorefiles = false; + + // Look for the section + $section = new stdclass(); + $section->course = $this->get_courseid(); + $section->section = $data->number; + // Section doesn't exist, create it with all the info from backup + if (!$secrec = $DB->get_record('course_sections', (array)$section)) { + $section->name = $data->name; + $section->summary = $data->summary; + $section->summaryformat = $data->summaryformat; + $section->sequence = ''; + $section->visible = $data->visible; + $newitemid = $DB->insert_record('course_sections', $section); + $restorefiles = true; + + // Section exists, update non-empty information + } else { + $section->id = $secrec->id; + if (empty($secrec->name)) { + $section->name = $data->name; + } + if (empty($secrec->summary)) { + $section->summary = $data->summary; + $section->summaryformat = $data->summaryformat; + $restorefiles = true; + } + $DB->update_record('course_sections', $section); + $newitemid = $secrec->id; + } + + // Annotate the section mapping, with restorefiles option if needed + $this->set_mapping('course_section', $oldid, $newitemid, $restorefiles); + + // If needed, adjust course->numsections + if ($numsections = $DB->get_field('course', 'numsections', array('id' => $this->get_courseid()))) { + if ($numsections < $section->section) { + $DB->set_field('course', 'numsections', $section->section, array('id' => $this->get_courseid())); + } + } + } + + protected function after_execute() { + // Add section related files, with 'course_section' itemid to match + $this->add_related_files('course', 'section', 'course_section'); + } +} + + +/** * Structure step that will read the course.xml file, loading it and performing * various actions depending of the site/restore settings. Note that target * course always exist before arriving here so this step will be updating -- 2.43.0