From 05cef658ddd48621b74344971ee0c696e4ea5f0c Mon Sep 17 00:00:00 2001 From: Jamie Pratt Date: Tue, 2 Aug 2011 18:34:47 +0700 Subject: [PATCH] MDL-28564 "implement support for extra answer fields in question export and import" implemented support for extra answer fields in questiontype base export and import functions. --- question/type/questiontypebase.php | 71 +++++++++++++++++++----------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/question/type/questiontypebase.php b/question/type/questiontypebase.php index d127c6ebe28..2a5618b861d 100644 --- a/question/type/questiontypebase.php +++ b/question/type/questiontypebase.php @@ -940,23 +940,32 @@ class question_type { $qo->qtype = $question_type; foreach ($extraquestionfields as $field) { - $qo->$field = $format->getpath($data, array('#', $field, 0, '#'), $qo->$field); + $qo->$field = $format->getpath($data, array('#', $field, 0, '#'), ''); } // run through the answers $answers = $data['#']['answer']; $a_count = 0; - $extraasnwersfields = $this->extra_answer_fields(); - if (is_array($extraasnwersfields)) { - // TODO import the answers, with any extra data. - } else { - foreach ($answers as $answer) { - $ans = $format->import_answer($answer); + $extraanswersfields = $this->extra_answer_fields(); + if (is_array($extraanswersfields)) { + array_shift($extraanswersfields); + } + foreach ($answers as $answer) { + $ans = $format->import_answer($answer); + if (!$this->has_html_answers()) { + $qo->answer[$a_count] = $ans->answer['text']; + } else { $qo->answer[$a_count] = $ans->answer; - $qo->fraction[$a_count] = $ans->fraction; - $qo->feedback[$a_count] = $ans->feedback; - ++$a_count; } + $qo->fraction[$a_count] = $ans->fraction; + $qo->feedback[$a_count] = $ans->feedback; + if (is_array($extraanswersfields)) { + foreach ($extraanswersfields as $field) { + $qo->{$field}[$a_count] = + $format->getpath($answer, array('#', $field, 0, '#'), ''); + } + } + ++$a_count; } return $qo; } @@ -977,29 +986,39 @@ class question_type { array_shift($extraquestionfields); $expout=''; foreach ($extraquestionfields as $field) { - $exportedvalue = $question->options->$field; - if (!empty($exportedvalue) && htmlspecialchars($exportedvalue) != $exportedvalue) { - $exportedvalue = ''; - } + $exportedvalue = self::wrap_html_special_chars($question->options->$field); $expout .= " <$field>{$exportedvalue}\n"; } - $extraasnwersfields = $this->extra_answer_fields(); - if (is_array($extraasnwersfields)) { - // TODO export answers with any extra data - } else { - foreach ($question->options->answers as $answer) { - $percent = 100 * $answer->fraction; - $expout .= " \n"; - $expout .= $format->writetext($answer->answer, 3, false); - $expout .= " \n"; - $expout .= $format->writetext($answer->feedback, 4, false); - $expout .= " \n"; - $expout .= " \n"; + $extraanswersfields = $this->extra_answer_fields(); + if (is_array($extraanswersfields)) { + array_shift($extraanswersfields); + } + foreach ($question->options->answers as $answer) { + $percent = 100 * $answer->fraction; + $expout .= " \n"; + $expout .= $format->writetext($answer->answer, 3, false); + $expout .= " \n"; + $expout .= $format->writetext($answer->feedback, 4, false); + $expout .= " \n"; + if (is_array($extraanswersfields)) { + foreach ($extraanswersfields as $field) { + $exportedvalue = self::wrap_html_special_chars($answer->$field); + $expout .= " <$field>{$exportedvalue}\n"; + } } + + + $expout .= " \n"; } return $expout; } + protected static function wrap_html_special_chars($text) { + if (!empty($text) && htmlspecialchars($text) != $text) { + $text = ''; + } + return $text; + } /** * Abstract function implemented by each question type. It runs all the code