From a2f1f65db48eb990aed11823f9c7e7a2866b5fb8 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Fri, 29 Aug 2014 10:36:01 +0800 Subject: [PATCH] MDL-47011 core_grade: added naive natural weight calculation to gradebook --- lib/grade/grade_category.php | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 143d3aef878..a7f8caea339 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -461,6 +461,9 @@ class grade_category extends grade_object { // needed mostly for SUM agg type $this->auto_update_max($items); + // Needed for Natural aggregation type. + $this->auto_update_weights(); + $grade_inst = new grade_grade(); $fields = 'g.'.implode(',g.', $grade_inst->required_fields); @@ -1026,6 +1029,46 @@ class grade_category extends grade_object { } } + /** + * Recalculate the weights of the grade items in this category. + * THIS DOES NOT TAKE INTO ACCOUNT HIDDEN ACTIVITIES, GROUPS. + */ + private function auto_update_weights() { + if ($this->aggregation != GRADE_AGGREGATE_SUM) { + // This is only required if we are using natural weights. + return; + } + $children = $this->get_children(); + + $grade_item = null; + + // Calculate the sum of the grademax's of all the items within this category. + $totalgrademax = 0; + foreach ($children as $sortorder => $child) { + $grade_item = null; + + if ($child['type'] == 'item') { + $grade_item = $child['object']; + } else if ($child['type'] == 'category') { + $grade_item = $child['object']->load_grade_item(); + } + $totalgrademax += $grade_item->grademax; + } + + reset($children); + foreach ($children as $sortorder => $child) { + $grade_item = null; + + if ($child['type'] == 'item') { + $grade_item = $child['object']; + } else if ($child['type'] == 'category') { + $grade_item = $child['object']->load_grade_item(); + } + $grade_item->aggregationcoef2 = $grade_item->grademax/$totalgrademax; + $grade_item->update(); + } + } + /** * Internal function for category grades summing *