From 9d452f139f221cffc37db4f9fa3737abc182266c Mon Sep 17 00:00:00 2001 From: skodak Date: Thu, 12 Jul 2007 17:36:18 +0000 Subject: [PATCH] MDL-10364 calcualtion validation improvements --- grade/report/grader/edit_item_form.php | 11 ++++------ lib/grade/grade_item.php | 29 +++++++++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/grade/report/grader/edit_item_form.php b/grade/report/grader/edit_item_form.php index 6a65dd5791..4444684ecb 100644 --- a/grade/report/grader/edit_item_form.php +++ b/grade/report/grader/edit_item_form.php @@ -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; } } diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 27c62054a6..70415336b6 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -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; + } } } ?> -- 2.39.5