Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-28564 "implement support for extra answer fields in question expo…

…rt and

import" implemented support for extra answer fields in questiontype base
export and import functions.
  • Loading branch information...
commit 05cef658ddd48621b74344971ee0c696e4ea5f0c 1 parent 4ad6128
@jamiepratt jamiepratt authored timhunt committed
Showing with 45 additions and 26 deletions.
  1. +45 −26 question/type/questiontypebase.php
View
71 question/type/questiontypebase.php
@@ -940,23 +940,32 @@ public function import_from_xml($data, $question, $format, $extra=null) {
$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 @@ public function export_to_xml($question, $format, $extra=null) {
array_shift($extraquestionfields);
$expout='';
foreach ($extraquestionfields as $field) {
- $exportedvalue = $question->options->$field;
- if (!empty($exportedvalue) && htmlspecialchars($exportedvalue) != $exportedvalue) {
- $exportedvalue = '<![CDATA[' . $exportedvalue . ']]>';
- }
+ $exportedvalue = self::wrap_html_special_chars($question->options->$field);
$expout .= " <$field>{$exportedvalue}</$field>\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 .= " <answer fraction=\"$percent\">\n";
- $expout .= $format->writetext($answer->answer, 3, false);
- $expout .= " <feedback>\n";
- $expout .= $format->writetext($answer->feedback, 4, false);
- $expout .= " </feedback>\n";
- $expout .= " </answer>\n";
+ $extraanswersfields = $this->extra_answer_fields();
+ if (is_array($extraanswersfields)) {
+ array_shift($extraanswersfields);
+ }
+ foreach ($question->options->answers as $answer) {
+ $percent = 100 * $answer->fraction;
+ $expout .= " <answer fraction=\"$percent\">\n";
+ $expout .= $format->writetext($answer->answer, 3, false);
+ $expout .= " <feedback>\n";
+ $expout .= $format->writetext($answer->feedback, 4, false);
+ $expout .= " </feedback>\n";
+ if (is_array($extraanswersfields)) {
+ foreach ($extraanswersfields as $field) {
+ $exportedvalue = self::wrap_html_special_chars($answer->$field);
+ $expout .= " <$field>{$exportedvalue}</$field>\n";
+ }
}
+
+
+ $expout .= " </answer>\n";
}
return $expout;
}
+ protected static function wrap_html_special_chars($text) {
+ if (!empty($text) && htmlspecialchars($text) != $text) {
+ $text = '<![CDATA[' . $text . ']]>';
+ }
+ return $text;
+ }
/**
* Abstract function implemented by each question type. It runs all the code
Please sign in to comment.
Something went wrong with that request. Please try again.