Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-33424 Images missing when restoring a 1.9 backup

  • Loading branch information...
commit 3f28674535f8b8f20ba0a49417e41244a7a5fe93 1 parent 8673a98
Jean-Michel Vedrine authored
View
61 backup/converter/moodle1/handlerlib.php
@@ -1041,6 +1041,24 @@ class moodle1_question_bank_handler extends moodle1_xml_handler {
private $qtypehandlers = null;
/**
+ * Return the file manager instance used.
+ *
+ * @return moodle1_file_manager
+ */
+ public function get_file_manager() {
+ return $this->fileman;
+ }
+
+ /**
+ * Returns the information about the question category context being currently parsed
+ *
+ * @return array with keys contextid, contextlevel and contextinstanceid
+ */
+ public function get_current_category_context() {
+ return $this->currentcategory;
+ }
+
+ /**
* Registers path that are not qtype-specific
*/
public function get_paths() {
@@ -1191,6 +1209,17 @@ public function process_question(array $data, array $raw) {
$data['generalfeedbackformat'] = FORMAT_HTML;
}
+ // Migrate files in questiontext.
+ $this->fileman->contextid = $this->currentcategory['contextid'];
+ $this->fileman->component = 'question';
+ $this->fileman->filearea = 'questiontext';
+ $this->fileman->itemid = $data['id'];
+ $data['questiontext'] = moodle1_converter::migrate_referenced_files($data['questiontext'], $this->fileman);
+
+ // Migrate files in generalfeedback.
+ $this->fileman->filearea = 'generalfeedback';
+ $data['generalfeedback'] = moodle1_converter::migrate_referenced_files($data['generalfeedback'], $this->fileman);
+
// replay the upgrade step 2010080901 - updating question image
if (!empty($data['image'])) {
if (textlib::substr(textlib::strtolower($data['image']), 0, 7) == 'http://') {
@@ -1736,6 +1765,14 @@ protected function write_answers(array $answers, $qtype) {
foreach ($answers as $elementname => $elements) {
foreach ($elements as $element) {
$answer = $this->convert_answer($element, $qtype);
+ // Migrate images in answertext.
+ if ($answer['answerformat'] == FORMAT_HTML) {
+ $answer['answertext'] = $this->migrate_files($answer['answertext'], 'question', 'answer', $answer['id']);
+ }
+ // Migrate images in feedback.
+ if ($answer['feedbackformat'] == FORMAT_HTML) {
+ $answer['feedback'] = $this->migrate_files($answer['feedback'], 'question', 'answerfeedback', $answer['id']);
+ }
$this->write_xml('answer', $answer, array('/answer/id'));
}
}
@@ -1743,6 +1780,27 @@ protected function write_answers(array $answers, $qtype) {
}
/**
+ * Migrate files belonging to one qtype plugin text field.
+ *
+ * @param array $text the html fragment containing references to files
+ * @param string $component the component for restored files
+ * @param string $filearea the file area for restored files
+ * @param int $itemid the itemid for restored files
+ *
+ * @return string the text for this field, after files references have been processed
+ */
+ protected function migrate_files($text, $component, $filearea, $itemid) {
+ $context = $this->qbankhandler->get_current_category_context();
+ $fileman = $this->qbankhandler->get_file_manager();
+ $fileman->contextid = $context['contextid'];
+ $fileman->component = $component;
+ $fileman->filearea = $filearea;
+ $fileman->itemid = $itemid;
+ $text = moodle1_converter::migrate_referenced_files($text, $fileman);
+ return $text;
+ }
+
+ /**
* Writes the grouped numerical_units structure
*
* @param array $numericalunits
@@ -1900,7 +1958,8 @@ private function convert_answer(array $old, $qtype) {
if ($qtype !== 'multichoice') {
$new['answerformat'] = FORMAT_PLAIN;
} else {
- $new['answerformat'] = FORMAT_MOODLE;
+ $new['answertext'] = text_to_html($new['answertext'], false, false, true);
+ $new['answerformat'] = FORMAT_HTML;
}
if ($CFG->texteditors !== 'textarea') {
View
2  question/type/match/backup/moodle1/lib.php
@@ -76,6 +76,8 @@ public function process_question(array $data, array $raw) {
$match['questiontextformat'] = $data['oldquestiontextformat'];
}
+ $match['questiontext'] = $this->migrate_files(
+ $match['questiontext'], 'qtype_match', 'subquestion', $match['id']);
$this->write_xml('match', $match, array('/match/id'));
}
}
View
12 question/type/multichoice/backup/moodle1/lib.php
@@ -65,7 +65,7 @@ public function process_question(array $data, array $raw) {
'answernumbering' => 'abc',
));
}
- $this->write_multichoice($data['multichoice'], $data['oldquestiontextformat']);
+ $this->write_multichoice($data['multichoice'], $data['oldquestiontextformat'], $data['id']);
}
/**
@@ -73,8 +73,9 @@ public function process_question(array $data, array $raw) {
*
* @param array $multichoices the grouped structure
* @param int $oldquestiontextformat - {@see moodle1_question_bank_handler::process_question()}
+ * @param int $questionid question id
*/
- protected function write_multichoice(array $multichoices, $oldquestiontextformat) {
+ protected function write_multichoice(array $multichoices, $oldquestiontextformat, $questionid) {
global $CFG;
// the grouped array is supposed to have just one element - let us use foreach anyway
@@ -101,6 +102,13 @@ protected function write_multichoice(array $multichoices, $oldquestiontextformat
$multichoice['incorrectfeedbackformat'] = $oldquestiontextformat;
}
+ $multichoice['correctfeedback'] = $this->migrate_files(
+ $multichoice['correctfeedback'], 'question', 'correctfeedback', $questionid);
+ $multichoice['partiallycorrectfeedback'] = $this->migrate_files(
+ $multichoice['partiallycorrectfeedback'], 'question', 'partiallycorrectfeedback', $questionid);
+ $multichoice['incorrectfeedback'] = $this->migrate_files(
+ $multichoice['incorrectfeedback'], 'question', 'incorrectfeedback', $questionid);
+
$this->write_xml('multichoice', $multichoice, array('/multichoice/id'));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.