mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-10364 calcualtion validation improvements
This commit is contained in:
parent
9029762c3c
commit
9d452f139f
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user