From 1600029de9bafbd717a7a015a9d1f78f26e39472 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 24 Aug 2012 14:19:27 +0100 Subject: [PATCH] MDL-32464 qformat multianswer: fix missing include. Also, add a sample file that can be used for testing, and add a unit test to verify this is working. --- question/format/multianswer/format.php | 31 ++++++++++++---------- question/type/multianswer/questiontype.php | 6 ++--- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/question/format/multianswer/format.php b/question/format/multianswer/format.php index 45179d8d9f6..d7a7ac788a4 100644 --- a/question/format/multianswer/format.php +++ b/question/format/multianswer/format.php @@ -17,10 +17,9 @@ /** * Embedded answer (Cloze) question importer. * - * @package qformat - * @subpackage multianswer - * @copyright 2003 Henrik Kaipe - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package qformat_multianswer + * @copyright 2003 Henrik Kaipe + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -31,32 +30,36 @@ defined('MOODLE_INTERNAL') || die(); * Importer that imports a text file containing a single Multianswer question * from a text file. * - * @copyright 2003 Henrik Kaipe - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright 2003 Henrik Kaipe + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class qformat_multianswer extends qformat_default { public function provide_import() { - return true; + return true; } - protected function readquestions($lines) { + public function readquestions($lines) { + question_bank::get_qtype('multianswer'); // Ensure the multianswer code is loaded. + // For this class the method has been simplified as // there can never be more than one question for a - // multianswer import + // multianswer import. $questions = array(); $questiontext = array(); $questiontext['text'] = implode('', $lines); - $questiontext['format'] = 0 ; + $questiontext['format'] = FORMAT_MOODLE; $questiontext['itemid'] = ''; $question = qtype_multianswer_extract_question($questiontext); - $question->questiontext = $question->questiontext['text'] ; - $question->questiontextformat = 0 ; + $question->questiontext = $question->questiontext['text']; + $question->questiontextformat = 0; - $question->qtype = MULTIANSWER; + $question->qtype = 'multianswer'; $question->generalfeedback = ''; - $question->course = $this->course; + $question->generalfeedbackformat = FORMAT_MOODLE; + $question->length = 1; + $question->penalty = 0.3333333; if (!empty($question)) { $name = html_to_text(implode(' ', $lines)); diff --git a/question/type/multianswer/questiontype.php b/question/type/multianswer/questiontype.php index 3dc969aa496..ba333d7a311 100644 --- a/question/type/multianswer/questiontype.php +++ b/question/type/multianswer/questiontype.php @@ -303,7 +303,7 @@ function qtype_multianswer_extract_question($text) { $question->defaultmark = 0; // Will be increased for each answer norm for ($positionkey = 1; - preg_match('/'.ANSWER_REGEX.'/', $question->questiontext['text'], $answerregs); + preg_match('/'.ANSWER_REGEX.'/s', $question->questiontext['text'], $answerregs); ++$positionkey) { $wrapped = new stdClass(); $wrapped->generalfeedback['text'] = ''; @@ -389,7 +389,7 @@ function qtype_multianswer_extract_question($text) { $answerindex = 0; $remainingalts = $answerregs[ANSWER_REGEX_ALTERNATIVES]; - while (preg_match('/~?'.ANSWER_ALTERNATIVE_REGEX.'/', $remainingalts, $altregs)) { + while (preg_match('/~?'.ANSWER_ALTERNATIVE_REGEX.'/s', $remainingalts, $altregs)) { if ('=' == $altregs[ANSWER_ALTERNATIVE_REGEX_FRACTION]) { $wrapped->fraction["$answerindex"] = '1'; } else if ($percentile = $altregs[ANSWER_ALTERNATIVE_REGEX_PERCENTILE_FRACTION]) { @@ -411,7 +411,7 @@ function qtype_multianswer_extract_question($text) { } if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_NUMERICAL]) - && preg_match('~'.NUMERICAL_ALTERNATIVE_REGEX.'~', + && preg_match('~'.NUMERICAL_ALTERNATIVE_REGEX.'~s', $altregs[ANSWER_ALTERNATIVE_REGEX_ANSWER], $numregs)) { $wrapped->answer[] = $numregs[NUMERICAL_CORRECT_ANSWER]; if (array_key_exists(NUMERICAL_ABS_ERROR_MARGIN, $numregs)) {