Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

gradebook backup and restore fixes

  • Loading branch information...
commit 0afbd23c9eadc3fb1310930cafc22a94caa4902f 1 parent 88e794d
toyomoyo authored

Showing 1 changed file with 35 additions and 11 deletions. Show diff stats Hide diff stats

  1. +35 11 backup/restorelib.php
46 backup/restorelib.php
@@ -1195,14 +1195,14 @@ function restore_create_gradebook($restore,$xml_file) {
1195 1195 // Flag to mark if we must continue
1196 1196 $continue = true;
1197 1197
1198   - //Process categories
  1198 + // Process categories
1199 1199 if ($categoriescount && $continue) {
1200 1200 if (!defined('RESTORE_SILENTLY')) {
1201 1201 echo '<li>'.get_string('gradecategories','grades').'</li>';
1202 1202 }
1203 1203 $counter = 0;
1204 1204 while ($counter < $categoriescount) {
1205   - //Fetch recordset_size records in each iteration
  1205 + // Fetch recordset_size records in each iteration
1206 1206 $recs = get_records_select("backup_ids","table_name = 'grade_categories' AND backup_code = '$restore->backup_unique_code'",
1207 1207 "old_id",
1208 1208 "old_id, old_id",
@@ -1210,18 +1210,24 @@ function restore_create_gradebook($restore,$xml_file) {
1210 1210 $recordset_size);
1211 1211 if ($recs) {
1212 1212 foreach ($recs as $rec) {
1213   - //Get the full record from backup_ids
  1213 + // Get the full record from backup_ids
1214 1214 $data = backup_getid($restore->backup_unique_code,'grade_categories',$rec->old_id);
1215 1215 if ($data) {
1216   - //Now get completed xmlized object
  1216 + // Now get completed xmlized object
1217 1217 $info = $data->info;
1218 1218 //traverse_xmlize($info); //Debug
1219 1219 //print_object ($GLOBALS['traverse_array']); //Debug
1220 1220 //$GLOBALS['traverse_array']=""; //Debug
1221 1221 //Now build the GRADE_PREFERENCES record structure
1222 1222
1223   - $dbrec->courseid = $restore->course_id;
1224   - $dbrec->parent = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
  1223 + $dbrec->courseid = $restore->course_id;
  1224 +
  1225 + // get the new grade category parent
  1226 + if (!empty($info['GRADE_CATEGORY']['#']['PARENT']['0']['#'])) {
  1227 + $parent = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
  1228 + $dbrec->parent = $parent->new_id;
  1229 + }
  1230 +
1225 1231 $dbrec->fullname = backup_todb($info['GRADE_CATEGORY']['#']['FULLNAME']['0']['#']);
1226 1232 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#']);
1227 1233 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#']);
@@ -1232,9 +1238,22 @@ function restore_create_gradebook($restore,$xml_file) {
1232 1238 //if the fullname doesn't exist
1233 1239 if (!$prerec = get_record('grade_categories','courseid',$dbrec->courseid,'fullname',$dbrec->fullname)) {
1234 1240 $newid = insert_record('grade_categories',$dbrec);
1235   - $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->oldid,$newid);
  1241 + $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$newid);
  1242 + // update this record so we can put in the right paths
  1243 + // this can only be done after we got the new id
  1244 + $dbrec->id = $newid;
  1245 + include_once($CFG->dirroot.'/lib/grade/grade_category.php');
  1246 + // rebuild the path, we need only parents info
  1247 + // the order of restoring should ensure that the parent and grandparent(s)
  1248 + // are already restored
  1249 + $dbrec->path = grade_category::build_path($dbrec);
  1250 + // this is not needed in the xml because
  1251 + // given this parent and grandparent(s) we can recalculate the depth
  1252 + $dbrec->depth = grade_category::get_depth_from_path($dbrec->path);
  1253 + update_record('grade_categories', $dbrec);
1236 1254 } else {
1237   - $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->oldid,$rec->oldid);
  1255 + // if fullname already exists, we should keep the current grade category
  1256 + $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$rec->oldid);
1238 1257 }
1239 1258 }
1240 1259 //Increment counters
@@ -1344,7 +1363,9 @@ function restore_create_gradebook($restore,$xml_file) {
1344 1363 $dbrec->courseid = $restore->course_id;
1345 1364
1346 1365 if (!empty($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'])) {
1347   - $dbrec->categoryid = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
  1366 +
  1367 + $category = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
  1368 + $dbrec->categoryid = $category->new_id;
1348 1369 }
1349 1370
1350 1371 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#']);
@@ -1369,11 +1390,14 @@ function restore_create_gradebook($restore,$xml_file) {
1369 1390 }
1370 1391
1371 1392 // iteminstance should point to new mod
1372   - $dbrec->iteminstance = backup_getid($restore->backup_unique_code,'course_modules', $iteminstance);
  1393 +
  1394 + $cm = backup_getid($restore->backup_unique_code,'course_modules', $iteminstance);
  1395 + $dbrec->iteminstance = $cm->new_id;
1373 1396
1374 1397 } else if ($dbrec->itemtype == 'category') {
1375 1398 // the item instance should point to the new grade category
1376   - $dbrec->iteminstance = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
  1399 + $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
  1400 + $dbrec->iteminstance = $category->new_id;
1377 1401 }
1378 1402
1379 1403 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);

0 comments on commit 0afbd23

Please sign in to comment.
Something went wrong with that request. Please try again.