Exemple #1
0
 unset($itemdata->locktime);
 $convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
 foreach ($convert as $param) {
     if (property_exists($itemdata, $param)) {
         $itemdata->{$param} = unformat_float($itemdata->{$param});
     }
 }
 if (isset($itemdata->aggregationcoef2)) {
     $itemdata->aggregationcoef2 = $itemdata->aggregationcoef2 / 100.0;
 }
 // When creating a new category, a number of grade item fields are filled out automatically, and are required.
 // If the user leaves these fields empty during creation of a category, we let the default values take effect
 // Otherwise, we let the user-entered grade item values take effect
 $grade_item = $grade_category->load_grade_item();
 $grade_item_copy = fullclone($grade_item);
 grade_item::set_properties($grade_item, $itemdata);
 if (empty($grade_item->id)) {
     $grade_item->id = $grade_item_copy->id;
 }
 if (empty($grade_item->grademax) && $grade_item->grademax != '0') {
     $grade_item->grademax = $grade_item_copy->grademax;
 }
 if (empty($grade_item->grademin) && $grade_item->grademin != '0') {
     $grade_item->grademin = $grade_item_copy->grademin;
 }
 if (empty($grade_item->gradepass) && $grade_item->gradepass != '0') {
     $grade_item->gradepass = $grade_item_copy->gradepass;
 }
 if (empty($grade_item->aggregationcoef) && $grade_item->aggregationcoef != '0') {
     $grade_item->aggregationcoef = $grade_item_copy->aggregationcoef;
 }
 /**
  * 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});
 }