/**
  * Tests the setting of the grade_grades aggregationweight column.
  * Currently, this is only a regression test for MDL-51715.
  * This must be run before sub_test_grade_category_set_parent(), which alters
  * the fixture.
  */
 protected function sub_test_grade_category_generate_grades_aggregationweight()
 {
     global $DB;
     // Start of regression test for MDL-51715.
     // grade_categories [1] and [2] are child categories of [0]
     // Ensure that grades have been generated with fixture data.
     $childcat1 = new grade_category($this->grade_categories[1]);
     $childcat1itemid = $childcat1->load_grade_item()->id;
     $childcat1->generate_grades();
     $childcat2 = new grade_category($this->grade_categories[2]);
     $childcat2itemid = $childcat2->load_grade_item()->id;
     $childcat2->generate_grades();
     $parentcat = new grade_category($this->grade_categories[0]);
     $parentcat->generate_grades();
     // Drop low and and re-generate to produce 'dropped' aggregation status.
     $parentcat->droplow = 1;
     $parentcat->generate_grades();
     $this->assertTrue($DB->record_exists_select('grade_grades', "aggregationstatus='dropped' and itemid in (?,?)", array($childcat1itemid, $childcat2itemid)));
     $this->assertFalse($DB->record_exists_select('grade_grades', "aggregationstatus='dropped' and aggregationweight > 0.00"), "aggregationweight should be 0.00 if aggregationstatus=='dropped'");
     // Reset grade data to be consistent with fixture data.
     $parentcat->droplow = 0;
     $parentcat->generate_grades();
     // Blank out the final grade for one of the child categories and re-generate
     // to produce 'novalue' aggregationstatus.  Direct DB update is testing shortcut.
     $DB->set_field('grade_grades', 'finalgrade', null, array('itemid' => $childcat1itemid));
     $parentcat->generate_grades();
     $this->assertTrue($DB->record_exists_select('grade_grades', "aggregationstatus='novalue' and itemid = ?", array($childcat1itemid)));
     $this->assertFalse($DB->record_exists_select('grade_grades', "aggregationstatus='novalue' and aggregationweight > 0.00"), "aggregationweight should be 0.00 if aggregationstatus=='novalue'");
     // Re-generate to be consistent with fixture data.
     $childcat1->generate_grades();
     $parentcat->generate_grades();
     // End of regression test for MDL-51715.
 }
Exemple #2
0
/**
 * This function migrades all the pre 1.9 gradebook data from xml
 */
