]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14589 new function returning area files in tree structure
authorskodak <skodak>
Sun, 21 Jun 2009 18:22:52 +0000 (18:22 +0000)
committerskodak <skodak>
Sun, 21 Jun 2009 18:22:52 +0000 (18:22 +0000)
lib/file/file_storage.php

index 280bca42b72ebee53054384308f1dde5041352b7..dabab6a0ce3e4cbfc1f33906fd1bceba59601fdf 100644 (file)
@@ -211,6 +211,49 @@ class file_storage {
         return $result;
     }
 
+    /**
+     * Returns array based tree structure of area files
+     * @param int $contextid
+     * @param string $filearea
+     * @param int $itemid
+     * @return array each dir represented by dirname, subdirs, files and dirfile array elements
+     */
+    public function get_area_tree($contextid, $filearea, $itemid) {
+        $result = array('dirname'=>'', 'dirfile'=>null, 'subdirs'=>array(), 'files'=>array());
+        $files = $this->get_area_files($contextid, $filearea, $itemid, $sort="itemid, filepath, filename", true);
+        // first create directory structure
+        foreach ($files as $hash=>$dir) {
+            if (!$dir->is_directory()) {
+                continue;
+            }
+            unset($files[$hash]);
+            if ($dir->get_filepath() === '/') {
+                $result['dirfile'] = $dir;
+                continue;
+            }
+            $parts = explode('/', trim($dir->get_filepath(),'/'));
+            $pointer =& $result;
+            foreach ($parts as $part) {
+                if (!isset($pointer['subdirs'][$part])) {
+                    $pointer['subdirs'][$part] = array('dirname'=>$part, 'dirfile'=>null, 'subdirs'=>array(), 'files'=>array());
+                }
+                $pointer =& $pointer['subdirs'][$part];
+            }
+            $pointer['dirfile'] = $dir;
+            unset($pointer);
+        }
+        foreach ($files as $hash=>$file) {
+            $parts = explode('/', trim($file->get_filepath(),'/'));
+            $pointer =& $result;
+            foreach ($parts as $part) {
+                $pointer =& $pointer['subdirs'][$part];
+            }
+            $pointer['files'][$file->get_filename()] = $file;
+            unset($pointer);
+        }
+        return $result;
+    }
+
     /**
      * Returns all files and otionally directories
      * @param int $contextid