diff --git a/backup/backuplib.php b/backup/backuplib.php index 4d7c5489ed1..0e5ff1839e8 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -1363,9 +1363,51 @@ $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 @@ -1399,18 +1441,13 @@ $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; @@ -1431,14 +1468,16 @@ // 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 diff --git a/backup/restorelib.php b/backup/restorelib.php index d860243da35..d0b20f14ebb 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -946,14 +946,19 @@ // 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) @@ -1180,12 +1185,45 @@ $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 '