--- loncom/publisher/lonpubdir.pm 2014/01/21 18:23:35 1.149
+++ loncom/publisher/lonpubdir.pm 2014/03/11 01:16:49 1.150
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Authoring Space Directory Lister
#
-# $Id: lonpubdir.pm,v 1.149 2014/01/21 18:23:35 musolffc Exp $
+# $Id: lonpubdir.pm,v 1.150 2014/03/11 01:16:49 musolffc Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -79,15 +79,23 @@ sub handler {
my $numdir = 0;
my $numres = 0;
+ # Retrieving value for sortby from QUERY_STRING
+ &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['sortby']);
+
+ # Default sort by 'Name'
+ if (! exists($env{'form.sortby'})) {
+ $env{'form.sortby'} = 'filename';
+ }
+
# Start off the directory table.
$r->print(&Apache::loncommon::start_data_table()
.&Apache::loncommon::start_data_table_header_row()
.'
'.&mt('Type').' | '
.''.&mt('Actions').' | '
- .''.&mt('Name').' | '
+ .'Name ▼ | '
.''.&mt('Title').' | '
.''.&mt('Status').' | '
- .''.&mt('Last Modified').' | '
+ .'Last Modified ▼ | '
.&Apache::loncommon::end_data_table_header_row()
);
@@ -95,25 +103,51 @@ sub handler {
my $dirptr=16384; # Mask indicating a directory in stat.cmode.
opendir(DIR,$fn);
+ my $filehash = {};
+ my $sortby = $env{'form.sortby'};
my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
foreach my $filename (@files) {
next if ($filename eq '.DS_Store');
- my ($cdev,$cino,$cmode,$cnlink,
- $cuid,$cgid,$crdev,$csize,
- $catime,$cmtime,$cctime,
- $cblksize,$cblocks)=stat($fn.'/'.$filename);
-
- my $extension='';
- if ($filename=~/\.(\w+)$/) { $extension=$1; }
- if ($cmode&$dirptr) {
- &putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
- } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
- &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir,
- $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
- } else {
- # "hidden" extension and not a directory, so hide it away.
+ my ($cmode,$cmtime)=(stat($fn.'/'.$filename))[2,9];
+ # If you want to sort by "last modified", we need to make this hash.
+ if ($sortby eq 'cmtime') {
+ $filehash->{ $filename } = {"cmtime" => $cmtime,};
}
- }
+ # Otherwise sort by name. Don't bother with filehash. Continue printing contents.
+ else {
+ my $extension='';
+ if ($filename=~/\.(\w+)$/) { $extension=$1; }
+ if ($cmode&$dirptr) {
+ &putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
+ } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
+ &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir,
+ $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
+ } else {
+ # "hidden" extension and not a directory, so hide it away.
+ }
+ }
+ };
+
+ # Sorting files by "last modified" if that's what you selected
+ if ($sortby eq 'cmtime') {
+ my @sorted_files = sort {
+ $filehash->{$b}->{$sortby} <=> $filehash->{$a}->{$sortby}
+ } (keys(%{$filehash}));
+ foreach my $filename (@sorted_files) {
+ my ($cmode,$cmtime)=(stat($fn.'/'.$filename))[2,9];
+ my $extension='';
+ if ($filename=~/\.(\w+)$/) { $extension=$1; }
+ if ($cmode&$dirptr) {
+ &putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
+ } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
+ &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir,
+ $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
+ } else {
+ # "hidden" extension and not a directory, so hide it away.
+ };
+ };
+ };
+
closedir(DIR);
$r->print(&Apache::loncommon::end_data_table()