From 2cae75aacc0f1829aa957228e95a6033fc8f2caf Mon Sep 17 00:00:00 2001 From: Matt Petro Date: Fri, 19 Oct 2012 10:59:42 -0400 Subject: [PATCH] MDL-36025 qtype calculated: Fix handling of "-0" --- question/type/calculated/questiontype.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index c0ab753f040..0a91a531632 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -1227,7 +1227,7 @@ class qtype_calculated extends question_type { 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 @@ class qtype_calculated extends question_type { } 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) {