backup will not back up grade categories and grades if some grade items are not backe...
authortoyomoyo <toyomoyo>
Wed, 13 Jun 2007 09:06:44 +0000 (09:06 +0000)
committertoyomoyo <toyomoyo>
Wed, 13 Jun 2007 09:06:44 +0000 (09:06 +0000)
backup/backuplib.php
backup/restorelib.php

index 4d7c5489ed16717be5a8103260b67aa15eb2c094..0e5ff1839e89b70e44e1d2bba92cc03776881899 100644 (file)
 
         $status = true;
 
+        // see if ALL grade items of type mod of this course are being backed up
+        // if not, we do not need to backup grade category and associated grade items/grades
+        $backupall = true;        
+        
+        if ($grade_items = get_records_sql("SELECT * FROM {$CFG->prefix}grade_items 
+                                            WHERE courseid = $preferences->backup_course
+                                            ORDER BY sortorder ASC")) { 
+            foreach ($grade_items as $grade_item) {
+
+                // do not restore if this grade_item is a mod, and 
+                if ($grade_item->itemtype == 'mod') {
+
+                    // get module information
+                    // if no user data selected, we skip this grade_item
+                    if (!backup_userdata_selected($preferences,$grade_item->itemmodule,$grade_item->iteminstance)) {
+                        //print_object($grade_item);
+                        $backupall = false;
+                        break;
+                    }
+                }
+            }
+        }
+        
         //Gradebook header
         fwrite ($bf,start_tag("GRADEBOOK",2,true));
+        
+        // Now backup grade_item (inside grade_category)
+        if ($backupall) {
+            $status = backup_gradebook_category_info($bf,$preferences);
+        }
+        $status = backup_gradebook_item_info($bf,$preferences, $backupall);
+        $status = backup_gradebook_outcomes_info($bf, $preferences);
+        // back up grade outcomes
 
+        //Gradebook footer
+        $status = fwrite ($bf,end_tag("GRADEBOOK",2,true));
+        return $status;
+    }
+
+
+    function backup_gradebook_category_info($bf,$preferences) {
+        global $CFG;
+        
+        $status = true;
+        
         //Output grade_category
         
         // getting grade categories, but make sure parents come before children
             $status = fwrite ($bf,end_tag("GRADE_CATEGORIES",3,true));
         }
         
-        // Now backup grade_item (inside grade_category)
-        $status = backup_gradebook_item_info($bf,$preferences);
-        $status = backup_gradebook_outcomes_info($bf, $preferences);
-        // back up grade outcomes
-
-        //Gradebook footer
-        $status = fwrite ($bf,end_tag("GRADEBOOK",2,true));
         return $status;
+      
     }
 
+
     //Backup gradebook_item (called from backup_gradebook_info
