Skip to content

Commit

Permalink
MDL-50062 gradebook: Fixed behaviour when changing aggregation mathods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Cooper authored and Frederic Massart committed Aug 6, 2015
1 parent eb1d954 commit b8ca792
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
16 changes: 6 additions & 10 deletions course/modlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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();
}
}
Expand Down
8 changes: 3 additions & 5 deletions grade/edit/tree/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@
} 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 == '') {
Expand Down
68 changes: 44 additions & 24 deletions lib/grade/grade_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2430,31 +2430,17 @@ public static function set_properties(&$instance, $params) {

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)) {

//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;

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;
}
if (!empty($params) && isset($params->aggregation)) {
// If aggregation has changed, find the appropriate aggregation coefficients.
$defaultcoefficients = self::get_default_aggregation_coefficient_values($params->aggregation);

$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);
}
}
$updateparams = array(
'categoryid' => $instance->id,
'aggregationcoef' => $defaultcoefficients['aggregationcoefficient'],
'aggregationcoef2' => $defaultcoefficients['aggregationcoefficient2']
);

$DB->execute("update {grade_items} set aggregationcoef=:aggregationcoef, aggregationcoef2=:aggregationcoef2 where categoryid=:categoryid", $updateparams);
}
}

Expand Down Expand Up @@ -2562,4 +2548,38 @@ public static function updated_forced_settings() {
$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;
}
}
8 changes: 5 additions & 3 deletions lib/grade/grade_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -1345,11 +1345,13 @@ public function set_parent($parentid) {
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();
Expand Down
8 changes: 3 additions & 5 deletions mod/workshop/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit b8ca792

Please sign in to comment.