function test_grade_category_build_path()
 {
     $grade_category = new grade_category($this->grade_categories[1]);
     $this->assertTrue(method_exists($grade_category, 'build_path'));
     $path = grade_category::build_path($grade_category);
     $this->assertEqual($grade_category->path, $path);
 }
Exemple #2
0
 /**
  * put all activity grade items in the correct grade category and mark all for recalculation
  */
 protected function after_execute()
 {
     global $DB;
     $conditions = array('backupid' => $this->get_restoreid(), 'itemname' => 'grade_item');
     $rs = $DB->get_recordset('backup_ids_temp', $conditions);
     // We need this for calculation magic later on.
     $mappings = array();
     if (!empty($rs)) {
         foreach ($rs as $grade_item_backup) {
             // Store the oldid with the new id.
             $mappings[$grade_item_backup->itemid] = $grade_item_backup->newitemid;
             $updateobj = new stdclass();
             $updateobj->id = $grade_item_backup->newitemid;
             //if this is an activity grade item that needs to be put back in its correct category
             if (!empty($grade_item_backup->parentitemid)) {
                 $oldcategoryid = $this->get_mappingid('grade_category', $grade_item_backup->parentitemid, null);
                 if (!is_null($oldcategoryid)) {
                     $updateobj->categoryid = $oldcategoryid;
                     $DB->update_record('grade_items', $updateobj);
                 }
             } else {
                 //mark course and category items as needing to be recalculated
                 $updateobj->needsupdate = 1;
                 $DB->update_record('grade_items', $updateobj);
             }
         }
     }
     $rs->close();
     // We need to update the calculations for calculated grade items that may reference old
     // grade item ids using ##gi\d+##.
     // $mappings can be empty, use 0 if so (won't match ever)
     list($sql, $params) = $DB->get_in_or_equal(array_values($mappings), SQL_PARAMS_NAMED, 'param', true, 0);
     $sql = "SELECT gi.id, gi.calculation\n                  FROM {grade_items} gi\n                 WHERE gi.id {$sql} AND\n                       calculation IS NOT NULL";
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $gradeitem) {
         // Collect all of the used grade item id references
         if (preg_match_all('/##gi(\\d+)##/', $gradeitem->calculation, $matches) < 1) {
             // This calculation doesn't reference any other grade items... EASY!
             continue;
         }
         // For this next bit we are going to do the replacement of id's in two steps:
         // 1. We will replace all old id references with a special mapping reference.
         // 2. We will replace all mapping references with id's
         // Why do we do this?
         // Because there potentially there will be an overlap of ids within the query and we
         // we substitute the wrong id.. safest way around this is the two step system
         $calculationmap = array();
         $mapcount = 0;
         foreach ($matches[1] as $match) {
             // Check that the old id is known to us, if not it was broken to begin with and will
             // continue to be broken.
             if (!array_key_exists($match, $mappings)) {
                 continue;
             }
             // Our special mapping key
             $mapping = '##MAPPING' . $mapcount . '##';
             // The old id that exists within the calculation now
             $oldid = '##gi' . $match . '##';
             // The new id that we want to replace the old one with.
             $newid = '##gi' . $mappings[$match] . '##';
             // Replace in the special mapping key
             $gradeitem->calculation = str_replace($oldid, $mapping, $gradeitem->calculation);
             // And record the mapping
             $calculationmap[$mapping] = $newid;
             $mapcount++;
         }
         // Iterate all special mappings for this calculation and replace in the new id's
         foreach ($calculationmap as $mapping => $newid) {
             $gradeitem->calculation = str_replace($mapping, $newid, $gradeitem->calculation);
         }
         // Update the calculation now that its being remapped
         $DB->update_record('grade_items', $gradeitem);
     }
     $rs->close();
     // Need to correct the grade category path and parent
     $conditions = array('courseid' => $this->get_courseid());
     $rs = $DB->get_recordset('grade_categories', $conditions);
     // Get all the parents correct first as grade_category::build_path() loads category parents from the DB
     foreach ($rs as $gc) {
         if (!empty($gc->parent)) {
             $grade_category = new stdClass();
             $grade_category->id = $gc->id;
             $grade_category->parent = $this->get_mappingid('grade_category', $gc->parent);
             $DB->update_record('grade_categories', $grade_category);
         }
     }
     $rs->close();
     // Now we can rebuild all the paths
     $rs = $DB->get_recordset('grade_categories', $conditions);
     foreach ($rs as $gc) {
         $grade_category = new stdClass();
         $grade_category->id = $gc->id;
         $grade_category->path = grade_category::build_path($gc);
         $grade_category->depth = substr_count($grade_category->path, '/') - 1;
         $DB->update_record('grade_categories', $grade_category);
     }
     $rs->close();
     // Restore marks items as needing update. Update everything now.
     grade_regrade_final_grades($this->get_courseid());
 }
