function backup_gradebook_info($bf, $preferences)
{
    global $CFG;
    require_once $CFG->libdir . '/gradelib.php';
    //first make sure items are properly sorted and everything is ok
    grade_category::fetch_course_tree($preferences->backup_course, true);
    grade_regrade_final_grades($preferences->backup_course);
    $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 *\n                                              FROM {$CFG->prefix}grade_items\n                                             WHERE courseid = {$preferences->backup_course}\n                                                   AND itemtype = 'mod'")) {
        foreach ($grade_items as $grade_item) {
            // get module information
            // if some activities not selected, we do not backup categories at all
            if (!backup_mod_selected($preferences, $grade_item->itemmodule, $grade_item->iteminstance)) {
                $backupall = false;
                break;
            }
        }
        unset($grade_items);
        //free memory
    }
    //Gradebook header
    fwrite($bf, start_tag("GRADEBOOK", 2, true));
    $status = backup_gradebook_outcomes_info($bf, $preferences);
    $status = backup_gradebook_grade_letters_info($bf, $preferences);
    // Now backup grade_item (inside grade_category)
    if ($backupall) {
        $status = backup_gradebook_category_info($bf, $preferences);
    }
    $status = backup_gradebook_item_info($bf, $preferences, $backupall);
    // backup gradebook histories (only if grade history is enabled and selected)
    if (empty($CFG->disablegradehistory) && $preferences->backup_gradebook_history) {
        $status = backup_gradebook_outcomes_history($bf, $preferences);
        $status = backup_gradebook_categories_history_info($bf, $preferences);
        $status = backup_gradebook_items_history_info($bf, $preferences);
        $status = backup_gradebook_grades_history_info($bf, $preferences);
    }
    //Gradebook footer
    $status = fwrite($bf, end_tag("GRADEBOOK", 2, true));
    return $status;
}
function backup_gradebook_info($bf, $preferences)
{
    global $CFG;
    $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\n                                            WHERE courseid = {$preferences->backup_course}\n                                            AND itemtype != 'course'\n                                            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);
    $status = backup_gradebook_outcomes_courses_info($bf, $preferences);
    // backup gradebook histories
    if ($preferences->backup_gradebook_history) {
        $status = backup_gradebook_categories_history_info($bf, $preferences);
        $status = backup_gradebook_grades_history_info($bf, $preferences);
        $status = backup_gradebook_items_history_info($bf, $preferences);
        $status = backup_gradebook_outcomes_history($bf, $preferences);
    }
    //Gradebook footer
    $status = fwrite($bf, end_tag("GRADEBOOK", 2, true));
    return $status;
}