From b8ca7925c0382c3ec2d6112fc081d331de5a9288 Mon Sep 17 00:00:00 2001 From: Dave Cooper Date: Thu, 11 Jun 2015 16:26:15 +0800 Subject: [PATCH] MDL-50062 gradebook: Fixed behaviour when changing aggregation mathods. --- course/modlib.php | 16 ++++----- grade/edit/tree/item.php | 8 ++--- lib/grade/grade_category.php | 66 +++++++++++++++++++++++------------- lib/grade/grade_item.php | 8 +++-- mod/workshop/lib.php | 8 ++--- 5 files changed, 60 insertions(+), 46 deletions(-) diff --git a/course/modlib.php b/course/modlib.php index 3d492644c53..3d750b176d6 100644 --- a/course/modlib.php +++ b/course/modlib.php @@ -235,11 +235,9 @@ function edit_module_post_actions($moduleinfo, $course) { $gradecategory = $grade_item->get_parent_category(); if (!empty($moduleinfo->add)) { if (grade_category::aggregation_uses_aggregationcoef($gradecategory->aggregation)) { - if ($gradecategory->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { - $grade_item->aggregationcoef = 1; - } else { - $grade_item->aggregationcoef = 0; - } + $defaults = grade_category::get_default_aggregation_coefficient_values($gradecategory->aggregation); + $grade_item->aggregationcoef = $defaults['aggregationcoefficient']; + $grade_item->aggregationcoef2 = $default['aggregationcoefficient2']; $grade_item->update(); } } @@ -304,11 +302,9 @@ function edit_module_post_actions($moduleinfo, $course) { $gradecategory = $outcome_item->get_parent_category(); if ($outcomeexists == false) { if (grade_category::aggregation_uses_aggregationcoef($gradecategory->aggregation)) { - if ($gradecategory->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { - $outcome_item->aggregationcoef = 1; - } else { - $outcome_item->aggregationcoef = 0; - } + $defaults = grade_category::get_default_aggregation_coefficient_values($gradecategory->aggregation); + $outcome_item->aggregationcoef = $defaults['aggregationcoefficient']; + $outcome_item->aggregationcoef2 = $defaults['aggregationcoefficient2']; $outcome_item->update(); } } diff --git a/grade/edit/tree/item.php b/grade/edit/tree/item.php index acbf5812a71..779cd520766 100644 --- a/grade/edit/tree/item.php +++ b/grade/edit/tree/item.php @@ -112,11 +112,9 @@ if ($mform->is_cancelled()) { } else if ($data = $mform->get_data(false)) { // If unset, give the aggregationcoef a default based on parent aggregation method if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') { - if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { - $data->aggregationcoef = 1; - } else { - $data->aggregationcoef = 0; - } + $defaults = grade_category::get_default_aggregation_coefficient_values($parent_category->aggregation); + $data->aggregationcoef = $defaults['aggregationcoefficient']; + $data->aggregationcoef2 = $defaults['aggregationcoefficient2']; } if (!isset($data->gradepass) || $data->gradepass == '') { diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index fc80f248313..a54c55baabc 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -2430,31 +2430,17 @@ class grade_category extends grade_object { parent::set_properties($instance, $params); - //if they've changed aggregation type we made need to do some fiddling to provide appropriate defaults - if (!empty($params->aggregation)) { + if (!empty($params) && isset($params->aggregation)) { + // If aggregation has changed, find the appropriate aggregation coefficients. + $defaultcoefficients = self::get_default_aggregation_coefficient_values($params->aggregation); - //weight and extra credit share a column :( Would like a default of 1 for weight and 0 for extra credit - //Flip from the default of 0 to 1 (or vice versa) if ALL items in the category are still set to the old default. - if (self::aggregation_uses_aggregationcoef($params->aggregation)) { - $sql = $defaultaggregationcoef = null; + $updateparams = array( + 'categoryid' => $instance->id, + 'aggregationcoef' => $defaultcoefficients['aggregationcoefficient'], + 'aggregationcoef2' => $defaultcoefficients['aggregationcoefficient2'] + ); - if (!self::aggregation_uses_extracredit($params->aggregation)) { - //if all items in this category have aggregation coefficient of 0 we can change it to 1 ie evenly weighted - $sql = "select count(id) from {grade_items} where categoryid=:categoryid and aggregationcoef!=0"; - $defaultaggregationcoef = 1; - } else { - //if all items in this category have aggregation coefficient of 1 we can change it to 0 ie no extra credit - $sql = "select count(id) from {grade_items} where categoryid=:categoryid and aggregationcoef!=1"; - $defaultaggregationcoef = 0; - } - - $params = array('categoryid'=>$instance->id); - $count = $DB->count_records_sql($sql, $params); - if ($count===0) { //category is either empty or all items are set to a default value so we can switch defaults - $params['aggregationcoef'] = $defaultaggregationcoef; - $DB->execute("update {grade_items} set aggregationcoef=:aggregationcoef where categoryid=:categoryid",$params); - } - } + $DB->execute("update {grade_items} set aggregationcoef=:aggregationcoef, aggregationcoef2=:aggregationcoef2 where categoryid=:categoryid", $updateparams); } } @@ -2562,4 +2548,38 @@ class grade_category extends grade_object { $sql = "UPDATE {grade_items} SET needsupdate=? WHERE itemtype=? or itemtype=?"; $DB->execute($sql, $params); } + + /** + * Determine the default aggregation coefficients for a given aggregation method. + * + * @param int $aggregationmethod The aggregation method to be inspected + * @return array $defaultcoefficients The default values to use + */ + public static function get_default_aggregation_coefficient_values($aggregationmethod) { + $defaultcoefficients = array(); + + switch ($aggregationmethod) { + case GRADE_AGGREGATE_WEIGHTED_MEAN: + $defaultcoefficients['aggregationcoefficient'] = 1; + $defaultcoefficients['aggregationcoefficient2'] = 0; + break; + case GRADE_AGGREGATE_SUM: + $defaultcoefficients['aggregationcoefficient'] = 0; + $defaultcoefficients['aggregationcoefficient2'] = 1; + break; + case GRADE_AGGREGATE_WEIGHTED_MEAN2: + case GRADE_AGGREGATE_EXTRACREDIT_MEAN: + case GRADE_AGGREGATE_MEAN: + case GRADE_AGGREGATE_MEDIAN: + case GRADE_AGGREGATE_MIN: + case GRADE_AGGREGATE_MAX: + case GRADE_AGGREGATE_MODE: + default: + $defaultcoefficients['aggregationcoefficient'] = 0; + $defaultcoefficients['aggregationcoefficient2'] = 0; + break; + } + + return $defaultcoefficients; + } } diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 267e6ada450..4d4d954b0af 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -1345,11 +1345,13 @@ class grade_item extends grade_object { return false; } - // MDL-19407 If moving from a non-SWM category to a SWM category, convert aggregationcoef to 0 $currentparent = $this->load_parent_category(); - if ($currentparent->aggregation != GRADE_AGGREGATE_WEIGHTED_MEAN2 && $parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) { - $this->aggregationcoef = 0; + if ($currentparent->aggregation != $parent_category->aggregation) { + // If we're changing category, set appropriate default aggregation coefficients. + $defaults = grade_category::get_default_aggregation_coefficient_values($parent_category->aggregation); + $this->aggregationcoef = $defaults['aggregationcoefficient']; + $this->aggregationcoef2 = $defaults['aggregationcoefficient2']; } $this->force_regrading(); diff --git a/mod/workshop/lib.php b/mod/workshop/lib.php index 9fe500daa0a..6fe33d71662 100644 --- a/mod/workshop/lib.php +++ b/mod/workshop/lib.php @@ -1190,11 +1190,9 @@ function workshop_grade_item_category_update($workshop) { if (!empty($workshop->add)) { $gradecategory = $gradeitem->get_parent_category(); if (grade_category::aggregation_uses_aggregationcoef($gradecategory->aggregation)) { - if ($gradecategory->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { - $gradeitem->aggregationcoef = 1; - } else { - $gradeitem->aggregationcoef = 0; - } + $defaults = grade_category::get_default_aggregation_coefficient_values($gradecategory->aggregation); + $gradeitem->aggregationcoef = $defaults['aggregationcoefficient']; + $gradeitem->aggregationcoef2 = $defaults['aggregationcoefficient2']; $gradeitem->update(); } }