Exemple #3
0
 /**
  * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
  *
  * @param string $source from where was the object updated (mod/forum, manual, etc.)
  * @return bool success
  */
 public function update($source = null)
 {
     // load the grade item or create a new one
     $this->load_grade_item();
     // force recalculation of path;
     if (empty($this->path)) {
         $this->path = grade_category::build_path($this);
         $this->depth = substr_count($this->path, '/') - 1;
         $updatechildren = true;
     } else {
         $updatechildren = false;
     }
     $this->apply_forced_settings();
     // these are exclusive
     if ($this->droplow > 0) {
         $this->keephigh = 0;
     } else {
         if ($this->keephigh > 0) {
             $this->droplow = 0;
         }
     }
     // Recalculate grades if needed
     if ($this->qualifies_for_regrading()) {
         $this->force_regrading();
     }
     $this->timemodified = time();
     $result = parent::update($source);
     // now update paths in all child categories
     if ($result and $updatechildren) {
         if ($children = grade_category::fetch_all(array('parent' => $this->id))) {
             foreach ($children as $child) {
                 $child->path = null;
                 $child->depth = 0;
                 $child->update($source);
             }
         }
     }
     return $result;
 }
Exemple #4
0
    protected function after_execute() {
        global $DB;

        $conditions = array(
            'backupid' => $this->get_restoreid(),
            'itemname' => 'grade_item'//,
            //'itemid'   => $itemid
        );
        $rs = $DB->get_recordset('backup_ids_temp', $conditions);

        if (!empty($rs)) {
            foreach($rs as $grade_item_backup) {
                $updateobj = new stdclass();
                $updateobj->id = $grade_item_backup->newitemid;

                //if this is an activity grade item that needs to be put back in its correct category
                if (!empty($grade_item_backup->parentitemid)) {
                    $updateobj->categoryid = $this->get_mappingid('grade_category', $grade_item_backup->parentitemid);
                } else {
                    //mark course and category items as needing to be recalculated
                    $updateobj->needsupdate=1;
                }
                $DB->update_record('grade_items', $updateobj);
            }
        }
        $rs->close();

        //need to correct the grade category path and parent
        $conditions = array(
            'courseid' => $this->get_courseid()
        );
        $grade_category = new stdclass();

        $rs = $DB->get_recordset('grade_categories', $conditions);
        if (!empty($rs)) {
            //get all the parents correct first as grade_category::build_path() loads category parents from the DB
            foreach($rs as $gc) {
                if (!empty($gc->parent)) {
                    $grade_category->id = $gc->id;
                    $grade_category->parent = $this->get_mappingid('grade_category', $gc->parent);
                    $DB->update_record('grade_categories', $grade_category);
                }
            }
        }
        if (isset($grade_category->parent)) {
            unset($grade_category->parent);
        }
        $rs->close();

        $rs = $DB->get_recordset('grade_categories', $conditions);
        if (!empty($rs)) {
            //now we can rebuild all the paths
            foreach($rs as $gc) {
                $grade_category->id = $gc->id;
                $grade_category->path = grade_category::build_path($gc);
                $DB->update_record('grade_categories', $grade_category);
            }
        }
        $rs->close();

        //Restore marks items as needing update. Update everything now.
        grade_regrade_final_grades($this->get_courseid());
    }
