Merge branch 'MDL-50062-28' of git://github.com/FMCorz/moodle into MOODLE_28_STABLE

This commit is contained in:
Eloy Lafuente (stronk7)
2015-08-12 01:45:30 +02:00
11 changed files with 806 additions and 72 deletions
-22
View File
@@ -224,17 +224,6 @@ function edit_module_post_actions($moduleinfo, $course) {
// Use updated grade_item.
$grade_item = $items[$itemid];
}
$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;
}
$grade_item->update();
}
}
}
}
@@ -293,17 +282,6 @@ function edit_module_post_actions($moduleinfo, $course) {
} else if (isset($moduleinfo->gradecat)) {
$outcome_item->set_parent($moduleinfo->gradecat);
}
$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;
}
$outcome_item->update();
}
}
}
}
}
+15 -7
View File
@@ -110,13 +110,19 @@ if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data(false)) {
// If unset, give the aggregationcoef a default based on parent aggregation method
// This is a new item, and the category chosen is different than the default category.
if (empty($grade_item->id) && isset($data->parentcategory) && $parent_category->id != $data->parentcategory) {
$parent_category = grade_category::fetch(array('id' => $data->parentcategory));
}
// If unset, give the aggregation values a default based on parent aggregation method.
$defaults = grade_category::get_default_aggregation_coefficient_values($parent_category->aggregation);
if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') {
if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
$data->aggregationcoef = 1;
} else {
$data->aggregationcoef = 0;
}
$data->aggregationcoef = $defaults['aggregationcoef'];
}
if (!isset($data->weightoverride)) {
$data->weightoverride = $defaults['weightoverride'];
}
if (!isset($data->gradepass) || $data->gradepass == '') {
@@ -145,6 +151,8 @@ if ($mform->is_cancelled()) {
}
if (isset($data->aggregationcoef2) && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
$data->aggregationcoef2 = $data->aggregationcoef2 / 100.0;
} else {
$data->aggregationcoef2 = $defaults['aggregationcoef2'];
}
$grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
@@ -162,7 +170,7 @@ if ($mform->is_cancelled()) {
// set parent if needed
if (isset($data->parentcategory)) {
$grade_item->set_parent($data->parentcategory, 'gradebook');
$grade_item->set_parent($data->parentcategory, false);
}
} else {
+15 -3
View File
@@ -114,8 +114,18 @@ $mform->set_data($item);
if ($data = $mform->get_data()) {
if (!isset($data->aggregationcoef)) {
$data->aggregationcoef = 0;
// This is a new item, and the category chosen is different than the default category.
if (empty($grade_item->id) && isset($data->parentcategory) && $parent_category->id != $data->parentcategory) {
$parent_category = grade_category::fetch(array('id' => $data->parentcategory));
}
// If unset, give the aggregation values a default based on parent aggregation method.
$defaults = grade_category::get_default_aggregation_coefficient_values($parent_category->aggregation);
if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') {
$data->aggregationcoef = $defaults['aggregationcoef'];
}
if (!isset($data->weightoverride)) {
$data->weightoverride = $defaults['weightoverride'];
}
if (property_exists($data, 'calculation')) {
@@ -140,6 +150,8 @@ if ($data = $mform->get_data()) {
}
if (isset($data->aggregationcoef2) && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
$data->aggregationcoef2 = $data->aggregationcoef2 / 100.0;
} else {
$data->aggregationcoef2 = $defaults['aggregationcoef2'];
}
$grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
@@ -200,7 +212,7 @@ if ($data = $mform->get_data()) {
} else {
// set parent if needed
if (isset($data->parentcategory)) {
$grade_item->set_parent($data->parentcategory, 'gradebook');
$grade_item->set_parent($data->parentcategory, false);
}
}
@@ -0,0 +1,472 @@
@core @core_grades
Feature: Changing the aggregation of an item affects its weight and extra credit definition
In order to switch to another aggregation method
As an teacher
I need to be able to edit the grade category settings
Background:
Given the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "grade categories" exist:
| fullname | course | aggregation |
| Cat mean | C1 | 0 |
| Cat median | C1 | 2 |
| Cat min | C1 | 4 |
| Cat max | C1 | 6 |
| Cat mode | C1 | 8 |
| Cat weighted | C1 | 10 |
| Cat weighted2 | C1 | 10 |
| Cat simple | C1 | 11 |
| Cat ec | C1 | 12 |
| Cat natural | C1 | 13 |
And the following "grade items" exist:
| itemname | course | category | aggregationcoef | aggregationcoef2 | weightoverride |
| Item a1 | C1 | ? | 0 | 0 | 0 |
| Item a2 | C1 | ? | 0 | 0.40 | 1 |
| Item a3 | C1 | ? | 1 | 0.10 | 1 |
| Item a4 | C1 | ? | 1 | 0 | 0 |
| Item b1 | C1 | Cat natural | 0 | 0 | 0 |
| Item b2 | C1 | Cat natural | 0 | 0.40 | 1 |
| Item b3 | C1 | Cat natural | 1 | 0.10 | 1 |
| Item b4 | C1 | Cat natural | 1 | 0 | 0 |
And I log in as "admin"
And I set the following administration settings values:
| grade_aggregations_visible | Mean of grades,Weighted mean of grades,Simple weighted mean of grades,Mean of grades (with extra credits),Median of grades,Lowest grade,Highest grade,Mode of grades,Natural |
And I am on site homepage
And I follow "Course 1"
And I navigate to "Grades" node in "Course administration"
And I navigate to "Grader report" node in "Grade administration"
And I turn editing mode on
And I follow "Edit Cat mean"
And I set the following fields to these values:
| Weight adjusted | 1 |
| Weight | 20 |
| Extra credit | 0 |
And I press "Save changes"
And I follow "Edit Cat median"
And I set the following fields to these values:
| Weight adjusted | 1 |
| Weight | 5 |
| Extra credit | 0 |
And I press "Save changes"
And I follow "Edit Cat min"
And I set the following fields to these values:
| Weight adjusted | 0 |
| Weight | 0 |
| Extra credit | 1 |
And I press "Save changes"
And I follow "Edit Item a1"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a2"
And the field "Weight adjusted" matches value "1"
And the field "id_aggregationcoef2" matches value "40.0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a3"
And the field "Weight adjusted" matches value "1"
And the field "id_aggregationcoef2" matches value "10.0"
And the field "Extra credit" matches value "1"
And I press "Cancel"
And I follow "Edit Item a4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "1"
And I press "Cancel"
And I follow "Edit Item b1"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b2"
And the field "Weight adjusted" matches value "1"
And the field "id_aggregationcoef2" matches value "40.0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b3"
And the field "Weight adjusted" matches value "1"
And the field "id_aggregationcoef2" matches value "10.0"
And the field "Extra credit" matches value "1"
And I press "Cancel"
And I follow "Edit Item b4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "1"
And I press "Cancel"
Scenario: Switching a category from Natural aggregation to Mean of grades and back
Given I follow "Edit Course 1"
And I set the field "Aggregation" to "Mean of grades"
When I press "Save changes"
And I follow "Edit Item a1"
Then I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Item a2"
And I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Item a3"
And I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Item a4"
And I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Cat mean"
And I expand all fieldsets
And I should not see "Weight adjusted"
And I should not see "Weight" in the "#id_headerparent" "css_element"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Cat median"
And I expand all fieldsets
And I should not see "Weight adjusted"
And I should not see "Weight" in the "#id_headerparent" "css_element"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Cat min"
And I expand all fieldsets
And I should not see "Weight adjusted"
And I should not see "Weight" in the "#id_headerparent" "css_element"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Cat natural"
And I set the field "Aggregation" to "Mean of grades"
And I press "Save changes"
And I follow "Edit Item b1"
And I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Item b2"
And I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Item b3"
And I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
And I follow "Edit Item b4"
And I should not see "Weight adjusted"
And I should not see "Weight"
And I should not see "Extra credit"
And I press "Cancel"
# Switching back.
And I follow "Edit Course 1"
And I set the field "Aggregation" to "Natural"
And I press "Save changes"
And I follow "Edit Item a1"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a2"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a3"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat mean"
And I expand all fieldsets
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat median"
And I expand all fieldsets
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat min"
And I expand all fieldsets
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat natural"
And I set the field "Aggregation" to "Natural"
And I press "Save changes"
And I follow "Edit Item b1"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b2"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b3"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
Scenario: Switching a category from Natural aggregation to Weighted mean of grades and back
Given I follow "Edit Course 1"
And I set the field "Aggregation" to "Weighted mean of grades"
When I press "Save changes"
And I follow "Edit Item a1"
Then I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Item a2"
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Item a3"
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Item a4"
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Cat mean"
And I expand all fieldsets
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Cat median"
And I expand all fieldsets
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Cat min"
And I expand all fieldsets
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Cat natural"
And I set the field "Aggregation" to "Weighted mean of grades"
And I press "Save changes"
And I follow "Edit Item b1"
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Item b2"
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Item b3"
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
And I follow "Edit Item b4"
And I should not see "Weight adjusted"
And I should not see "Extra credit"
And the field "Item weight" matches value "1"
And I press "Cancel"
# Switching back.
And I follow "Edit Course 1"
And I set the field "Aggregation" to "Natural"
And I press "Save changes"
And I follow "Edit Item a1"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a2"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a3"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat mean"
And I expand all fieldsets
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat median"
And I expand all fieldsets
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat min"
And I expand all fieldsets
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Cat natural"
And I set the field "Aggregation" to "Natural"
And I press "Save changes"
And I follow "Edit Item b1"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b2"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b3"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item b4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
@javascript
Scenario: Switching grade items between categories
# Move to same aggregation (Natural).
Given I navigate to "Categories and items" node in "Grade administration > Setup"
And I set the field "Select Item a1" to "1"
And I set the field "Select Item a2" to "1"
And I set the field "Select Item a3" to "1"
And I set the field "Select Item a4" to "1"
When I select "Cat natural" from the "Move selected items to" singleselect
And I navigate to "Grader report" node in "Grade administration"
And I follow "Edit Item a1"
Then the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a2"
And the field "Weight adjusted" matches value "1"
And the field "id_aggregationcoef2" matches value "40.0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a3"
And the field "Weight adjusted" matches value "1"
And the field "id_aggregationcoef2" matches value "10.0"
And the field "Extra credit" matches value "1"
And I press "Cancel"
And I follow "Edit Item a4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "1"
And I press "Cancel"
# Move to Mean of grades (with extra credit).
And I navigate to "Categories and items" node in "Grade administration > Setup"
And I set the field "Select Item a1" to "1"
And I set the field "Select Item a2" to "1"
And I set the field "Select Item a3" to "1"
And I set the field "Select Item a4" to "1"
And I select "Cat ec" from the "Move selected items to" singleselect
And I navigate to "Grader report" node in "Grade administration"
And I follow "Edit Item a1"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a2"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a3"
And the field "Extra credit" matches value "1"
And I press "Cancel"
And I follow "Edit Item a4"
And the field "Extra credit" matches value "1"
And I press "Cancel"
# Move to Simple weight mean of grades.
And I navigate to "Categories and items" node in "Grade administration > Setup"
And I set the field "Select Item a1" to "1"
And I set the field "Select Item a2" to "1"
And I set the field "Select Item a3" to "1"
And I set the field "Select Item a4" to "1"
And I select "Cat simple" from the "Move selected items to" singleselect
And I navigate to "Grader report" node in "Grade administration"
And I follow "Edit Item a1"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a2"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a3"
And the field "Extra credit" matches value "1"
And I press "Cancel"
And I follow "Edit Item a4"
And the field "Extra credit" matches value "1"
And I press "Cancel"
# Move to Weighted mean of grades.
And I navigate to "Categories and items" node in "Grade administration > Setup"
And I set the field "Select Item a1" to "1"
And I set the field "Select Item a2" to "1"
And I set the field "Select Item a3" to "1"
And I set the field "Select Item a4" to "1"
And I select "Cat weighted" from the "Move selected items to" singleselect
And I navigate to "Grader report" node in "Grade administration"
And I follow "Edit Item a1"
And the field "Item weight" matches value "1"
And I set the field "Item weight" to "2"
And I press "Save changes"
And I follow "Edit Item a2"
And the field "Item weight" matches value "1"
And I set the field "Item weight" to "5"
And I press "Save changes"
And I follow "Edit Item a3"
And the field "Item weight" matches value "1"
And I set the field "Item weight" to "8"
And I press "Save changes"
And I follow "Edit Item a4"
And the field "Item weight" matches value "1"
And I set the field "Item weight" to "11"
And I press "Save changes"
# Move to same (Weighted mean of grades).
And I navigate to "Categories and items" node in "Grade administration > Setup"
And I set the field "Select Item a1" to "1"
And I set the field "Select Item a2" to "1"
And I set the field "Select Item a3" to "1"
And I set the field "Select Item a4" to "1"
And I select "Cat weighted2" from the "Move selected items to" singleselect
And I wait "2" seconds
And I navigate to "Grader report" node in "Grade administration"
And I follow "Edit Item a1"
And the field "Item weight" matches value "2"
And I press "Save changes"
And I follow "Edit Item a2"
And the field "Item weight" matches value "5"
And I press "Save changes"
And I follow "Edit Item a3"
And the field "Item weight" matches value "8"
And I press "Save changes"
And I follow "Edit Item a4"
And the field "Item weight" matches value "11"
And I press "Save changes"
# Move back to Natural.
And I navigate to "Categories and items" node in "Grade administration > Setup"
And I set the field "Select Item a1" to "1"
And I set the field "Select Item a2" to "1"
And I set the field "Select Item a3" to "1"
And I set the field "Select Item a4" to "1"
And I select "Course 1" from the "Move selected items to" singleselect
And I navigate to "Grader report" node in "Grade administration"
And I follow "Edit Item a1"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a2"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a3"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
And I follow "Edit Item a4"
And the field "Weight adjusted" matches value "0"
And the field "Extra credit" matches value "0"
And I press "Cancel"
+1 -1
View File
@@ -159,7 +159,7 @@ Feature: View gradebook when scales are used
Examples:
| aggregation | coursetotal1 | coursetotal2 | coursetotal3 | coursetotal4 | coursetotal5 |overallavg | courseperc2 | courseperc3 | contrib2 | contrib3 |
| Mean of grades | 100.00 | 75.00 | 50.00 | 25.00 | 0.00 | 50.00 | 75.00 % | 50.00 % | 75.00 % | 50.00 % |
| Weighted mean of grades | - | - | - | - | - | - | - | - | 0.00 % | 0.00 % |
| Weighted mean of grades | 100.00 | 75.00 | 50.00 | 25.00 | 0.00 | 50.00 | 75.00 % | 50.00 % | 75.00 % | 50.00 % |
| Simple weighted mean of grades | 100.00 | 75.00 | 50.00 | 25.00 | 0.00 | 50.00 | 75.00 % | 50.00 % | 75.00 % | 50.00 % |
| Mean of grades (with extra credits) | 100.00 | 75.00 | 50.00 | 25.00 | 0.00 | 50.00 | 75.00 % | 50.00 % | 75.00 % | 50.00 % |
| Median of grades | 100.00 | 75.00 | 50.00 | 25.00 | 0.00 | 50.00 | 75.00 % | 50.00 % | 75.00 % | 50.00 % |
@@ -125,7 +125,7 @@ Feature: View gradebook when single item scales are used
Examples:
| aggregation | contrib1 | cattotal1 | coursetotal1 | catavg | overallavg |
| Mean of grades | 100.00 % | 100.00 | 100.00 | 100.00 | 100.00 |
| Weighted mean of grades | 0.00 % | 100.00 | - | 100.00 | - |
| Weighted mean of grades | 0.00 % | 100.00 | 100.00 | 100.00 | 100.00 |
| Simple weighted mean of grades | 0.00 % | - | - | - | - |
| Mean of grades (with extra credits) | 100.00 % | 100.00 | 100.00 | 100.00 | 100.00 |
| Median of grades | 100.00 % | 100.00 | 100.00 | 100.00 | 100.00 |
+53 -23
View File
@@ -2057,9 +2057,11 @@ class grade_category extends grade_object {
unset($items); // not needed
unset($cats); // not needed
$children_array = grade_category::_get_children_recursion($category);
ksort($children_array);
$children_array = array();
if (is_object($category)) {
$children_array = grade_category::_get_children_recursion($category);
ksort($children_array);
}
return $children_array;
@@ -2428,33 +2430,36 @@ class grade_category extends grade_object {
public static function set_properties(&$instance, $params) {
global $DB;
$fromaggregation = $instance->aggregation;
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)) {
// The aggregation method is changing and this category has already been saved.
if (isset($params->aggregation) && !empty($instance->id)) {
$achildwasdupdated = false;
//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;
// Get all its children.
$children = $instance->get_children();
foreach ($children as $child) {
$item = $child['object'];
if ($child['type'] == 'category') {
$item = $item->load_grade_item();
}
$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);
// Set the new aggregation fields.
if ($item->set_aggregation_fields_for_aggregation($fromaggregation, $params->aggregation)) {
$item->update();
$achildwasdupdated = true;
}
}
// If this is the course category, it is possible that its grade item was set as needsupdate
// by one of its children. If we keep a reference to that stale object we might cause the
// needsupdate flag to be lost. It's safer to just reload the grade_item from the database.
if ($achildwasdupdated && !empty($instance->grade_item) && $instance->is_course_category()) {
$instance->grade_item = null;
$instance->load_grade_item();
}
}
}
@@ -2562,4 +2567,29 @@ class grade_category extends grade_object {
$sql = "UPDATE {grade_items} SET needsupdate=? WHERE itemtype=? or itemtype=?";
$DB->execute($sql, $params);
}
/**
* Determine the default aggregation values for a given aggregation method.
*
* @param int $aggregationmethod The aggregation method constant value.
* @return array Containing the keys 'aggregationcoef', 'aggregationcoef2' and 'weightoverride'.
*/
public static function get_default_aggregation_coefficient_values($aggregationmethod) {
$defaultcoefficients = array(
'aggregationcoef' => 0,
'aggregationcoef2' => 0,
'weightoverride' => 0
);
switch ($aggregationmethod) {
case GRADE_AGGREGATE_WEIGHTED_MEAN:
$defaultcoefficients['aggregationcoef'] = 1;
break;
case GRADE_AGGREGATE_SUM:
$defaultcoefficients['aggregationcoef2'] = 1;
break;
}
return $defaultcoefficients;
}
}
+61 -4
View File
@@ -1329,9 +1329,12 @@ class grade_item extends grade_object {
* Sets this item's categoryid. A generic method shared by objects that have a parent id of some kind.
*
* @param int $parentid The ID of the new parent
* @param bool $updateaggregationfields Whether or not to convert the aggregation fields when switching between category.
* Set this to false when the aggregation fields have been updated in prevision of the new
* category, typically when the item is freshly created.
* @return bool True if success
*/
public function set_parent($parentid) {
public function set_parent($parentid, $updateaggregationfields = true) {
if ($this->is_course_item() or $this->is_category_item()) {
print_error('cannotsetparentforcatoritem');
}
@@ -1345,11 +1348,10 @@ 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 ($updateaggregationfields) {
$this->set_aggregation_fields_for_aggregation($currentparent->aggregation, $parent_category->aggregation);
}
$this->force_regrading();
@@ -1361,6 +1363,61 @@ class grade_item extends grade_object {
return $this->update();
}
/**
* Update the aggregation fields when the aggregation changed.
*
* This method should always be called when the aggregation has changed, but also when
* the item was moved to another category, even it if uses the same aggregation method.
*
* Some values such as the weight only make sense within a category, once moved the
* values should be reset to let the user adapt them accordingly.
*
* Note that this method does not save the grade item.
* {@link grade_item::update()} has to be called manually after using this method.
*
* @param int $from Aggregation method constant value.
* @param int $to Aggregation method constant value.
* @return boolean True when at least one field was changed, false otherwise
*/
public function set_aggregation_fields_for_aggregation($from, $to) {
$defaults = grade_category::get_default_aggregation_coefficient_values($to);
$origaggregationcoef = $this->aggregationcoef;
$origaggregationcoef2 = $this->aggregationcoef2;
$origweighoverride = $this->weightoverride;
if ($from == GRADE_AGGREGATE_SUM && $to == GRADE_AGGREGATE_SUM && $this->weightoverride) {
// Do nothing. We are switching from SUM to SUM and the weight is overriden,
// a teacher would not expect any change in this situation.
} else if ($from == GRADE_AGGREGATE_WEIGHTED_MEAN && $to == GRADE_AGGREGATE_WEIGHTED_MEAN) {
// Do nothing. The weights can be kept in this case.
} else if (in_array($from, array(GRADE_AGGREGATE_SUM, GRADE_AGGREGATE_EXTRACREDIT_MEAN, GRADE_AGGREGATE_WEIGHTED_MEAN2))
&& in_array($to, array(GRADE_AGGREGATE_SUM, GRADE_AGGREGATE_EXTRACREDIT_MEAN, GRADE_AGGREGATE_WEIGHTED_MEAN2))) {
// Reset all but the the extra credit field.
$this->aggregationcoef2 = $defaults['aggregationcoef2'];
$this->weightoverride = $defaults['weightoverride'];
if ($to != GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
// Normalise extra credit, except for 'Mean with extra credit' which supports higher values than 1.
$this->aggregationcoef = min(1, $this->aggregationcoef);
}
} else {
// Reset all.
$this->aggregationcoef = $defaults['aggregationcoef'];
$this->aggregationcoef2 = $defaults['aggregationcoef2'];
$this->weightoverride = $defaults['weightoverride'];
}
$acoefdiff = grade_floats_different($origaggregationcoef, $this->aggregationcoef);
$acoefdiff2 = grade_floats_different($origaggregationcoef2, $this->aggregationcoef2);
$weightoverride = grade_floats_different($origweighoverride, $this->weightoverride);
return $acoefdiff || $acoefdiff2 || $weightoverride;
}
/**
* Makes sure value is a valid grade value.
*
+178
View File
@@ -734,4 +734,182 @@ class core_grade_item_testcase extends grade_base_testcase {
return $DB->get_record('grade_items', array('id' => $item->id));
}
public function test_set_aggregation_fields_for_aggregation() {
$course = $this->getDataGenerator()->create_course();
$gi = new grade_item(array('courseid' => $course->id, 'itemtype' => 'manual'), false);
$methods = array(GRADE_AGGREGATE_MEAN, GRADE_AGGREGATE_MEDIAN, GRADE_AGGREGATE_MIN, GRADE_AGGREGATE_MAX,
GRADE_AGGREGATE_MODE, GRADE_AGGREGATE_WEIGHTED_MEAN, GRADE_AGGREGATE_WEIGHTED_MEAN2,
GRADE_AGGREGATE_EXTRACREDIT_MEAN, GRADE_AGGREGATE_SUM);
// Switching from and to the same aggregation using the defaults.
foreach ($methods as $method) {
$defaults = grade_category::get_default_aggregation_coefficient_values($method);
$gi->aggregationcoef = $defaults['aggregationcoef'];
$gi->aggregationcoef2 = $defaults['aggregationcoef2'];
$gi->weightoverride = $defaults['weightoverride'];
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($method, $method));
$this->assertEquals($defaults['aggregationcoef'], $gi->aggregationcoef);
$this->assertEquals($defaults['aggregationcoef2'], $gi->aggregationcoef2);
$this->assertEquals($defaults['weightoverride'], $gi->weightoverride);
}
// Extra credit is kept across aggregation methods that support it.
foreach ($methods as $from) {
$fromsupportsec = grade_category::aggregation_uses_extracredit($from);
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
foreach ($methods as $to) {
$tosupportsec = grade_category::aggregation_uses_extracredit($to);
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
// Set the item to be extra credit, if supported.
if ($fromsupportsec) {
$gi->aggregationcoef = 1;
} else {
$gi->aggregationcoef = $fromdefaults['aggregationcoef'];
}
// We ignore those fields, we know it is never used for extra credit.
$gi->aggregationcoef2 = $todefaults['aggregationcoef2'];
$gi->weightoverride = $todefaults['weightoverride'];
if ($fromsupportsec && $tosupportsec) {
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals(1, $gi->aggregationcoef);
} else if ($fromsupportsec && !$tosupportsec) {
if ($to == GRADE_AGGREGATE_WEIGHTED_MEAN) {
// Special case, aggregationcoef is used but for weights.
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
} else {
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
}
} else {
// The source does not support extra credit, everything will be reset.
if (($from == GRADE_AGGREGATE_WEIGHTED_MEAN || $to == GRADE_AGGREGATE_WEIGHTED_MEAN) && $from != $to) {
// Special case, aggregationcoef is used but for weights.
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
} else {
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
}
}
}
}
// Extra credit can be higher than one for GRADE_AGGREGATE_EXTRACREDIT_MEAN, but will be normalised for others.
$from = GRADE_AGGREGATE_EXTRACREDIT_MEAN;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
foreach ($methods as $to) {
if (!grade_category::aggregation_uses_extracredit($to)) {
continue;
}
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$gi->aggregationcoef = 8;
// Ignore those fields, they are not used for extra credit.
$gi->aggregationcoef2 = $todefaults['aggregationcoef2'];
$gi->weightoverride = $todefaults['weightoverride'];
if ($to == $from) {
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals(8, $gi->aggregationcoef);
} else {
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals(1, $gi->aggregationcoef);
}
}
// Weights are reset.
$from = GRADE_AGGREGATE_SUM;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$gi->aggregationcoef = $fromdefaults['aggregationcoef'];
$gi->aggregationcoef2 = 0.321;
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_WEIGHTED_MEAN;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
$this->assertEquals($todefaults['aggregationcoef2'], $gi->aggregationcoef2);
$this->assertEquals($todefaults['weightoverride'], $gi->weightoverride);
$gi->aggregationcoef = $fromdefaults['aggregationcoef'];
$gi->aggregationcoef2 = 0.321;
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_SUM;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
$this->assertEquals($todefaults['aggregationcoef2'], $gi->aggregationcoef2);
$this->assertEquals($todefaults['weightoverride'], $gi->weightoverride);
// Weight is kept when using SUM with weight override.
$from = GRADE_AGGREGATE_SUM;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$gi->aggregationcoef = $fromdefaults['aggregationcoef'];
$gi->aggregationcoef2 = 0.321;
$gi->weightoverride = 1;
$to = GRADE_AGGREGATE_SUM;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
$this->assertEquals(0.321, $gi->aggregationcoef2);
$this->assertEquals(1, $gi->weightoverride);
$gi->aggregationcoef2 = 0.321;
$gi->aggregationcoef = $fromdefaults['aggregationcoef'];
$gi->weightoverride = 1;
$to = GRADE_AGGREGATE_WEIGHTED_MEAN;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
$this->assertEquals($todefaults['aggregationcoef2'], $gi->aggregationcoef2);
$this->assertEquals($todefaults['weightoverride'], $gi->weightoverride);
// Weight is kept when staying in weighted mean.
$from = GRADE_AGGREGATE_WEIGHTED_MEAN;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$gi->aggregationcoef = 18;
$gi->aggregationcoef2 = $fromdefaults['aggregationcoef2'];
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_WEIGHTED_MEAN;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals(18, $gi->aggregationcoef);
$this->assertEquals($todefaults['aggregationcoef2'], $gi->aggregationcoef2);
$this->assertEquals($todefaults['weightoverride'], $gi->weightoverride);
$gi->aggregationcoef = 18;
$gi->aggregationcoef2 = $fromdefaults['aggregationcoef2'];
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_SUM;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
$this->assertEquals($todefaults['aggregationcoef2'], $gi->aggregationcoef2);
$this->assertEquals($todefaults['weightoverride'], $gi->weightoverride);
}
}
+10
View File
@@ -287,6 +287,16 @@ class behat_data_generators extends behat_base {
if (isset($data['gradetype'])) {
$data['gradetype'] = constant("GRADE_TYPE_" . strtoupper($data['gradetype']));
}
if (!empty($data['category']) && !empty($data['courseid'])) {
$cat = grade_category::fetch(array('fullname' => $data['category'], 'courseid' => $data['courseid']));
if (!$cat) {
throw new Exception('Could not resolve category with name "' . $data['category'] . '"');
}
unset($data['category']);
$data['categoryid'] = $cat->id;
}
return $data;
}
-11
View File
@@ -1177,17 +1177,6 @@ function workshop_grade_item_category_update($workshop) {
$gradeitem->set_parent($workshop->gradinggradecategory);
}
}
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;
}
$gradeitem->update();
}
}
}
}
}