File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.8: download - view: text, annotated - select for diffs
Wed Mar 6 23:56:13 2002 UTC (22 years, 4 months ago) by minaeibi
Branches: MAIN
CVS tags: HEAD
Added 2 criteria for discimination Factor

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

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