MDL-21432 backup - restore sections
authorEloy Lafuente <stronk7@moodle.org>
Thu, 22 Jul 2010 00:23:52 +0000 (00:23 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Thu, 22 Jul 2010 00:23:52 +0000 (00:23 +0000)
backup/moodle2/restore_final_task.class.php
backup/moodle2/restore_section_task.class.php
backup/moodle2/restore_stepslib.php

index d0a958c..71fa38a 100644 (file)
@@ -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
index 3249a71..962f171 100644 (file)
@@ -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;
index 1378610..e8b5eeb 100644 (file)
@@ -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