--- loncom/cgi/quotacheck.pl 2014/07/31 15:45:31 1.4 +++ loncom/cgi/quotacheck.pl 2014/07/31 15:57:24 1.5 @@ -6,7 +6,7 @@ $|=1; # requested domain, or current server should belong to requested # domain. # -# $Id: quotacheck.pl,v 1.4 2014/07/31 15:45:31 musolffc Exp $ +# $Id: quotacheck.pl,v 1.5 2014/07/31 15:57:24 musolffc Exp $ # # Copyright Michigan State University Board of Trustees # @@ -93,7 +93,6 @@ sub main { # (b) value of domain item in query string # (c) default login domain for current server # - if (($Apache::lonnet::env{'user.name'}) && ($Apache::lonnet::env{'user.domain'})) { my $q = CGI->new; %params = $q->Vars; @@ -101,9 +100,7 @@ sub main { if ($params{'type'} eq 'Community') { $crstype = $params{'type'}; } - if ($params{'fixeddom'}) { - $reqdom = $params{'fixeddom'}; - } + if ($params{'fixeddom'}) { $reqdom = $params{'fixeddom'} } } if (($reqdom eq '') && ($ENV{'QUERY_STRING'})) { &LONCAPA::loncgi::cgi_getitems($ENV{'QUERY_STRING'},\%gets); @@ -125,7 +122,7 @@ sub main { &Apache::lonhtmlcommon::add_breadcrumb ({href=>$script."?domain=$reqdom", text=>"Content disk usage"}); - if ($params{'gosearch'}) { + if ( ($params{'gosearch'}) || ($params{'sortby'}) ) { &Apache::lonhtmlcommon::add_breadcrumb ({href=>$script."?domain=$reqdom", text=>"Result"}); @@ -138,6 +135,19 @@ sub main { &Apache::lonhtmlcommon::breadcrumbs('Course/Community status'). '

'.&Apache::lonlocal::mt('Quotas for uploaded course content').'

'. '

'.$domdesc.'

