From 5c0b5ba57506c6f78ff6021b31c67ee698099624 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 22 Jan 2014 12:45:08 +0000 Subject: [PATCH] MDL-43809 qformat_xml messes up $s in multianswer questions. --- question/format/xml/format.php | 2 +- question/format/xml/tests/xmlformat_test.php | 25 +++++++++ question/type/multianswer/tests/helper.php | 59 +++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/question/format/xml/format.php b/question/format/xml/format.php index 43c17e03df6..df9e6094bbe 100644 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -1270,7 +1270,7 @@ class qformat_xml extends qformat_default { case 'multianswer': foreach ($question->options->questions as $index => $subq) { - $expout = preg_replace('~{#' . $index . '}~', $subq->questiontext, $expout); + $expout = str_replace('{#' . $index . '}', $subq->questiontext, $expout); } break; diff --git a/question/format/xml/tests/xmlformat_test.php b/question/format/xml/tests/xmlformat_test.php index 448cc279370..8396838b0d4 100644 --- a/question/format/xml/tests/xmlformat_test.php +++ b/question/format/xml/tests/xmlformat_test.php @@ -1423,6 +1423,31 @@ END; $this->assert_same_xml($expectedxml, $xml); } + public function test_export_multianswer_withdollars() { + $qdata = test_question_maker::get_question_data('multianswer', 'dollarsigns'); + + $exporter = new qformat_xml(); + $xml = $exporter->writequestion($qdata); + + $expectedxml = ' + + + Multianswer with $s + + + Which is the right order? {1:MULTICHOICE:=y,y,$3~$3,y,y} + + + + + 0.3333333 + 0 + +'; + + $this->assert_same_xml($expectedxml, $xml); + } + public function test_import_files_as_draft() { $this->resetAfterTest(); $this->setAdminUser(); diff --git a/question/type/multianswer/tests/helper.php b/question/type/multianswer/tests/helper.php index 92a51e489be..7c40776ee01 100644 --- a/question/type/multianswer/tests/helper.php +++ b/question/type/multianswer/tests/helper.php @@ -37,7 +37,7 @@ require_once($CFG->dirroot . '/question/type/multianswer/question.php'); */ class qtype_multianswer_test_helper extends question_test_helper { public function get_test_questions() { - return array('twosubq', 'fourmc', 'numericalzero'); + return array('twosubq', 'fourmc', 'numericalzero', 'dollarsigns'); } /** @@ -196,6 +196,63 @@ class qtype_multianswer_test_helper extends question_test_helper { return $qdata; } + /** + * Makes a multianswer question onetaining one blank in some text. + * This question has no hints. + * + * @return object the question definition data, as it might be returned from + * get_question_options. + */ + public function get_multianswer_question_data_dollarsigns() { + $qdata = new stdClass(); + test_question_maker::initialise_question_data($qdata); + + $qdata->name = 'Multianswer with $s'; + $qdata->questiontext = + 'Which is the right order? {#1}'; + $qdata->generalfeedback = ''; + + $qdata->defaultmark = 1.0; + $qdata->qtype = 'multianswer'; + + $mc = new stdClass(); + test_question_maker::initialise_question_data($mc); + + $mc->name = 'Multianswer with $s'; + $mc->questiontext = '{1:MULTICHOICE:=y,y,$3~$3,y,y}'; + $mc->generalfeedback = ''; + $mc->penalty = 0.0; + $mc->qtype = 'multichoice'; + + $mc->options = new stdClass(); + $mc->options->layout = 0; + $mc->options->single = 1; + $mc->options->shuffleanswers = 1; + $mc->options->correctfeedback = ''; + $mc->options->correctfeedbackformat = 1; + $mc->options->partiallycorrectfeedback = ''; + $mc->options->partiallycorrectfeedbackformat = 1; + $mc->options->incorrectfeedback = ''; + $mc->options->incorrectfeedbackformat = 1; + $mc->options->answernumbering = 0; + $mc->options->shownumcorrect = 0; + + $mc->options->answers = array( + 23 => new question_answer(23, 'y,y,$3', 0, '', FORMAT_HTML), + 24 => new question_answer(24, '$3,y,y', 0, '', FORMAT_HTML), + ); + + $qdata->options = new stdClass(); + $qdata->options->questions = array( + 1 => $mc, + ); + + $qdata->hints = array( + ); + + return $qdata; + } + /** * Makes a multianswer question about completing two blanks in some text. * @return object the question definition data, as it might be returned from