MDL-34748 repository_filesystem: Sort files by title by default
authorMarina Glancy <marina@moodle.com>
Mon, 13 Aug 2012 03:54:54 +0000 (11:54 +0800)
committerMarina Glancy <marina@moodle.com>
Mon, 13 Aug 2012 03:58:58 +0000 (11:58 +0800)
repository/filesystem/lib.php

index b295113..6eb3b3b 100644 (file)
@@ -91,37 +91,49 @@ class repository_filesystem extends repository {
         $list['dynload'] = true;
         $list['nologin'] = true;
         $list['nosearch'] = true;
+        // retrieve list of files and directories and sort them
+        $fileslist = array();
+        $dirslist = array();
         if ($dh = opendir($this->root_path)) {
             while (($file = readdir($dh)) != false) {
                 if ( $file != '.' and $file !='..') {
-                    if (filetype($this->root_path.$file) == 'file') {
-                        $list['list'][] = array(
-                            'title' => $file,
-                            'source' => $path.'/'.$file,
-                            'size' => filesize($this->root_path.$file),
-                            'datecreated' => filectime($this->root_path.$file),
-                            'datemodified' => filemtime($this->root_path.$file),
-                            'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file, 90))->out(false),
-                            'icon' => $OUTPUT->pix_url(file_extension_icon($file, 24))->out(false)
-                        );
+                    if (is_file($this->root_path.$file)) {
+                        $fileslist[] = $file;
                     } else {
-                        if (!empty($path)) {
-                            $current_path = $path . '/'. $file;
-                        } else {
-                            $current_path = $file;
-                        }
-                        $list['list'][] = array(
-                            'title' => $file,
-                            'children' => array(),
-                            'datecreated' => filectime($this->root_path.$file),
-                            'datemodified' => filemtime($this->root_path.$file),
-                            'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
-                            'path' => $current_path
-                            );
+                        $dirslist[] = $file;
                     }
                 }
             }
         }
+        collatorlib::asort($fileslist, collatorlib::SORT_STRING);
+        collatorlib::asort($dirslist, collatorlib::SORT_STRING);
+        // fill the $list['list']
+        foreach ($dirslist as $file) {
+            if (!empty($path)) {
+                $current_path = $path . '/'. $file;
+            } else {
+                $current_path = $file;
+            }
+            $list['list'][] = array(
+                'title' => $file,
+                'children' => array(),
+                'datecreated' => filectime($this->root_path.$file),
+                'datemodified' => filemtime($this->root_path.$file),
+                'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
+                'path' => $current_path
+                );
+        }
+        foreach ($fileslist as $file) {
+            $list['list'][] = array(
+                'title' => $file,
+                'source' => $path.'/'.$file,
+                'size' => filesize($this->root_path.$file),
+                'datecreated' => filectime($this->root_path.$file),
+                'datemodified' => filemtime($this->root_path.$file),
+                'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file, 90))->out(false),
+                'icon' => $OUTPUT->pix_url(file_extension_icon($file, 24))->out(false)
+            );
+        }
         $list['list'] = array_filter($list['list'], array($this, 'filter'));
         return $list;
     }