From 488366bed8374ab32e351da206e8037845565b8f Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 1 Mar 2016 16:17:56 +0800 Subject: [PATCH] MDL-53301 grades: Update gradeitems aggregationcoef2 only when required --- lib/grade/grade_category.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 672fc08f2f4..ea6c24cd4cf 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -1596,19 +1596,20 @@ class grade_category extends grade_object { } else if (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE) { // We will not aggregate the scales, so we can ignore upating their weights. continue; - } - - if (!$oldextracreditcalculation && $gradeitem->aggregationcoef > 0) { - // For an item with extra credit ignore other weigths and overrides. - // Do not change anything at all if it's weight was already overridden. - if (!$gradeitem->weightoverride) { - $gradeitem->aggregationcoef2 = $totalgrademax ? ($gradeitem->grademax / $totalgrademax) : 0; - $gradeitem->update(); - } + } else if (!$oldextracreditcalculation && $gradeitem->aggregationcoef > 0 && $gradeitem->weightoverride) { + // For an item with extra credit ignore other weigths and overrides but do not change anything at all + // if it's weight was already overridden. continue; } - if (!$gradeitem->weightoverride) { + // Store the previous value here, no need to update if it is the same value. + $prevaggregationcoef2 = $gradeitem->aggregationcoef2; + + if (!$oldextracreditcalculation && $gradeitem->aggregationcoef > 0 && !$gradeitem->weightoverride) { + // For an item with extra credit ignore other weigths and overrides. + $gradeitem->aggregationcoef2 = $totalgrademax ? ($gradeitem->grademax / $totalgrademax) : 0; + + } else if (!$gradeitem->weightoverride) { // Calculations with a grade maximum of zero will cause problems. Just set the weight to zero. if ($totaloverriddenweight >= 1 || $totalnonoverriddengrademax == 0 || $gradeitem->grademax == 0) { // There is no more weight to distribute. @@ -1619,7 +1620,7 @@ class grade_category extends grade_object { $gradeitem->aggregationcoef2 = ($gradeitem->grademax/$totalnonoverriddengrademax) * (1 - $totaloverriddenweight); } - $gradeitem->update(); + } 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 @@ -1631,6 +1632,9 @@ class grade_category extends grade_object { } else { $gradeitem->aggregationcoef2 = $overridearray[$gradeitem->id]['weight'] / $normalisetotal; } + } + + if (grade_floatval($prevaggregationcoef2) !== grade_floatval($gradeitem->aggregationcoef2)) { // Update the grade item to reflect these changes. $gradeitem->update(); }