]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-18772 fixed aut oupdating of grademax for SUM aggregation type
authorskodak <skodak>
Sun, 5 Apr 2009 20:17:04 +0000 (20:17 +0000)
committerskodak <skodak>
Sun, 5 Apr 2009 20:17:04 +0000 (20:17 +0000)
lib/grade/grade_category.php

index 74dd0193058af4c6aa8c1726585e5cd787efe74a..f95b69f6d31e1bea7a181d5eddc84554f997cbbb 100644 (file)
@@ -424,6 +424,9 @@ class grade_category extends grade_object {
             $items = $DB->get_records_sql($sql, $params);
         }
 
+        // needed mostly for SUM agg type
+        $this->auto_update_max($items);
+
         $grade_inst = new grade_grade();
         $fields = 'g.'.implode(',g.', $grade_inst->required_fields);
 
@@ -691,6 +694,50 @@ class grade_category extends grade_object {
         return $agg_grade;
     }
 
+    /**
+     * Some aggregation tpyes may update max grade
+     * @param array $items sub items
+     * @return void
+     */
+    private function auto_update_max($items) {
+        if ($this->aggregation != GRADE_AGGREGATE_SUM) {
+            // not needed at all
+            return;
+        }
+
+        if (!$items) {
+            if ($this->grade_item->grademax != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE) {
+                $this->grade_item->grademax  = 0;
+                $this->grade_item->grademin  = 0;
+                $this->grade_item->gradetype = GRADE_TYPE_VALUE;
+                $this->grade_item->update('aggregation');
+            }
+            return;
+        }
+
+        $max = 0;
+
+        //find max grade
+        foreach ($items as $item) {
+            if ($item->aggregationcoef > 0) {
+                // extra credit from this activity - does not affect total
+                continue;
+            }
+            if ($item->gradetype == GRADE_TYPE_VALUE) {
+                $max += $item->grademax;
+            } else if ($item->gradetype == GRADE_TYPE_SCALE) {
+                $max += $item->grademax - 1; // scales min is 1
+            }
+        }
+
+        if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE){
+            $this->grade_item->grademax  = $max;
+            $this->grade_item->grademin  = 0;
+            $this->grade_item->gradetype = GRADE_TYPE_VALUE;
+            $this->grade_item->update('aggregation');
+        }
+    }
+
     /**
      * internal function for category grades summing
      *
@@ -721,28 +768,6 @@ class grade_category extends grade_object {
             }
         }
 
-        $max = 0;
-
-        //find max grade
-        foreach ($items as $item) {
-            if ($item->aggregationcoef > 0) {
-                // extra credit from this activity - does not affect total
-                continue;
-            }
-            if ($item->gradetype == GRADE_TYPE_VALUE) {
-                $max += $item->grademax;
-            } else if ($item->gradetype == GRADE_TYPE_SCALE) {
-                $max += $item->grademax - 1; // scales min is 1
-            }
-        }
-
-        if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE){
-            $this->grade_item->grademax = $max;
-            $this->grade_item->grademin = 0;
-            $this->grade_item->gradetype = GRADE_TYPE_VALUE;
-            $this->grade_item->update('aggregation');
-        }
-
         $this->apply_limit_rules($grade_values);
 
         $sum = array_sum($grade_values);