'); + my $changejs = <<"ENDSCRIPT"; + +ENDSCRIPT + + print($changejs); # # If this is for an authenticated user (i.e., not IP-based access) @@ -166,15 +176,24 @@ sub main { $script,\$numtitles, 'quotacheck',undef,undef,undef, \@codetitles,$reqdom,'quotacheck',$reqdom)); - if ($params{'gosearch'}) { + if ( ($params{'gosearch'}) || ($params{'sortby'}) ) { if ($params{'official'} eq 'on') { $Apache::lonnet::env{'form.state'} = $params{'state'}; } + # Sort by course title (cdesc) as default, not reversed + my $sortby = $params{'sortby'}; + unless ($sortby =~ m{^(quota|current_disk_usage|percent|quotatype|instcode)$}) { + $sortby = 'cdesc'; + } + my $sortorder; + if ($params{'sortorder'} eq 'rev') { $sortorder = 'rev'; } + my %courses = &Apache::loncommon::search_courses($reqdom,$crstype,$filter,$numtitles, undef,undef,undef,\@codetitles); my @showcourses = keys(%courses); - &print_usage($lonhost,$reqdom,\@showcourses); + &print_usage($lonhost,$reqdom,\@showcourses,$sortby,$sortorder); } + print(&Apache::loncommon::end_page()); return; } @@ -184,7 +203,7 @@ sub main { } sub print_usage { - my ($lonhost,$dom,$courses) = @_; + my ($lonhost,$dom,$courses,$sortby,$sortorder) = @_; my @domains = &Apache::lonnet::current_machine_domains(); my @ids=&Apache::lonnet::current_machine_ids(); my $domain = &Apache::lonnet::host_domain($lonhost); @@ -225,16 +244,31 @@ sub print_usage { @showcourses = keys(%courseshash); } } + if (@showcourses) { print(&Apache::loncommon::start_data_table(). - &Apache::loncommon::start_data_table_header_row(). - ''.&Apache::lonlocal::mt('Course Type').''. - ''.&Apache::lonlocal::mt('Course Title').''. - ''.&Apache::lonlocal::mt('Institutional Code').''. - ''.&Apache::lonlocal::mt('Quota (MB)').''. - ''.&Apache::lonlocal::mt('Usage (MB)').''. - ''.&Apache::lonlocal::mt('Percent usage').''. - &Apache::loncommon::end_data_table_header_row()); + &Apache::loncommon::start_data_table_header_row() + .'' + .&Apache::lonlocal::mt('Course Type') + .'' + .'' + .&Apache::lonlocal::mt('Course Title') + .'' + .'' + .&Apache::lonlocal::mt('Institutional Code') + .'' + .'' + .&Apache::lonlocal::mt('Quota (MB)') + .'' + .'' + .&Apache::lonlocal::mt('Usage (MB)') + .'' + .'' + .&Apache::lonlocal::mt('Percent usage') + .'' + .&Apache::loncommon::end_data_table_header_row()); + + my $usagehash = {}; # Sortable hash of courses foreach my $cid (@showcourses) { my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'}); my $cdesc = $courseinfo{'description'}; @@ -285,7 +319,6 @@ sub print_usage { foreach my $subdir ('docs','supplemental') { $current_disk_usage += &Apache::lonnet::diskusage($dom,$cnum,"userfiles/$subdir",1); } - $current_disk_usage=int($current_disk_usage/1024); } my $percent; if (($quota == 0) || ($quota =~ /[^\d\.]/)) { @@ -296,13 +329,61 @@ sub print_usage { $current_disk_usage = sprintf("%.0f",$current_disk_usage); $quota = sprintf("%.0f",$quota); $percent = sprintf("%.0f",$percent); + + # Enter sortable data into hash + $usagehash->{ $cid } = { + "quotatype" => $quotatype, + "cdesc" => $cdesc, + "instcode" => $instcode, + "quota" => $quota, + "current_disk_usage" => $current_disk_usage, + "percent" => $percent, + }; + } + + # Sort courses by $sortby. "cdesc" is the default. + my @sorted_courses; + if ($sortby =~ m{^(quota|current_disk_usage|percent)$}) { + # Numerical fields + if ($sortorder eq "rev") { + @sorted_courses = sort { + $usagehash->{$a}->{$sortby} <=> $usagehash->{$b}->{$sortby} + or + uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"}) + } (keys(%{$usagehash})); + } else { + @sorted_courses = sort { + $usagehash->{$b}->{$sortby} <=> $usagehash->{$a}->{$sortby} + or + uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"}) + } (keys(%{$usagehash})); + } + } elsif ($sortby =~ m{^(cdesc|quotatype|instcode)$}) { + # String fields + if ($sortorder eq "rev") { + @sorted_courses = sort { + uc($usagehash->{$b}->{$sortby}) cmp uc($usagehash->{$a}->{$sortby}) + or + uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"}) + } (keys(%{$usagehash})); + } else { + @sorted_courses = sort { + uc($usagehash->{$a}->{$sortby}) cmp uc($usagehash->{$b}->{$sortby}) + or + uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"}) + } (keys(%{$usagehash})); + } + } + + # Print data for each course. + foreach my $course (@sorted_courses) { print(&Apache::loncommon::start_data_table_row(). - ''.$quotatype.''. - ''.$cdesc.''. - ''.$instcode.''. - ''.$quota.''. - ''.$current_disk_usage.''. - ''.$percent.''. + ''.$usagehash->{$course}->{"quotatype"}.''. + ''.$usagehash->{$course}->{"cdesc"}.''. + ''.$usagehash->{$course}->{"instcode"}.''. + ''.$usagehash->{$course}->{"quota"}.''. + ''.$usagehash->{$course}->{"current_disk_usage"}.''. + ''.$usagehash->{$course}->{"percent"}.''. &Apache::loncommon::end_data_table_row() ); }