MDL-22139 backup - Move gradebook.xml generation from course to final task
authorEloy Lafuente <stronk7@moodle.org>
Tue, 13 Jul 2010 09:36:34 +0000 (09:36 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Tue, 13 Jul 2010 09:36:34 +0000 (09:36 +0000)
backup/moodle2/backup_course_task.class.php
backup/moodle2/backup_final_task.class.php
backup/moodle2/backup_stepslib.php

index b3b20f5..1d8a653 100644 (file)
@@ -91,11 +91,6 @@ class backup_course_task extends backup_task {
             $this->add_step(new backup_comments_structure_step('course_comments', 'comments.xml'));
         }
 
-        if (backup_plan_dbops::require_gradebook_backup($this->get_courseid(), $this->get_backupid())) {
-            $this->add_step(new backup_gradebook_structure_step('course_gradebook','gradebook.xml'));
-        }
-
-
         // Generate the logs file (conditionally)
         if ($this->get_setting_value('logs')) {
             //$this->add_step(new backup_course_logs_structure_step('course_logs', 'logs.xml'));
index bdfaabe..1b4fb1f 100644 (file)
@@ -40,6 +40,10 @@ class backup_final_task extends backup_task {
         $coursectxid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
         $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid));
 
+        // Set the backup::VAR_COURSEID setting to course, we'll need that in some steps
+        $courseid = $this->get_courseid();
+        $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $courseid));
+
         // Generate the groups file with the final annotated groups and groupings
         // including membership based on setting
         $this->add_step(new backup_groups_structure_step('groups', 'groups.xml'));
@@ -75,6 +79,10 @@ class backup_final_task extends backup_task {
         // This must be executed before backup files
         $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
 
+        // Generate the gradebook file with categories and course grade items. Do it conditionally, using
+        // execute_condition() so only will be excuted if ALL module grade_items in course have been exported
+        $this->add_step(new backup_gradebook_structure_step('course_gradebook','gradebook.xml'));
+
         // Generate the files.xml file with all the (final) annotated files. At the same
         // time copy all the files from moodle storage to backup storage (uses custom
         // backup_nested_element for that)
index adc0b41..fcc054f 100644 (file)
@@ -620,6 +620,15 @@ class backup_comments_structure_step extends backup_structure_step {
  */
 class backup_gradebook_structure_step extends backup_structure_step {
 
+    /**
+     * We need to decide conditionally, based on dynamic information
+     * about the execution of this step. Only will be executed if all
+     * the module gradeitems have been already included in backup
+     */
+    protected function execute_condition() {
+        return backup_plan_dbops::require_gradebook_backup($this->get_courseid(), $this->get_backupid());
+    }
+
     protected function define_structure() {
 
         // are we including user info?
@@ -650,8 +659,8 @@ class backup_gradebook_structure_step extends backup_structure_step {
 
         //grade_categories
         $grade_categories = new backup_nested_element('grade_categories');
-        $grade_category   = new backup_nested_element('grade_category', null, array('courseid', 
-                'parent', 'depth', 'path', 'fullname', 'aggregation', 'keephigh', 
+        $grade_category   = new backup_nested_element('grade_category', null, array('courseid',
+                'parent', 'depth', 'path', 'fullname', 'aggregation', 'keephigh',
                 'dropload', 'aggregateonlygraded', 'aggregateoutcomes', 'aggregatesubcats',
                 'timecreated', 'timemodified'));
 
@@ -679,7 +688,7 @@ class backup_gradebook_structure_step extends backup_structure_step {
 
         //if itemtype == manual then item is a calculated item so isn't attached to an activity and we need to back it up here
         $grade_items_array = grade_item::fetch_all(array('itemtype' => 'manual', 'courseid' => $this->get_courseid()));
-        
+
         //$grade_items_array==false and not an empty array if no items. set_source_array() fails if you pass a bool
         if ($grade_items_array) {
             $grade_item->set_source_array($grade_items_array);