function restore_migrate_old_gradebook($restore, $xml_file)
{
    global $CFG;
    $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 (empty($info)) {
        return $status;
    }
    // make sure top course category exists
    $course_category = grade_category::fetch_course_category($restore->course_id);
    $course_category->load_grade_item();
    // 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
    $importing = !empty($SESSION->restore->importing);
    // there should not be a way to import old backups, but anyway ;-)
    if ($importing) {
        $restoreall = false;
    } else {
        $prev_grade_items = grade_item::fetch_all(array('courseid' => $restore->course_id));
        $prev_grade_cats = grade_category::fetch_all(array('courseid' => $restore->course_id));
        // if any categories already present, skip restore of categories from backup
        if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
            $restoreall = false;
        }
        unset($prev_grade_items);
        unset($prev_grade_cats);
    }
    // force creation of all grade_items - the course_modules already exist
    grade_force_full_regrading($restore->course_id);
    grade_grab_course_grades($restore->course_id);
    // Start ul
    if (!defined('RESTORE_SILENTLY')) {
        echo '<ul>';
    }
    /// Process letters
    $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
    // respect current grade letters if defined
    if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeletters', 'grades') . '</li>';
        }
        // Fetch recordset_size records in each iteration
        $recs = get_records_select("backup_ids", "table_name = 'grade_letter' AND backup_code = {$restore->backup_unique_code}", "", "old_id");
        if ($recs) {
            foreach ($recs as $rec) {
                // Get the full record from backup_ids
                $data = backup_getid($restore->backup_unique_code, 'grade_letter', $rec->old_id);
                if ($data) {
                    $info = $data->info;
                    $dbrec = new object();
                    $dbrec->contextid = $context->id;
                    $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['GRADE_LOW']['0']['#']);
                    $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
                    insert_record('grade_letters', $dbrec);
                }
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        echo '<li>' . get_string('categories', 'grades') . '</li>';
    }
    //Fetch recordset_size records in each iteration
    $recs = get_records_select("backup_ids", "table_name = 'grade_category' AND backup_code = {$restore->backup_unique_code}", "old_id", "old_id");
    $cat_count = count($recs);
    if ($recs) {
        foreach ($recs as $rec) {
            //Get the full record from backup_ids
            $data = backup_getid($restore->backup_unique_code, 'grade_category', $rec->old_id);
            if ($data) {
                //Now get completed xmlized object
                $info = $data->info;
                if ($restoreall) {
                    if ($cat_count == 1) {
                        $course_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
                        $course_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
                        $course_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
                        $course_category->aggregateonlygraded = 0;
                        $course_category->update('restore');
                        $grade_category = $course_category;
                    } else {
                        $grade_category = new grade_category();
                        $grade_category->courseid = $restore->course_id;
                        $grade_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
                        $grade_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
                        $grade_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
                        $grade_category->aggregateonlygraded = 0;
                        $grade_category->insert('restore');
                        $grade_category->load_grade_item();
                        // force cretion of grade_item
                    }
                } else {
                    $grade_category = null;
                }
                /// now, restore grade_items
                $items = array();
                if (!empty($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'])) {
                    //Iterate over items
                    foreach ($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'] as $ite_info) {
                        $modname = backup_todb($ite_info['#']['MODULE_NAME']['0']['#'], false);
                        $olditeminstance = backup_todb($ite_info['#']['CMINSTANCE']['0']['#'], false);
                        if (!($mod = backup_getid($restore->backup_unique_code, $modname, $olditeminstance))) {
                            continue;
                            // not restored
                        }
                        $iteminstance = $mod->new_id;
                        if (!($cm = get_coursemodule_from_instance($modname, $iteminstance, $restore->course_id))) {
                            continue;
                            // does not exist
                        }
                        if (!($grade_item = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => $cm->modname, 'iteminstance' => $cm->instance, 'courseid' => $cm->course, 'itemnumber' => 0)))) {
                            continue;
                            // no item yet??
                        }
                        if ($grade_category) {
                            $grade_item->sortorder = backup_todb($ite_info['#']['SORT_ORDER']['0']['#'], false);
                            $grade_item->set_parent($grade_category->id);
                        }
                        if ($importing or $grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance)) {
                            // module instance not selected when restored using granular
                            // skip this item
                            continue;
                        }
                        //Now process grade excludes
                        if (empty($ite_info['#']['GRADE_EXCEPTIONS'])) {
                            continue;
                        }
                        foreach ($ite_info['#']['GRADE_EXCEPTIONS']['0']['#']['GRADE_EXCEPTION'] as $exc_info) {
                            if ($u = backup_getid($restore->backup_unique_code, "user", backup_todb($exc_info['#']['USERID']['0']['#']))) {
                                $userid = $u->new_id;
                                $grade_grade = new grade_grade(array('itemid' => $grade_item->id, 'userid' => $userid));
                                $grade_grade->excluded = 1;
                                if ($grade_grade->id) {
                                    $grade_grade->update('restore');
                                } else {
                                    $grade_grade->insert('restore');
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //End ul
        echo '</ul>';
    }
    return $status;
}
 /**
  * Create a grade_category.
  *
  * @param array|stdClass $record
  * @return stdClass the grade category record
  */
 public function create_grade_category($record = null)
 {
     global $CFG;
     $this->gradecategorycounter++;
     $i = $this->gradecategorycounter;
     if (!isset($record['fullname'])) {
         $record['fullname'] = 'Grade category ' . $i;
     }
     // For gradelib classes.
     require_once $CFG->libdir . '/gradelib.php';
     // Create new grading category in this course.
     $gradecategory = new grade_category($record, false);
     $gradecategory->apply_default_settings();
     $gradecategory->apply_forced_settings();
     $gradecategory->insert();
     // This creates a default grade item for the category
     $gradeitem = $gradecategory->load_grade_item();
     if (isset($record->parentcategory)) {
         $gradecategory->set_parent($data->parentcategory);
     }
     $gradecategory->update_from_db();
     return $gradecategory->get_record_data();
 }