MDL-46819 core_grades: Negative weights are changed to 0
authorAdrian Greeve <adrian@moodle.com>
Thu, 9 Oct 2014 08:33:53 +0000 (16:33 +0800)
committerAdrian Greeve <adrian@moodle.com>
Mon, 13 Oct 2014 00:19:24 +0000 (08:19 +0800)
Negative weights in the natural aggregation are not currently permitted.
Any figure below zero is changed to a zero.

grade/tests/behat/grade_natural_normalisation.feature
lib/grade/grade_category.php

index 5512a3f..a2d50cb 100644 (file)
@@ -250,3 +250,18 @@ Feature: We can use natural aggregation and weights will be normalised to a tota
   Then the field "Weight of Test assignment five" matches value "80.0"
   And the field "Weight of Test assignment six" matches value "40.0"
   And the field "Weight of Test assignment seven" matches value "60.0"
+
+  @javascript
+  Scenario: Overriding a grade item with a negative value results in the value being changed to zero.
+
+  When I set the field "Override weight of Test assignment five" to "1"
+  And I set the field "Weight of Test assignment five" to "-15"
+  And I press "Save changes"
+  Then the field "Weight of Test assignment five" matches value "0.0"
+  And the field "Weight of Test assignment six" matches value "40.0"
+  And the field "Weight of Test assignment seven" matches value "60.0"
+  And I set the field "Override weight of Test assignment six" to "1"
+  And I set the field "Weight of Test assignment six" to "-25"
+  And I press "Save changes"
+  Then the field "Weight of Test assignment six" matches value "0.0"
+  And the field "Weight of Test assignment seven" matches value "100.0"
index b70030c..32fd6f5 100644 (file)
@@ -1373,10 +1373,13 @@ class grade_category extends grade_object {
                             (1 - $totaloverriddenweight);
                 }
                 $gradeitem->update();
-            } else if ((!$automaticgradeitemspresent && $normalisetotal != 1) || ($requiresnormalising)) {
+            } else if ((!$automaticgradeitemspresent && $normalisetotal != 1) || ($requiresnormalising)
+                    || $overridearray[$gradeitem->id]['weight'] < 0) {
                 // Just divide the overriden weight for this item against the total weight override of all
                 // items in this category.
-                if ($normalisetotal == 0) {
+                if ($normalisetotal == 0 || $overridearray[$gradeitem->id]['weight'] < 0) {
+                    // If the normalised total equals zero, or the weight value is less than zero,
+                    // set the weight for the grade item to zero.
                     $gradeitem->aggregationcoef2 = 0;
                 } else {
                     $gradeitem->aggregationcoef2 = $overridearray[$gradeitem->id]['weight'] / $normalisetotal;