From a00bff10e590cdf64289b0a4364b5d7e2b3cccee Mon Sep 17 00:00:00 2001
From: Jean-Michel Vedrine
Date: Sun, 29 Sep 2013 08:14:32 +0200
Subject: [PATCH] MDL-42033 Files in subdirs not exported correctly by question
XML format
---
question/format.php | 2 +-
question/format/xml/format.php | 18 ++++----
question/format/xml/tests/xmlformat_test.php | 47 ++++++++++++++++++++
3 files changed, 58 insertions(+), 9 deletions(-)
diff --git a/question/format.php b/question/format.php
index 7dfe33056a9..97b60cfa56a 100644
--- a/question/format.php
+++ b/question/format.php
@@ -387,7 +387,7 @@ class qformat_default {
$question->modifiedby = $USER->id;
$question->timemodified = time();
$fileoptions = array(
- 'subdirs' => false,
+ 'subdirs' => true,
'maxfiles' => -1,
'maxbytes' => 0,
);
diff --git a/question/format/xml/format.php b/question/format/xml/format.php
index 60febbbdef7..e4bfb7f2e1f 100644
--- a/question/format/xml/format.php
+++ b/question/format/xml/format.php
@@ -169,11 +169,13 @@ class qformat_xml extends qformat_default {
}
$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(
@@ -181,11 +183,11 @@ class qformat_xml extends qformat_default {
'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 @@ class qformat_xml extends qformat_default {
if ($file->is_directory()) {
continue;
}
- $string .= '';
+ $string .= '';
$string .= base64_encode($file->get_content());
- $string .= '';
+ $string .= "\n";
}
return $string;
}
diff --git a/question/format/xml/tests/xmlformat_test.php b/question/format/xml/tests/xmlformat_test.php
index 19cc8727bfc..73ca5dbbb3e 100644
--- a/question/format/xml/tests/xmlformat_test.php
+++ b/question/format/xml/tests/xmlformat_test.php
@@ -1463,4 +1463,51 @@ END;
$this->assertEquals('/', $file->filepath);
$this->assertEquals(6, $file->size);
}
+
+ public function test_import_truefalse_wih_files() {
+ $this->resetAfterTest();
+ $this->setAdminUser();
+
+ $xml = '
+
+ truefalse
+
+
+ This text file contains the word Moodle.
]]>
+TW9vZGxl
+
+
+ For further information, see the documentation about Moodle.]]>
+
+ 1.0000000
+ 1.0000000
+ 0
+
+ true
+
+
+
+
+
+ false
+
+
+
+
+ ';
+ $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);
+ }
}