MDL-33424 Images missing when restoring a 1.9 backup
authorJean-Michel Vedrine <vedrine@vedrine.org>
Tue, 1 Jan 2013 17:23:05 +0000 (18:23 +0100)
committerJean-Michel Vedrine <vedrine@vedrine.org>
Sat, 9 Mar 2013 12:37:27 +0000 (13:37 +0100)
backup/converter/moodle1/handlerlib.php
question/type/match/backup/moodle1/lib.php
question/type/multichoice/backup/moodle1/lib.php

index a2744c2..94c0b1d 100644 (file)
@@ -1040,6 +1040,24 @@ class moodle1_question_bank_handler extends moodle1_xml_handler {
     /** @var array holds the instances of qtype specific conversion handlers */
     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
      */
@@ -1191,6 +1209,17 @@ class moodle1_question_bank_handler extends moodle1_xml_handler {
             $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,12 +1765,41 @@ abstract class moodle1_qtype_handler extends moodle1_plugin_handler {
         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'));
             }
         }
         $this->xmlwriter->end_tag('answers');
     }
 
+    /**
+     * 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
      *
@@ -1900,7 +1958,8 @@ abstract class moodle1_qtype_handler extends moodle1_plugin_handler {
         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') {
index 94ba827..f084711 100644 (file)
@@ -78,6 +78,8 @@ class moodle1_qtype_match_handler extends moodle1_qtype_handler {
                     $match['questiontextformat'] = $data['oldquestiontextformat'];
                 }
 
+                $match['questiontext'] = $this->migrate_files(
+                        $match['questiontext'], 'qtype_match', 'subquestion', $match['id']);
                 $this->write_xml('match', $match, array('/match/id'));
             }
         }
index bb1a459..9913c9c 100644 (file)
@@ -65,7 +65,7 @@ class moodle1_qtype_multichoice_handler extends moodle1_qtype_handler {
                 'answernumbering'                => 'abc',
             ));
         }
-        $this->write_multichoice($data['multichoice'], $data['oldquestiontextformat']);
+        $this->write_multichoice($data['multichoice'], $data['oldquestiontextformat'], $data['id']);
     }
 
     /**
@@ -73,8 +73,9 @@ class moodle1_qtype_multichoice_handler extends moodle1_qtype_handler {
      *
      * @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 @@ class moodle1_qtype_multichoice_handler extends moodle1_qtype_handler {
                 $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'));
         }
     }