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); + + // Unescape *s in the bits. + $cleanbits = array(); + foreach ($bits as $bit) { + $cleanbits[] = str_replace('\*', '*', $bit); + } + + // Put it back together with spaces to look nice. + return trim(implode(' ', $cleanbits)); + } + public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) { if ($component == 'question' && $filearea == 'answerfeedback') { diff --git a/question/type/shortanswer/renderer.php b/question/type/shortanswer/renderer.php index 501b1bff52e..e7879bfb680 100644 --- a/question/type/shortanswer/renderer.php +++ b/question/type/shortanswer/renderer.php @@ -117,6 +117,7 @@ class qtype_shortanswer_renderer extends qtype_renderer { return ''; } - return get_string('correctansweris', 'qtype_shortanswer', s($answer->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();