Merge branch 'MDL-50757-master' of git://github.com/damyon/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Fri, 3 Jul 2015 09:26:18 +0000 (11:26 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Fri, 3 Jul 2015 09:26:18 +0000 (11:26 +0200)
lib/db/upgrade.php
lib/db/upgradelib.php
lib/tests/upgradelib_test.php

index 280a713..10992e8 100644 (file)
@@ -4061,6 +4061,8 @@ function xmldb_main_upgrade($oldversion) {
     if ($oldversion < 2014111000.00) {
         // Coming from 2.7 or older, we need to flag the step minmaxgrade to be ignored.
         set_config('upgrade_minmaxgradestepignored', 1);
+        // Coming from 2.7 or older, we need to flag the step for changing calculated grades to be regraded.
+        set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
 
         // Main savepoint reached.
         upgrade_main_savepoint(true, 2014111000.00);
@@ -4431,6 +4433,8 @@ function xmldb_main_upgrade($oldversion) {
 
             // To skip running the same script on the upgrade to the next major release.
             set_config('upgrade_calculatedgradeitemsignored', 1);
+            // This config value is never used again.
+            unset_config('upgrade_calculatedgradeitemsonlyregrade');
         }
 
         // Main savepoint reached.
index bb0248e..2c901e6 100644 (file)
@@ -587,7 +587,10 @@ function upgrade_calculated_grade_items($courseid = null) {
         $singlecoursesql = "AND ns.id = :courseid";
         $params['courseid'] = $courseid;
     }
-    $siteminmaxtouse = $CFG->grade_minmaxtouse;
+    $siteminmaxtouse = 1;
+    if (isset($CFG->grade_minmaxtouse)) {
+        $siteminmaxtouse = $CFG->grade_minmaxtouse;
+    }
     $courseidsql = "SELECT ns.id
                       FROM (
                         SELECT c.id, coalesce(" . $DB->sql_compare_text('gs.value') . ", :siteminmax) AS gradevalue
@@ -653,11 +656,15 @@ function upgrade_calculated_grade_items($courseid = null) {
         }
     }
 
-    foreach ($affectedcourses as $courseid) {
-        // Check to see if the gradebook freeze is already in affect.
-        $gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid->courseid);
-        if (!$gradebookfreeze) {
-            set_config('gradebook_calculations_freeze_' . $courseid->courseid, 20150627);
+    foreach ($affectedcourses as $affectedcourseid) {
+        if (isset($CFG->upgrade_calculatedgradeitemsonlyregrade) && !($courseid)) {
+            $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $affectedcourseid->courseid));
+        } else {
+            // Check to see if the gradebook freeze is already in affect.
+            $gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $affectedcourseid->courseid);
+            if (!$gradebookfreeze) {
+                set_config('gradebook_calculations_freeze_' . $affectedcourseid->courseid, 20150627);
+            }
         }
     }
 }
\ No newline at end of file
index 3809095..fc06095 100644 (file)
@@ -549,7 +549,7 @@ class core_upgradelib_testcase extends advanced_testcase {
     /**
      * Test the upgrade function for flagging courses with calculated grade item problems.
      */
-    public function test_upgrade_calculated_grade_items() {
+    public function test_upgrade_calculated_grade_items_freeze() {
         global $DB, $CFG;
         $this->resetAfterTest();
 
@@ -676,4 +676,69 @@ class core_upgradelib_testcase extends advanced_testcase {
         $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course1->id});
         $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course2->id});
     }
+
+    function test_upgrade_calculated_grade_items_regrade() {
+        global $DB, $CFG;
+        $this->resetAfterTest();
+
+        // Create a user.
+        $user = $this->getDataGenerator()->create_user();
+
+        // Create a course.
+        $course = $this->getDataGenerator()->create_course();
+
+        // Enrol the user in the course.
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+        $maninstance1 = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
+        $manual = enrol_get_plugin('manual');
+        $manual->enrol_user($maninstance1, $user->id, $studentrole->id);
+
+        set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
+
+        // Creating a category for a grade item.
+        $gradecategory = new grade_category();
+        $gradecategory->fullname = 'calculated grade category';
+        $gradecategory->courseid = $course->id;
+        $gradecategory->insert();
+        $gradecategoryid = $gradecategory->id;
+
+        // This is a manual grade item.
+        $gradeitem = new grade_item();
+        $gradeitem->itemname = 'grade item one';
+        $gradeitem->itemtype = 'manual';
+        $gradeitem->categoryid = $gradecategoryid;
+        $gradeitem->courseid = $course->id;
+        $gradeitem->idnumber = 'gi1';
+        $gradeitem->insert();
+
+        // Changing the category into a calculated grade category.
+        $gradecategoryitem = grade_item::fetch(array('iteminstance' => $gradecategory->id));
+        $gradecategoryitem->calculation = '=##gi' . $gradeitem->id . '##/2';
+        $gradecategoryitem->grademax = 50;
+        $gradecategoryitem->grademin = 15;
+        $gradecategoryitem->update();
+
+        // Setting a grade for the student.
+        $grade = $gradeitem->get_grade($user->id, true);
+        $grade->finalgrade = 50;
+        $grade->update();
+
+        grade_regrade_final_grades($course->id);
+        $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id));
+        $grade->rawgrademax = 100;
+        $grade->rawgrademin = 0;
+        $grade->update();
+        $this->assertNotEquals($gradecategoryitem->grademax, $grade->rawgrademax);
+        $this->assertNotEquals($gradecategoryitem->grademin, $grade->rawgrademin);
+
+        // This is the function that we are testing. If we comment out this line, then the test fails because the grade items
+        // are not flagged for regrading.
+        upgrade_calculated_grade_items();
+        grade_regrade_final_grades($course->id);
+
+        $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id));
+
+        $this->assertEquals($gradecategoryitem->grademax, $grade->rawgrademax);
+        $this->assertEquals($gradecategoryitem->grademin, $grade->rawgrademin);
+    }
 }