From 0aa433a054d9dcb7838f458dab098ea0bd947f79 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 9 Mar 2012 18:45:54 +0000 Subject: [PATCH] MDL-31981 shortanswer qtype does not handle \* in correct response. --- question/type/shortanswer/question.php | 22 ++++++++++++++++++ question/type/shortanswer/renderer.php | 3 ++- .../type/shortanswer/simpletest/helper.php | 23 ++++++++++++++++++- .../shortanswer/simpletest/testquestion.php | 6 +++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/question/type/shortanswer/question.php b/question/type/shortanswer/question.php index a5408714927..a875c8c1a05 100644 --- a/question/type/shortanswer/question.php +++ b/question/type/shortanswer/question.php @@ -101,6 +101,28 @@ class qtype_shortanswer_question extends question_graded_by_strategy return preg_match($regexp, trim($string)); } + public function get_correct_response() { + $response = parent::get_correct_response(); + if ($response) { + $response['answer'] = $this->clean_response($response['answer']); + } + return $response; + } + + public function clean_response($answer) { + // Break the string on non-escaped asterisks. + $bits = preg_split('/(?answer)); + return get_string('correctansweris', 'qtype_shortanswer', + s($question->clean_response($answer->answer))); } } diff --git a/question/type/shortanswer/simpletest/helper.php b/question/type/shortanswer/simpletest/helper.php index 71a0f409111..533b5802d75 100644 --- a/question/type/shortanswer/simpletest/helper.php +++ b/question/type/shortanswer/simpletest/helper.php @@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die(); */ class qtype_shortanswer_test_helper extends question_test_helper { public function get_test_questions() { - return array('frogtoad', 'frogonly'); + return array('frogtoad', 'frogonly', 'escapedwildcards'); } /** @@ -130,4 +130,25 @@ class qtype_shortanswer_test_helper extends question_test_helper { return $qdata; } + + /** + * Makes a shortanswer question with just the correct ansewer 'frog', and + * no other answer matching. + * @return qtype_shortanswer_question + */ + public function make_shortanswer_question_escapedwildcards() { + question_bank::load_question_definition_classes('shortanswer'); + $sa = new qtype_shortanswer_question(); + test_question_maker::initialise_a_question($sa); + $sa->name = 'Question with escaped * in the answer.'; + $sa->questiontext = 'How to you write x times y in C? __________'; + $sa->generalfeedback = 'In C, this expression is written x * y.'; + $sa->usecase = false; + $sa->answers = array( + 13 => new question_answer(13, '*x\*y*', 1.0, 'Well done.', FORMAT_HTML), + ); + $sa->qtype = question_bank::get_qtype('shortanswer'); + + return $sa; + } } diff --git a/question/type/shortanswer/simpletest/testquestion.php b/question/type/shortanswer/simpletest/testquestion.php index 4db7d6b4b79..54aa98758a5 100644 --- a/question/type/shortanswer/simpletest/testquestion.php +++ b/question/type/shortanswer/simpletest/testquestion.php @@ -145,6 +145,12 @@ class qtype_shortanswer_question_test extends UnitTestCase { $question->get_correct_response()); } + public function test_get_correct_response_escapedwildcards() { + $question = test_question_maker::make_question('shortanswer', 'escapedwildcards'); + + $this->assertEqual(array('answer' => 'x*y'), $question->get_correct_response()); + } + public function test_get_question_summary() { $sa = test_question_maker::make_question('shortanswer'); $qsummary = $sa->get_question_summary();