File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.31: download - view: text, annotated - select for diffs
Thu Jul 25 19:30:24 2002 UTC (21 years, 10 months ago) by minaeibi
Branches: MAIN
CVS tags: HEAD
removing global variables

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: lonstatistics.pm,v 1.31 2002/07/25 19:30:24 minaeibi Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (Navigate problems for statistical reports
   29: # YEAR=2001
   30: # 5/5,7/9,7/25/1,8/11,9/13,9/26,10/5,10/9,10/22,10/26 Behrouz Minaei
   31: # 11/1,11/4,11/16,12/14,12/16,12/18,12/20,12/31 Behrouz Minaei
   32: # YEAR=2002
   33: # 1/22,2/1,2/6,2/25,3/2,3/6,3/17,3/21,3/22,3/26,4/7,5/6 Behrouz Minaei
   34: # 5/12,5/14,5/15,5/19,5/26,7/16,25/7  Behrouz Minaei
   35: #
   36: ###
   37: 
   38: package Apache::lonstatistics; 
   39: 
   40: use strict;
   41: use Apache::Constants qw(:common :http);
   42: use Apache::lonnet();
   43: use Apache::lonhomework;
   44: use Apache::loncommon;
   45: use Apache::loncoursedata;
   46: use Apache::lonhtmlcommon;
   47: use Apache::lonproblemanalysis;
   48: use Apache::lonproblemstatistics;
   49: use Apache::lonstudentassessment;
   50: use Apache::lonchart;
   51: use HTML::TokeParser;
   52: use GDBM_File;
   53: 
   54: 
   55: sub CheckFormElement {
   56:     my ($cache, $ENVName, $cacheName, $default)=@_;
   57: 
   58:     if(defined($ENV{'form.'.$ENVName})) {
   59:         $cache->{$cacheName} = $ENV{'form.'.$ENVName};
   60:     } elsif(!defined($cache->{$cacheName})) {
   61:         $cache->{$cacheName} = $default;
   62:     }
   63: 
   64:     return;
   65: }
   66: 
   67: sub ProcessFormData{
   68:     my ($cache)=@_;
   69: 
   70:     $cache->{'reportKey'} = 'false';
   71: 
   72:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   73:                                             ['sort','download','reportSelected',
   74:                                              'StudentAssessmentStudent']);
   75:     &CheckFormElement($cache, 'Status', 'Status', 'Active');
   76:     &CheckFormElement($cache, 'postdata', 'reportSelected', 'Class list');
   77:     &CheckFormElement($cache, 'reportSelected', 'reportSelected', 
   78:                       'Class list');
   79:     $cache->{'reportSelected'} = 
   80:         &Apache::lonnet::unescape($cache->{'reportSelected'});
   81:     &CheckFormElement($cache, 'DownloadAll', 'DownloadAll', 'false');
   82:     &CheckFormElement($cache, 'sort', 'sort', 'fullname');
   83:     &CheckFormElement($cache, 'download', 'download', 'false');
   84: 
   85:     if(defined($ENV{'form.CreateStudentAssessment'}) ||
   86:        defined($ENV{'form.NextStudent'}) ||
   87:        defined($ENV{'form.PreviousStudent'})) {
   88:         $cache->{'reportSelected'} = 'Student Assessment';
   89:     }
   90:     if(defined($ENV{'form.NextStudent'})) {
   91:         $cache->{'StudentAssessmentMove'} = 'next';
   92:     } elsif(defined($ENV{'form.PreviousStudent'})) {
   93:         $cache->{'StudentAssessmentMove'} = 'previous';
   94:     } else {
   95:         $cache->{'StudentAssessmentMove'} = 'selected';
   96:     }
   97:     &CheckFormElement($cache, 'StudentAssessmentMap', 'StudentAssessmentMap', 
   98:                       'All Maps');
   99:     &CheckFormElement($cache, 'StudentAssessmentStudent', 
  100:                       'StudentAssessmentStudent', 'All Students');
  101:     $cache->{'StudentAssessmentStudent'} = 
  102:         &Apache::lonnet::unescape($cache->{'StudentAssessmentStudent'});
  103: 
  104:     foreach (keys(%ENV)) {
  105:         if(/form\.Analyze:::/) {
  106: #            $cache->{'reportSelected'} = 'Analyze';
  107: #            $cache->{'reportKey'} = 'Problem Analysis';
  108:             my ($uri, $title, $part, $problem);
  109:             (undef, $uri, $title, $part, $problem)=split(':::', $_);
  110:             $cache->{'AnalyzeURI'}     = $uri;
  111:             $cache->{'AnalyzeTitle'}   = $title;
  112:             $cache->{'AnalyzePart'}    = $part;
  113:             $cache->{'AnalyzeProblem'} = $problem;
  114:             
  115:             &CheckFormElement($cache, 'Interval', 'Interval', '1');
  116:         }
  117:     }
  118: 
  119:     return;
  120: 
  121:     # Select page to display
  122:     if(defined($ENV{'form.ProblemStatistics'}) ||
  123:        defined($ENV{'form.ProblemStatisticsRecalculate'}) || 
  124:        defined($ENV{'form.DisplayCSVFormat'})) {
  125:         $cache->{'GoToPage'} = 'ProblemStatistics';
  126:         &CheckFormElement($cache, 'DisplayCSVFormat',
  127:                           'DisplayFormat', 'Display Table Format');
  128:         &CheckFormElement($cache, 'Ascend','ProblemStatisticsAscend',
  129:                           'Ascending');
  130:         &CheckFormElement($cache, 'Maps', 'ProblemStatisticsMap', 
  131:                           'All Maps');
  132:     } elsif(defined($ENV{'form.ProblemAnalysis'})) {
  133:         $cache->{'GoToPage'} = 'ProblemAnalysis';
  134:         &CheckFormElement($cache, 'Interval', 'Interval', '1');
  135:     } elsif(defined($ENV{'form.DoDiffGraph'})) {
  136:         $cache->{'GoToPage'} = 'DoDiffGraph';
  137:     } elsif(defined($ENV{'form.PercentWrongGraph'})) {
  138:         $cache->{'GoToPage'} = 'PercentWrongGraph';
  139:     } elsif(defined($ENV{'form.ActivityLog'})) {
  140:         $cache->{'GoToPage'} = 'ActivityLog';
  141:     } else {
  142:         $cache->{'GoToPage'} = 'Menu';
  143:     }
  144: 
  145:     &CheckFormElement($cache, 'Status', 'Status', 'Active');
  146: 
  147:     return;
  148: }
  149: 
  150: =pod
  151: 
  152: =item &SortStudents()
  153: 
  154: Determines which students to display and in which order.  Which are 
  155: displayed are determined by their status(active/expired).  The order
  156: is determined by the sort button pressed (default to username).  The
  157: type of sorting is username, lastname, or section.
  158: 
  159: =over 4
  160: 
  161: Input: $students, $CacheData
  162: 
  163: $students: A array pointer to a list of students (username:domain)
  164: 
  165: $CacheData: A pointer to the hash tied to the cached data
  166: 
  167: Output: \@order
  168: 
  169: @order: An ordered list of students (username:domain)
  170: 
  171: =back
  172: 
  173: =cut
  174: 
  175: sub SortStudents {
  176:     my ($cache)=@_;
  177: 
  178:     my @students = split(':::',$cache->{'NamesOfStudents'});
  179:     my @sorted1Students=();
  180:     foreach (@students) {
  181:         if($cache->{'Status'} eq 'Any' || 
  182:            $cache->{$_.':Status'} eq $cache->{'Status'}) {
  183:             push(@sorted1Students, $_);
  184:         }
  185:     }
  186: 
  187:     my $sortBy = '';
  188:     if(defined($cache->{'sort'})) {
  189:         $sortBy = ':'.$cache->{'sort'};
  190:     }
  191:     my @order = sort { $cache->{$a.$sortBy} cmp $cache->{$b.$sortBy} ||
  192:                        $cache->{$a.':fullname'} cmp $cache->{$b.':fullname'} } 
  193:                 @sorted1Students;
  194: 
  195:     return \@order;
  196: }
  197: 
  198: sub PrepareData {
  199:     my ($c, $cacheDB)=@_;
  200: 
  201:     # Test for access to the cache data
  202:     my $courseID=$ENV{'request.course.id'};
  203:     my $isRecalculate=0;
  204:     if(defined($ENV{'form.Recalculate'})) {
  205:         $isRecalculate=1;
  206:     }
  207: 
  208:     my $isCached = &Apache::loncoursedata::TestCacheData($cacheDB, 
  209:                                                          $isRecalculate);
  210:     if($isCached < 0) {
  211:         return "Unable to tie hash to db file.";
  212:     }
  213: 
  214:     # Download class list information if not using cached data
  215:     my %cache;
  216:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT,0640)) {
  217:         return "Unable to tie hash to db file.";
  218:     }
  219: 
  220:     if(!$isCached) {
  221:         my $processTopResourceMapReturn=
  222:             &Apache::loncoursedata::ProcessTopResourceMap(\%cache, $c);
  223:         if($processTopResourceMapReturn ne 'OK') {
  224:             untie(%cache);
  225:             return $processTopResourceMapReturn;
  226:         }
  227:     }
  228: 
  229:     if($c->aborted()) {
  230:         untie(%cache);
  231:         return 'aborted'; 
  232:     }
  233: 
  234:     my $classlist=&Apache::loncoursedata::DownloadClasslist($courseID,
  235:                                                 $cache{'ClasslistTimestamp'},
  236:                                                 $c);
  237:     foreach (keys(%$classlist)) {
  238:         if(/^(con_lost|error|no_such_host)/i) {
  239:             untie(%cache);
  240:             return "Error getting student data.";
  241:         }
  242:     }
  243: 
  244:     if($c->aborted()) {
  245:         untie(%cache);
  246:         return 'aborted'; 
  247:     }
  248: 
  249:     # Active is a temporary solution, remember to change
  250:     Apache::loncoursedata::ProcessClasslist(\%cache,$classlist,$courseID,$c);
  251:     if($c->aborted()) {
  252:         untie(%cache);
  253:         return 'aborted'; 
  254:     }
  255: 
  256:     &ProcessFormData(\%cache);
  257:     my $students = &SortStudents(\%cache);
  258: 
  259:     if($cache{'download'} ne 'false') {
  260:         my $who = $cache{'download'};
  261:         my $courseData = 
  262:             &Apache::loncoursedata::DownloadCourseInformation(
  263:                                              $who, $courseID, 
  264:                                              $cache{$who.':lastDownloadTime'});
  265:         &Apache::loncoursedata::ProcessStudentData(\%cache, $courseData, $who);
  266:         $cache{'download'} = 'false';
  267:     } elsif($cache{'DownloadAll'} ne 'false') {
  268:         my @allStudents;
  269:         if($cache{'DownloadAll'} eq 'sorted') {
  270:             @allStudents = @$students;
  271:         } else {
  272:             @allStudents = split(':::', $cache{'NamesOfStudents'});
  273:         }
  274:         foreach (@allStudents) {
  275:             my $courseData = 
  276:                 &Apache::loncoursedata::DownloadCourseInformation(
  277:                                              $_, $courseID, 
  278:                                              $cache{$_.':lastDownloadTime'});
  279:             &Apache::loncoursedata::ProcessStudentData(\%cache, $courseData, 
  280:                                                        $_);
  281:             if($c->aborted()) {
  282:                 untie(%cache);
  283:                 return 'aborted'; 
  284:             }
  285:         }
  286:         $cache{'DownloadAll'} = 'false';
  287:     }
  288: 
  289:     if($c->aborted()) {
  290:         untie(%cache);
  291:         return 'aborted'; 
  292:     }
  293: 
  294:     if($c->aborted()) {
  295:         untie(%cache);
  296:         return 'aborted'; 
  297:     }
  298: 
  299:     untie(%cache);
  300: 
  301:     return ('OK', $students);
  302: }
  303: 
  304: 
  305: # Create progress
  306: sub Create_PrgWin {
  307:     my ($r)=@_;
  308:     $r->print(<<ENDPOP);
  309:     <script>
  310:     popwin=open('','popwin','width=400,height=100');
  311:     popwin.document.writeln('<html><body bgcolor="#88DDFF">'+
  312:       '<title>LON-CAPA Statistics</title>'+
  313:       '<h4>Computation Progress</h4>'+
  314:       '<form name=popremain>'+
  315:       '<input type=text size=35 name=remaining value=Starting></form>'+
  316:       '</body></html>');
  317:     popwin.document.close();
  318:     </script>
  319: ENDPOP
  320: 
  321:     $r->rflush();
  322: }
  323: 
  324: # update progress
  325: sub Update_PrgWin {
  326:     my ($totalStudents,$index,$name,$r)=@_;
  327:     $r->print('<script>popwin.document.popremain.remaining.value="'.
  328:               'Computing '.$index.'/'.$totalStudents.': '.
  329:               $name.'";</script>');
  330:     $r->rflush();
  331: }
  332: 
  333: # close Progress Line
  334: sub Close_PrgWin {
  335:     my ($r)=@_;
  336:     $r->print('<script>popwin.close()</script>');
  337:     $r->rflush(); 
  338: }
  339: 
  340: # For loading the colored table for display or un-colored for print
  341: sub setbgcolor {
  342:     my $PrintTable=shift;
  343:     my %color;
  344:     if ($PrintTable){
  345: 	$color{"gb"}="#FFFFFF";
  346: 	$color{"red"}="#FFFFFF";
  347: 	$color{"yellow"}="#FFFFFF";
  348: 	$color{"green"}="#FFFFFF";
  349: 	$color{"purple"}="#FFFFFF";
  350:     } else {
  351: 	$color{"gb"}="#DDFFFF";
  352: 	$color{"red"}="#FFDDDD";
  353: 	$color{"yellow"}="#EEFFCC";
  354: 	$color{"green"}="#DDFFDD";
  355: 	$color{"purple"}="#FFDDFF";
  356:     }
  357: 
  358:     return \%color;
  359: }
  360: 
  361: sub BuildClasslist {
  362:     my ($cacheDB,$students,$studentInformation,$headings,$spacePadding)=@_;
  363: 
  364:     my %cache;
  365:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER,0640)) {
  366:         return '<html><body>Unable to tie database.</body></html>';
  367:     }
  368: 
  369:     my $Str='';
  370:     $Str .= '<table border="0"><tr><td bgcolor="#777777">'."\n";
  371:     $Str .= '<table border="0" cellpadding="3"><tr bgcolor="#e6ffff">'."\n";
  372: 
  373:     my $displayString = '<td align="left"><a href="/adm/statistics?';
  374:     $displayString .= 'sort=LINKDATA">DISPLAYDATA&nbsp</a></td>'."\n";
  375:     $Str .= &Apache::lonhtmlcommon::CreateStudentInformationHeadings(\%cache,
  376:                                                            $studentInformation,
  377:                                                            $headings,
  378:                                                            $displayString);
  379:     $Str .= '<td align="left">';
  380:     $Str .= '<a href="/adm/statistics?sort=lastDownloadTime">';
  381:     $Str .= 'Last Updated&nbsp</a></td>'."\n";
  382:     $Str .= '</tr>'."\n";
  383:     my $alternate=0;
  384:     foreach (@$students) {
  385:         my ($username, $domain) = split(':', $_);
  386:         if($alternate) {
  387:             $Str .= '<tr bgcolor="#ffffe6"><td>';
  388:         } else {
  389:             $Str .= '<tr bgcolor="#ffffc6"><td>';
  390:         }
  391:         $alternate = ($alternate + 1) % 2;
  392:         foreach my $data (@$studentInformation) {
  393:             if($data eq 'fullname') {
  394:                 $Str .= '<a href="/adm/statistics?reportSelected=';
  395:                 $Str .= &Apache::lonnet::escape('Student Assessment');
  396:                 $Str .= '&StudentAssessmentStudent=';
  397:                 $Str .= &Apache::lonnet::escape($cache{$_.':'.$data}).'">';
  398:                 #$Str .= 'Student Assessment'.'">';
  399:             }
  400: 
  401:             $Str .= $cache{$_.':'.$data}.'&nbsp';
  402: 
  403:             if($data eq 'fullname') {
  404:                 $Str .= '</a>';
  405:             }
  406: 
  407:             $Str .= '</td><td>';
  408:         }
  409: 
  410:         $Str .= '<a href="/adm/statistics?download='.$_.'">';
  411:         my $downloadTime = $cache{$_.':lastDownloadTime'};
  412:         if($downloadTime ne 'Not downloaded') {
  413:             $downloadTime = localtime($downloadTime);
  414:         }
  415:         $Str .= $downloadTime;
  416: 
  417:         $Str .= '&nbsp</a></td></tr>'."\n";
  418:     }
  419: 
  420:     $Str .= '</table></td></tr></table>'."\n";
  421: 
  422:     untie(%cache);
  423: 
  424:     return $Str;
  425: }
  426: 
  427: sub BuildStatistics {
  428:     my ($r)=@_;
  429: 
  430:     my $c = $r->connection;
  431:     my @studentInformation=('fullname','section','id','domain','username');
  432:     my @headings=('Full Name', 'Section', 'PID', 'Domain', 'User Name');
  433:     my $spacePadding = '   ';
  434:     my %reports = ('classlist'          => 'Class list',
  435:                    'problem_statistics' => 'Problem Statistics',
  436:                    'student_assessment' => 'Student Assessment',
  437:                    'reportSelected'     => 'Class list');
  438: 
  439:     my %cache;
  440:     my $courseID=$ENV{'request.course.id'};
  441:     my $cacheDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
  442:                   "_$ENV{'user.domain'}_$courseID\_statistics.db";
  443: 
  444:     my %color=&setbgcolor(0);
  445:     my ($returnValue, $students) = &PrepareData($c, $cacheDB);
  446:     if($returnValue ne 'OK') {
  447:         $r->print('<html><body>'.$returnValue."\n".'</body></html>');
  448:         return OK;
  449:     }
  450: 
  451:     my $GoToPage;
  452:     if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER,0640)) {
  453:         $GoToPage = $cache{'reportSelected'};
  454:         $reports{'reportSelected'} = $cache{'reportSelected'};
  455: #        if(defined($cache{'reportKey'}) && $cache{'reportKey'} ne 'false') {
  456: #            $reports{$cache{'reportKey'}} = $cache{'reportSelected'};
  457: #        }
  458: 
  459:         if(defined($cache{'OptionResponses'})) {
  460:             $reports{'problem_analysis'} = 'Problem Analysis';
  461:         }
  462: 
  463:         $r->print(&Apache::lonhtmlcommon::Title('LON-CAPA Statistics'));
  464:         $r->print('<form name="Statistics" ');
  465:         $r->print('method="post" action="/adm/statistics">');
  466:         $r->print(&Apache::lonhtmlcommon::CreateStatisticsMainMenu(
  467:                                                              $cache{'Status'}, 
  468:                                                              \%reports));
  469:         untie(%cache);
  470:     } else {
  471:         $r->print('<html><body>Unable to tie database.</body></html>');
  472:         return OK;
  473:     }
  474: 
  475:     if($GoToPage eq 'Activity Log') {
  476:         &Apache::lonproblemstatistics::Activity();
  477:     } elsif($GoToPage eq 'Problem Statistics') {
  478:         $r->print(
  479:         &Apache::lonproblemstatistics::BuildProblemStatisticsPage($cacheDB, 
  480:                                                                   $students, 
  481:                                                                   $courseID, 
  482:                                                                   $c,$r,
  483: 								  \%color));
  484:     } elsif($GoToPage eq 'Problem Analysis') {
  485:         $r->print(
  486:               &Apache::lonproblemanalysis::BuildProblemAnalysisPage($cacheDB));
  487:     } elsif($GoToPage eq 'Student Assessment') {
  488:         $r->print(
  489:             &Apache::lonstudentassessment::BuildStudentAssessmentPage($cacheDB,
  490:                                                                      $students,
  491:                                                                      $courseID,
  492:                                                                      $c));
  493:     } elsif($GoToPage eq 'Analyze') {
  494:         $r->print(&Apache::lonproblemanalysis::BuildAnalyzePage($cacheDB, 
  495:                                                                 $students, 
  496:                                                                 $courseID));
  497:     } elsif($GoToPage eq 'DoDiffGraph') {
  498:         &Apache::lonproblemstatistics::BuildDiffGraph($r);
  499:     } elsif($GoToPage eq 'PercentWrongGraph') {
  500:         &Apache::lonproblemstatistics::BuildWrongGraph($r);
  501:     } elsif($GoToPage eq 'Class list') {
  502:         $r->print(&BuildClasslist($cacheDB, $students, \@studentInformation,
  503:                                   \@headings, $spacePadding));
  504:     }
  505: 
  506:     $r->print('</form>'."\n");
  507:     $r->print("\n".'</body>'."\n".'</html>');
  508:     $r->rflush();
  509: 
  510:     return OK;
  511: }
  512: 
  513: # ================================================================ Main Handler
  514: 
  515: sub handler {
  516:     my $r=shift;
  517: 
  518:     unless(&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
  519:         $ENV{'user.error.msg'}=
  520:         $r->uri.":vgr:0:0:Cannot view grades for complete course";
  521:         return HTTP_NOT_ACCEPTABLE; 
  522:     }
  523: 
  524:     # Set document type for header only
  525:     if($r->header_only) {
  526:         if ($ENV{'browser.mathml'}) {
  527:             $r->content_type('text/xml');
  528:         } else {
  529:             $r->content_type('text/html');
  530:         }
  531:         &Apache::loncommon::no_cache($r);
  532:         $r->send_http_header;
  533:         return OK;
  534:     }
  535: 
  536:     unless($ENV{'request.course.fn'}) {
  537: 	my $requrl=$r->uri;
  538:         $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  539:         return HTTP_NOT_ACCEPTABLE; 
  540:     }
  541: 
  542:     $r->content_type('text/html');
  543:     $r->send_http_header;
  544: 
  545:     &BuildStatistics($r);
  546: 
  547:     return OK;
  548: }
  549: 1;
  550: __END__
  551: 

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