File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.10: download - view: text, annotated - select for diffs
Thu Mar 7 00:28:55 2002 UTC (22 years, 4 months ago) by minaeibi
Branches: MAIN
CVS tags: HEAD
leaaning the codes (2)

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: lonstatistics.pm,v 1.10 2002/03/07 00:28:55 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/05,7/09,7/25/01,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 Behrouz Minaei
   34: ###
   35: 
   36: package Apache::lonstatistics; 
   37: 
   38: use strict;
   39: use Apache::Constants qw(:common :http);
   40: use Apache::lonnet();
   41: use Apache::lonhomework;
   42: use HTML::TokeParser;
   43: use GDBM_File;
   44: 
   45: # -------------------------------------------------------------- Module Globals
   46: my %hash;
   47: my %CachData;
   48: my %GraphDat;
   49: my %maps;
   50: my %mapsort;
   51: my %section;
   52: my %StuBox;
   53: my %DiscFac;
   54: my %DisUp;
   55: my %DisLow;
   56: my $UpCnt;
   57: my $CurMap;
   58: my $CurSec;
   59: my $CurStu;
   60: my @cols;
   61: my @list;
   62: my @students;
   63: my $p_count;
   64: my $Pos;
   65: my $r;
   66: my $OpSel1;
   67: my $OpSel2;
   68: my $OpSelDis1;
   69: my $OpSelDis2;
   70: my $OpSel3;
   71: my $OpSel4;
   72: my $GData;
   73: my $cid;
   74: my $firstres;
   75: my $lastres;
   76: my $DiscFlag;
   77: my $HWN;
   78: my $P_Order;
   79: my %Header = (0,"Homework Sets Order",1,"#Stdnts",2,"Tries",3,"Mod",
   80:               4,"Mean",5,"#YES",6,"#yes",7,"%Wrng",8,"DoDiff",
   81:               9,"S.D.",10,"Skew.",11,"D.F.1st",12,"D.F.2nd");
   82: 
   83: #------- Processing upperlist and lowerlist according to each problem
   84: sub ProcessDisc {
   85:     my @List = @_;
   86:     @List = sort (@List);
   87:     my $Count = $#List+1;
   88:     my $Prb;
   89:     my @Dis;
   90:     my $Slvd=0;
   91:     my $tmp;
   92:     my $Sum1=0;
   93:     my $Sum2=0;
   94:     my $nIdx=0;
   95:     my $nStud=0;
   96:     my %Proc;
   97:     undef %Proc;
   98:     while ($nIdx<$Count) {
   99: 	($Prb,$tmp)=split(/\=/,$List[$nIdx]);
  100: 	@Dis=split(/\+/,$tmp);
  101: 	my $Temp = $Prb;
  102: 	do {
  103: 	    $nIdx++;
  104: 	    $nStud++;
  105: 	    $Sum1 += $Dis[0];
  106: 	    $Sum2 += $Dis[1];
  107: 	    ($Prb,$tmp)=split(/\=/,$List[$nIdx]);
  108: 	    @Dis=split(/\+/,$tmp);
  109: 	} while ( $Prb eq $Temp && $nIdx < $Count );
  110: #	$Proc{$Temp}=($Sum1/$nStud).':'.$nStud;
  111: 	$Proc{$Temp}=($Sum1/$nStud).':'.($Sum2/$nStud);
  112: #        $r->print("$nIdx) $Temp --> ($nStud) $Proc{$Temp} <br>");
  113: 	$Sum1=0;
  114: 	$Sum2=0;
  115: 	$nStud=0;
  116:     }
  117:     return %Proc;
  118: }
  119: 
  120: 
  121: #------- Creating Discimination factor   
  122: sub Discriminant {
  123:     my $Count=0;
  124:     foreach (keys(%DiscFac)){ 
  125: 	$Count++;
  126:     }
  127:     $UpCnt = int(0.27*$Count);
  128:     my $low=0;
  129:     my $up=$Count-$UpCnt;
  130:     my @UpList=();
  131:     my @LowList=();
  132:     $Count=0;
  133:     foreach my $key (sort(keys(%DiscFac))){ 
  134: 	$Count++;    
  135:         #$r->print("<br>$Count) $key = $DiscFac{$key}");
  136: 	if ($low < $UpCnt || $Count > $up) {
  137: 	    $low++;
  138: 	    my $str=$DiscFac{$key};
  139: 	    foreach(split(/\:/,$str)){
  140: 		if ($_) {
  141: 		    if ($low<$UpCnt){push(@LowList,$_);}
  142: 		    else {push(@UpList,$_);}
  143: 		}
  144: 	    }
  145: 	}
  146:     }
  147:     %DisUp=&ProcessDisc(@UpList);
  148:     %DisLow=&ProcessDisc(@LowList);
  149: }
  150: 
  151:    
  152: sub NumericSort {          
  153:     $a <=> $b;
  154: }
  155: 
  156: # ------ Create different Student Report 
  157: sub StudentReport {
  158: 
  159:     my ($sname,$sdom)=@_;
  160: 
  161:     if ( $sname eq 'All Students' ) {
  162: 	$r->print( '<h3><font color=blue>WARNING: 
  163:                     Please select a student</font></h3>' );
  164: 	return;
  165:     }
  166:     my $shome=&Apache::lonnet::homeserver($sname,$sdom);          
  167:     my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.':'.$cid,$shome );
  168:     my %result = ();
  169:     my $ResId;
  170:     my $PrOrd;
  171:     my $Code;
  172:     my $Tries;
  173:     my $TotalTries = 0;
  174:     my $ParCr = 0;
  175:     my $Wrongs;
  176:     my %TempHash;
  177:     my $Version;
  178:     my $LatestVersion;
  179:     my $PtrTry='';
  180:     my $PtrCod='';
  181:     my $SetNo=0;
  182:     my $Str = "\n".'<table border=2>'.
  183:               "\n".'<tr>'.
  184:               "\n".'<th> # </th>'.
  185: 	      "\n".'<th> Set Title </th>'.
  186: 	      "\n".'<th> Results </th>'.
  187: 	      "\n".'<th> Tries </th>'.
  188: 	      "\n".'</tr>';
  189:     unless ($reply=~/^error\:/) {
  190:         foreach (split(/\&/,$reply)){
  191:             my ($name,$value)=split(/\=/,&Apache::lonnet::unescape($_));
  192:             $result{$name}=$value;
  193:         }
  194:         foreach my $CurCol (@cols) {
  195: 	    if (!$CurCol){
  196: 		my $Set=&Apache::lonnet::declutter($hash{'map_id_'.$1});
  197: 		if ( $Set ) {
  198: 		    $SetNo++;
  199: 		    $Str .= "\n"."<tr>".
  200: 			    "\n"."<td> $SetNo </td>".
  201:                             "\n"."<td> $Set </td>".
  202:                             "\n"."<td> $PtrCod </td>".
  203:                             "\n"."<td> $PtrTry</td>".
  204:                             "\n"."</tr>";
  205: 		}
  206: 		$PtrTry='';
  207: 		$PtrCod='';
  208: 		next; 
  209: 	    }
  210: 	    ($PrOrd,$ResId)=split(/\:/,$CurCol);
  211:             $ResId=~/(\d+)\.(\d+)/;
  212:             my $Map = &Apache::lonnet::declutter( $hash{'map_id_'.$1} );
  213:             if ( $CurMap ne 'All Maps' ) {
  214: 		my ( $ResMap, $NameMap ) = split(/\=/,$CurMap);
  215: 		if ( $Map ne $ResMap ) { next; }
  216: 	    }
  217: 	    my $meta=$hash{'src_'.$ResId};
  218: 	    my $PartNo = 0;
  219: 	    undef %TempHash;
  220: 	    foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))){
  221: 		if ($_=~/^stores\_(\d+)\_tries$/) {
  222:                     my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
  223: 		    if ( $TempHash{"$Part"} eq '' ) { 
  224: 			$TempHash{"$Part"} = $Part;
  225: 			$TempHash{$PartNo}=$Part;
  226: 			$TempHash{"$Part.Code"} = '-';  
  227: 			$TempHash{"$Part.PrOrd"} = $PrOrd+$PartNo;  
  228: 			$PartNo++;
  229: 		    }
  230: 		}
  231:             }
  232: 
  233:             my $Prob = $Map.'___'.$2.'___'.
  234:                        &Apache::lonnet::declutter( $hash{'src_'.$ResId} );
  235:             $Code='U';
  236:             $Tries = 0;
  237:             $Wrongs = 0;
  238:   	    $LatestVersion = $result{"version:$Prob"};
  239: 	    if ( $LatestVersion ) {
  240: 		for ( my $Version=1; $Version<=$LatestVersion; $Version++ ) {
  241: 		    my $vkeys = $result{"$Version:keys:$Prob"};
  242: 		    my @keys = split(/\:/,$vkeys);		
  243:   
  244: 		    foreach my $Key (@keys) {		  
  245: 			if (($Key=~/\.(\w+)\.solved$/) && ($Key!~/^\d+\:/)) {
  246: 			    my $Part = $1;
  247: 			    $Tries = $result{"$Version:$Prob:resource.$Part.tries"};
  248: 			    $TempHash{"$Part.Tries"} = ($Tries) ? $Tries : 0; 
  249: 			    $TotalTries += $Tries;
  250: 			    my $Val = $result{"$Version:$Prob:resource.$Part.solved"};
  251: 			    if ( $Val eq 'correct_by_student' )
  252:                                 { $Wrongs = $Tries - 1; $Code = 'Y'; } 
  253: 			    elsif ( $Val eq 'correct_by_override' )
  254:                                 { $Wrongs = $Tries - 1; $Code = 'y'; }                        
  255: 			    elsif ( $Val eq 'incorrect_attempted' || 
  256:                                 $Val eq 'incorrect_by_override' )
  257: 		                { $Wrongs = $Tries; $Code = 'N'; }
  258: 			    $TempHash{"$Part.Code"} = $Code;
  259: 			    $TempHash{"$Part.Wrongs"} = $Wrongs;
  260: 			}
  261:      		    }
  262:                 }
  263: 		for ( my $n = 0; $n < $PartNo; $n++ ) {		  
  264: 		    my $part = $TempHash{$n};
  265: 		    if ($PtrTry ne '') {$PtrTry .= ',';}
  266: 		    $PtrTry .= "$TempHash{$part.'.Tries'}";
  267:                     $PtrCod .= "$TempHash{$part.'.Code'}";    
  268: 		}
  269:             }
  270: 	    else { 
  271: 		for(my $n=0; $n<$PartNo; $n++) { 
  272: 		    if ($PtrTry ne '') {$PtrTry .= ',';}
  273: 		    $PtrTry .= "0";
  274:                     $PtrCod .= "-"; 
  275: 		}
  276: 	    }
  277:         }
  278:     }
  279:     $Str .= "\n".'</table>';
  280:     $r->print($Str);
  281:     $r->rflush();
  282: }
  283: 
  284: sub CreateTable {
  285:     my $ColNo=0;
  286:     foreach (keys(%Header)){ 
  287: 	$ColNo++;
  288:     } 
  289:     my ($Hd, $Hid)=@_;
  290:     if ( $Hd == 1 ) {
  291: 	$r->print('<br><a href="'.$hash{'src_'.$Hid}.
  292:                   '" target="_blank">'.$hash{'title_'.$Hid}.'</a>');
  293:     }
  294:     my $Result = "\n".'<table border=2>';
  295:     $Result .= '<tr><th>P#</th>'."\n";
  296:     for ( my $nIdx=0; $nIdx < $ColNo; $nIdx++ ) { 
  297: 	$Result .= '<th>'.'<input type="submit" name="sort" value="'.
  298:                    $Header{$nIdx}.'" />'.'</th>'."\n";
  299:     }
  300:     $Result .= "\n".'</tr>'."\n";    
  301:     $r->print( $Result );
  302:     $r->rflush();
  303: }
  304: 
  305: sub CloseTable {
  306:     $r->print("\n".'</table>'."\n");
  307:     $r->rflush();
  308: }
  309:  
  310: # ------------------------------------------- Prepare Statistics Table
  311: sub PreStatTable {
  312:     my $CacheDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
  313:                   "_$ENV{'user.domain'}_$cid\_statistics.db";
  314:     my $GraphDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
  315:                   "_$ENV{'user.domain'}_$cid\_graph.db";
  316:     my $OpSel11='';
  317:     my $OpSel12='';
  318:     my $OpSel13='';
  319:     my $Status = $ENV{'form.status'};
  320:     if ( $Status eq 'Any' ) { $OpSel13='selected'; }
  321:     elsif ($Status eq 'Expired' ) { $OpSel12 = 'selected'; }
  322:     else { $OpSel11 = 'selected'; }
  323: 
  324:     my $Ptr = '';
  325:     $Ptr .= '<br><b> Student Status: &nbsp; </b>'."\n".
  326:             '<select name="status">'. 
  327:             '<option '.$OpSel11.' >Active</option>'."\n".
  328:             '<option '.$OpSel12.' >Expired</option>'."\n".
  329: 	    '<option '.$OpSel13.' >Any</option> </select> '."\n";
  330:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  331:     $Ptr .= '<input type=submit name=sort value="Recalculate Statistics"/>'."\n";
  332: 
  333:     $Ptr .= '<br><b> Sorting Type: &nbsp; </b>'."\n".
  334:             '<select name="order"> <option '.$OpSel1.' >Ascending</option>'."\n".
  335: 	    '<option '.$OpSel2.'>Descending</option> </select> '."\n";
  336:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  337:     $Ptr .= '<input type="submit" name="sort" value="DoDiff Graph" />'."\n";
  338:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  339:     $Ptr .= '<input type="submit" name="sort" value="%Wrong Graph" />'."\n";
  340: 
  341:     $Ptr .= '<pre>'.
  342:     '<b>  #Stdnts</b>: Total Number of Students opened the problem.<br>'. 
  343:     '<b>  Tries  </b>: Total Number of Tries for solving the problem.<br>'. 
  344:     '<b>  Mod   </b> : Maximunm Number of Tries for solving the problem.<br>'. 
  345:     '<b>  Mean   </b>: Average Number of the tries. [ Tries / #Stdnts ]<br>'.
  346:     '<b>  #YES   </b>: Number of students solved the problem correctly.<br>'. 
  347:     '<b>  #yes   </b>: Number of students solved the problem by override.<br>'.
  348:     '<b>  %Wrng  </b>: Percentage of students tried to solve the problem but'.
  349:     ' still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]<br>'.
  350: #    '  DoDiff : Degree of Difficulty of the problem. [ Tries/(#YES+#yes+0.1) ]<br>'. Kashy formula
  351:     '<b>  DoDiff </b>: Degree of Difficulty of the problem. [ 1 - ((#YES+#yes) / Tries) ]<br>'. #Gerd formula
  352:     '<b>  S.D.  </b> : Standard Deviation of the tries.'.
  353:     '[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1)'.
  354:     ' where Xi denotes every student\'s tries ]<br>'.
  355:     '<b>  Skew.  </b>: Skewness of the students tries.'.
  356: 	' [ (sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3) ]<br>'.
  357:     '<b>  Dis.F. </b>: Discrimination Factor: A Standard for '.
  358: 	'evaluating the problem according to a Criterion<br>'.
  359: 	'<b>           [Applied Criterion in %27 Upper Students - '.
  360: 	'Applied the same Criterion in %27 Lower Students]</b><br>'.
  361:     '<b>           1st </b>Criterion for Sorting the Students: '.
  362: 	'Sum of Partial Credit Awarded / Total Number of Tries<br>'.
  363:     '<b>           2nd </b>Criterion for Sorting the Students: '.
  364: 	'Total number of Correct Answers / Total Number of Tries'.	
  365:             '</pre>';
  366: 
  367:     $r->print($Ptr);
  368:     $r->rflush();	
  369: 
  370:     if ((-e "$CacheDB")&&($ENV{'form.sort'} ne 'Recalculate Statistics')) {
  371: 	if (tie(%CachData,'GDBM_File',"$CacheDB",&GDBM_READER,0640)) {
  372: 	    tie(%GraphDat,'GDBM_File',$GraphDB,&GDBM_WRCREAT,0640);
  373: 	    &Cache_Statistics();
  374:         }
  375:         else {
  376: 	    $r->print("Unable to tie hash to db file");
  377:         }
  378:     }
  379:     else {
  380: 	if (tie(%CachData,'GDBM_File',$CacheDB,&GDBM_WRCREAT,0640)) {
  381: 	    tie(%GraphDat,'GDBM_File',$GraphDB,&GDBM_WRCREAT,0640);
  382: 	    foreach (keys %DiscFac) {delete $CachData{$_};}
  383: 	    foreach (keys %CachData) {delete $CachData{$_};}
  384: 	    $DiscFlag=0;
  385: 	    &Build_Statistics();
  386: 	}
  387:         else {
  388: 	    $r->print("Unable to tie hash to db file");
  389:         }
  390:     }
  391:     #$r->print('Total instances of the problems : '.($p_count*($#students+1)));
  392: 
  393:     untie(%CachData);
  394:     untie(%GraphDat);
  395: }
  396: 
  397: 
  398: # ------------------------------------- Find the section of student in a course
  399: 
  400: sub usection {
  401:     my ($udom,$unam,$courseid,$ActiveFlag)=@_;
  402:     $courseid=~s/\_/\//g;
  403:     $courseid=~s/^(\w)/\/$1/;
  404:     foreach (split(/\&/,&Apache::lonnet::reply('dump:'.
  405:              $udom.':'.$unam.':roles',
  406:              &Apache::lonnet::homeserver($unam,$udom)))){
  407:         my ($key,$value)=split(/\=/,$_);
  408:         $key=&Apache::lonnet::unescape($key);
  409:         if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
  410:             my $section=$1;
  411:             if ($key eq $courseid.'_st') { $section=''; }
  412: 	    my ($dummy,$end,$start)=split(/\_/,&Apache::lonnet::unescape($value));
  413: 	    if ( $ActiveFlag ne 'Any' ) {
  414: 		my $now=time;
  415: 		my $notactive=0;
  416: 		if ($start) {
  417: 		    if ($now<$start) { $notactive=1; }
  418: 		}
  419: 		if ($end) {
  420: 		    if ($now>$end) { $notactive=1; }
  421: 		}
  422: 		if ((($ActiveFlag eq 'Expired') && $notactive == 1) || 
  423:                     (($ActiveFlag eq 'Active') && $notactive == 0 ) ) {
  424: 		    return $section;
  425: 		}
  426: 		else { return '-1'; } 
  427: 	    }
  428: 	    return $section;
  429:         }
  430:     }
  431:     return '-1';
  432: }
  433: 
  434: 
  435: # ------ Dump the Student's DB file and handling the data for statistics table 
  436: 
  437: sub ExtractStudentData {
  438:     my ($student,$coid)=@_;
  439:     my ($sname,$sdom) = split( /\:/, $student );
  440:     my $shome=&Apache::lonnet::homeserver( $sname,$sdom );          
  441:     my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.':'.$coid,$shome );
  442:     my %result = ();
  443:     my $ResId;
  444:     my $PrOrd;
  445:     my $Dis = '';
  446:     my $Code;
  447:     my $Tries;
  448:     my $ParCr;
  449:     my $TotalTries = 0;
  450:     my $TotalOpend = 0;
  451:     my $ProbSolved = 0;
  452:     my $ProbTot = 0;
  453:     my $TimeTot = 0;
  454:     my $TotParCr = 0;
  455:     my $Wrongs;
  456:     my %TempHash;
  457:     my $Version;
  458:     my $LatestVersion;
  459:     my $SecLimit;
  460:     my $MapLimit;
  461:     unless ($reply=~/^error\:/) {
  462:         foreach (split(/\&/,$reply)) {
  463:             my ($name,$value)=split(/\=/,&Apache::lonnet::unescape($_));
  464:             $result{$name}=$value;
  465:         }
  466:         foreach my $CurCol(@cols) {
  467: 	    ($PrOrd,$ResId)=split(/\:/,$CurCol);
  468: 	    if ( !$CurCol ) { next; }
  469:             $ResId=~/(\d+)\.(\d+)/;
  470: 	    my $MapId=$1;
  471: 	    my $PrbId=$2;
  472:             my $Map = &Apache::lonnet::declutter( $hash{'map_id_'.$MapId} );
  473:             if ( $CurMap ne 'All Maps' ) {
  474: 		my ( $ResMap, $NameMap ) = split(/\=/,$CurMap);
  475: 		if ( $Map ne $ResMap ) { next; }
  476: 	    }
  477: 	    my $meta=$hash{'src_'.$ResId};
  478: 	    my $PartNo = 0;
  479: 	    $Dis .= ':';
  480: 	    undef %TempHash;
  481: 	    foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
  482: 		if ($_=~/^stores\_(\d+)\_tries$/) {
  483:                     my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
  484: 		    if ( $TempHash{"$Part"} eq '' ) { 
  485: 			$TempHash{"$Part"} = $Part;
  486: 			$TempHash{$PartNo}=$Part;
  487: 			$TempHash{"$Part.Code"} = 'U';  
  488: 			$TempHash{"$Part.PrOrd"} = $PrOrd+$PartNo;
  489: 			$PartNo++;
  490: 		    }
  491: 		}
  492:             }
  493: 
  494:             my $Prob = $Map.'___'.$PrbId.'___'.
  495:                        &Apache::lonnet::declutter( $hash{'src_'.$ResId} );
  496:             $Code='U';
  497:             $Tries = 0;
  498: 	    $ParCr = 0;
  499:             $Wrongs = 0;
  500:   	    $LatestVersion = $result{"version:$Prob"};       
  501: 
  502: 	    if ( $LatestVersion ) {
  503: 		for ( my $Version=1; $Version<=$LatestVersion; $Version++ ) {
  504: 		    my $vkeys = $result{"$Version:keys:$Prob"};
  505: 		    my @keys = split(/\:/,$vkeys);		
  506:   
  507: 		    foreach my $Key (@keys) {		  
  508: 			if (($Key=~/\.(\w+)\.solved$/) && ($Key!~/^\d+\:/)) {
  509: 			    my $Part = $1;
  510: 			    $Tries = $result{"$Version:$Prob:resource.$Part.tries"};
  511: 			    $ParCr = $result{"$Version:$Prob:resource.$Part.awarded"};
  512: 			    my $Time = $result{"$Version:$Prob:timestamp"};
  513: 			    $TempHash{"$Part.Time"} = ($Time) ? $Time : 0;
  514: 			    $TempHash{"$Part.Tries"} = ($Tries) ? $Tries : 0;
  515: 			    $TempHash{"$Part.ParCr"} = ($ParCr) ? $ParCr : 0;        
  516: 			    $TotalTries += $TempHash{"$Part.Tries"};
  517: 			    $TotParCr += $TempHash{"$Part.ParCr"};
  518: 			    my $Val = $result{"$Version:$Prob:resource.$Part.solved"};
  519: 			    if ( $Val eq 'correct_by_student' )
  520:                                { $Wrongs = $Tries - 1; $Code = 'C'; } 
  521: 			    elsif ( $Val eq 'correct_by_override' )
  522:                                { $Wrongs = $Tries - 1; $Code = 'O'; }                        
  523: 			    elsif ( $Val eq 'incorrect_attempted' || 
  524:                                 $Val eq 'incorrect_by_override' )
  525: 		               { $Wrongs = $Tries; $Code = 'I'; }
  526: 			    $TempHash{"$Part.Code"} = $Code;
  527: 			    $TempHash{"$Part.Wrongs"} = $Wrongs;
  528: 			}
  529:      		    }
  530:                 } 
  531: 		for ( my $n = 0; $n < $PartNo; $n++ ) {		  
  532: 		    my $part = $TempHash{$n};
  533: 		    my $Yes = 0;
  534:                     if ( $TempHash{$part.'.Code'} eq 'C' ||
  535:                          $TempHash{$part.'.Code'} eq 'O'  ) 
  536: 		       {$ProbSolved++;$Yes=1;}		
  537: 
  538:  #		    my $ptr = "$hash{'title_'.$ResId}";
  539: 		    my $ptr = $TempHash{$part.'.PrOrd'}.':'.$ResId;
  540: 
  541: 		    if ( $PartNo > 1 ) {                
  542: 			$ptr .= "*(part $part)";
  543: 			$Dis .= ':';
  544: 		    }
  545: 		    my $Fac = ($TempHash{"$part.Tries"}) ? 
  546:                               ($TempHash{"$part.ParCr"}/$TempHash{"$part.Tries"}) : 0;
  547: 		    my $DisF;
  548: 		    if ( $Fac > 0 &&  $Fac < 1 ) { 
  549: 			$DisF = sprintf( "%.4f", $Fac );
  550: 		    }
  551: 		    else {$DisF = $Fac;}
  552: #		    $DisF .= '+'.$TempHash{"$part.Time"};
  553: 		    $TimeTot += $TempHash{"$part.Time"};
  554: 		    $Dis .= $TempHash{$part.'.PrOrd'}.'='.$DisF.'+'.$Yes;
  555: 		    $ptr .= ":$TempHash{$part.'.Tries'}".
  556: 		            ":$TempHash{$part.'.Wrongs'}".
  557:                             ":$TempHash{$part.'.Code'}";
  558: 		    push (@list, $ptr);
  559: 		    $TotalOpend++;
  560: 		    $ProbTot++;
  561: 		}
  562:             }
  563: 	    #else { 
  564: 		#for(my $n=0; $n<$PartNo; $n++) {
  565: 		#    push (@list, "$hash{'title_'.$ResId}*$ResId:0:0:U");
  566: 		#    $ProbTot++; 
  567: 		#}
  568: 	    #}
  569:         }
  570: 	if ( $TotalTries ) {
  571: 	    my $DisFac = ( $TotalTries ) ? ($TotParCr/$TotalTries) : 0;
  572: 	    my $DisFactor = sprintf( "%.4f", $DisFac );
  573: 	    $DiscFac{$DisFactor}=$Dis;
  574: 	    #my $time;
  575: 	    #if ($ProbSolved){
  576: 		#$time = int(($TimeTot/$ProbSolved)-10000000);
  577: 	    #}
  578: 	    #$DiscFac{($DisFactor.':'.$sname.':'.$ProbTot.':'.$TotalOpend.':'.
  579:             #          $TotalTries.':'.$ProbSolved.':'.$time)}=$Dis;
  580: 	}
  581:     }
  582:     #$r->print($sname.' PrCr= '.$TotParCr.' Slvd= '.$ProbSolved.' Tries='.$TotalTries.'<br>');
  583: }
  584: 
  585: 
  586: # ------------------------------------------------------------ Build page table
  587: sub tracetable {
  588:     my ($rid,$beenhere)=@_;
  589:     $rid=~/(\d+)\.(\d+)/;
  590:     $maps{&Apache::lonnet::declutter($hash{'map_id_'.$1})}='';#$hash{'title_'.$rid}; 
  591:     #$maps{$HWN}=$hash{'title_'.$rid}; 
  592:     unless ($beenhere=~/\&$rid\&/) {
  593:        $beenhere.=$rid.'&'; 
  594:        if (defined($hash{'is_map_'.$rid})) {
  595: 	   my $cmap=$hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}};
  596:            if ( $cmap eq 'sequence' || $cmap eq 'page' ) { 
  597:                $cols[$#cols+1]=0;
  598: 	       $P_Order++;
  599: 	       $HWN=$P_Order;
  600:                $mapsort{$HWN} = $rid.':'; 
  601:                #$maps{&Apache::lonnet::declutter($hash{'src_'.$rid})}= 
  602:                #      $hash{'title_'.$rid}; 
  603:            }
  604:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
  605:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
  606: 	       my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
  607: 
  608:                &tracetable($hash{'map_start_'.$hash{'src_'.$rid}},
  609: 			   '&'.$frid.'&');
  610: 
  611: 	       $cols[$#cols+1]=($P_Order+1).':'.$frid;
  612:       
  613: 	       my $meta=$hash{'src_'.$frid};
  614: 	       my $PartNo = 0;
  615:                if ($meta) {
  616: 		   if ($meta=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  617: 		       foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
  618: 			   if ($_=~/^stores\_(\d+)\_tries$/) {
  619: 			       &Apache::lonnet::metadata($meta,$_.'.part');
  620: 			       $P_Order++;
  621: 			       $mapsort{$HWN} .= '&'.$P_Order;
  622: 			       $PartNo++;
  623: 			       $r->print('<br>'.$PartNo.'---'.$P_Order);
  624: 			   }
  625: 		       }
  626: 		   }
  627:                }
  628: 	   }
  629:        } else {
  630: 	   $cols[$#cols+1]=($P_Order+1).':'.$rid;
  631: 	   my $meta=$hash{'src_'.$rid};
  632: 	   my $PartNo = 0;
  633:            if ($meta) {
  634: 	       if ($meta=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  635: 		   foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
  636: 		       if ($_=~/^stores\_(\d+)\_tries$/) {
  637: 			   &Apache::lonnet::metadata($meta,$_.'.part');
  638: 			   $P_Order++;
  639: 			   $mapsort{$HWN} .= '&'.$P_Order;
  640: 			   $PartNo++;
  641: 		       }
  642: 		   }
  643: 	       }
  644: 	   }
  645:        }
  646:        if (defined($hash{'to_'.$rid})) {
  647:           foreach (split(/\,/,$hash{'to_'.$rid})){
  648:               &tracetable($hash{'goesto_'.$_},$beenhere);
  649:           }
  650:        }
  651:     }
  652: }
  653: 
  654: sub MySort {          
  655:     if ( $Pos > 0 ) {
  656: 	if ($ENV{'form.order'} eq 'Descending') {$b <=> $a;}
  657: 	else { $a <=> $b; }
  658:     }
  659:     else {
  660: 	if ($ENV{'form.order'} eq 'Descending') {$b cmp $a;}
  661: 	else { $a cmp $b; }
  662:     }
  663: }
  664: 
  665: sub Build_Statistics {
  666:     $r->print(<<ENDPOP);
  667:     <script>
  668:     popwin=open('','popwin','width=400,height=100');
  669:     popwin.document.writeln('<html><body bgcolor="#88DDFF">'+
  670:       '<title>LON-CAPA Statistics</title>'+
  671:       '<h4>Computation Progress</h4>'+
  672:       '<form name=popremain>'+
  673:       '<input type=text size=35 name=remaining value=Starting></form>'+
  674:       '</body></html>');
  675:     popwin.document.close();
  676:     </script>
  677: ENDPOP
  678: 
  679:     $r->rflush();
  680: # ---------------------------- Gathering the Data of students' tries
  681:     my $index;
  682:     for ($index=0;$index<=$#students;$index++) {
  683: #----------- update progress
  684:         $r->print('<script>popwin.document.popremain.remaining.value="'.
  685:                   'Computing '.($index+1).'/'.($#students+1).': '.
  686:                   $students[$index].'";</script>');
  687:         $r->rflush();
  688: 
  689:         &ExtractStudentData($students[$index],$cid);
  690:     }
  691: 
  692: # -------------------- sorting the Data
  693:     $r->print('<script>popwin.document.popremain.remaining.value="'.
  694:               'Calculating Discrimination Factors...";</script>');
  695: 
  696:     @list = sort (@list);
  697: 
  698: 
  699:     &Discriminant();
  700: 
  701:     $OpSel2='';
  702:     $OpSel1='selected';
  703:  		   
  704:     $p_count = 0; 
  705:     my $nIdx = 0;
  706:     my $dummy; 
  707:     my $p_val;
  708:     my $ResId;
  709:     my $NoElements = $#list + 1;
  710: #-------------------------------- loop for data representation
  711:     foreach (sort keys %mapsort) {
  712: 	my ($Hid,$pr)=split(/\:/,$mapsort{$_});
  713: 	my @lpr=split(/\&/,$pr);
  714: 	&CreateTable(1,$Hid);
  715: 	for (my $i=1; $i<=$#lpr; $i++) {
  716: 	    my %storestats=();
  717: 	    my ($PrOrd,$Prob,$Tries,$Wrongs,$Code)=split(/\:/,$list[$nIdx]);
  718: 	    my $Temp = $Prob;
  719: 	    my $MxTries = 0;
  720: 	    my $TotalTries = 0;
  721: 	    my $YES = 0;
  722: 	    my $Incorrect = 0;
  723: 	    my $Override = 0;
  724: 	    my $StdNo = 0;
  725: 	    my @StdLst;
  726: 	    while ( $PrOrd == $lpr[$i] ) 
  727: 	    {
  728: 		$nIdx++;
  729: 		$StdNo++;
  730: 		$StdLst[ $StdNo ] = $Tries;
  731: 		$TotalTries += $Tries;
  732: 		if ( $MxTries < $Tries ) { $MxTries = $Tries; } 
  733: 		if ( $Code eq 'C' ){ $YES++; }
  734: 		elsif( $Code eq 'I' ) { $Incorrect++; }
  735: 		elsif( $Code eq 'O' ) { $Override++; }
  736: 		elsif( $Code eq 'U' ) { $StdNo--; }
  737: 		($PrOrd,$Prob,$Tries,$Wrongs,$Code)=split(/\:/,$list[$nIdx]);
  738: 	    }	
  739: 
  740: 	    $p_count++;
  741: 	    my $Dummy;
  742: 	    ($ResId,$Dummy)=split(/\*/,$Temp);
  743: 
  744: 	    $Temp = '<a href="'.$hash{'src_'.$ResId}.
  745:                 '" target="_blank">'.$hash{'title_'.$ResId}.$Dummy.'</a>';
  746:     
  747: 	    my $res = &Apache::lonnet::declutter($hash{'src_'.$ResId});
  748: 	    my $urlres=$res;
  749: 
  750: 	    $ResId=~/(\d+)\.(\d+)/;
  751: 	    my $Map = &Apache::lonnet::declutter( $hash{'map_id_'.$1} );
  752: 	    $urlres=$Map;
  753:  
  754: 	    $res = '<a href="'.$hash{'src_'.$ResId}.'">'.$res.'</a>';
  755: 	    #$Map = '<a href="'.$Map.'">'.$res.'</a>';
  756: 
  757: #------------------------ Compute the Average of Tries about one problem
  758: 	    my $Average = ($StdNo) ? $TotalTries/$StdNo : 0;
  759: 
  760: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___timestamp'}=time;       
  761: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___stdno'}=$StdNo;
  762: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___avetries'}=$Average;
  763:    
  764: #-------------------------------- Compute percentage of Wrong tries
  765: 	    my $Wrong = ( $StdNo ) ? 100 * ( $Incorrect / $StdNo ) : 0;
  766: 
  767: #-------------------------------- Compute Standard Deviation
  768: 	    my $StdDev = 0; 
  769: 	    if ( $StdNo > 1 ) {
  770: 		for ( my $n = 0; $n < $StdNo; $n++ ) {
  771: 		    my $Dif = $StdLst[ $n ]-$Average;
  772: 		    $StdDev += $Dif*$Dif;
  773: 		} 
  774: 		$StdDev /= ( $StdNo - 1 );
  775: 		$StdDev = sqrt( $StdDev );
  776: 	    }
  777: 
  778: #-------------------------------- Compute Degree of Difficulty
  779: 	    my $DoDiff = 0;
  780: 	    if( $TotalTries > 0 ) {
  781: 		$DoDiff = 1 - ( ( $YES + $Override ) / $TotalTries );
  782: #	    $DoDiff =  ($TotalTries)/($YES + $Override+ 0.1);	    
  783: 	    }
  784:        
  785: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___difficulty'}=$DoDiff;
  786: 
  787: #-------------------------------- Compute the Skewness
  788: 	    my $Skewness = 0;
  789: 	    my $Sum = 0; 
  790: 	    if ( $StdNo > 0 && $StdDev > 0 ) {
  791: 		for ( my $n = 0; $n < $StdNo; $n++ ) {
  792: 		    my $Dif = $StdLst[ $n ]-$Average;
  793: 		    $Skewness += $Dif*$Dif*$Dif;
  794: 		} 
  795: 		$Skewness /= $StdNo;
  796: 	    $Skewness /= $StdDev*$StdDev*$StdDev;
  797: 	    }
  798: 
  799: #--------------------- Compute the Discrimination Factors
  800: 	    my ($Up1,$Up2)=split(/\:/,$DisUp{$lpr[$i]});
  801: 	    my ($Lw1,$Lw2)=split(/\:/,$DisLow{$lpr[$i]});
  802: 	    my $Dis1 = $Up1 - $Lw1;
  803: 	    my $Dis2 = $Up2 - $Lw2;
  804: 	    my $_D1 = sprintf("%.2f", $Dis1);
  805: 	    my $_D2 = sprintf("%.2f", $Dis2);
  806: 
  807: #-----------------  Some restition in presenting the float numbers
  808: 	    my $Avg = sprintf( "%.2f", $Average );
  809: 	    my $Wrng = sprintf( "%.1f", $Wrong );
  810: 	    my $SD = sprintf( "%.1f", $StdDev );
  811: 	    my $DoD = sprintf( "%.2f", $DoDiff );
  812: 	    my $Sk = sprintf( "%.1f", $Skewness );
  813: 	    my $join = $PrOrd.':'.$Temp.':'.$StdNo.':'.
  814:                        $TotalTries.':'.$MxTries.':'.$Avg.':'.
  815:                        $YES.':'.$Override.':'.$Wrng.':'.$DoD.':'.
  816: 		       $SD.':'.$Sk.':'.$_D1.':'.$_D2.':'.$Prob;
  817: 	    $CachData{($p_count-1)}=$join;
  818: 
  819: 	    $urlres=~/^(\w+)\/(\w+)/;
  820: 	    if ($StdNo) { 
  821: 		&Apache::lonnet::put('resevaldata',\%storestats,$1,$2); 
  822: 	    }
  823: #-------------------------------- Row of statistical table
  824: 	    if ( $DiscFlag == 0 ) {
  825: 		&TableRow($join,$i,($p_count-1));
  826: 	    } 
  827: 	}
  828: 	&CloseTable();
  829:     }
  830: #--------------------- close Progress Line
  831:     $r->print('<script>popwin.close()</script>');
  832:     $r->rflush(); 
  833: }
  834: 
  835: sub Cache_Statistics {
  836:     my @list = ();
  837:     my $Useful;
  838:     my $UnUseful;
  839:     my %myHeader = reverse( %Header );
  840:     $Pos = $myHeader{$ENV{'form.sort'}};
  841:     if ($Pos > 0) {$Pos++;}
  842:     $p_count = 0;
  843:     foreach my $key( keys %CachData) { 
  844: 	my @Temp=split(/\:/,$CachData{$key});
  845: 	if ( $Pos == 0 ) {
  846: 	    ($UnUseful,$Useful)=split(/\>/,$Temp[$Pos]);
  847: 	}
  848: 	else {
  849: 	    $Useful = $Temp[$Pos];
  850: 	}   
  851: 	$list[$p_count]=$Useful.'&'.$CachData{$key};
  852:         $p_count++;
  853:     }
  854: 
  855:     @list = sort MySort (@list);
  856: 
  857:     my $nIdx=0;
  858: 
  859:     if ( $Pos == 0 ) {
  860: 	foreach (sort keys %mapsort) {
  861: 	    my ($Hid,$pr)=split(/\:/,$mapsort{$_});
  862: 	    &CreateTable(1,$Hid);
  863: 	    my @lpr=split(/\&/,$pr);
  864: 	    for (my $i=1; $i<=$#lpr; $i++) {
  865: 		my($Pre, $Post) = split(/\&/,$list[$nIdx]); 
  866: 		&TableRow($Post,$i,$nIdx);
  867: 		$nIdx++;
  868: 	    }
  869: 	    &CloseTable();
  870: 	}
  871:     }
  872:     else {
  873: 	&CreateTable(0);
  874: 	for ( my $nIdx = 0; $nIdx < $p_count; $nIdx++ ) {
  875: 	    my($Pre, $Post) = split(/\&/,$list[$nIdx]); 
  876: 	    &TableRow($Post,$nIdx,$nIdx);
  877: 	} 
  878: 	&CloseTable();
  879:     }
  880: }
  881: 
  882: sub TableRow {
  883:     my ($Str,$Idx,$RealIdx)=@_;
  884:     my($PrOrd,$Temp,$StdNo,$TotalTries,$MxTries,$Avg,$YES,
  885:        $Override,$Wrng,$DoD,$SD,$Sk,$_D1,$_D2,$Prob)=split(/\:/,$Str);	
  886:     $r->print( "\n".'<tr>'.
  887:                "\n".'<td>'.($RealIdx+1).'</td>'.
  888:                "\n".'<td>'.$Temp.'</td>'.
  889:                "\n".'<td bgcolor="#EEFFCC"> '.$StdNo.'</td>'.
  890:                "\n".'<td bgcolor="#EEFFCC">'.$TotalTries.'</td>'.
  891:                "\n".'<td bgcolor="#EEFFCC">'.$MxTries.'</td>'.
  892:                "\n".'<td bgcolor="#DDFFFF">'.$Avg.'</td>'.
  893:                "\n".'<td bgcolor="#DDFFFF"> '.$YES.'</td>'.
  894:                "\n".'<td bgcolor="#DDFFFF"> '.$Override.'</td>'.
  895:                "\n".'<td bgcolor="#FFDDDD"> '.$Wrng.'</td>'.
  896:                "\n".'<td bgcolor="#FFDDDD">'.$DoD.'</td>'.
  897:                "\n".'<td bgcolor="#DDFFDD"> '.$SD.'</td>'.
  898:                "\n".'<td bgcolor="#DDFFDD"> '.$Sk.'</td>'.
  899:                "\n".'<td bgcolor="#FFDDFF"> '.$_D1.'</td>'.
  900:                "\n".'<td bgcolor="#FFDDFF"> '.$_D2.'</td>'.
  901:                "\n".'</tr>' );
  902:     $GraphDat{$RealIdx}=$DoD.':'.$Wrng;
  903: }
  904: 
  905: # ------------------------------------------- Prepare data for Graphical chart
  906: 
  907: sub GetGraphData {
  908:     my $Tag = shift;
  909:     my $Col;
  910:     my $data='';
  911:     my $count = 0;
  912:     my $Max = 0;
  913:     my $cid=$ENV{'request.course.id'};
  914:     my $GraphDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
  915:                   "_$ENV{'user.domain'}_$cid\_graph.db";
  916:     foreach (keys %GraphDat) {delete $GraphDat{$_};}
  917:     if (-e "$GraphDB") {
  918: 	if (tie(%GraphDat,'GDBM_File',"$GraphDB",&GDBM_READER,0640)) {
  919: 	    if ( $Tag eq 'DoDiff Graph' ) {
  920: 		$Tag = 'Degree-of-Difficulty';
  921: 		$Col = 0;
  922: 	    }
  923: 	    else {
  924: 		$Tag = 'Wrong-Percentage';
  925: 		$Col = 1;
  926: 	    }
  927: 	    foreach (sort NumericSort keys %GraphDat) { 
  928: 		my @Temp=split(/\:/,$GraphDat{$_});
  929:                 my $inf = $Temp[$Col]; 
  930: 		if ( $Max < $inf ) {$Max = $inf;}
  931: 		$data .= $inf.',';
  932: 		$count++;
  933: 	    }
  934:             untie(%GraphDat);
  935: 	    my $Course = $ENV{'course.'.$cid.'.description'};
  936: 	    $Course =~ s/\ /"_"/eg;
  937: 	    $GData=$Course.'&'.$Tag.'&'.$Max.'&'.$count.'&'.$data;
  938: 
  939: 	}
  940: 	else {
  941: 	    $r->print("Unable to tie hash to db file");
  942: 	}
  943:     }
  944: }
  945: 
  946: 
  947: sub initial {
  948: # --------------------------------- Initialize the global varaibles
  949:   undef @students;
  950:   undef @cols;
  951:   undef %maps;
  952:   undef %section;
  953:   undef %StuBox;
  954:   undef @list;
  955:   undef %CachData;
  956:   undef %GraphDat;
  957:   undef %DiscFac;
  958:   undef $CurMap;
  959:   undef $CurSec;
  960:   undef $CurStu;
  961:   undef $p_count;
  962:   undef $Pos;
  963:   undef $GData;
  964:   $DiscFlag=0; 
  965:   $P_Order=100000;
  966:   $HWN=$P_Order;
  967: }
  968: 
  969: 
  970: sub ClassList {
  971: 
  972:     &GetStatus();
  973: 
  974:     $cid=$ENV{'request.course.id'};
  975:     my $chome=$ENV{'course.'.$cid.'.home'};
  976:     my ($cdom,$cnum)=split(/\_/,$cid);
  977: # ----------------------- Get first and last resource, see if there is anything
  978:     $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
  979:     $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
  980:     if (($firstres) && ($lastres)) {
  981: # ----------------------------------------------------------------- Render page
  982: 	my $classlst=&Apache::lonnet::reply
  983:                  ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
  984: 	my $StudNo = 0;
  985: 	my $now=time;
  986: 	unless ($classlst=~/^error\:/) {
  987: 	    foreach (sort split(/\&/,$classlst)) {
  988: 		my ($name,$value)=split(/\=/,$_);
  989: 		my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
  990: 		$name=&Apache::lonnet::unescape($name);
  991: 		my ($sname,$sdom)=split(/\:/,$name);
  992: 		my $active=1;
  993: 		my $Status=$ENV{'form.status'};
  994: 		$Status = ($Status) ? $Status : 'Active';
  995: 		if ( ( ($end) && $now > $end ) && 
  996:                      ( ($Status eq 'Active') ) ) { $active=0; }
  997: 		if ( ($Status eq 'Expired') && 
  998:                      ($end == 0 || $now < $end) ) { $active=0; }
  999: 		if ($active) {
 1000: 		    my $thisindex=$#students+1;
 1001: 		    $name=&Apache::lonnet::unescape($name);
 1002: 		    $students[$thisindex]=$name;
 1003: 		    my ($sname,$sdom)=split(/\:/,$name);
 1004: 		    #my %reply=&Apache::lonnet::idrget($sdom,$sname);
 1005: 		    #my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
 1006: 		    #         ':environment:lastname&generation&firstname&middlename',
 1007: 		    #         &Apache::lonnet::homeserver($sname,$sdom));
 1008: 		    my $ssec=&usection($sdom,$sname,$cid,$Status);
 1009: 		    if ($ssec==-1) {next;}
 1010: 		    $ssec=($ssec) ? $ssec : '(none)';
 1011: 		    #$ssec=(int($ssec)) ? int($ssec) : $ssec;
 1012: 		    $section{$ssec}=$ssec;
 1013: 		    if ($CurSec eq 'All Sections' || $ssec eq $CurSec) {
 1014: 			$students[$StudNo]=$name;
 1015: 			$StuBox{$sname}=$sdom;
 1016: 		    }
 1017: 		    $StudNo++;
 1018: 	        }
 1019: 	    }
 1020: 	}
 1021: 	else {
 1022: 	    $r->print('<h1>Could not access course data</h1>');
 1023: 	} 
 1024:         $r->print("Total number of students : ".($#students+1));
 1025:         $r->rflush();
 1026: # --------------- Find all assessments and put them into some linear-like order
 1027: 	&tracetable($firstres,'&'.$lastres.'&');
 1028: #    my $c=0;
 1029: #    foreach (sort keys %mapsort) {
 1030: #	$r->print('<br>'.$c.')'.$_.' ... '.$mapsort{$_});
 1031: #	$c++;
 1032: #    }
 1033:     }
 1034: 
 1035: # ------------------------------------------------------------- End render page 
 1036:     else {
 1037: 	$r->print('<h3>Undefined course sequence</h3>');
 1038:     }
 1039:     &MapSecOptions();
 1040: }
 1041: 
 1042: 
 1043: sub Menu {
 1044:     my $InpStr = $ENV{'form.sort'};
 1045:     if ( $InpStr eq 'DoDiff Graph' || $InpStr eq '%Wrong Graph' ) {      
 1046: 	&GetGraphData($InpStr);
 1047:     	$r->print('<IMG src="/cgi-bin/graph.gif?'.$GData.'" />');
 1048:     }
 1049:     else {
 1050: 	$r->print('<html><head><title>LON-CAPA Statistics</title></head>');
 1051: 
 1052: 
 1053:         $r->print('<body bgcolor="#FFFFFF">'.
 1054:                   '<script>window.focus(); window.width=500;window.height=500; </script>'.
 1055:                   '<img align=right src=/adm/lonIcons/lonlogos.gif>');
 1056: # ---------------------------------------------------------------- Course title
 1057:         $r->print('<h1> Course : "'.
 1058:                   $ENV{'course.'.$ENV{'request.course.id'}.
 1059:                   '.description'}.'"</h1><h2>'.localtime().'</h2>');
 1060: # ------------------------------- This is going to take a while, produce output
 1061:         $r->rflush();	
 1062: 
 1063: 	$r->print("\n".'<form name=stat method=post action="/adm/statistics" >');
 1064: 
 1065: 	my $content = $ENV{'form.sort'};
 1066: 	if ($content eq '' || $content eq 'Return to Menu') {
 1067: 	    my $Ptr = '<h3>';
 1068: 	    $Ptr .= '<input type=submit name=sort value="Problem Evaluation"/>';
 1069: 	    $Ptr .= '<br><br>';
 1070: 	    $Ptr .= '<input type=submit name=sort value="Student Assessment"/>';
 1071: 	    $Ptr .= '</h3>';
 1072: 	    $r->print( $Ptr );
 1073:       	}
 1074: 	else {
 1075: 	    &initial();
 1076: 	    &ClassList();
 1077: 	    if ( $content eq 'Student Assessment' || 
 1078:                     $content eq 'Create Student Report' ) {
 1079: 		&StudentOptions();
 1080: 		&StudentReport($CurStu,$StuBox{"$CurStu"});
 1081: 	    }
 1082: 	    else {
 1083: 		&PreStatTable();
 1084: 	    }
 1085: 	}
 1086: 	$r->print("\n".'</form>'.
 1087:                   "\n".'</body>'.
 1088:                   "\n".'</html>');
 1089: 	$r->rflush();
 1090:     }
 1091: }
 1092: 
 1093: sub StudentOptions {
 1094:     my $OpSel5='';
 1095:     $CurStu = $ENV{'form.student'};
 1096:     if ( $CurStu eq '' ) { 
 1097:         $CurStu = 'All Students';
 1098:         $OpSel5 = 'selected';
 1099:     }
 1100:     my $Ptr ='';
 1101: # ----------------------------------- Loading the Students Combobox
 1102:     $Ptr .= '<br><b>Select Student</b>'."\n".
 1103:        	    '<select name="student">'."\n". 
 1104: 	    '<option '.$OpSel5.'>All Students</option>';                     	     	     
 1105:     foreach my $key ( sort keys %StuBox ) {	          
 1106: 	$Ptr .= '<option';
 1107: 	if ($CurStu eq $key) {$Ptr .= ' selected';}     
 1108:         $Ptr .= '>'.$key."</option>\n";	     
 1109:     }
 1110:     $Ptr .= '</select>';
 1111:     $Ptr .= '<br><input type="submit" name="sort" value="Create Student Report" />';
 1112:     $r->print( $Ptr );
 1113:     $r->rflush();	
 1114: }
 1115: 
 1116: sub GetStatus {
 1117:     $OpSel1='';
 1118:     $OpSel2='';
 1119:     $OpSel3='';
 1120:     $OpSel4='';
 1121: 
 1122:     if ( $ENV{'form.order'} eq 'Descending' ) { $OpSel2='selected'; }
 1123:     else { $OpSel1 = 'selected'; }
 1124: 
 1125:     my %myHeader = reverse( %Header );
 1126:     $Pos = $myHeader{$ENV{'form.sort'}};
 1127:     if ($Pos == 0) {
 1128: 	$OpSel1 = 'selected';
 1129: 	$ENV{'form.order'}='Ascendig';
 1130:     }
 1131: 
 1132:     $CurMap = $ENV{'form.maps'};
 1133:     if ( $CurMap eq '' ) { 
 1134: 	$CurMap = 'All Maps';
 1135: 	$OpSel3 = 'selected';
 1136:     }
 1137:     $CurSec = $ENV{'form.section'};
 1138:     if ( $CurSec eq '' ) { 
 1139: 	$CurSec = 'All Sections';
 1140:         $OpSel4 = 'selected';
 1141:     }
 1142: }
 1143: 
 1144: 
 1145: sub MapSecOptions {
 1146: # ----------------------------------- Loading the Maps Combobox
 1147:     my $Ptr = '<br>';
 1148:     $Ptr .= '<br><input type="submit" name="sort" value="Return to Menu" />';
 1149:     $Ptr .= '<br><b> Select &nbsp; Map &nbsp; &nbsp; </b>'."\n".
 1150:        	    '<select name="maps">'."\n". 
 1151: 	    '<option '.$OpSel3.'>All Maps</option>';                     	     	     
 1152:     foreach my $key ( sort keys %maps ) {	          
 1153: 	$Ptr .= '<option';
 1154:         if ($CurMap eq $key) {$Ptr .= ' selected';}	     
 1155: 	$Ptr .= '>'.$key."</option>\n";	     
 1156:     }
 1157:     $Ptr .= '</select>';
 1158:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
 1159: 
 1160: # ----------------------------------- Loading the Sections Combobox
 1161:     $Ptr .= '<br><b>Select Section</b>'."\n".
 1162:        	    '<select name="section">'."\n". 
 1163: 	    '<option '.$OpSel4.'>All Sections</option>';                     	     	     
 1164:     foreach my $key ( sort keys %section ) {	          
 1165: 	$Ptr .= '<option';
 1166:         if ($CurSec eq $key) {$Ptr .= ' selected';}     
 1167: 	$Ptr .= '>'.$key."</option>"."\n";	     
 1168:     }
 1169:     $Ptr .= '</select>'."\n";
 1170: 
 1171:     $r->print( $Ptr );
 1172:     $r->rflush();
 1173: }
 1174: 
 1175: 
 1176: # ================================================================ Main Handler
 1177: 
 1178: sub handler {
 1179:     $r=shift;
 1180: 
 1181:     if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
 1182: # ------------------------------------------- Set document type for header only
 1183: 	if ($r->header_only) {
 1184: 	    if ($ENV{'browser.mathml'}) {
 1185: 		$r->content_type('text/xml');
 1186: 	    } 
 1187: 	    else {
 1188: 		$r->content_type('text/html');
 1189: 	    }
 1190: 	    $r->send_http_header;
 1191: 	    return OK;
 1192: 	}    
 1193: 	my $requrl=$r->uri;
 1194: # ----------------------------------------------------------------- Tie db file
 1195: 
 1196: 	undef %hash;
 1197: 
 1198: 	if ($ENV{'request.course.fn'}) {
 1199: 	    my $fn=$ENV{'request.course.fn'};
 1200: 	    if (-e "$fn.db") {
 1201: 		if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
 1202: # ------------------------------------------------------------------- Hash tied
 1203: 		    $r->content_type('text/html');
 1204: 		    $r->send_http_header;
 1205: 		    &Menu();
 1206: 		}
 1207: 		else {
 1208: 		    $r->content_type('text/html');
 1209: 		    $r->send_http_header;
 1210: 		    $r->print('<html><body>Coursemap undefined.</body></html>');
 1211: 		}
 1212: # ------------------------------------------------------------------ Untie hash
 1213: 		unless (untie(%hash)) {
 1214: 		    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
 1215:                             "Could not untie coursemap $fn (browse).</font>"); 
 1216: 		}
 1217: 
 1218: # -------------------------------------------------------------------- All done
 1219: 		return OK;
 1220: # ----------------------------------------------- Errors, hash could no be tied
 1221: 	    }
 1222: 	} 
 1223: 	else {
 1224: 	    $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
 1225: 	    return HTTP_NOT_ACCEPTABLE; 
 1226: 	}
 1227:     }
 1228:     else {
 1229:         $ENV{'user.error.msg'}=
 1230:         $r->uri.":vgr:0:0:Cannot view grades for complete course";
 1231: 
 1232:         return HTTP_NOT_ACCEPTABLE; 
 1233:     }
 1234: }
 1235: 1;
 1236: __END__
 1237: 
 1238: 
 1239: 

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