backup MDL-23918 Fixed up wording in the backup and restore stages.
[moodle.git] / backup / moodle2 / restore_section_task.class.php
index c8d9672..454487c 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
@@ -45,7 +46,11 @@ class restore_section_task extends restore_task {
      */
     public function get_taskbasepath() {
 
-        return $this->get_basepath() . '/sections/section_' . $info->sectionid;
+        return $this->get_basepath() . '/sections/section_' . $this->info->sectionid;
+    }
+
+    public function get_contextid() {
+        return $this->contextid;
     }
 
     /**
@@ -53,12 +58,78 @@ class restore_section_task extends restore_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;
     }
 
+    /**
+     * Exceptionally override the execute method, so, based in the section_included setting, we are able
+     * to skip the execution of one task completely
+     */
+    public function execute() {
+
+        // Find activity_included_setting
+        if (!$this->get_setting_value('included')) {
+            $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
+
+        } else { // Setting tells us it's ok to execute
+            parent::execute();
+        }
+    }
+
+    /**
+     * Specialisation that, first of all, looks for the setting within
+     * the task with the the prefix added and later, delegates to parent
+     * without adding anything
+     */
+    public function get_setting($name) {
+        $namewithprefix = 'section_' . $this->info->sectionid . '_' . $name;
+        $result = null;
+        foreach ($this->settings as $key => $setting) {
+            if ($setting->get_name() == $namewithprefix) {
+                if ($result != null) {
+                    throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
+                } else {
+                    $result = $setting;
+                }
+            }
+        }
+        if ($result) {
+            return $result;
+        } else {
+            // Fallback to parent
+            return parent::get_setting($name);
+        }
+    }
+
+    /**
+     * Define the contents in the course that must be
+     * processed by the link decoder
+     */
+    static public function define_decode_contents() {
+        $contents = array();
+
+        $contents[] = new restore_decode_content('course_sections', 'summary', 'course_section');
+
+        return $contents;
+    }
+
+    /**
+     * Define the decoding rules for links belonging
+     * to the sections to be executed by the link decoder
+     */
+    static public function define_decode_rules() {
+        return array();
+    }
+
 // Protected API starts here
 
     /**
@@ -74,6 +145,12 @@ class restore_section_task extends restore_task {
         // Define section_included (to decide if the whole task must be really executed)
         $settingname = $settingprefix . 'included';
         $section_included = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
+        if (is_number($this->info->title)) {
+            $label = get_string('includesection', 'backup', $this->info->title);
+        } else {
+            $label = $this->info->title;
+        }
+        $section_included->get_ui()->set_label($label);
         $this->add_setting($section_included);
 
         // Define section_userinfo. Dependent of:
@@ -82,7 +159,7 @@ class restore_section_task extends restore_task {
         $settingname = $settingprefix . 'userinfo';
         $selectvalues = array(0=>get_string('no')); // Safer options
         $defaultvalue = false;                      // Safer default
-        if (isset($info->settings[$settingname]) && $info->settings[$settingname]) { // Only enabled when available
+        if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
             $defaultvalue = true;
         }