From 94e7961403f9fe1b4ba51bdb868604f5bde5759a Mon Sep 17 00:00:00 2001
From: skodak <skodak>
Date: Sat, 22 Mar 2008 21:20:33 +0000
Subject: [PATCH] MDL-14008 various SUM aggregation bugfixes; merged from
 MOODLE_19_STABLE

---
 grade/edit/tree/category_form.php    |  2 +-
 grade/edit/tree/item.php             |  5 ++++-
 grade/edit/tree/item_form.php        |  5 +++--
 grade/edit/tree/outcomeitem.php      | 26 +++++++++++++++-----------
 grade/edit/tree/outcomeitem_form.php | 17 +++++++++++++++--
 5 files changed, 38 insertions(+), 17 deletions(-)

diff --git a/grade/edit/tree/category_form.php b/grade/edit/tree/category_form.php
index b65f8cbec3..7789be6141 100644
--- a/grade/edit/tree/category_form.php
+++ b/grade/edit/tree/category_form.php
@@ -65,7 +65,6 @@ class edit_category_form extends moodleform {
         } else {
             $mform->addElement('checkbox', 'aggregateoutcomes', get_string('aggregateoutcomes', 'grades'));
             $mform->setHelpButton('aggregateoutcomes', array('aggregateoutcomes', get_string('aggregateoutcomes', 'grades'), 'grade'), true);
-            $mform->disabledIf('aggregateoutcomes', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
             if ((int)$CFG->grade_aggregateoutcomes_flag & 2) {
                 $mform->setAdvanced('aggregateoutcomes');
             }
@@ -235,6 +234,7 @@ class edit_category_form extends moodleform {
                         $mform->setHelpButton('aggregationcoef', array('aggregationcoef', get_string('aggregationcoef', 'grades'), 'grade'), true);
                     }
                 }
+                
             }
 
             if ($grade_item->is_calculated()) {
diff --git a/grade/edit/tree/item.php b/grade/edit/tree/item.php
index d412179b17..ce1bbc7aa6 100644
--- a/grade/edit/tree/item.php
+++ b/grade/edit/tree/item.php
@@ -58,6 +58,7 @@ if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) {
     $item = $grade_item->get_record_data();
 
     if ($grade_item->is_course_item()) {
+        $parent_category = null;
         $item->parentcategory = 0;
     } else if ($grade_item->is_category_item()) {
         $parent_category = $grade_item->get_parent_category();
@@ -90,7 +91,9 @@ $item->gradepass       = format_float($item->gradepass, $decimalpoints);
 $item->multfactor      = format_float($item->multfactor, 4);
 $item->plusfactor      = format_float($item->plusfactor, 4);
 
-if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
+if (empty($parent_category)) {
+    $item->aggregationcoef = 0;
+} else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
     $item->aggregationcoef = $item->aggregationcoef > 0 ? 1 : 0;
 } else {
     $item->aggregationcoef = format_float($item->aggregationcoef, 4);
diff --git a/grade/edit/tree/item_form.php b/grade/edit/tree/item_form.php
index f43a707ef5..1a65097de4 100644
--- a/grade/edit/tree/item_form.php
+++ b/grade/edit/tree/item_form.php
@@ -153,7 +153,7 @@ class edit_item_form extends moodleform {
                     $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextra') ? 'aggregationcoefextra' : 'aggregationcoef';
 
                 } else if ($cat->aggregation == GRADE_AGGREGATE_SUM) {
-                    $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
+                    $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasump') ? 'aggregationcoefextrasum' : 'aggregationcoef';
 
                 } else {
                     $coefstring = 'aggregationcoef';
@@ -271,6 +271,7 @@ class edit_item_form extends moodleform {
                     $aggcoef = '';
                     if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
                         $aggcoef = 'aggregationcoefweight';
+
                     } else if ($parent_category->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
                         $aggcoef = 'aggregationcoefextra';
 
@@ -280,7 +281,7 @@ class edit_item_form extends moodleform {
 
                     if ($aggcoef !== '') {
                         $agg_el->setLabel(get_string($aggcoef, 'grades'));
-                        $mform->setHelpButton('aggregationcoef', array($aggcoef, get_string($aggcoef, 'grades'), 'grade'), true);
+                        $mform->setHelpButton('aggregationcoef', array('aggregationcoef', get_string('aggregationcoef', 'grades'), 'grade'), true);
                     }
                 }
             }
diff --git a/grade/edit/tree/outcomeitem.php b/grade/edit/tree/outcomeitem.php
index 25a418977f..1d7d5ef14a 100644
--- a/grade/edit/tree/outcomeitem.php
+++ b/grade/edit/tree/outcomeitem.php
@@ -58,16 +58,8 @@ if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) {
     }
     $item = $grade_item->get_record_data();
 
-    if ($grade_item->is_course_item()) {
-        $item->parentcategory = 0;
-    } else if ($grade_item->is_category_item()) {
-        $parent_category = $grade_item->get_parent_category();
-        $parent_category = $parent_category->get_parent_category();
-        $item->parentcategory = $parent_category->id;
-    } else {
-        $parent_category = $grade_item->get_parent_category();
-        $item->parentcategory = $parent_category->id;
-    }
+    $parent_category = $grade_item->get_parent_category();
+    $item->parentcategory = $parent_category->id;
 
     if ($item->itemtype == 'mod') {
         $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
@@ -96,12 +88,24 @@ if ($item->hidden > 1) {
 $item->locked = !empty($item->locked);
 
 $item->gradepass       = format_float($item->gradepass, $decimalpoints);
-$item->aggregationcoef = format_float($item->aggregationcoef, 4);
+
+if (empty($parent_category)) {
+    $item->aggregationcoef = 0;
+} else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
+    $item->aggregationcoef = $item->aggregationcoef > 0 ? 1 : 0;
+} else {
+    $item->aggregationcoef = format_float($item->aggregationcoef, 4);
+}
 
 $mform->set_data($item);
 
 
 if ($data = $mform->get_data(false)) {
+
+    if (!isset($data->aggregationcoef)) {
+        $data->aggregationcoef = 0;
+    }
+
     if (array_key_exists('calculation', $data)) {
         $data->calculation = grade_item::normalize_formula($data->calculation, $course->id);
     }
diff --git a/grade/edit/tree/outcomeitem_form.php b/grade/edit/tree/outcomeitem_form.php
index 606ec10264..8c99a5a651 100644
--- a/grade/edit/tree/outcomeitem_form.php
+++ b/grade/edit/tree/outcomeitem_form.php
@@ -103,6 +103,9 @@ class edit_outcomeitem_form extends moodleform {
                 } else if ($cat->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
                     $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextra') ? 'aggregationcoefextra' : 'aggregationcoef';
 
+                } else if ($cat->aggregation == GRADE_AGGREGATE_SUM) {
+                    $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasump') ? 'aggregationcoefextrasum' : 'aggregationcoef';
+
                 } else {
                     $coefstring = 'aggregationcoef';
                 }
@@ -117,8 +120,13 @@ class edit_outcomeitem_form extends moodleform {
         }
 
         if ($coefstring !== '') {
-            $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
-            $mform->setHelpButton('aggregationcoef', array('aggregationcoef', get_string('aggregationcoef', 'grades'), 'grade'));
+            if ($coefstring == 'aggregationcoefextrasum') {
+                // advcheckbox is not compatible with disabledIf!
+                $mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
+            } else {
+                $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
+            }
+            $mform->setHelpButton('aggregationcoef', array('aggregationcoef', get_string('aggregationcoef', 'grades'), 'grade'), true);
         }
 
 /// hidden params
@@ -190,9 +198,14 @@ class edit_outcomeitem_form extends moodleform {
                     $aggcoef = '';
                     if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
                         $aggcoef = 'aggregationcoefweight';
+
                     } else if ($parent_category->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
                         $aggcoef = 'aggregationcoefextra';
+
+                    } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
+                        $aggcoef = 'aggregationcoefextrasum';
                     }
+
                     if ($aggcoef !== '') {
                         $agg_el->setLabel(get_string($aggcoef, 'grades'));
                         $mform->setHelpButton('aggregationcoef', array('aggregationcoef', get_string('aggregationcoef', 'grades'), 'grade'));
-- 
2.39.5