File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.7: download - view: text, annotated - select for diffs
Wed Mar 6 18:42:01 2002 UTC (22 years, 4 months ago) by minaeibi
Branches: MAIN
CVS tags: HEAD
Added Discrimination Factor

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

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