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.
This commit is contained in:
Jamie Pratt
2011-08-22 23:32:47 +01:00
committed by Tim Hunt
parent 24654d977b
commit 80fa621c50
+45 -26
View File
@@ -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 = '<![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