From 39fbd5c81807a4f0565e1a674033c3a03a1ce3dc Mon Sep 17 00:00:00 2001 From: kaipe Date: Sun, 1 Aug 2004 22:22:20 +0000 Subject: [PATCH] Added support for some PHP style mathematical functions in the correct answer formula for calculated questions Documentation on the functions can be found at http://uk.php.net/manual/en/ref.math.php --- lang/en/quiz.php | 6 ++ .../questiontypes/calculated/questiontype.php | 87 +++++++++++++++++-- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/lang/en/quiz.php b/lang/en/quiz.php index 7b524a91ad..8a938e59a4 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -103,6 +103,11 @@ $string['fillouttwochoices'] = 'You must fill out at least two choices. Choices $string['forceregeneration'] = 'force regeneration'; $string['fractionsaddwrong'] = 'The positive grades you have chosen do not add up to 100%%
Instead, they add up to $a%%
Do you want to go back and fix this question?'; $string['fractionsnomax'] = 'One of the answers should be 100%%, so that it is
possible to get a full grade for this question.
Do you want to go back and fix this question?'; +$string['functiontakesatleasttwo'] = 'The function $a must have at least two arguments'; +$string['functiontakesnoargs'] = 'The function $a does not take any arguments'; +$string['functiontakesonearg'] = 'The function $a must have exactly one argument'; +$string['functiontakesoneortwoargs'] = 'The function $a must have either one or two arguments'; +$string['functiontakestwoargs'] = 'The function $a must have exactly two arguments'; $string['generatevalue'] = 'Generate a new value between'; $string['geometric'] = 'Geometric'; $string['gift'] = 'GIFT format'; @@ -252,6 +257,7 @@ $string['type'] = 'Type'; $string['uniform'] = 'decimals, from a uniform distribution'; $string['unit'] = 'Unit'; $string['unknowntype'] = 'Question type not supported at line $a. The question will be ignored'; +$string['unsupportedformulafunction'] = 'The function $a is not supported'; $string['viewallanswers'] = 'View $a completed quizzes'; $string['viewallreports'] = 'View reports for $a attempts'; $string['warningsdetected'] = '$a warning(s) detected'; diff --git a/mod/quiz/questiontypes/calculated/questiontype.php b/mod/quiz/questiontypes/calculated/questiontype.php index 8daa17398e..f62979316d 100644 --- a/mod/quiz/questiontypes/calculated/questiontype.php +++ b/mod/quiz/questiontypes/calculated/questiontype.php @@ -426,8 +426,8 @@ function quiz_qtype_calculated_calculate_answer($formula, $individualdata, $calculated->answer = $calculated->min = $calculated->max = ''; return $calculated; - } else if (ereg('^(.*([^- 0-9+*./()eE])).*$', $formula, $regs)) { - $calculated->answer = get_string('syntaxerror', 'quiz', $regs[1]); + } else if ($error = quiz_qtype_calculated_find_formula_errors($formula)) { + $calculated->answer = $error; $calculated->min = $calculated->max = ''; return $calculated; } @@ -526,6 +526,7 @@ function quiz_qtype_calculated_calculate_answer($formula, $individualdata, return $calculated; } + function quiz_qtype_calculated_find_formula_errors($formula) { /// Validates the formula submitted from the question edit page. /// Returns false if everything is alright. @@ -536,14 +537,84 @@ function quiz_qtype_calculated_find_formula_errors($formula) { $formula = str_replace($regs[0], '1', $formula); } + // Strip away empty space + $formula = str_replace(' ', '', $formula); - if (!ereg('[^- 0-9+*/:.(>?)!e=|