MDL-62849 files lib: Add method to return all files.
authorAdrian Greeve <abgreeve@gmail.com>
Mon, 9 Jul 2018 03:05:57 +0000 (11:05 +0800)
committerAdrian Greeve <abgreeve@gmail.com>
Tue, 17 Jul 2018 02:25:11 +0000 (10:25 +0800)
Added a function which returns all the files from all directories
in the draftarea.

lib/filelib.php
lib/form/filemanager.php
lib/tests/filelib_test.php

index a8c844a..2d189a3 100644 (file)
@@ -757,6 +757,34 @@ function file_get_drafarea_files($draftitemid, $filepath = '/') {
     return $data;
 }
 
+/**
+ * Returns all of the files in the draftarea.
+ *
+ * @param  int $draftitemid The draft item ID
+ * @param  string $filepath path for the uploaded files.
+ * @return array An array of files associated with this draft item id.
+ */
+function file_get_all_files_in_draftarea(int $draftitemid, string $filepath = '/') : array {
+    $files = [];
+    $draftfiles = file_get_drafarea_files($draftitemid, $filepath);
+    file_get_drafarea_folders($draftitemid, $filepath, $draftfiles);
+
+    if (!empty($draftfiles)) {
+        foreach ($draftfiles->list as $draftfile) {
+            if ($draftfile->type == 'file') {
+                $files[] = $draftfile;
+            }
+        }
+
+        if (isset($draftfiles->children)) {
+            foreach ($draftfiles->children as $draftfile) {
+                $files = array_merge($files, file_get_all_files_in_draftarea($draftitemid, $draftfile->filepath));
+            }
+        }
+    }
+    return $files;
+}
+
 /**
  * Returns draft area itemid for a given element.
  *
index 71686d0..26c2271 100644 (file)
@@ -333,7 +333,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element implements temp
             return;
         }
 
-        $draftfiles = file_get_drafarea_files($value);
+        $draftfiles = file_get_all_files_in_draftarea($value);
         $wrongfiles = array();
 
         if (empty($draftfiles)) {
@@ -341,7 +341,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element implements temp
             return;
         }
 
-        foreach ($draftfiles->list as $file) {
+        foreach ($draftfiles as $file) {
             if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
                 $wrongfiles[] = $file->filename;
             }
index 01d03bd..a6ff45f 100644 (file)
@@ -1424,6 +1424,55 @@ EOF;
             $this->assertContains($file->get_filename(), $expected);
         }
     }
+
+    /**
+     * Test that all files in the draftarea are returned.
+     */
+    public function test_file_get_all_files_in_draftarea() {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        $filerecord = ['filename' => 'basepic.jpg'];
+        $file = self::create_draft_file($filerecord);
+
+        $secondrecord = [
+            'filename' => 'infolder.jpg',
+            'filepath' => '/assignment/',
+            'itemid' => $file->get_itemid()
+        ];
+        $file = self::create_draft_file($secondrecord);
+
+        $thirdrecord = [
+            'filename' => 'deeperfolder.jpg',
+            'filepath' => '/assignment/pics/',
+            'itemid' => $file->get_itemid()
+        ];
+        $file = self::create_draft_file($thirdrecord);
+
+        $fourthrecord = [
+            'filename' => 'differentimage.jpg',
+            'filepath' => '/secondfolder/',
+            'itemid' => $file->get_itemid()
+        ];
+        $file = self::create_draft_file($fourthrecord);
+
+        // This record has the same name as the last record, but it's in a different folder.
+        // Just checking this is also returned.
+        $fifthrecord = [
+            'filename' => 'differentimage.jpg',
+            'filepath' => '/assignment/pics/',
+            'itemid' => $file->get_itemid()
+        ];
+        $file = self::create_draft_file($fifthrecord);
+
+        $allfiles = file_get_all_files_in_draftarea($file->get_itemid());
+        $this->assertCount(5, $allfiles);
+        $this->assertEquals($filerecord['filename'], $allfiles[0]->filename);
+        $this->assertEquals($secondrecord['filename'], $allfiles[1]->filename);
+        $this->assertEquals($thirdrecord['filename'], $allfiles[2]->filename);
+        $this->assertEquals($fourthrecord['filename'], $allfiles[3]->filename);
+        $this->assertEquals($fifthrecord['filename'], $allfiles[4]->filename);
+    }
 }
 
 /**