File:  [LON-CAPA] / loncom / cgi / quotacheck.pl
Revision 1.5: download - view: text, annotated - select for diffs
Thu Jul 31 15:57:24 2014 UTC (9 years, 11 months ago) by musolffc
Branches: MAIN
CVS tags: HEAD
Courses displayed in quota list are now sortable by clicking on column headers

    1: #!/usr/bin/perl
    2: $|=1;
    3: # Display quotas for uploaded course content, current disk usage and
    4: # percent usage for courses and communities for requested domain.
    5: # Requester should either be an active domain coordinator in 
    6: # requested domain, or current server should belong to requested
    7: # domain.
    8: #
    9: # $Id: quotacheck.pl,v 1.5 2014/07/31 15:57:24 musolffc Exp $
   10: #
   11: # Copyright Michigan State University Board of Trustees
   12: #
   13: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   14: #
   15: # LON-CAPA is free software; you can redistribute it and/or modify
   16: # it under the terms of the GNU General Public License as published by
   17: # the Free Software Foundation; either version 2 of the License, or
   18: # (at your option) any later version.
   19: #
   20: # LON-CAPA is distributed in the hope that it will be useful,
   21: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   22: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   23: # GNU General Public License for more details.
   24: #
   25: # You should have received a copy of the GNU General Public License
   26: # along with LON-CAPA; if not, write to the Free Software
   27: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   28: #
   29: # /home/httpd/html/adm/gpl.txt
   30: #
   31: # http://www.lon-capa.org/
   32: #
   33: 
   34: use strict;
   35: 
   36: use lib '/home/httpd/lib/perl/';
   37: use Apache::lonnet();
   38: use Apache::loncommon();
   39: use Apache::lonlocal();
   40: use LONCAPA::Configuration();
   41: use LONCAPA::loncgi();
   42: use LONCAPA::lonauthcgi();
   43: use File::Find;
   44: use CGI qw(:standard);
   45: use LONCAPA;
   46: 
   47: my $perlvar=&LONCAPA::Configuration::read_conf('loncapa.conf');
   48: my $lonhost;
   49: if (ref($perlvar) eq 'HASH') {
   50:     $lonhost = $perlvar->{'lonHostID'};
   51: }
   52: undef($perlvar);
   53: 
   54: my $script = "/cgi-bin/quotacheck.pl";
   55: 
   56: print &LONCAPA::loncgi::cgi_header('text/html',1);
   57: &main($lonhost);
   58: 
   59: sub main {
   60:     my ($lonhost) = @_;
   61:     if ($lonhost eq '') {
   62:         &Apache::lonlocal::get_language_handle();
   63:         &Apache::lonhtmlcommon::add_breadcrumb
   64:         ({href=>$script,
   65:           text=>"Content disk usage"});
   66:         print(&Apache::loncommon::start_page('Course/Community disk usage and quotas').
   67:               &Apache::lonhtmlcommon::breadcrumbs('Course/Community status').
   68:               '<p class="LC_error">'.
   69:               &Apache::lonlocal::mt("Error: could not determine server's LON-CAPA hostID.").
   70:               '</p>'
   71:               &Apache::loncommon::end_page());
   72:         return;
   73:     }
   74:     if (&LONCAPA::lonauthcgi::check_ipbased_access('diskusage')) {
   75:         &LONCAPA::loncgi::check_cookie_and_load_env();
   76:     } else {
   77:         if (!&LONCAPA::loncgi::check_cookie_and_load_env()) {
   78:             &Apache::lonlocal::get_language_handle();
   79:             print(&LONCAPA::loncgi::missing_cookie_msg());
   80:             return;
   81:         }
   82:         if (!&LONCAPA::lonauthcgi::can_view('diskusage')) {
   83:             &Apache::lonlocal::get_language_handle();
   84:             print(&LONCAPA::lonauthcgi::unauthorized_msg('diskusage'));
   85:             return;
   86:         }
   87:     }
   88:     my (%gets,%posted,$reqdom,$crstype,%params);
   89: 
   90: #
   91: #  Get domain -- if this is for an authenticated user (i.e., not IP-based access)
   92: #  Set domain in the order (a) value of fixeddom form element, if submitted
   93: #                          (b) value of domain item in query string
   94: #                          (c) default login domain for current server   
   95: #
   96:     if (($Apache::lonnet::env{'user.name'}) && ($Apache::lonnet::env{'user.domain'})) {
   97:         my $q = CGI->new;
   98:         %params = $q->Vars;
   99:         $crstype = 'Course';
  100:         if ($params{'type'} eq 'Community') {
  101:             $crstype = $params{'type'};
  102:         }
  103:         if ($params{'fixeddom'}) { $reqdom = $params{'fixeddom'} }
  104:     }
  105:     if (($reqdom eq '') && ($ENV{'QUERY_STRING'})) {
  106:         &LONCAPA::loncgi::cgi_getitems($ENV{'QUERY_STRING'},\%gets);
  107:         if (ref($gets{'domain'}) eq 'ARRAY') {
  108:             $gets{'domain'}->[0] =~ s/^\s+|\s+$//g;
  109:             if ($gets{'domain'}->[0] =~ /^$LONCAPA::match_domain$/) {
  110:                 my $domdesc = &Apache::lonnet::domain($gets{'domain'}->[0]);
  111:                 unless ($domdesc eq '') {
  112:                     $reqdom = $gets{'domain'}->[0];
  113:                 }
  114:             }
  115:         }
  116:     }
  117:     if ($reqdom eq '') {
  118:         $reqdom = &Apache::lonnet::default_login_domain();
  119:     }
  120: 
  121:     &Apache::lonlocal::get_language_handle();
  122:     &Apache::lonhtmlcommon::add_breadcrumb
  123:     ({href=>$script."?domain=$reqdom",
  124:        text=>"Content disk usage"});
  125:     if ( ($params{'gosearch'}) || ($params{'sortby'}) ) {
  126:         &Apache::lonhtmlcommon::add_breadcrumb
  127:             ({href=>$script."?domain=$reqdom",
  128:               text=>"Result"});
  129:     }
  130:     my $domdesc = &Apache::lonnet::domain($reqdom,'description');
  131:     my $starthash = {
  132:         add_entries => {'onload' => "javascript:courseSet(document.filterpicker.official, 'load');"},
  133:     };
  134:     print(&Apache::loncommon::start_page('Course/Community disk usage and quotas', undef, $starthash).
  135:           &Apache::lonhtmlcommon::breadcrumbs('Course/Community status').
  136:           '<h2>'.&Apache::lonlocal::mt('Quotas for uploaded course content').'</h2>'.
  137:           '<h3>'.$domdesc.'</h3>');
  138:     my $changejs = <<"ENDSCRIPT";
  139: <script>
  140: function changeSort(sortby) {
  141:     document.filterpicker.sortby.value = sortby;
  142:     if (('$params{'sortby'}' == sortby) && ('$params{'sortorder'}' != 'rev')) { 
  143:         document.filterpicker.sortorder.value = 'rev'; 
  144:     }
  145:     document.filterpicker.submit();
  146: }
  147: </script>
  148: ENDSCRIPT
  149: 
  150:     print($changejs);
  151: 
  152: #
  153: #  If this is for an authenticated user (i.e., not IP-based access)
  154: #  create display to choose filters to restrict courses/communities displayed
  155: #  (e.g., recent activity, recently created, institutional code, course owner etc.)
  156: #
  157: 
  158:     if (($Apache::lonnet::env{'user.name'}) && ($Apache::lonnet::env{'user.domain'})) {
  159:         my ($numtitles,@codetitles);
  160:         print(&Apache::loncommon::js_changer());
  161:         my ($filterlist,$filter) = &get_filters($reqdom,\%params);
  162:         $Apache::lonnet::env{'form.official'} = $params{'official'};
  163:         if ($params{'official'}) {
  164:             my @standardnames = &Apache::loncommon::get_standard_codeitems();
  165:             pop(@standardnames);
  166:             foreach my $item (@standardnames) {
  167:                 if ($params{'official'} eq 'on') {
  168:                     $Apache::lonnet::env{'form.'.$item} = $params{$item};
  169:                 } else {
  170:                     $Apache::lonnet::env{'form.'.$item} = 0;
  171:                 }
  172:             }
  173:             $Apache::lonnet::env{'form.state'} = $params{'state'};
  174:         }
  175:         print(&Apache::loncommon::build_filters($filterlist,$crstype,undef,undef,$filter,
  176:                                                 $script,\$numtitles,
  177:                                                 'quotacheck',undef,undef,undef,
  178:                                                 \@codetitles,$reqdom,'quotacheck',$reqdom));
  179:         if ( ($params{'gosearch'}) || ($params{'sortby'}) ) {
  180:             if ($params{'official'} eq 'on') {
  181:                 $Apache::lonnet::env{'form.state'} = $params{'state'};
  182:             }
  183:             # Sort by course title (cdesc) as default, not reversed
  184:             my $sortby = $params{'sortby'};
  185:             unless  ($sortby =~ m{^(quota|current_disk_usage|percent|quotatype|instcode)$}) {
  186:                 $sortby = 'cdesc';
  187:             }
  188:             my $sortorder;
  189:             if ($params{'sortorder'} eq 'rev') { $sortorder = 'rev'; }
  190: 
  191:             my %courses = &Apache::loncommon::search_courses($reqdom,$crstype,$filter,$numtitles,
  192:                                                              undef,undef,undef,\@codetitles);
  193:             my @showcourses = keys(%courses);
  194:             &print_usage($lonhost,$reqdom,\@showcourses,$sortby,$sortorder);
  195:         }
  196: 
  197:         print(&Apache::loncommon::end_page());
  198:         return;
  199:     }
  200:     &print_usage($lonhost,$reqdom);
  201:     print(&Apache::loncommon::end_page());
  202:     return;
  203: }
  204: 
  205: sub print_usage {
  206:     my ($lonhost,$dom,$courses,$sortby,$sortorder) = @_;
  207:     my @domains = &Apache::lonnet::current_machine_domains();
  208:     my @ids=&Apache::lonnet::current_machine_ids();
  209:     my $domain = &Apache::lonnet::host_domain($lonhost);
  210: 
  211: #
  212: # If user's current role is domain coordinator, domain of courses/communities
  213: # to be shown needs to be domain being coordinated. 
  214: #
  215:     if ($Apache::lonnet::env{'request.role'} =~ m{^dc\./}) {
  216:         $domain = $Apache::lonnet::env{'request.role.domain'};
  217:         unless ($dom eq $domain) {
  218:             my $otherdomdesc = &Apache::lonnet::domain($domain,'description');
  219:             print('<p class="LC_error">'.
  220:                   &Apache::lonlocal::mt('Requested domain does not match domain being coordinated.').
  221:                   '</p>'."\n".
  222:                   '<p class="LC_info">'.
  223:                   &Apache::lonlocal::mt('Show quotas for the domain being coordinated: [_1]',
  224:                                         '<a href="'.$script.'?domain='.$domain.'">'.
  225:                                         $otherdomdesc.'</a>').
  226:                   '</p>');
  227:             return;
  228:         }
  229:     } else {
  230:         unless (grep(/^\Q$dom\E/,@domains)) {
  231:             print('<p class="LC_error">'.
  232:                   &Apache::lonlocal::mt('Requested domain is not hosted on this server.').
  233:                   '</p>');
  234:             return;
  235:         }
  236:     }
  237:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
  238:     my @showcourses;
  239:     if (ref($courses) eq 'ARRAY') {
  240:         @showcourses = @{$courses};
  241:     } else {
  242:         my %courseshash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',undef,undef,'.');
  243:         if (keys(%courseshash)) {
  244:             @showcourses = keys(%courseshash);
  245:         }
  246:     }
  247: 
  248:     if (@showcourses) {
  249:         print(&Apache::loncommon::start_data_table().
  250:             &Apache::loncommon::start_data_table_header_row()
  251:             .'<th><a href="javascript:changeSort('."'quotatype'".')">'
  252:                 .&Apache::lonlocal::mt('Course Type')
  253:                 .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  254:             .'<th><a href="javascript:changeSort('."'cdesc'".')">'
  255:                 .&Apache::lonlocal::mt('Course Title')
  256:                 .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  257:             .'<th><a href="javascript:changeSort('."'instcode'".')">'
  258:                 .&Apache::lonlocal::mt('Institutional Code')
  259:                 .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  260:             .'<th><a href="javascript:changeSort('."'quota'".')">'
  261:                 .&Apache::lonlocal::mt('Quota (MB)')
  262:                 .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  263:             .'<th><a href="javascript:changeSort('."'current_disk_usage'".')">'
  264:                 .&Apache::lonlocal::mt('Usage (MB)')
  265:                 .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  266:             .'<th><a href="javascript:changeSort('."'percent'".')">'
  267:                 .&Apache::lonlocal::mt('Percent usage')
  268:                 .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  269:             .&Apache::loncommon::end_data_table_header_row());
  270:              
  271:         my $usagehash = {};  # Sortable hash of courses
  272:         foreach my $cid (@showcourses) {
  273:             my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
  274:             my $cdesc = $courseinfo{'description'};
  275:             my $cnum = $courseinfo{'num'};
  276:             my $chome = $courseinfo{'home'};
  277:             my $crstype = $courseinfo{'type'};
  278:             if ($crstype eq '') {
  279:                 if ($cnum =~ /^$LONCAPA::match_community$/) {
  280:                     $crstype = 'Community';
  281:                 } else {
  282:                     $crstype = 'Course';
  283:                 }
  284:             }
  285:             my $instcode = $courseinfo{'internal.coursecode'};
  286:             my $quota = $courseinfo{'internal.uploadquota'};
  287:             $quota =~ s/[^\d\.]+//g;
  288:             my $quotatype = 'unofficial';
  289:             if ($crstype eq 'Community') {
  290:                 $quotatype = 'community';
  291:             } elsif ($courseinfo{'internal.coursecode'}) {
  292:                 $quotatype = 'official';
  293:             } elsif ($courseinfo{'internal.textbook'}) {
  294:                 $quotatype = 'textbook';
  295:             }
  296:             if ($quota eq '') {
  297:                 $quota = $domdefs{$crstype.'quota'};
  298:             }
  299:             $quota =~ s/[^\d\.]+//g;
  300:             if ($quota eq '') {
  301:                 $quota = 500;
  302:             }
  303:             my $current_disk_usage = 0;
  304:             if (grep(/^\Q$chome\E$/,@ids)) {
  305:                 my $dir = &propath($dom,$cnum).'/userfiles/';
  306:                 foreach my $subdir ('docs','supplemental') {
  307:                     my $ududir = "$dir/$subdir";
  308:                     my $total_size=0;
  309:                     my $code=sub {
  310:                         if (-d $_) { return;}
  311:                         $total_size+=(stat($_))[7];
  312:                     };
  313:                     chdir($ududir);
  314:                     find($code,$ududir);
  315:                     $total_size=int($total_size/(1024*1024));
  316:                     $current_disk_usage += $total_size;
  317:                 }
  318:             } else {
  319:                 foreach my $subdir ('docs','supplemental') {
  320:                     $current_disk_usage += &Apache::lonnet::diskusage($dom,$cnum,"userfiles/$subdir",1);
  321:                 }
  322:             }
  323:             my $percent;
  324:             if (($quota == 0) || ($quota =~ /[^\d\.]/)) {
  325:                 $percent = 100.0;
  326:             } else {
  327:                 $percent = 100*($current_disk_usage/$quota);
  328:             }
  329:             $current_disk_usage = sprintf("%.0f",$current_disk_usage);
  330:             $quota = sprintf("%.0f",$quota);
  331:             $percent = sprintf("%.0f",$percent);
  332: 
  333:             # Enter sortable data into hash
  334:             $usagehash->{ $cid } = {
  335:                 "quotatype"             => $quotatype,
  336:                 "cdesc"                 => $cdesc,
  337:                 "instcode"              => $instcode,
  338:                 "quota"                 => $quota,
  339:                 "current_disk_usage"    => $current_disk_usage,
  340:                 "percent"               => $percent,
  341:             };
  342:         }
  343: 
  344:         # Sort courses by $sortby.  "cdesc" is the default.
  345:         my @sorted_courses;
  346:         if ($sortby =~ m{^(quota|current_disk_usage|percent)$}) {
  347:             # Numerical fields
  348:             if ($sortorder eq "rev") {
  349:                 @sorted_courses = sort {
  350:                     $usagehash->{$a}->{$sortby} <=> $usagehash->{$b}->{$sortby}
  351:                         or
  352:                     uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"})
  353:                 } (keys(%{$usagehash}));
  354:             } else {
  355:                 @sorted_courses = sort {
  356:                     $usagehash->{$b}->{$sortby} <=> $usagehash->{$a}->{$sortby}
  357:                         or
  358:                     uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"})
  359:                 } (keys(%{$usagehash}));
  360:             }
  361:         } elsif ($sortby =~ m{^(cdesc|quotatype|instcode)$}) {
  362:             # String fields
  363:             if ($sortorder eq "rev") {
  364:                 @sorted_courses = sort {
  365:                     uc($usagehash->{$b}->{$sortby}) cmp uc($usagehash->{$a}->{$sortby})
  366:                         or
  367:                     uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"})
  368:                 } (keys(%{$usagehash}));
  369:             } else {
  370:                 @sorted_courses = sort {
  371:                     uc($usagehash->{$a}->{$sortby}) cmp uc($usagehash->{$b}->{$sortby})
  372:                         or
  373:                     uc($usagehash->{$a}->{"cdesc"}) cmp uc($usagehash->{$b}->{"cdesc"})
  374:                 } (keys(%{$usagehash}));
  375:             }
  376:         }
  377: 
  378:         # Print data for each course.
  379:         foreach my $course (@sorted_courses) {
  380:             print(&Apache::loncommon::start_data_table_row().
  381:                   '<td>'.$usagehash->{$course}->{"quotatype"}.'</td>'.
  382:                   '<td>'.$usagehash->{$course}->{"cdesc"}.'</td>'.
  383:                   '<td>'.$usagehash->{$course}->{"instcode"}.'</td>'.
  384:                   '<td>'.$usagehash->{$course}->{"quota"}.'</td>'.
  385:                   '<td>'.$usagehash->{$course}->{"current_disk_usage"}.'</td>'.
  386:                   '<td>'.$usagehash->{$course}->{"percent"}.'</td>'.
  387:                    &Apache::loncommon::end_data_table_row()
  388:                   );
  389:         }
  390:         print(&Apache::loncommon::end_data_table().'<br /><br />');
  391:     } else {
  392:         print(&Apache::lonlocal::mt('No courses match search criteria.'));
  393:     }
  394:     return;
  395: }
  396: 
  397: sub get_filters {
  398:     my ($dom,$params) = @_;
  399:     my @filterlist = ('descriptfilter','instcodefilter','ownerfilter',
  400:                       'ownerdomfilter','coursefilter','sincefilter');
  401:     # created filter
  402:     my $loncaparev = &Apache::lonnet::get_server_loncaparev($dom);
  403:     if ($loncaparev ne 'unknown_cmd') {
  404:         push(@filterlist,'createdfilter');
  405:     }
  406:     my %filter;
  407:     foreach my $item (@filterlist) {
  408:         $filter{$item} = '';
  409:     }
  410:     if (ref($params) eq 'HASH') {
  411:         foreach my $item (@filterlist) {
  412:             $filter{$item} = $params->{$item};
  413:         }
  414:     }
  415:     return (\@filterlist,\%filter);
  416: }
  417: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>