MDL-67335 qtype_calculatedmulti: allow HTML in answer options

This commit is contained in:
Philipp Imhof
2024-01-17 08:03:14 +01:00
parent 580c009cac
commit cf62f5cc76
8 changed files with 227 additions and 57 deletions
+29 -7
View File
@@ -160,6 +160,7 @@ class qformat_xml extends qformat_default {
if ($istext) {
if (!is_string($xml)) {
$this->error(get_string('invalidxml', 'qformat_xml'));
return false;
}
$xml = trim($xml);
}
@@ -863,7 +864,21 @@ class qformat_xml extends qformat_default {
if (empty($ans->answer['text'])) {
$ans->answer['text'] = '*';
}
$qo->answer[] = $ans->answer['text'];
// The qtype_calculatedmulti allows HTML in answer options.
if ($question['@']['type'] == 'calculatedmulti') {
// If the import file contains a "format" attribute for the answer text,
// then use it. Otherwise, we must set the answerformat to FORMAT_PLAIN,
// because the question has been exported from a Moodle version that
// did not yet allow HTML answer options.
if (array_key_exists('format', $answer['@'])) {
$ans->answer['format'] = $this->trans_format($answer['@']['format']);
} else {
$ans->answer['format'] = FORMAT_PLAIN;
}
$qo->answer[] = $ans->answer;
} else {
$qo->answer[] = $ans->answer['text'];
}
$qo->feedback[] = $ans->feedback;
$qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
// Fraction as a tag is deprecated.
@@ -901,7 +916,7 @@ class qformat_xml extends qformat_default {
}
}
$datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
$datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'] ?? [];
$qo->dataset = array();
$qo->datasetindex= 0;
foreach ($datasets as $dataset) {
@@ -1439,12 +1454,19 @@ class qformat_xml extends qformat_default {
foreach ($question->options->answers as $answer) {
$percent = 100 * $answer->fraction;
$expout .= " <answer fraction=\"{$percent}\">\n";
// For qtype_calculatedmulti, answer options (choices) can be in plain text or in HTML
// format, so we need to specify when exporting a question.
if ($component == 'qtype_calculatedmulti') {
$expout .= "<answer fraction=\"{$percent}\" {$this->format($answer->answerformat)}>\n";
} else {
$expout .= "<answer fraction=\"{$percent}\">\n";
}
// The "<text/>" tags are an added feature, old files won't have them.
$expout .= $this->writetext($answer->answer, 3);
$expout .= " <tolerance>{$answer->tolerance}</tolerance>\n";
$expout .= " <tolerancetype>{$answer->tolerancetype}</tolerancetype>\n";
$expout .= " <correctanswerformat>" .
$expout .= $this->writetext($answer->answer);
$expout .= $this->write_files($this->answerfiles[$answer->id]);
$expout .= " <tolerance>{$answer->tolerance}</tolerance>\n";
$expout .= " <tolerancetype>{$answer->tolerancetype}</tolerancetype>\n";
$expout .= " <correctanswerformat>" .
$answer->correctanswerformat . "</correctanswerformat>\n";
$expout .= " <correctanswerlength>" .
$answer->correctanswerlength . "</correctanswerlength>\n";