MDL-78217 grade: Recalculate Parent Weights on Zero Child Weights

The maximum grade for that category is considered as zero when all child
weights within a category are zero.
This commit is contained in:
Shamim Rezaie 2023-09-21 03:39:37 +10:00
parent 6aa05cfb9a
commit aa30501b80
4 changed files with 25 additions and 3 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -91,6 +91,9 @@ const recalculateNaturalWeights = (categoryElement) => {
// Does the grade item require normalising? // Does the grade item require normalising?
let requiresNormalising = false; let requiresNormalising = false;
// Is there an error in the weight calculations?
let erroneous = false;
// This array keeps track of the id and weight of every grade item that has been overridden. // This array keeps track of the id and weight of every grade item that has been overridden.
const overrideArray = {}; const overrideArray = {};
@ -202,7 +205,11 @@ const recalculateNaturalWeights = (categoryElement) => {
overrideArray[childElement.dataset.itemid].weight < 0) { overrideArray[childElement.dataset.itemid].weight < 0) {
if (overrideArray[childElement.dataset.itemid].weight < 0) { if (overrideArray[childElement.dataset.itemid].weight < 0) {
weightInput.value = formatWeight(0); weightInput.value = formatWeight(0);
} else { }
// Zero is a special case. If the total is zero then we need to set the weight of the parent category to zero.
if (normaliseTotal !== 0) {
erroneous = true;
const error = normaliseTotal > 100 ? 'erroroverweight' : 'errorunderweight'; const error = normaliseTotal > 100 ? 'erroroverweight' : 'errorunderweight';
// eslint-disable-next-line promise/always-return,promise/catch-or-return // eslint-disable-next-line promise/always-return,promise/catch-or-return
getString(error, 'core_grades').then((errorString) => { getString(error, 'core_grades').then((errorString) => {
@ -212,6 +219,18 @@ const recalculateNaturalWeights = (categoryElement) => {
} }
} }
} }
if (!erroneous) {
const categoryGradeMax = parseFloat(categoryElement.dataset.grademax);
if (categoryGradeMax !== totalGradeMax) {
// The category grade max is not the same as the total grade max, so we need to update the category grade max.
categoryElement.dataset.grademax = totalGradeMax;
const parentCategory = document.querySelector(selectors.categoryByIdentifier(categoryElement.dataset.parentCategory));
if (parentCategory && (parseInt(parentCategory.dataset.aggregation) === grade.aggregation.sum)) {
recalculateNaturalWeights(parentCategory);
}
}
}
}; };
/** /**

View file

@ -234,6 +234,9 @@ Feature: We can use natural aggregation and weights will be normalised to a tota
Scenario: Overriding a grade item with a negative value results in the value being changed to zero. Scenario: Overriding a grade item with a negative value results in the value being changed to zero.
When I set the field "Override weight of Test assignment five" to "1" When I set the field "Override weight of Test assignment five" to "1"
And I set the field "Weight of Test assignment five" to "-15" And I set the field "Weight of Test assignment five" to "-15"
Then the field "Weight of Test assignment five" matches value "0.0"
And the field "Weight of Test assignment six" matches value "40.0"
And the field "Weight of Test assignment seven" matches value "60.0"
And I press "Save changes" And I press "Save changes"
Then the field "Weight of Test assignment five" matches value "0.0" Then the field "Weight of Test assignment five" matches value "0.0"
And the field "Weight of Test assignment six" matches value "40.0" And the field "Weight of Test assignment six" matches value "40.0"