Skip to content

Commit

Permalink
Merge branch 'MDL-29060' of git://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Nov 23, 2011
2 parents 1bb98bd + 7e7fdf5 commit 48e74d2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
22 changes: 0 additions & 22 deletions question/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,26 +876,4 @@ protected function format_question_text($question) {
return html_to_text(format_text($question->questiontext,
$question->questiontextformat, $formatoptions), 0, false);
}

/**
* convert files into text output in the given format.
* @param array
* @param string encoding method
* @return string $string
*/
protected function writefiles($files, $encoding='base64') {
if (empty($files)) {
return '';
}
$string = '';
foreach ($files as $file) {
if ($file->is_directory()) {
continue;
}
$string .= '<file name="' . $file->get_filename() . '" encoding="' . $encoding . '">';
$string .= base64_encode($file->get_content());
$string .= '</file>';
}
return $string;
}
}
49 changes: 35 additions & 14 deletions question/format/xml/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,27 @@ public function writetext($raw, $indent = 0, $short = true) {
return $xml;
}

/**
* Generte the XML to represent some files.
* @param array of store array of stored_file objects.
* @return string $string the XML.
*/
public function write_files($files) {
if (empty($files)) {
return '';
}
$string = '';
foreach ($files as $file) {
if ($file->is_directory()) {
continue;
}
$string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
$string .= base64_encode($file->get_content());
$string .= '</file>';
}
return $string;
}

protected function presave_process($content) {
// Override to allow us to add xml headers and footers
return '<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -1110,11 +1131,11 @@ public function writequestion($question) {
$expout .= " </name>\n";
$expout .= " <questiontext {$this->format($question->questiontextformat)}>\n";
$expout .= $this->writetext($question->questiontext, 3);
$expout .= $this->writefiles($question->questiontextfiles);
$expout .= $this->write_files($question->questiontextfiles);
$expout .= " </questiontext>\n";
$expout .= " <generalfeedback {$this->format($question->generalfeedbackformat)}>\n";
$expout .= $this->writetext($question->generalfeedback, 3);
$expout .= $this->writefiles($question->generalfeedbackfiles);
$expout .= $this->write_files($question->generalfeedbackfiles);
$expout .= " </generalfeedback>\n";
if ($question->qtype != 'multianswer') {
$expout .= " <defaultgrade>{$question->defaultmark}</defaultgrade>\n";
Expand Down Expand Up @@ -1191,7 +1212,7 @@ public function writequestion($question) {
$expout .= " <instructions " .
$this->format($question->options->instructionsformat) . ">\n";
$expout .= $this->writetext($question->options->instructions, 3);
$expout .= $this->writefiles($files);
$expout .= $this->write_files($files);
$expout .= " </instructions>\n";
}
break;
Expand All @@ -1207,7 +1228,7 @@ public function writequestion($question) {
$expout .= " <subquestion " .
$this->format($subquestion->questiontextformat) . ">\n";
$expout .= $this->writetext($subquestion->questiontext, 3);
$expout .= $this->writefiles($files);
$expout .= $this->write_files($files);
$expout .= " <answer>\n";
$expout .= $this->writetext($subquestion->answertext, 4);
$expout .= " </answer>\n";
Expand Down Expand Up @@ -1235,7 +1256,7 @@ public function writequestion($question) {
$expout .= " <graderinfo " .
$this->format($question->options->graderinfoformat) . ">\n";
$expout .= $this->writetext($question->options->graderinfo, 3);
$expout .= $this->writefiles($fs->get_area_files($contextid, 'qtype_essay',
$expout .= $this->write_files($fs->get_area_files($contextid, 'qtype_essay',
'graderinfo', $question->id));
$expout .= " </graderinfo>\n";
break;
Expand All @@ -1255,21 +1276,21 @@ public function writequestion($question) {
'correctfeedback', $question->id);
$expout .= " <correctfeedback>\n";
$expout .= $this->writetext($question->options->correctfeedback, 3);
$expout .= $this->writefiles($files);
$expout .= $this->write_files($files);
$expout .= " </correctfeedback>\n";

$files = $fs->get_area_files($contextid, $component,
'partiallycorrectfeedback', $question->id);
$expout .= " <partiallycorrectfeedback>\n";
$expout .= $this->writetext($question->options->partiallycorrectfeedback, 3);
$expout .= $this->writefiles($files);
$expout .= $this->write_files($files);
$expout .= " </partiallycorrectfeedback>\n";

$files = $fs->get_area_files($contextid, $component,
'incorrectfeedback', $question->id);
$expout .= " <incorrectfeedback>\n";
$expout .= $this->writetext($question->options->incorrectfeedback, 3);
$expout .= $this->writefiles($files);
$expout .= $this->write_files($files);
$expout .= " </incorrectfeedback>\n";

foreach ($question->options->answers as $answer) {
Expand All @@ -1287,7 +1308,7 @@ public function writequestion($question) {
$files = $fs->get_area_files($contextid, $component,
'instruction', $question->id);
$expout .= $this->writetext($answer->feedback);
$expout .= $this->writefiles($answer->feedbackfiles);
$expout .= $this->write_files($answer->feedbackfiles);
$expout .= " </feedback>\n";
$expout .= "</answer>\n";
}
Expand All @@ -1312,7 +1333,7 @@ public function writequestion($question) {
$expout .= " <instructions " .
$this->format($question->options->instructionsformat) . ">\n";
$expout .= $this->writetext($question->options->instructions, 3);
$expout .= $this->writefiles($files);
$expout .= $this->write_files($files);
$expout .= " </instructions>\n";
}

Expand Down Expand Up @@ -1418,10 +1439,10 @@ public function write_answer($answer, $extra = '') {
$output = '';
$output .= " <answer fraction=\"$percent\" {$this->format($answer->answerformat)}>\n";
$output .= $this->writetext($answer->answer, 3);
$output .= $this->writefiles($answer->answerfiles);
$output .= $this->write_files($answer->answerfiles);
$output .= " <feedback {$this->format($answer->feedbackformat)}>\n";
$output .= $this->writetext($answer->feedback, 4);
$output .= $this->writefiles($answer->feedbackfiles);
$output .= $this->write_files($answer->feedbackfiles);
$output .= " </feedback>\n";
$output .= $extra;
$output .= " </answer>\n";
Expand Down Expand Up @@ -1471,7 +1492,7 @@ public function write_hint($hint, $contextid) {
if (!empty($hint->options)) {
$output .= ' <options>' . $this->xml_escape($hint->options) . "</options>\n";
}
$output .= $this->writefiles($files);
$output .= $this->write_files($files);
$output .= " </hint>\n";
return $output;
}
Expand All @@ -1494,7 +1515,7 @@ public function write_combined_feedback($questionoptions, $questionid, $contexti

$output .= " <{$field} {$this->format($questionoptions->$formatfield)}>\n";
$output .= ' ' . $this->writetext($questionoptions->$field);
$output .= $this->writefiles($files);
$output .= $this->write_files($files);
$output .= " </{$field}>\n";
}

Expand Down

0 comments on commit 48e74d2

Please sign in to comment.