Permalink
Browse files

MDL-42033 Files in subdirs not exported correctly by question XML format

  • Loading branch information...
1 parent acbd69f commit a00bff10e590cdf64289b0a4364b5d7e2b3cccee Jean-Michel Vedrine committed Sep 29, 2013
Showing with 58 additions and 9 deletions.
  1. +1 −1 question/format.php
  2. +10 −8 question/format/xml/format.php
  3. +47 −0 question/format/xml/tests/xmlformat_test.php
View
@@ -387,7 +387,7 @@ public function importprocess($category) {
$question->modifiedby = $USER->id;
$question->timemodified = time();
$fileoptions = array(
- 'subdirs' => false,
+ 'subdirs' => true,
'maxfiles' => -1,
'maxbytes' => 0,
);
@@ -169,23 +169,25 @@ public function import_files_as_draft($xml) {
}
$fs = get_file_storage();
$itemid = file_get_unused_draft_itemid();
- $filenames = array();
+ $filepaths = array();
foreach ($xml as $file) {
- $filename = $file['@']['name'];
- if (in_array($filename, $filenames)) {
- debugging('Duplicate file in XML: ' . $filename, DEBUG_DEVELOPER);
+ $filename = $this->getpath($file, array('@', 'name'), '', true);
+ $filepath = $this->getpath($file, array('@', 'path'), '/', true);
+ $fullpath = $filepath . $filename;
+ if (in_array($fullpath, $filepaths)) {
+ debugging('Duplicate file in XML: ' . $fullpath, DEBUG_DEVELOPER);
continue;
}
$filerecord = array(
'contextid' => context_user::instance($USER->id)->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $itemid,
- 'filepath' => '/',
+ 'filepath' => $filepath,
'filename' => $filename,
);
$fs->create_file_from_string($filerecord, base64_decode($file['#']));
- $filenames[] = $filename;
+ $filepaths[] = $fullpath;
}
return $itemid;
}
@@ -1088,9 +1090,9 @@ public function write_files($files) {
if ($file->is_directory()) {
continue;
}
- $string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
+ $string .= '<file name="' . $file->get_filename() . '" path="' . $file->get_filepath() . '" encoding="base64">';
$string .= base64_encode($file->get_content());
- $string .= '</file>';
+ $string .= "</file>\n";
}
return $string;
}
@@ -1463,4 +1463,51 @@ public function test_import_files_as_draft() {
$this->assertEquals('/', $file->filepath);
$this->assertEquals(6, $file->size);
}
+
+ public function test_import_truefalse_wih_files() {
+ $this->resetAfterTest();
+ $this->setAdminUser();
+
+ $xml = '<question type="truefalse">
+ <name>
+ <text>truefalse</text>
+ </name>
+ <questiontext format="html">
+ <text><![CDATA[<p><a href="@@PLUGINFILE@@/myfolder/moodle.txt">This text file</a> contains the word Moodle.</p>]]></text>
+<file name="moodle.txt" path="/myfolder/" encoding="base64">TW9vZGxl</file>
+ </questiontext>
+ <generalfeedback format="html">
+ <text><![CDATA[<p>For further information, see the documentation about Moodle.</p>]]></text>
+</generalfeedback>
+ <defaultgrade>1.0000000</defaultgrade>
+ <penalty>1.0000000</penalty>
+ <hidden>0</hidden>
+ <answer fraction="100" format="moodle_auto_format">
+ <text>true</text>
+ <feedback format="html">
+ <text></text>
+ </feedback>
+ </answer>
+ <answer fraction="0" format="moodle_auto_format">
+ <text>false</text>
+ <feedback format="html">
+ <text></text>
+ </feedback>
+ </answer>
+ </question>';
+ $xmldata = xmlize($xml);
+
+ $importer = new qformat_xml();
+ $q = $importer->import_truefalse($xmldata['question']);
+
+ $draftitemid = $q->questiontextitemid;
+ $files = file_get_drafarea_files($draftitemid, '/myfolder/');
+
+ $this->assertEquals(1, count($files->list));
+
+ $file = $files->list[0];
+ $this->assertEquals('moodle.txt', $file->filename);
+ $this->assertEquals('/myfolder/', $file->filepath);
+ $this->assertEquals(6, $file->size);
+ }
}

0 comments on commit a00bff1

Please sign in to comment.