Index: /files/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/files/index.php,v
retrieving revision 1.121.2.2
diff -u -r1.121.2.2 index.php
--- index.php	19 Feb 2008 13:12:55 -0000	1.121.2.2
+++ index.php	17 Apr 2008 10:45:48 -0000
@@ -700,6 +700,9 @@
    global $USER, $CFG;
    global $choose;

+    // Get the sort parameter if there is one
+    $sort = optional_param('sort', 1, PARAM_INT);
+
    $fullpath = $basedir.$wdir;
    $dirlist = array();

@@ -739,6 +742,13 @@
    $strfolder = get_string("folder");
    $strfile   = get_string("file");

+    // Set sort arguments so that clicking on a column that is already sorted reverses the sort order
+    $sortvalues = array(1,2,3);
+    foreach ($sortvalues as &$sortvalue) {
+	    if ($sortvalue == $sort) {
+            $sortvalue = -$sortvalue;
+        }
+    }

    echo "<form action=\"index.php\" method=\"post\" id=\"dirform\">";
    echo "<div>";
@@ -748,9 +758,9 @@
    echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\" class=\"files\">";
    echo "<tr>";
    echo "<th class=\"header\" scope=\"col\"></th>";
-    echo "<th class=\"header name\" scope=\"col\">$strname</th>";
-    echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
-    echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
+    echo "<th class=\"header name\" scope=\"col\"><a href=\"" . qualified_me(). "&sort={$sortvalues[0]}\">$strname</a></th>";
+    echo "<th class=\"header size\" scope=\"col\"><a href=\"" . qualified_me(). "&sort={$sortvalues[1]}\">$strsize</a></th>";
+    echo "<th class=\"header date\" scope=\"col\"><a href=\"" . qualified_me(). "&sort={$sortvalues[2]}\">$strmodified</a></th>";
    echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
    echo "</tr>\n";

@@ -758,14 +768,57 @@
        $dirlist[] = '..';
    }

+    // Sort parameter indicates column to sort by, and parity gives the direction
+	switch ($sort) {
+    case 1:
+        $sortcmp = 'return strcasecmp($a[0],$b[0]);';
+        break;
+    case -1:
+        $sortcmp = 'return strcasecmp($b[0],$a[0]);';
+        break;
+    case 2:
+        $sortcmp = 'return ($a[1] - $b[1]);';
+        break;
+    case -2:
+        $sortcmp = 'return ($b[1] - $a[1]);';
+        break;
+    case 3:
+        $sortcmp = 'return ($a[2] - $b[2]);';
+        break;
+    case -3:
+        $sortcmp = 'return ($b[2] - $a[2]);';
+        break;
+	}
+	
+	// Create a 2D array of directories and sort
+    $dirdetails = array();
+    foreach ($dirlist as $dir) {
+        $filename = $fullpath."/".$dir;
+        $filesize = display_size(get_directory_size("$fullpath/$dir"));
+        $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
+        $row = array($dir, $filesize, $filedate);
+		array_push($dirdetails, $row);
+ 		usort($dirdetails, create_function('$a,$b', $sortcmp)); 
+ 	}
+
+	// Create a 2D array of files and sort
+    $filedetails = array();
+    foreach ($filelist as $file) {
+        $filename = $fullpath."/".$file;
+        $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
+        $filesize = filesize($filename);
+        $row = array($file, $filesize, $filedate);
+		array_push($filedetails, $row);
+		usort($filedetails, create_function('$a,$b', $sortcmp)); 
+	}
+
    $count = 0;

-    if (!empty($dirlist)) {
-        asort($dirlist);
-        foreach ($dirlist as $dir) {
+    if (!empty($dirdetails)) {
+        foreach ($dirdetails as $dir) {
            echo "<tr class=\"folder\">";

-            if ($dir == '..') {
+            if ($dir[0] == '..') {
                $fileurl = rawurlencode(dirname($wdir));
                print_cell();
                // alt attribute intentionally empty to prevent repetition in screen reader
@@ -776,13 +829,13 @@

            } else {
                $count++;
-                $filename = $fullpath."/".$dir;
-                $fileurl  = rawurlencode($wdir."/".$dir);
-                $filesafe = rawurlencode($dir);
-                $filesize = display_size(get_directory_size("$fullpath/$dir"));
-                $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
+                $filename = $fullpath."/".$dir[0];
+                $fileurl  = rawurlencode($wdir."/".$dir[0]);
+                $filesafe = rawurlencode($dir[0]);
+                $filesize = $dir[1];
+                $filedate = $dir[2];
                print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
-                print_cell("left", "<a href=\"index.php?id=$id&wdir=$fileurl&choose=$choose\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".htmlspecialchars($dir)."</a>", 'name');
+                print_cell("left", "<a href=\"index.php?id=$id&wdir=$fileurl&choose=$choose\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".htmlspecialchars($dir[0])."</a>", 'name');
                print_cell("right", $filesize, 'size');
                print_cell("right", $filedate, 'date');
                print_cell("right", "<a href=\"index.php?id=$id&wdir=$wdir&file=$filesafe&action=rename&choose=$choose\">$strrename</a>", 'commands');
@@ -793,18 +846,17 @@
    }


-    if (!empty($filelist)) {
-        asort($filelist);
-        foreach ($filelist as $file) {
+    if (!empty($filedetails)) {
+        foreach ($filedetails as $file) {

-            $icon = mimeinfo("icon", $file);
+            $icon = mimeinfo("icon", $file[0]);

            $count++;
-            $filename    = $fullpath."/".$file;
+            $filename    = $fullpath."/".$file[0];
            $fileurl     = trim($wdir, "/")."/$file";
-            $filesafe    = rawurlencode($file);
+            $filesafe    = rawurlencode($file[0]);
            $fileurlsafe = rawurlencode($fileurl);
-            $filedate    = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
+            $filedate    = $file[2];

            $selectfile = trim($fileurl, "/");

@@ -818,11 +870,11 @@
                $ffurl = str_replace('//', '/', "/file.php?file=/$id/$fileurl");
            }
            link_to_popup_window ($ffurl, "display",
-                                  "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".htmlspecialchars($file),
-                                  480, 640);
+                                  "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".htmlspecialchars($file[0]),
+                                  480, 640, , 'menubar=1,scrollbars=1');
            echo "</td>";

-            $file_size = filesize($filename);
+            $file_size = $file[1];
            print_cell("right", display_size($file_size), 'size');
            print_cell("right", $filedate, 'date');