/** * Test upgrade minmaxgrade step. */ public function test_upgrade_minmaxgrade() { global $CFG, $DB; require_once $CFG->libdir . '/gradelib.php'; $initialminmax = $CFG->grade_minmaxtouse; $this->resetAfterTest(); $c1 = $this->getDataGenerator()->create_course(); $c2 = $this->getDataGenerator()->create_course(); $c3 = $this->getDataGenerator()->create_course(); $u1 = $this->getDataGenerator()->create_user(); $a1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1, 'grade' => 100)); $a2 = $this->getDataGenerator()->create_module('assign', array('course' => $c2, 'grade' => 100)); $a3 = $this->getDataGenerator()->create_module('assign', array('course' => $c3, 'grade' => 100)); $cm1 = get_coursemodule_from_instance('assign', $a1->id); $ctx1 = context_module::instance($cm1->id); $assign1 = new assign($ctx1, $cm1, $c1); $cm2 = get_coursemodule_from_instance('assign', $a2->id); $ctx2 = context_module::instance($cm2->id); $assign2 = new assign($ctx2, $cm2, $c2); $cm3 = get_coursemodule_from_instance('assign', $a3->id); $ctx3 = context_module::instance($cm3->id); $assign3 = new assign($ctx3, $cm3, $c3); // Give a grade to the student. $ug = $assign1->get_user_grade($u1->id, true); $ug->grade = 10; $assign1->update_grade($ug); $ug = $assign2->get_user_grade($u1->id, true); $ug->grade = 20; $assign2->update_grade($ug); $ug = $assign3->get_user_grade($u1->id, true); $ug->grade = 30; $assign3->update_grade($ug); // Run the upgrade. upgrade_minmaxgrade(); // Nothing has happened. $this->assertFalse($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c1->id))); $this->assertSame(false, grade_get_setting($c1->id, 'minmaxtouse', false, true)); $this->assertFalse($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c1->id))); $this->assertFalse($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c2->id))); $this->assertSame(false, grade_get_setting($c2->id, 'minmaxtouse', false, true)); $this->assertFalse($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c2->id))); $this->assertFalse($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c3->id))); $this->assertSame(false, grade_get_setting($c3->id, 'minmaxtouse', false, true)); $this->assertFalse($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c3->id))); // Create inconsistency in c1 and c2. $giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $a1->id, 'courseid' => $c1->id, 'itemnumber' => 0); $gi = grade_item::fetch($giparams); $gi->grademin = 5; $gi->update(); $giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $a2->id, 'courseid' => $c2->id, 'itemnumber' => 0); $gi = grade_item::fetch($giparams); $gi->grademax = 50; $gi->update(); // C1 and C2 should be updated, but the course setting should not be set. $CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE; // Run the upgrade. upgrade_minmaxgrade(); // C1 and C2 were partially updated. $this->assertTrue($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c1->id))); $this->assertSame(false, grade_get_setting($c1->id, 'minmaxtouse', false, true)); $this->assertTrue($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c1->id))); $this->assertTrue($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c2->id))); $this->assertSame(false, grade_get_setting($c2->id, 'minmaxtouse', false, true)); $this->assertTrue($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c2->id))); // Nothing has happened for C3. $this->assertFalse($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c3->id))); $this->assertSame(false, grade_get_setting($c3->id, 'minmaxtouse', false, true)); $this->assertFalse($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c3->id))); // Course setting should not be set on a course that has the setting already. $CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM; grade_set_setting($c1->id, 'minmaxtouse', -1); // Sets different value than constant to check that it remained the same. // Run the upgrade. upgrade_minmaxgrade(); // C2 was updated. $this->assertSame((string) GRADE_MIN_MAX_FROM_GRADE_GRADE, grade_get_setting($c2->id, 'minmaxtouse', false, true)); // Nothing has happened for C1. $this->assertSame('-1', grade_get_setting($c1->id, 'minmaxtouse', false, true)); // Nothing has happened for C3. $this->assertFalse($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c3->id))); $this->assertSame(false, grade_get_setting($c3->id, 'minmaxtouse', false, true)); $this->assertFalse($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c3->id))); // Final check, this time we'll unset the default config. unset($CFG->grade_minmaxtouse); grade_set_setting($c1->id, 'minmaxtouse', null); // Run the upgrade. upgrade_minmaxgrade(); // C1 was updated. $this->assertSame((string) GRADE_MIN_MAX_FROM_GRADE_GRADE, grade_get_setting($c1->id, 'minmaxtouse', false, true)); // Nothing has happened for C3. $this->assertFalse($DB->record_exists('config', array('name' => 'show_min_max_grades_changed_' . $c3->id))); $this->assertSame(false, grade_get_setting($c3->id, 'minmaxtouse', false, true)); $this->assertFalse($DB->record_exists('grade_items', array('needsupdate' => 1, 'courseid' => $c3->id))); // Restore value. $CFG->grade_minmaxtouse = $initialminmax; }
/** * Use the grade min and max from the grade_item. * * This is reserved for core use after an upgrade. * * @param int $courseid The current course id. */ function grade_upgrade_use_min_max_from_grade_item($courseid) { grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM); grade_force_full_regrading($courseid); // Do this now, because it probably happened to late in the page load to be happen automatically. grade_regrade_final_grades($courseid); }
/** * Checks what should happen with the course grade setting minmaxtouse. * * This is related to the upgrade step at the time the setting was added. * * @see MDL-48618 * @return void */ protected function check_minmaxtouse() { global $CFG, $DB; require_once $CFG->libdir . '/gradelib.php'; $userinfo = $this->task->get_setting_value('users'); $settingname = 'minmaxtouse'; $courseid = $this->get_courseid(); $minmaxtouse = $DB->get_field('grade_settings', 'value', array('courseid' => $courseid, 'name' => $settingname)); $version28start = 2014111000.0; $version28last = 2014111006.05; $version29start = 2015051100.0; $version29last = 2015060400.02; $target = $this->get_task()->get_target(); if ($minmaxtouse === false && ($target != backup::TARGET_CURRENT_ADDING && $target != backup::TARGET_EXISTING_ADDING)) { // The setting was not found because this setting did not exist at the time the backup was made. // And we are not restoring as merge, in which case we leave the course as it was. $version = $this->get_task()->get_info()->moodle_version; if ($version < $version28start) { // We need to set it to use grade_item, but only if the site-wide setting is different. No need to notice them. if ($CFG->grade_minmaxtouse != GRADE_MIN_MAX_FROM_GRADE_ITEM) { grade_set_setting($courseid, $settingname, GRADE_MIN_MAX_FROM_GRADE_ITEM); } } else { if ($version >= $version28start && $version < $version28last || $version >= $version29start && $version < $version29last) { // They should be using grade_grade when the course has inconsistencies. $sql = "SELECT gi.id\n FROM {grade_items} gi\n JOIN {grade_grades} gg\n ON gg.itemid = gi.id\n WHERE gi.courseid = ?\n AND (gi.itemtype != ? AND gi.itemtype != ?)\n AND (gg.rawgrademax != gi.grademax OR gg.rawgrademin != gi.grademin)"; // The course can only have inconsistencies when we restore the user info, // we do not need to act on existing grades that were not restored as part of this backup. if ($userinfo && $DB->record_exists_sql($sql, array($courseid, 'course', 'category'))) { // Display the notice as we do during upgrade. set_config('show_min_max_grades_changed_' . $courseid, 1); if ($CFG->grade_minmaxtouse != GRADE_MIN_MAX_FROM_GRADE_GRADE) { // We need set the setting as their site-wise setting is not GRADE_MIN_MAX_FROM_GRADE_GRADE. // If they are using the site-wide grade_grade setting, we only want to notice them. grade_set_setting($courseid, $settingname, GRADE_MIN_MAX_FROM_GRADE_GRADE); } } } else { // This should never happen because from now on minmaxtouse is always saved in backups. } } } }
$strgrades = get_string('grades'); $pagename = get_string('coursesettings', 'grades'); $navigation = grade_build_nav(__FILE__, $pagename, $courseid); $returnurl = $CFG->wwwroot . '/grade/index.php?id=' . $course->id; $mform = new course_settings_form(); $settings = grade_get_settings($course->id); $mform->set_data($settings); if ($mform->is_cancelled()) { redirect($returnurl); } else { if ($data = $mform->get_data()) { $data = (array) $data; $general = array('displaytype', 'decimalpoints', 'aggregationposition'); foreach ($data as $key => $value) { if (!in_array($key, $general) and strpos($key, 'report_') !== 0 and strpos($key, 'import_') !== 0 and strpos($key, 'export_') !== 0) { continue; } if ($value == -1) { $value = null; } grade_set_setting($course->id, $key, $value); } redirect($returnurl); } } print_grade_page_head($courseid, 'settings', 'coursesettings', get_string('coursesettings', 'grades')); print_box_start('generalbox boxaligncenter boxwidthnormal centerpara'); echo get_string('coursesettingsexplanation', 'grades'); print_box_end(); $mform->display(); print_footer($course);
/** * Test that the upgrade script correctly flags courses to be frozen due to letter boundary problems. */ public function test_upgrade_course_letter_boundary() { global $CFG, $DB; $this->resetAfterTest(true); require_once $CFG->libdir . '/db/upgradelib.php'; // Create a user. $user = $this->getDataGenerator()->create_user(); // Create some courses. $courses = array(); $contexts = array(); for ($i = 0; $i < 45; $i++) { $course = $this->getDataGenerator()->create_course(); $context = context_course::instance($course->id); if (in_array($i, array(2, 5, 10, 13, 14, 19, 23, 25, 30, 34, 36))) { // Assign good letter boundaries. $this->assign_good_letter_boundary($context->id); } if (in_array($i, array(3, 6, 11, 15, 20, 24, 26, 31, 35))) { // Assign bad letter boundaries. $this->assign_bad_letter_boundary($context->id); } if (in_array($i, array(3, 9, 10, 11, 18, 19, 20, 29, 30, 31, 40))) { grade_set_setting($course->id, 'displaytype', '3'); } else { if (in_array($i, array(8, 17, 28))) { grade_set_setting($course->id, 'displaytype', '2'); } } if (in_array($i, array(37, 43))) { // Show. grade_set_setting($course->id, 'report_user_showlettergrade', '1'); } else { if (in_array($i, array(38, 42))) { // Hide. grade_set_setting($course->id, 'report_user_showlettergrade', '0'); } } $assignrow = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'name' => 'Test!')); $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $assignrow->id, 'courseid' => $course->id)); if (in_array($i, array(6, 13, 14, 15, 23, 24, 34, 35, 36, 41))) { grade_item::set_properties($gi, array('display' => 3)); $gi->update(); } else { if (in_array($i, array(12, 21, 32))) { grade_item::set_properties($gi, array('display' => 2)); $gi->update(); } } $gradegrade = new grade_grade(); $gradegrade->itemid = $gi->id; $gradegrade->userid = $user->id; $gradegrade->rawgrade = 55.5563; $gradegrade->finalgrade = 55.5563; $gradegrade->rawgrademax = 100; $gradegrade->rawgrademin = 0; $gradegrade->timecreated = time(); $gradegrade->timemodified = time(); $gradegrade->insert(); $contexts[] = $context; $courses[] = $course; } upgrade_course_letter_boundary(); // No system setting for grade letter boundaries. // [0] A course with no letter boundaries. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[0]->id})); // [1] A course with letter boundaries which are default. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[1]->id})); // [2] A course with letter boundaries which are custom but not affected. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[2]->id})); // [3] A course with letter boundaries which are custom and will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[3]->id}); // [4] A course with no letter boundaries, but with a grade item with letter boundaries which are default. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[4]->id})); // [5] A course with no letter boundaries, but with a grade item with letter boundaries which are not default, but not affected. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[5]->id})); // [6] A course with no letter boundaries, but with a grade item with letter boundaries which are not default which will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[6]->id}); // System setting for grade letter boundaries (default). set_config('grade_displaytype', '3'); for ($i = 0; $i < 45; $i++) { unset_config('gradebook_calculations_freeze_' . $courses[$i]->id); } upgrade_course_letter_boundary(); // [7] A course with no grade display settings for the course or grade items. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[7]->id})); // [8] A course with grade display settings, but for something that isn't letters. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[8]->id})); // [9] A course with grade display settings of letters which are default. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[9]->id})); // [10] A course with grade display settings of letters which are not default, but not affected. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[10]->id})); // [11] A course with grade display settings of letters which are not default, which will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[11]->id}); // [12] A grade item with display settings that are not letters. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[12]->id})); // [13] A grade item with display settings of letters which are default. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[13]->id})); // [14] A grade item with display settings of letters which are not default, but not affected. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[14]->id})); // [15] A grade item with display settings of letters which are not default, which will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[15]->id}); // System setting for grade letter boundaries (custom with problem). $systemcontext = context_system::instance(); $this->assign_bad_letter_boundary($systemcontext->id); for ($i = 0; $i < 45; $i++) { unset_config('gradebook_calculations_freeze_' . $courses[$i]->id); } upgrade_course_letter_boundary(); // [16] A course with no grade display settings for the course or grade items. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[16]->id}); // [17] A course with grade display settings, but for something that isn't letters. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[17]->id})); // [18] A course with grade display settings of letters which are default. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[18]->id}); // [19] A course with grade display settings of letters which are not default, but not affected. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[19]->id})); // [20] A course with grade display settings of letters which are not default, which will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[20]->id}); // [21] A grade item with display settings which are not letters. Grade total will be affected so should be frozen. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[21]->id}); // [22] A grade item with display settings of letters which are default. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[22]->id}); // [23] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[23]->id})); // [24] A grade item with display settings of letters which are not default, which will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[24]->id}); // [25] A course which is using the default grade display setting, but has updated the grade letter boundary (not 57) Should not be frozen. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[25]->id})); // [26] A course that is using the default display setting (letters) and altered the letter boundary with 57. Should be frozen. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[26]->id}); // System setting not showing letters. set_config('grade_displaytype', '2'); for ($i = 0; $i < 45; $i++) { unset_config('gradebook_calculations_freeze_' . $courses[$i]->id); } upgrade_course_letter_boundary(); // [27] A course with no grade display settings for the course or grade items. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[27]->id})); // [28] A course with grade display settings, but for something that isn't letters. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[28]->id})); // [29] A course with grade display settings of letters which are default. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[29]->id}); // [30] A course with grade display settings of letters which are not default, but not affected. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[30]->id})); // [31] A course with grade display settings of letters which are not default, which will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[31]->id}); // [32] A grade item with display settings which are not letters. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[32]->id})); // [33] All system defaults. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[33]->id})); // [34] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[34]->id})); // [35] A grade item with display settings of letters which are not default, which will be affected. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[35]->id}); // [36] A course with grade display settings of letters with modified and good boundary (not 57) Should not be frozen. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[36]->id})); // Previous site conditions still exist. for ($i = 0; $i < 45; $i++) { unset_config('gradebook_calculations_freeze_' . $courses[$i]->id); } upgrade_course_letter_boundary(); // [37] Site setting for not showing the letter column and course setting set to show (frozen). $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[37]->id}); // [38] Site setting for not showing the letter column and course setting set to hide. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[38]->id})); // [39] Site setting for not showing the letter column and course setting set to default. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[39]->id})); // [40] Site setting for not showing the letter column and course setting set to default. Course display set to letters (frozen). $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[40]->id}); // [41] Site setting for not showing the letter column and course setting set to default. Grade item display set to letters (frozen). $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[41]->id}); // Previous site conditions still exist. for ($i = 0; $i < 45; $i++) { unset_config('gradebook_calculations_freeze_' . $courses[$i]->id); } set_config('grade_report_user_showlettergrade', '1'); upgrade_course_letter_boundary(); // [42] Site setting for showing the letter column, but course setting set to hide. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[42]->id})); // [43] Site setting for showing the letter column and course setting set to show (frozen). $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[43]->id}); // [44] Site setting for showing the letter column and course setting set to default (frozen). $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[44]->id}); }
public function test_grade_grade_min_max_with_category_item() { global $CFG, $DB; $initialminmaxtouse = $CFG->grade_minmaxtouse; $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_user(); $coursegi = grade_item::fetch_course_item($course->id); // Create a category item. $gc = new grade_category(array('courseid' => $course->id, 'fullname' => 'test'), false); $gc->insert(); $gi = $gc->get_grade_item(); $gi->grademax = 100; $gi->grademin = 0; $gi->update(); // Fetch the category item. $giparams = array('itemtype' => 'category', 'iteminstance' => $gc->id); $gi = grade_item::fetch($giparams); $this->assertEquals(0, $gi->grademin); $this->assertEquals(100, $gi->grademax); // Give a grade to the student. $gi->update_final_grade($user->id, 10); // Check the grade min/max stored in gradebook. $gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id)); $this->assertEquals(0, $gg->get_grade_min()); $this->assertEquals(100, $gg->get_grade_max()); // Change the min/max grade of the item. $gi->grademin = 2; $gi->grademax = 50; $gi->update(); // Fetch the updated item. $gi = grade_item::fetch($giparams); // Now check the grade grade min/max with system setting. $CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM; grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting. $gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id)); $this->assertEquals(0, $gg->get_grade_min()); $this->assertEquals(100, $gg->get_grade_max()); // Now with other system setting. $CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE; grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting, and reset static cache. $gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id)); $this->assertEquals(0, $gg->get_grade_min()); $this->assertEquals(100, $gg->get_grade_max()); // Now with overriden setting in course. $CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM; grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE); $gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id)); $this->assertEquals(0, $gg->get_grade_min()); $this->assertEquals(100, $gg->get_grade_max()); $CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE; grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM); $gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id)); $this->assertEquals(0, $gg->get_grade_min()); $this->assertEquals(100, $gg->get_grade_max()); $CFG->grade_minmaxtouse = $initialminmaxtouse; }
/** * Test get_grades_items function case student */ public function test_get_grade_items_student() { $this->resetAfterTest(true); $s1grade = 80; $s2grade = 60; list($course, $teacher, $student1, $student2, $assignment) = $this->load_data($s1grade, $s2grade); grade_set_setting($course->id, 'report_user_showrank', 1); grade_set_setting($course->id, 'report_user_showpercentage', 1); grade_set_setting($course->id, 'report_user_showgrade', 1); grade_set_setting($course->id, 'report_user_showfeedback', 1); grade_set_setting($course->id, 'report_user_showweight', 1); grade_set_setting($course->id, 'report_user_showcontributiontocoursetotal', 1); grade_set_setting($course->id, 'report_user_showlettergrade', 1); grade_set_setting($course->id, 'report_user_showaverage', 1); $this->setUser($student1); $studentgrades = gradereport_user_external::get_grade_items($course->id, $student1->id); $studentgrades = external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); // No warnings returned. $this->assertCount(0, $studentgrades['warnings']); // Check that only grades for the student in the teacher group are returned. $this->assertCount(1, $studentgrades['usergrades']); $this->assertCount(2, $studentgrades['usergrades'][0]['gradeitems']); $this->assertEquals($course->id, $studentgrades['usergrades'][0]['courseid']); $this->assertEquals($student1->id, $studentgrades['usergrades'][0]['userid']); $this->assertEquals('mod', $studentgrades['usergrades'][0]['gradeitems'][0]['itemtype']); $this->assertEquals('assign', $studentgrades['usergrades'][0]['gradeitems'][0]['itemmodule']); $this->assertEquals($assignment->id, $studentgrades['usergrades'][0]['gradeitems'][0]['iteminstance']); $this->assertEquals($assignment->cmidnumber, $studentgrades['usergrades'][0]['gradeitems'][0]['cmid']); $this->assertEquals(0, $studentgrades['usergrades'][0]['gradeitems'][0]['itemnumber']); $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['outcomeid']); $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['scaleid']); $this->assertEquals(80, $studentgrades['usergrades'][0]['gradeitems'][0]['graderaw']); $this->assertEquals('80.00', $studentgrades['usergrades'][0]['gradeitems'][0]['gradeformatted']); $this->assertEquals(0, $studentgrades['usergrades'][0]['gradeitems'][0]['grademin']); $this->assertEquals(100, $studentgrades['usergrades'][0]['gradeitems'][0]['grademax']); $this->assertEquals('0–100', $studentgrades['usergrades'][0]['gradeitems'][0]['rangeformatted']); $this->assertEquals('80.00 %', $studentgrades['usergrades'][0]['gradeitems'][0]['percentageformatted']); $this->assertEmpty($studentgrades['usergrades'][0]['gradeitems'][0]['feedback']); $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradehiddenbydate']); $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeneedsupdate']); $this->assertFalse($studentgrades['usergrades'][0]['gradeitems'][0]['gradeishidden']); $this->assertEquals('B-', $studentgrades['usergrades'][0]['gradeitems'][0]['lettergradeformatted']); $this->assertEquals(1, $studentgrades['usergrades'][0]['gradeitems'][0]['rank']); $this->assertEquals(2, $studentgrades['usergrades'][0]['gradeitems'][0]['numusers']); $this->assertEquals(70, $studentgrades['usergrades'][0]['gradeitems'][0]['averageformatted']); // Hide one grade for the user. $gradegrade = new grade_grade(array('userid' => $student1->id, 'itemid' => $studentgrades['usergrades'][0]['gradeitems'][0]['id']), true); $gradegrade->set_hidden(1); $studentgrades = gradereport_user_external::get_grade_items($course->id, $student1->id); $studentgrades = external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); // Check we get only the course final grade. $this->assertCount(1, $studentgrades['usergrades']); $this->assertCount(1, $studentgrades['usergrades'][0]['gradeitems']); $this->assertEquals('course', $studentgrades['usergrades'][0]['gradeitems'][0]['itemtype']); }
/** * Configure current courses to use correct grading setting. * * Current courses should be forcibly set to use the 2.6 style grading initially. * * @return void */ function upgrade_mark_grading_configuration() { global $CFG, $DB; require_once $CFG->libdir . '/gradelib.php'; // Identify the courses that have inconsistencies grade_item vs grade_grade. $sql = "SELECT DISTINCT(gi.courseid)\n FROM {grade_items} gi\n JOIN {grade_grades} gg\n ON gg.itemid = gi.id\n WHERE (gi.itemtype != ? AND gi.itemtype != ?)\n AND (gg.rawgrademax != gi.grademax OR gg.rawgrademin != gi.grademin)"; $rs = $DB->get_recordset_sql($sql, array('course', 'category')); foreach ($rs as $record) { set_config('noshow_min_max_grades_changed_' . $record->courseid, 1); // Set course to use grade_item value for max/min calculations. grade_set_setting($record->courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM); } $rs->close(); }
/** * Validate that the version 1 plugin deletes appropriate associations when * deleting a course */ public function test_version1importdeletecoursedeletesassociations() { global $DB, $CFG, $USER; require_once $CFG->dirroot . '/user/lib.php'; require_once $CFG->dirroot . '/lib/gradelib.php'; require_once $CFG->dirroot . '/group/lib.php'; require_once $CFG->dirroot . '/lib/conditionlib.php'; require_once $CFG->dirroot . '/lib/enrollib.php'; require_once $CFG->dirroot . '/tag/lib.php'; require_once $CFG->dirroot . '/lib/questionlib.php'; // Setup. $initialnumcontexts = $DB->count_records('context', array('contextlevel' => CONTEXT_COURSE)); $DB->delete_records('block_instances'); // Set up the course with one section, including default blocks. set_config('defaultblocks_topics', 'search_forums'); set_config('maxsections', 10, 'moodlecourse'); $this->run_core_course_import(array('shortname' => 'deleteassociationsshortname', 'numsections' => 1)); // Create a user record. $record = new stdClass(); $record->username = '******'; $record->password = '******'; $userid = user_create_user($record); // Create a course-level role. $courseid = $DB->get_field('course', 'id', array('shortname' => 'deleteassociationsshortname')); $coursecontext = context_course::instance($courseid); $roleid = create_role('deleterole', 'deleterole', 'deleterole'); set_role_contextlevels($roleid, array(CONTEXT_COURSE)); $enrol = new stdClass(); $enrol->enrol = 'manual'; $enrol->courseid = $courseid; $enrol->status = ENROL_INSTANCE_ENABLED; if (!$DB->record_exists('enrol', (array) $enrol)) { $DB->insert_record('enrol', $enrol); } // Assign the user to the course-level role. enrol_try_internal_enrol($courseid, $userid, $roleid); // Create a grade item. $gradeitem = new grade_item(array('courseid' => $courseid, 'itemtype' => 'manual', 'itemname' => 'testitem'), false); $gradeitem->insert(); $gradegrade = new grade_grade(array('itemid' => $gradeitem->id, 'userid' => $userid), false); // Assign the user a grade. $gradegrade->insert(); // Create a grade outcome. $gradeoutcome = new grade_outcome(array('courseid' => $courseid, 'shortname' => 'bogusshortname', 'fullname' => 'bogusfullname')); $gradeoutcome->insert(); // Create a grade scale. $gradescale = new grade_scale(array('courseid' => $courseid, 'name' => 'bogusname', 'userid' => $userid, 'scale' => 'bogusscale', 'description' => 'bogusdescription')); $gradescale->insert(); // Set a grade setting value. grade_set_setting($courseid, 'bogus', 'bogus'); // Set up a grade letter. $gradeletter = new stdClass(); $gradeletter->contextid = $coursecontext->id; $gradeletter->lowerboundary = 80; $gradeletter->letter = 'A'; $DB->insert_record('grade_letters', $gradeletter); // Set up a forum instance. $forum = new stdClass(); $forum->course = $courseid; $forum->intro = 'intro'; $forum->id = $DB->insert_record('forum', $forum); // Add it as a course module. $forum->module = $DB->get_field('modules', 'id', array('name' => 'forum')); $forum->instance = $forum->id; $cmid = add_course_module($forum); // Set up a completion record. $completion = new stdClass(); $completion->coursemoduleid = $cmid; $completion->completionstate = 0; $completion->userid = 9999; $completion->timemodified = time(); $DB->insert_record('course_modules_completion', $completion); // Set up a completion condition. $forum->id = $cmid; $ci = new condition_info($forum, CONDITION_MISSING_EVERYTHING, false); $ci->add_completion_condition($cmid, COMPLETION_ENABLED); // Set the blocks position. $instances = $DB->get_records('block_instances', array('parentcontextid' => $coursecontext->id)); $page = new stdClass(); $page->context = $coursecontext; $page->pagetype = 'course-view-*'; $page->subpage = false; foreach ($instances as $instance) { blocks_set_visibility($instance, $page, 1); } // Create a group. $group = new stdClass(); $group->name = 'testgroup'; $group->courseid = $courseid; $groupid = groups_create_group($group); // Add the user to the group. groups_add_member($groupid, $userid); // Create a grouping containing our group. $grouping = new stdClass(); $grouping->name = 'testgrouping'; $grouping->courseid = $courseid; $groupingid = groups_create_grouping($grouping); groups_assign_grouping($groupingid, $groupid); // Set up a user tag. tag_set('course', $courseid, array('testtag')); // Add a course-level log. add_to_log($courseid, 'bogus', 'bogus'); // Set up the default course question category. $newcategory = question_make_default_categories(array($coursecontext)); // Create a test question. $question = new stdClass(); $question->qtype = 'truefalse'; $form = new stdClass(); $form->category = $newcategory->id; $form->name = 'testquestion'; $form->correctanswer = 1; $form->feedbacktrue = array('text' => 'bogustext', 'format' => FORMAT_HTML); $form->feedbackfalse = array('text' => 'bogustext', 'format' => FORMAT_HTML); $question = question_bank::get_qtype('truefalse')->save_question($question, $form); if (function_exists('course_set_display')) { // Set a "course display" setting. course_set_display($courseid, 1); } // Make a bogus backup record. $backupcourse = new stdClass(); $backupcourse->courseid = $courseid; $DB->insert_record('backup_courses', $backupcourse); // Add a user lastaccess record. $lastaccess = new stdClass(); $lastaccess->userid = $userid; $lastaccess->courseid = $courseid; $DB->insert_record('user_lastaccess', $lastaccess); // Make a bogus backup log record. $log = new stdClass(); $log->backupid = $courseid; $log->timecreated = time(); $log->loglevel = 1; $log->message = 'bogus'; $DB->insert_record('backup_logs', $log); // Get initial counts. $initialnumcourse = $DB->count_records('course'); $initialnumroleassignments = $DB->count_records('role_assignments'); $initialnumuserenrolments = $DB->count_records('user_enrolments'); $initialnumgradeitems = $DB->count_records('grade_items'); $initialnumgradegrades = $DB->count_records('grade_grades'); $initialnumgradeoutcomes = $DB->count_records('grade_outcomes'); $initialnumgradeoutcomescourses = $DB->count_records('grade_outcomes_courses'); $initialnumscale = $DB->count_records('scale'); $initialnumgradesettings = $DB->count_records('grade_settings'); $initialnumgradeletters = $DB->count_records('grade_letters'); $initialnumforum = $DB->count_records('forum'); $initialnumcoursemodules = $DB->count_records('course_modules'); $initialnumcoursemodulescompletion = $DB->count_records('course_modules_completion'); $initialnumcoursemodulesavailability = $DB->count_records('course_modules_availability'); $initialnumblockinstances = $DB->count_records('block_instances'); $initialnumblockpositions = $DB->count_records('block_positions'); $initialnumgroups = $DB->count_records('groups'); $initialnumgroupsmembers = $DB->count_records('groups_members'); $initialnumgroupings = $DB->count_records('groupings'); $initialnumgroupingsgroups = $DB->count_records('groupings_groups'); $initialnumtaginstance = $DB->count_records('tag_instance'); $initialnumcoursesections = $DB->count_records('course_sections'); $initialnumquestioncategories = $DB->count_records('question_categories'); $initialnumquestion = $DB->count_records('question'); if (self::$coursedisplay) { $initialnumcoursedisplay = $DB->count_records('course_display'); } $initialnumbackupcourses = $DB->count_records('backup_courses'); $initialnumuserlastaccess = $DB->count_records('user_lastaccess'); $initialnumbackuplogs = $DB->count_records('backup_logs'); // Delete the course. $data = array('action' => 'delete', 'shortname' => 'deleteassociationsshortname'); $this->run_core_course_import($data, false); // Validate the result. $this->assertEquals($DB->count_records('course'), $initialnumcourse - 1); $this->assertEquals($DB->count_records('role_assignments'), $initialnumroleassignments - 1); $this->assertEquals($DB->count_records('user_enrolments'), $initialnumuserenrolments - 1); $this->assertEquals($DB->count_records('grade_items'), $initialnumgradeitems - 2); $this->assertEquals($DB->count_records('grade_grades'), $initialnumgradegrades - 1); $this->assertEquals($DB->count_records('grade_outcomes'), $initialnumgradeoutcomes - 1); $this->assertEquals($DB->count_records('grade_outcomes_courses'), $initialnumgradeoutcomescourses - 1); $this->assertEquals($DB->count_records('scale'), $initialnumscale - 1); $this->assertEquals($DB->count_records('grade_settings'), $initialnumgradesettings - 1); $this->assertEquals($DB->count_records('grade_letters'), $initialnumgradeletters - 1); $this->assertEquals($DB->count_records('forum'), $initialnumforum - 1); $this->assertEquals($DB->count_records('course_modules'), $initialnumcoursemodules - 1); /* Uncomment the two lines below when this fix is available: http://tracker.moodle.org/browse/MDL-32988 $this->assertEquals($DB->count_records('course_modules_completion'), $initialnumcourse_modules_completion - 1); $this->assertEquals($DB->count_records('course_modules_availability'), $initialnumcourse_modules_availability - 1); */ $this->assertEquals($initialnumblockinstances - 4, $DB->count_records('block_instances')); $this->assertEquals($DB->count_records('block_positions'), 0); $this->assertEquals($DB->count_records('groups'), $initialnumgroups - 1); $this->assertEquals($DB->count_records('groups_members'), $initialnumgroupsmembers - 1); $this->assertEquals($DB->count_records('groupings'), $initialnumgroupings - 1); $this->assertEquals($DB->count_records('groupings_groups'), $initialnumgroupingsgroups - 1); $this->assertEquals($DB->count_records('log', array('course' => $courseid)), 0); $this->assertEquals($DB->count_records('tag_instance'), $initialnumtaginstance - 1); $this->assertEquals($DB->count_records('course_sections'), $initialnumcoursesections - 1); $this->assertEquals($DB->count_records('question_categories'), $initialnumquestioncategories - 1); $this->assertEquals($DB->count_records('question'), $initialnumquestion - 1); if (self::$coursedisplay) { $this->assertEquals($DB->count_records('course_display'), $initialnumcoursedisplay - 1); } $this->assertEquals($DB->count_records('backup_courses'), $initialnumbackupcourses - 1); $this->assertEquals($DB->count_records('user_lastaccess'), $initialnumuserlastaccess - 1); }