MDL-21432 backup - restore course/tags/rolenames/files
authorEloy Lafuente <stronk7@moodle.org>
Wed, 21 Jul 2010 15:52:03 +0000 (15:52 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Wed, 21 Jul 2010 15:52:03 +0000 (15:52 +0000)
backup/moodle2/backup_stepslib.php
backup/moodle2/restore_course_task.class.php
backup/moodle2/restore_final_task.class.php
backup/moodle2/restore_root_task.class.php
backup/moodle2/restore_stepslib.php

index 994546c..9ec6969 100644 (file)
@@ -269,7 +269,7 @@ class backup_course_structure_step extends backup_structure_step {
             'defaultgroupingid', 'lang', 'theme',
             'timecreated', 'timemodified',
             'requested', 'restrictmodules',
-            'enablecompletion'));
+            'enablecompletion', 'completionstartonenrol', 'completionnotify'));
 
         $category = new backup_nested_element('category', array('id'), array(
             'name', 'description'));
index 4aaedce..eccc25f 100644 (file)
@@ -54,7 +54,7 @@ class restore_course_task extends restore_task {
     public function build() {
 
         // TODO: Link all the course steps here
-        // Executed conditionally if restoring to new course or if overwrite_conf setting is enabled
+        // 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_course_structure_step('course_info', 'course.xml'));
         }
index 6af9cae..d0a958c 100644 (file)
@@ -35,6 +35,8 @@ class restore_final_task extends restore_task {
      */
     public function build() {
 
+        // TODO: Apply all the block_positions accumulated along the process
+
         // Clean the temp dir (conditionally) and drop temp table
         $this->add_step(new restore_drop_and_clean_temp_stuff('drop_and_clean_temp_stuff'));
 
index 98128fe..5c3012b 100644 (file)
@@ -66,10 +66,6 @@ class restore_root_task extends restore_task {
         // Unconditionally, load create all the needed outcomes
         $this->add_step(new restore_outcomes_structure_step('create_scales', 'outcomes.xml'));
 
-        // Unconditionally, load create all the needed outcomes.
-        // TODO: restore outcomes
-        // $this->add_step(new restore_outcomes_structure_step('create_outcomes', 'outcomes.xml'));
-
         // At the end, mark it as built
         $this->built = true;
     }
index 59e012f..47c52c7 100644 (file)
@@ -456,7 +456,9 @@ class restore_outcomes_structure_step extends restore_structure_step {
 
 /*
  * Structure step that will read the course.xml file, loading it and performing
- * various actions depending of the site/restore settings
+ * various actions depending of the site/restore settings. Note that target
+ * course always exist before arriving here so this step will be updating
+ * the course record (never inserting)
  */
 class restore_course_structure_step extends restore_structure_step {
 
@@ -472,8 +474,87 @@ class restore_course_structure_step extends restore_structure_step {
 
     // Processing functions go here
     public function process_course($data) {
-        // TODO: don't forget to remap defaultgroupingid
-        print_object('stopped before processing course. Continue here');
+        global $CFG, $DB;
+
+        $data = (object)$data;
+        $coursetags = isset($data->tags['tag']) ? $data->tags['tag'] : array();
+        $coursemodules = isset($data->allowed_modules['module']) ? $data->allowed_modules['module'] : array();
+        $oldid = $data->id; // We'll need this later
+        debugging ('review the these lines of process_course() to change to settings once available', DEBUG_DEVELOPER);
+        // TODO: Get fullname, shortname, startdate and category from settings
+        // $fullname  = $this->get_setting_value('course_fullname');
+        // $shortname = $this->get_setting_value('course_shortname');
+        // $category  = $this->get_setting_value('course_category');
+        // $startdate = $this->get_setting_value('course_startdate');
+        // TODO: Delete this lines once we are getting the vars above from settings
+        $fullname = $this->task->get_info()->original_course_fullname;
+        $shortname= $this->task->get_info()->original_course_shortname;
+        $startdate= $this->task->get_info()->original_course_startdate;
+        $category = get_course_category()->id;
+
+        // Calculate final course names, to avoid dupes
+        list($fullname, $shortname) = restore_dbops::calculate_course_names($this->get_courseid(), $fullname, $shortname);
+
+        // Need to change some fields before updating the course record
+        $data->id = $this->get_courseid();
+        $data->fullname = $fullname;
+        $data->shortname= $shortname;
+        $data->idnumber = '';
+        $data->category = $category;
+        $data->startdate= $this->apply_date_offset($data->startdate);
+        if ($data->defaultgroupingid) {
+            $data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid);
+        }
+        if (empty($CFG->enablecompletion) || !$this->get_setting_value('userscompletion')) {
+            $data->enablecompletion = 0;
+            $data->completionstartonenrol = 0;
+            $data->completionnotify = 0;
+        }
+        $languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search
+        if (!array_key_exists($data->lang, $languages)) {
+            $data->lang = '';
+        }
+        $themes = get_list_of_themes(); // Get themes for quick search later
+        if (!in_array($data->theme, $themes) || empty($CFG->allowcoursethemes)) {
+            $data->theme = '';
+        }
+
+        // Course record ready, update it
+        $DB->update_record('course', $data);
+
+        // Course tags
+        if (!empty($CFG->usetags) && isset($coursetags)) { // if enabled in server and present in backup
+            $tags = array();
+            foreach ($coursetags as $coursetag) {
+                $coursetag = (object)$coursetag;
+                $tags[] = $coursetag->rawname;
+            }
+            tag_set('course', $this->get_courseid(), $tags);
+        }
+        // Course allowed modules
+        if (!empty($data->restrictmodules) && !empty($coursemodules)) {
+            $available = get_plugin_list('mod');
+            foreach ($coursemodules as $coursemodule) {
+                $mname = $coursemodule['modulename'];
+                if (array_key_exists($mname, $available)) {
+                    if ($module = $DB->get_record('modules', array('name' => $mname, 'visible' => 1))) {
+                        $rec = new stdclass();
+                        $rec->course = $this->get_courseid();
+                        $rec->module = $module->id;
+                        if (!$DB->record_exists('course_allowed_modules', (array)$rec)) {
+                            $DB->insert_record('course_allowed_modules', $rec);
+                        }
+                    }
+                }
+            }
+        }
+        // Role name aliases
+        restore_dbops::set_course_role_names($this->get_restoreid(), $this->get_courseid());
     }
 
+    protected function after_execute() {
+        // Add course related files, without itemid to match
+        $this->add_related_files('course', 'summary', null);
+        $this->add_related_files('course', 'legacy', null);
+    }
 }