Exemple #5
0
 /**
  * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
  * @param string $source from where was the object updated (mod/forum, manual, etc.)
  * @return boolean success
  */
 function update($source = null)
 {
     // load the grade item or create a new one
     $this->load_grade_item();
     // force recalculation of path;
     if (empty($this->path)) {
         $this->path = grade_category::build_path($this);
         $this->depth = substr_count($this->path, '/') - 1;
     }
     $this->apply_forced_settings();
     // these are exclusive
     if ($this->droplow > 0) {
         $this->keephigh = 0;
     } else {
         if ($this->keephigh > 0) {
             $this->droplow = 0;
         }
     }
     // Recalculate grades if needed
     if ($this->qualifies_for_regrading()) {
         $this->force_regrading();
     }
     $this->timemodified = time();
     return parent::update($source);
 }
function restore_create_gradebook($restore, $xml_file)
{
    global $CFG, $db, $SESSION;
    $status = true;
    //Check it exists
    if (!file_exists($xml_file)) {
        return false;
    }
    // Get info from xml
    // info will contain the number of record to process
    $info = restore_read_xml_gradebook($restore, $xml_file);
    // If we have info, then process
    if ($info <= 0) {
        return $status;
    }
    // Count how many we have
    $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');
    $outcomescoursescount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_courses');
    $gchcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
    $gghcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
    $gihcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history');
    $gohcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history');
    // 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 or course 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")) {
        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>';
    }
    // fetch the course grade item
    require_once $CFG->libdir . '/grade/grade_item.php';
    require_once $CFG->libdir . '/grade/grade_category.php';
    require_once $CFG->libdir . '/gradelib.php';
    $courseitem = grade_item::fetch_course_item($restore->course_id);
    $coursecategory = grade_category::fetch_course_category($restore->course_id);
    // Number of records to get in every chunk
    $recordset_size = 2;
    // Flag to mark if we must continue
    $continue = true;
    // Process categories
    if ($categoriescount && $continue && $restoreall) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradecategories', 'grades') . '</li>';
        }
        $counter = 0;
        while ($counter < $categoriescount) {
            // Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_categories' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    // Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_categories', $rec->old_id);
                    if ($data) {
                        // Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        //Now build the GRADE_PREFERENCES record structure
                        $dbrec->courseid = $restore->course_id;
                        // categories are not backed up during import.
                        // however, depth 1 categories needs to be skipped during restore into exisiting course
                        // get the new grade category parent
                        //if (!empty($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']) && $info['GRADE_CATEGORY']['#']['PARENT']['0']['#'] != '$@NULL@$') {
                        $parent = backup_getid($restore->backup_unique_code, 'grade_categories', backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
                        if (isset($parent->new_id)) {
                            $dbrec->parent = $parent->new_id;
                        } else {
                            // orphans should get adopted by course category
                            $dbrec->parent = $coursecategory->id;
                        }
                        //}
                        $dbrec->fullname = backup_todb($info['GRADE_CATEGORY']['#']['FULLNAME']['0']['#']);
                        $dbrec->aggregation = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#']);
                        $dbrec->keephigh = backup_todb($info['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#']);
                        $dbrec->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROPLOW']['0']['#']);
                        $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
                        //Structure is equal to db, insert record
                        //if the fullname doesn't exist
                        if (!($prerec = get_record('grade_categories', 'courseid', $dbrec->courseid, 'fullname', $dbrec->fullname))) {
                            $newid = insert_record('grade_categories', $dbrec);
                            $status = backup_putid($restore->backup_unique_code, 'grade_categories', $rec->old_id, $newid);
                            // update this record so we can put in the right paths
                            // this can only be done after we got the new id
                            $dbrec->id = $newid;
                            include_once $CFG->dirroot . '/lib/grade/grade_category.php';
                            // rebuild the path, we need only parents info
                            // the order of restoring should ensure that the parent and grandparent(s)
                            // are already restored
                            $dbrec->path = grade_category::build_path($dbrec);
                            // this is not needed in the xml because
                            // given this parent and grandparent(s) we can recalculate the depth
                            $dbrec->depth = substr_count($dbrec->path, '/');
                            update_record('grade_categories', $dbrec);
                        } else {
                            // if fullname already exists, we should keep the current grade category
                            $status = backup_putid($restore->backup_unique_code, 'grade_categories', $rec->old_id, $rec->oldid);
                        }
                    }
                    //Increment counters
                    $counter++;
                    //Do some output
                    if ($counter % 1 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 20 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
            }
        }
    }
    // process outcomes
    if ($outcomecount && $continue) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeoutcomes', 'grades') . '</li>';
        }
        $counter = 0;
        while ($counter < $outcomecount) {
            //Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_outcomes' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    //Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_outcomes', $rec->old_id);
                    if ($data) {
                        //Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        //Now build the GRADE_PREFERENCES record structure
                        if ($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']) {
                            $dbrec->courseid = $restore->course_id;
                        } else {
                            $dbrec->courseid = NULL;
                        }
                        $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
                        $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#']);
                        if ($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']) {
                            $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']));
                            $dbrec->scaleid = $scale->new_id;
                        }
                        $modifier = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#']));
                        $dbrec->usermodified = $modifier->new_id;
                        // Structure is equal to db, insert record
                        // If the shortname doesn't exist
                        if (empty($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#'])) {
                            $prerec = get_record_sql("SELECT * FROM {$CFG->prefix}grade_outcomes\n                                                                   WHERE courseid IS NULL\n                                                                   AND shortname = '{$dbrec->shortname}'");
                        } else {
                            $prerec = get_record('grade_outcomes', 'courseid', $restore->course_id, 'shortname', $dbrec->shortname);
                        }
                        if (!$prerec) {
                            $newid = insert_record('grade_outcomes', $dbrec);
                        } else {
                            $newid = $prerec->id;
                        }
                        if ($newid) {
                            backup_putid($restore->backup_unique_code, "grade_outcomes", $rec->old_id, $newid);
                        }
                    }
                    //Increment counters
                    $counter++;
                    //Do some output
                    if ($counter % 1 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 20 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
            }
        }
    }
    // process outcomescourses
    if ($outcomescoursescount && $continue) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeoutcomescourses', 'grades') . '</li>';
        }
        $counter = 0;
        while ($counter < $outcomescoursescount) {
            //Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_outcomes_courses' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    //Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_outcomes_courses', $rec->old_id);
                    if ($data) {
                        //Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        $oldoutcomesid = backup_todb($info['GRADE_OUTCOMES_COURSE']['#']['OUTCOMEID']['0']['#']);
                        $newoutcome = backup_getid($restore->backup_unique_code, "grade_outcomes", $oldoutcomesid);
                        unset($dbrec);
                        $dbrec->courseid = $restore->course_id;
                        $dbrec->outcomeid = $newoutcome->new_id;
                        insert_record('grade_outcomes_courses', $dbrec);
                    }
                    //Increment counters
                    $counter++;
                    //Do some output
                    if ($counter % 1 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 20 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
            }
        }
    }
    // Process grade items (grade_grade)
    if ($itemscount && $continue) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeitems', 'grades') . '</li>';
        }
        $counter = 0;
        $counteritems = 0;
        while ($counteritems < $itemscount) {
            //Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_items' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counteritems, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    //Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_items', $rec->old_id);
                    if ($data) {
                        //Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        $dbrec->courseid = $restore->course_id;
                        if (isset($SESSION->restore->importing)) {
                            // if we are importing, points all grade_items to the course category
                            $coursecat = get_record('grade_categories', 'courseid', $restore->course_id, 'depth', 1);
                            $dbrec->categoryid = $coursecat->id;
                        } else {
                            if (!empty($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']) && $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'] != '$@NULL@$') {
                                $category = backup_getid($restore->backup_unique_code, 'grade_categories', backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
                                if ($category->new_id) {
                                    $dbrec->categoryid = $category->new_id;
                                } else {
                                    // this could be restoring into existing course, and grade item points to the old course grade item (category)
                                    // which was never imported. In this case we just point them to the new course item
                                    $dbrec->categoryid = $coursecategory->id;
                                }
                            }
                        }
                        $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#']);
                        $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
                        $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
                        /// this needs to point to either the new mod id
                        /// or the category id
                        $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
                        // do not restore if this grade_item is a mod, and
                        if ($dbrec->itemtype == 'mod') {
                            // iteminstance should point to new mod
                            $mod = backup_getid($restore->backup_unique_code, $dbrec->itemmodule, $iteminstance);
                            $dbrec->iteminstance = $mod->new_id;
                        } else {
                            if ($dbrec->itemtype == 'category') {
                                // the item instance should point to the new grade category
                                // only proceed if we are restoring all grade items
                                // need to skip for imports
                                if ($restoreall && !isset($SESSION->restore->importing)) {
                                    $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
                                    $counteritems++;
                                    continue;
                                }
                            } elseif ($dbrec->itemtype == 'course') {
                                // We don't restore course type to avoid duplicate course items
                                if ($restoreall && !isset($SESSION->restore->importing) && $restore->restoreto == 2) {
                                    // TODO any special code needed here to restore course item without duplicating it?
                                    // find the course category with depth 1, and course id = current course id
                                    // this would have been already restored
                                    $counteritems++;
                                    continue;
                                } else {
                                    $counteritems++;
                                    continue;
                                }
                            }
                        }
                        $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);
                        $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#']);
                        $dbrec->idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#']);
                        $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#']);
                        $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#']);
                        $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#']);
                        /// needs to be restored first
                        if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'])) {
                            $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#']));
                            $dbrec->scaleid = $scale->new_id;
                        }
                        /// needs to be restored first
                        $dbrec->outcomeid = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']));
                        $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#']);
                        $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#']);
                        $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#']);
                        $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#']);
                        $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#']);
                        $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#']);
                        $dbrec->needsupdate = backup_todb($info['GRADE_ITEM']['#']['NEEDSUPDATE']['0']['#']);
                        $dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#']);
                        $dbrec->timemodified = backup_todb($info['GRADE_ITEM']['#']['TIMEMODIFIED']['0']['#']);
                        // get the current sortorder, add 1 to it and use that
                        if ($lastitem = get_record_sql("SELECT sortorder, id FROM {$CFG->prefix}grade_items\n                                                        WHERE courseid = {$restore->course_id}\n                                                        ORDER BY sortorder DESC ", true)) {
                            // we just need the first one
                            $dbrec->sortorder = $lastitem->sortorder + 1;
                        } else {
                            // this is the first grade_item
                            $dbrec->sortorder = 1;
                        }
                        // always insert, since modules restored to existing courses are always inserted
                        $itemid = insert_record('grade_items', $dbrec);
                        if ($itemid) {
                            backup_putid($restore->backup_unique_code, 'grade_items', backup_todb($info['GRADE_ITEM']['#']['ID']['0']['#']), $itemid);
                        }
                        // no need to restore grades if user data is not selected
                        if ($dbrec->itemtype == 'mod' && !restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
                            // module instance not selected when restored using granular
                            // skip this item
                            $counteritems++;
                            continue;
                        }
                        /// now, restore grade_grades
                        if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']) && ($grades = $info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
                            //Iterate over items
                            for ($i = 0; $i < sizeof($grades); $i++) {
                                $ite_info = $grades[$i];
                                //traverse_xmlize($ite_info);
                                //Debug
                                //print_object ($GLOBALS['traverse_array']);                                                  //Debug
                                //$GLOBALS['traverse_array']="";                                                              //Debug
                                //Now build the GRADE_ITEM record structure
                                $grade = new object();
                                $grade->itemid = $itemid;
                                $user = backup_getid($restore->backup_unique_code, "user", backup_todb($ite_info['#']['USERID']['0']['#']));
                                $grade->userid = $user->new_id;
                                $grade->rawgrade = backup_todb($ite_info['#']['RAWGRADE']['0']['#']);
                                $grade->rawgrademax = backup_todb($ite_info['#']['RAWGRADEMAX']['0']['#']);
                                $grade->rawgrademin = backup_todb($ite_info['#']['RAWGRADEMIN']['0']['#']);
                                // need to find scaleid
                                if (backup_todb($ite_info['#']['RAWSCALEID']['0']['#'])) {
                                    $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($ite_info['#']['RAWSCALEID']['0']['#']));
                                    $grade->rawscaleid = $scale->new_id;
                                }
                                $grade->finalgrade = backup_todb($ite_info['#']['FINALGRADE']['0']['#']);
                                $grade->hidden = backup_todb($ite_info['#']['HIDDEN']['0']['#']);
                                $grade->locked = backup_todb($ite_info['#']['LOCKED']['0']['#']);
                                $grade->locktime = backup_todb($ite_info['#']['LOCKTIME']['0']['#']);
                                $grade->exported = backup_todb($ite_info['#']['EXPORTED']['0']['#']);
                                $grade->overridden = backup_todb($ite_info['#']['OVERRIDDEN']['0']['#']);
                                $grade->excluded = backup_todb($ite_info['#']['EXCLUDED']['0']['#']);
                                $grade->feedback = backup_todb($ite_info['#']['FEEDBACK']['0']['#']);
                                $grade->feedbackformat = backup_todb($ite_info['#']['FEEDBACKFORMAT']['0']['#']);
                                $grade->information = backup_todb($ite_info['#']['INFORMATION']['0']['#']);
                                $grade->informationformat = backup_todb($ite_info['#']['INFORMATIONFORMAT']['0']['#']);
                                $newid = insert_record('grade_grades', $grade);
                                if ($newid) {
                                    backup_putid($restore->backup_unique_code, "grade_grades", backup_todb($ite_info['#']['ID']['0']['#']), $newid);
                                }
                                $counter++;
                                if ($counter % 20 == 0) {
                                    if (!defined('RESTORE_SILENTLY')) {
                                        echo ".";
                                        if ($counter % 400 == 0) {
                                            echo "<br />";
                                        }
                                    }
                                    backup_flush(300);
                                }
                            }
                        }
                    }
                    $counteritems++;
                    // increment item count
                }
            }
        }
    }
    // process histories
    if ($gchcount && $continue && !isset($SESSION->restore->importing) && $restore->restore_gradebook_history) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradecategoryhistory', 'grades') . '</li>';
        }
        $counter = 0;
        while ($counter < $gchcount) {
            //Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_categories_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    //Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_categories_history', $rec->old_id);
                    if ($data) {
                        //Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        $oldobj = backup_getid($restore->backup_unique_code, "grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
                        if (empty($oldobj->new_id)) {
                            // if the old object is not being restored, can't restoring its history
                            $counter++;
                            continue;
                        }
                        $dbrec->oldid = $oldobj->new_id;
                        $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
                        $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
                        $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                        // loggeduser might not be restored, e.g. admin
                        if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                            $dbrec->loggeduser = $oldobj->new_id;
                        }
                        // this item might not have a parent at all, do not skip it if no parent is specified
                        if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
                            $oldobj = backup_getid($restore->backup_unique_code, "grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
                            if (empty($oldobj->new_id)) {
                                // if the parent category not restored
                                $counter++;
                                continue;
                            }
                        }
                        $dbrec->parent = $oldobj->new_id;
                        $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
                        // path needs to be rebuilt
                        if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
                            // to preserve the path and make it work, we need to replace the categories one by one
                            // we first get the list of categories in current path
                            if ($paths = explode("/", $path)) {
                                $newpath = '';
                                foreach ($paths as $catid) {
                                    if ($catid) {
                                        // find the new corresponding path
                                        $oldpath = backup_getid($restore->backup_unique_code, "grade_categories", $catid);
                                        $newpath .= "/{$oldpath->new_id}";
                                    }
                                }
                                $dbrec->path = $newpath;
                            }
                        }
                        $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
                        $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
                        $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
                        $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
                        $dbrec->courseid = $restore->course_id;
                        insert_record('grade_categories_history', $dbrec);
                        unset($dbrec);
                    }
                    //Increment counters
                    $counter++;
                    //Do some output
                    if ($counter % 1 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 20 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
            }
        }
    }
    // process histories
    if ($gghcount && $continue && !isset($SESSION->restore->importing) && $restore->restore_gradebook_history) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradegradeshistory', 'grades') . '</li>';
        }
        $counter = 0;
        while ($counter < $gghcount) {
            //Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_grades_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    //Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_grades_history', $rec->old_id);
                    if ($data) {
                        //Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        $oldobj = backup_getid($restore->backup_unique_code, "grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
                        if (empty($oldobj->new_id)) {
                            // if the old object is not being restored, can't restoring its history
                            $counter++;
                            continue;
                        }
                        $dbrec->oldid = $oldobj->new_id;
                        $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
                        $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
                        $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                        if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                            $dbrec->loggeduser = $oldobj->new_id;
                        }
                        $oldobj = backup_getid($restore->backup_unique_code, "grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
                        $dbrec->itemid = $oldobj->new_id;
                        if (empty($dbrec->itemid)) {
                            $counter++;
                            continue;
                            // grade item not being restored
                        }
                        $oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
                        $dbrec->userid = $oldobj->new_id;
                        $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
                        $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
                        $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
                        if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
                            $dbrec->usermodified = $oldobj->new_id;
                        }
                        $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
                        $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
                        $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
                        $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
                        $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
                        $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
                        $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
                        $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
                        $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
                        $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
                        $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
                        insert_record('grade_grades_history', $dbrec);
                        unset($dbrec);
                    }
                    //Increment counters
                    $counter++;
                    //Do some output
                    if ($counter % 1 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 20 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
            }
        }
    }
    // process histories
    if ($gihcount && $continue && !isset($SESSION->restore->importing) && $restore->restore_gradebook_history) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeitemshistory', 'grades') . '</li>';
        }
        $counter = 0;
        while ($counter < $gihcount) {
            //Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_items_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    //Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_items_history', $rec->old_id);
                    if ($data) {
                        //Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        $oldobj = backup_getid($restore->backup_unique_code, "grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
                        if (empty($oldobj->new_id)) {
                            // if the old object is not being restored, can't restoring its history
                            $counter++;
                            continue;
                        }
                        $dbrec->oldid = $oldobj->new_id;
                        $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
                        $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
                        $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                        if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                            $dbrec->loggeduser = $oldobj->new_id;
                        }
                        $oldobj = backup_getid($restore->backup_unique_code, 'grade_categories', backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
                        $oldobj->categoryid = $category->new_id;
                        if (empty($oldobj->categoryid)) {
                            $counter++;
                            continue;
                            // category not restored
                        }
                        $dbrec->itemname = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
                        $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
                        $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
                        // code from grade_items restore
                        $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
                        // do not restore if this grade_item is a mod, and
                        if ($dbrec->itemtype == 'mod') {
                            if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
                                // module instance not selected when restored using granular
                                // skip this item
                                $counter++;
                                continue;
                            }
                            // iteminstance should point to new mod
                            $mod = backup_getid($restore->backup_unique_code, $dbrec->itemmodule, $iteminstance);
                            $dbrec->iteminstance = $mod->new_id;
                        } else {
                            if ($dbrec->itemtype == 'category') {
                                // the item instance should point to the new grade category
                                // 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;
                                }
                            } elseif ($dbrec->itemtype == 'course') {
                                // We don't restore course type to avoid duplicate course items
                                if ($restoreall) {
                                    // TODO any special code needed here to restore course item without duplicating it?
                                    // find the course category with depth 1, and course id = current course id
                                    // this would have been already restored
                                    $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
                                    $dbrec->iteminstance = $cat->id;
                                } else {
                                    $counter++;
                                    continue;
                                }
                            }
                        }
                        $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
                        $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
                        $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
                        $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
                        $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
                        $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
                        $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
                        if ($oldobj = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
                            // scaleid is optional
                            $dbrec->scaleid = $oldobj->new_id;
                        }
                        if ($oldobj = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
                            // outcome is optional
                            $dbrec->outcomeid = $oldobj->new_id;
                        }
                        $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
                        $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
                        $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
                        $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
                        $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
                        $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
                        $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
                        $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
                        $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
                        insert_record('grade_items_history', $dbrec);
                        unset($dbrec);
                    }
                    //Increment counters
                    $counter++;
                    //Do some output
                    if ($counter % 1 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 20 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
            }
        }
    }
    // process histories
    if ($gohcount && $continue && !isset($SESSION->restore->importing) && $restore->restore_gradebook_history) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeoutcomeshistory', 'grades') . '</li>';
        }
        $counter = 0;
        while ($counter < $gohcount) {
            //Fetch recordset_size records in each iteration
            $recs = get_records_select("backup_ids", "table_name = 'grade_outcomes_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
            if ($recs) {
                foreach ($recs as $rec) {
                    //Get the full record from backup_ids
                    $data = backup_getid($restore->backup_unique_code, 'grade_outcomes_history', $rec->old_id);
                    if ($data) {
                        //Now get completed xmlized object
                        $info = $data->info;
                        //traverse_xmlize($info);                            //Debug
                        //print_object ($GLOBALS['traverse_array']);         //Debug
                        //$GLOBALS['traverse_array']="";                     //Debug
                        $oldobj = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
                        if (empty($oldobj->new_id)) {
                            // if the old object is not being restored, can't restoring its history
                            $counter++;
                            continue;
                        }
                        $dbrec->oldid = $oldobj->new_id;
                        $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
                        $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
                        $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                        if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                            $dbrec->loggeduser = $oldobj->new_id;
                        }
                        $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
                        $dbrec->fullname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
                        $oldobj = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
                        $dbrec->scaleid = $oldobj->new_id;
                        $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
                        insert_record('grade_outcomes_history', $dbrec);
                        unset($dbrec);
                    }
                    //Increment counters
                    $counter++;
                    //Do some output
                    if ($counter % 1 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 20 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //End ul
        echo '</ul>';
    }
    return $status;
}
 /**
  * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
  * @param string $source from where was the object updated (mod/forum, manual, etc.)
  * @return boolean success
  */
 function update($source = null)
 {
     // load the grade item or create a new one
     $this->load_grade_item();
     // force recalculation of path;
     if (empty($this->path)) {
         $this->path = grade_category::build_path($this);
         $this->depth = substr_count($this->path, '/') - 1;
     }
     // Recalculate grades if needed
     if ($this->qualifies_for_regrading()) {
         $this->force_regrading();
     }
     return parent::update($source);
 }