MDL-50062 gradebook: Fixed behaviour when changing aggregation mathods.

This commit is contained in:
Dave Cooper 2015-06-11 16:26:15 +08:00 committed by Frederic Massart
parent 032a4fe51c
commit 135efd5116
5 changed files with 60 additions and 46 deletions

View File

@ -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();
}
}

View File

@ -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 == '') {

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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();
}
}