Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-36025 qtype calculated: Fix handling of "-0"

  • Loading branch information...
commit 95ae9d9e5e16b5bcb42c91d963b572b8dece0bcf 1 parent 7027fe4
Matt Petro mpetrowi authored danpoltawski committed
Showing with 7 additions and 1 deletion.
  1. +7 −1 question/type/calculated/questiontype.php
8 question/type/calculated/questiontype.php
View
@@ -1227,7 +1227,7 @@ public function substitute_variables($str, $dataset) {
echo $OUTPUT->notification(get_string('notvalidnumber', 'qtype_calculated', $a));
$val = 1.0;
}
- if ($val < 0) {
+ if ($val <= 0) { // MDL-36025 Use parentheses for "-0"
$str = str_replace('{'.$name.'}', '('.$val.')', $str);
} else {
$str = str_replace('{'.$name.'}', $val, $str);
@@ -1255,6 +1255,7 @@ public function substitute_variables_and_eval($str, $dataset) {
} else if ($formula === '*') {
$str = '*';
} else {
+ $str = null;
eval('$str = '.$formula.';');
}
return $str;
@@ -1870,6 +1871,11 @@ function qtype_calculated_calculate_answer($formula, $individualdata,
// Exchange formula variables with the correct values...
$answer = question_bank::get_qtype('calculated')->substitute_variables_and_eval(
$formula, $individualdata);
+ if (!is_numeric($answer)) {
+ // Something went wrong, so just return NaN.
+ $calculated->answer = NAN;
+ return $calculated;
+ }
if ('1' == $answerformat) { /* Answer is to have $answerlength decimals */
/*** Adjust to the correct number of decimals ***/
if (stripos($answer, 'e')>0) {
Please sign in to comment.
Something went wrong with that request. Please try again.