]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10364 calcualtion validation improvements
authorskodak <skodak>
Thu, 12 Jul 2007 17:36:18 +0000 (17:36 +0000)
committerskodak <skodak>
Thu, 12 Jul 2007 17:36:18 +0000 (17:36 +0000)
grade/report/grader/edit_item_form.php
lib/grade/grade_item.php

index 6a65dd5791122cb2294105865cd4c3ab62343dd8..4444684ecb7de7a3bf5b74e782635870bf2fc57f 100644 (file)
@@ -113,13 +113,10 @@ class edit_item_form extends moodleform {
         $errors= array();
 
         if ($data['calculation'] != '') {
-            if (strpos($data['calculation'], '=') !== 0) {
-                $errors['calculation'] = get_string('calculationerror', 'grades');
-            } else {
-                $grade_item = new grade_item(array('id'=>$data['id'], 'itemtype'=>$data['itemtype'], 'courseid'=>$data['courseid']));
-                if (!$grade_item->validate_formula($data['calculation'])) {
-                    $errors['calculation'] = get_string('calculationerror', 'grades');
-                }
+            $grade_item = new grade_item(array('id'=>$data['id'], 'itemtype'=>$data['itemtype'], 'courseid'=>$data['courseid']));
+            $result = $grade_item->validate_formula($data['calculation']);
+            if ($result !== true) {
+                $errors['calculation'] = $result;
             }
         }
 
index 27c62054a6dbe4701008e455a5b82bf2b34aad89..70415336b6fa9602534588fd2da2f302d760c0d0 100644 (file)
@@ -1483,20 +1483,30 @@ class grade_item extends grade_object {
             return true;
         }
 
+        if (strpos($formula, '=') !== 0) {
+            return get_string('errorcalculationnoequal', 'grades');
+        }
+
         // prepare formula and init maths library
         $formula = preg_replace('/\[#(gi[0-9]+)#\]/', '\1', $formula);
         $formula = new calc_formula($formula);
 
         // get used items
         $useditems = $this->depends_on();
-        $gis = implode(',', $useditems);
-
-        $sql = "SELECT gi.*
-                  FROM {$CFG->prefix}grade_items gi
-                 WHERE gi.id IN ($gis) and gi.courseid={$this->courseid}"; // from the same course only!
 
-        if (!$grade_items = get_records_sql($sql)) {
+        if (empty($useditems)) {
             $grade_items = array();
+
+        } else {
+            $gis = implode(',', $useditems);
+    
+            $sql = "SELECT gi.*
+                      FROM {$CFG->prefix}grade_items gi
+                     WHERE gi.id IN ($gis) and gi.courseid={$this->courseid}"; // from the same course only!
+    
+            if (!$grade_items = get_records_sql($sql)) {
+                $grade_items = array();
+            }
         }
 
         $params = array();
@@ -1515,7 +1525,12 @@ class grade_item extends grade_object {
         $result = $formula->evaluate();
 
         // false as result indicates some problem
-        return ($result !== false);
+        if ($result === false) {
+            // TODO: add more error hints
+            return get_string('errorcalculationunknown', 'grades');
+        } else {
+            return true;
+        }
     }
 }
 ?>