-    function backup_gradebook_item_info($bf,$preferences) {
+    function backup_gradebook_item_info($bf,$preferences, $backupall) {
 
         global $CFG;
 
                 // do not restore if this grade_item is a mod, and 
                 if ($grade_item->itemtype == 'mod') {
 
-                    // get module information
-                    $mod = get_record('course_modules', 'id', $grade_item->iteminstance);
-                    $modt = get_record('modules', 'id', $mod->module);
-
                     // if no user data selected, we skip this grade_item
-                    if (!backup_userdata_selected($preferences,$modt->name,$mod->id)) {
+                    if (!backup_userdata_selected($preferences,$grade_item->itemmodule,$grade_item->iteminstance)) {
                         continue;
                     }
+                } else if ($grade_item->itemtype == 'category') {
+                    // if not all grade items are being backed up
+                    // we ignore this type of grade_item and grades associated
+                    if (!$backupall) {
+                        continue;      
+                    }            
                 }
 
                 //Begin grade_item
index d860243da355bb5bc65f5525bc3478f1ceb46e6f..d0b20f14ebbe34363f069d98469943118802021a 100644 (file)
                             // Yu: This part is called repeatedly for every instance, 
                             // so it is necessary to set the granular flag and check isset() 
                             // when the first instance of this type of mod is processed.
-                            if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
-                                // This defines whether we want to restore specific
-                                // instances of the modules (granular restore), or
-                                // whether we don't care and just want to restore
-                                // all module instances (non-granular).
-                                $restore->mods[$mod->type]->granular = true;
-                            } else {
-                                $restore->mods[$mod->type]->granular = false;
+                            
+                            //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
+                            
+                            if (!isset($restore->mods[$mod->type]->granular)) {
+                                if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {                              
+                                    // This defines whether we want to restore specific
+                                    // instances of the modules (granular restore), or
+                                    // whether we don't care and just want to restore
+                                    // all module instances (non-granular).
+                                    $restore->mods[$mod->type]->granular = true;
+                                } else {
+                                    $restore->mods[$mod->type]->granular = false;
+                                }
                             }
                           
                             //Check if we've to restore this module (and instance) 
         $categoriescount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories');
         $itemscount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items');
         $outcomecount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes');
-    
+   
+        // we need to know if all grade items that were backed up are being restored
+        // if that is not the case, we do not restore grade categories nor gradeitems of category type
+        // i.e. the aggregated grades of that category
+        
+        $restoreall = true; // set to false if any grade_item is not selected/restored
+        
+        if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'", "old_id", "old_id, old_id")) {
+            foreach ($recs as $rec) {
+              
+              
+                if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) { 
+
+                    $info = $data->info;
+                    // do not restore if this grade_item is a mod, and 
+                    $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
+                    
+                    
+                    if ($itemtype == 'mod') {
+
+                        $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);               
+                        $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
+                        if (!restore_userdata_selected($restore, $itemmodule, $iteminstance)) {
+                            // module instance not selected when restored using granular
+                            // we are not restoring all grade items, set flag to false
+                            // so that we do not process grade categories and related grade items/grades
+                            $restoreall = false;
+                            break;
+                        }
+                    }
+                }
+             }
+        }
+   
         // return if nothing to restore
         if (!$itemscount && !$categoriescount && !outcomecount) {
             return $status;  
         }
-            
+
         // Start ul
         if (!defined('RESTORE_SILENTLY')) {
             echo '<ul>';
         $continue = true;
 
         // Process categories
-        if ($categoriescount && $continue) {
+        if ($categoriescount && $continue && $restoreall) {
             if (!defined('RESTORE_SILENTLY')) {
                 echo '<li>'.get_string('gradecategories','grades').'</li>';
             }
                             
                             // do not restore if this grade_item is a mod, and 
                             if ($dbrec->itemtype == 'mod') {
-                                
-                                // get the old mod
-                                $mod = get_record('course_modules', 'id', $iteminstance);
-                                $modt = get_record('modules', 'id', $mod->module);
 
-                                if (!restore_userdata_selected($restore, $modt->name, $mod->id)) {
+                                if (!restore_userdata_selected($restore,  $dbrec->itemmodule, $iteminstance)) {
                                     // module instance not selected when restored using granular
                                     // skip this item
                                     $counteritems++;
 
                             } else if ($dbrec->itemtype == 'category') {
                                 // the item instance should point to the new grade category
-                                $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance); 
-                                $dbrec->iteminstance = $category->new_id;
+                                
+                                // only proceed if we are restoring all grade items
+                                if ($restoreall) {
+                                    $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance); 
+                                    $dbrec->iteminstance = $category->new_id;
+                                } else {
+                                    // otherwise we can safely ignore this grade item and subsequent 
+                                    // grade_raws, grade_finals etc
+                                    continue;  
+                                }
                             }
                             
                             $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);
             return array_key_exists($modid,$restore->mods[$modname]->instances) 
                 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
         }
+        
+        print_object($restore->mods[$modname]);
         return !empty($restore->mods[$modname]->userinfo);
     }