--- loncom/cgi/quotacheck.pl 2014/07/31 15:02:26 1.3 +++ loncom/cgi/quotacheck.pl 2014/08/05 19:32:19 1.6 @@ -6,7 +6,7 @@ $|=1; # requested domain, or current server should belong to requested # domain. # -# $Id: quotacheck.pl,v 1.3 2014/07/31 15:02:26 musolffc Exp $ +# $Id: quotacheck.pl,v 1.6 2014/08/05 19:32:19 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,8 @@ sub main { if ($params{'type'} eq 'Community') { $crstype = $params{'type'}; } - if ($params{'fixeddom'}) { - $reqdom = $params{'fixeddom'}; - } + if ($params{'fixeddom'}) { $reqdom = $params{'fixeddom'} } + unless ($params{'sortby'}) { $params{'sortby'} = 'cdesc'; } } if (($reqdom eq '') && ($ENV{'QUERY_STRING'})) { &LONCAPA::loncgi::cgi_getitems($ENV{'QUERY_STRING'},\%gets); @@ -125,16 +123,32 @@ 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"}); } my $domdesc = &Apache::lonnet::domain($reqdom,'description'); - print(&Apache::loncommon::start_page('Course/Community disk usage and quotas'). + my $starthash = { + add_entries => {'onload' => "javascript:courseSet(document.filterpicker.official, 'load');"}, + }; + print(&Apache::loncommon::start_page('Course/Community disk usage and quotas', undef, $starthash). &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) @@ -163,15 +177,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; } @@ -181,7 +204,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); @@ -222,16 +245,62 @@ 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()); + # Order in which columns are displayed from left to right + my @order = ('quotatype','cdesc','instcode','quota', + 'current_disk_usage','percent'); + + # Up and down arrows to indicate sort order + my @arrows = (' ▲',' ▼',''); + + # Default sort order and column title + my %columns = ( + quotatype => { + order => 'ascending', + text => &Apache::lonlocal::mt('Course Type'), + }, + cdesc => { + order => 'ascending', + text => &Apache::lonlocal::mt('Course Title'), + }, + instcode => { + order => 'ascending', + text => &Apache::lonlocal::mt('Institutional Code'), + }, + quota => { + order => 'descending', + text => &Apache::lonlocal::mt('Quota (MB)'), + }, + current_disk_usage => { + order => 'descending', + text => &Apache::lonlocal::mt('Usage (MB)'), + }, + percent => { + order => 'descending', + text => &Apache::lonlocal::mt('Percent usage'), + }, + ); + + # Print column headers + my $output = ''; + foreach my $key (@order) { + my $idx; + # Append an up or down arrow to sorted column + if ($sortby eq $key) { + $idx = ($columns{$key}{order} eq 'ascending') ? 0:1; + if ($sortorder eq 'rev') { $idx ++; } + $idx = $idx%2; + } else { $idx = 2; } # No arrow if column not sorted + $output .= ''.$columns{$key}{text} + .$arrows[$idx].''; + } + print(&Apache::loncommon::start_data_table() + .&Apache::loncommon::start_data_table_header_row().$output + .&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'}; @@ -282,7 +351,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\.]/)) { @@ -293,13 +361,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() ); }