File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.18: download - view: text, annotated - select for diffs
Tue May 14 23:17:06 2002 UTC (22 years ago) by minaeibi
Branches: MAIN
CVS tags: HEAD
Fixed bugs in option response problem analysis (thanks to Guy)
Showed the results of option leaves in cute tables.

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: lonstatistics.pm,v 1.18 2002/05/14 23:17:06 minaeibi Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (Navigate problems for statistical reports
   29: # YEAR=2001
   30: # 5/5,7/9,7/25/1,8/11,9/13,9/26,10/5,10/9,10/22,10/26 Behrouz Minaei
   31: # 11/1,11/4,11/16,12/14,12/16,12/18,12/20,12/31 Behrouz Minaei
   32: # YEAR=2002
   33: # 1/22,2/1,2/6,2/25,3/2,3/6,3/17,3/21,3/22,3/26,4/7,5/6 Behrouz Minaei
   34: # 5/12, 5/14 Behrouz Minaei
   35: #
   36: ###
   37: 
   38: package Apache::lonstatistics; 
   39: 
   40: use strict; 
   41: use Apache::Constants qw(:common :http);
   42: use Apache::lonnet();
   43: use Apache::lonhomework;
   44: use Apache::loncommon;
   45: use HTML::TokeParser;
   46: use GDBM_File;
   47: 
   48: # -------------------------------------------------------------- Module Globals
   49: my %hash;
   50: my %CachData;
   51: my %GraphDat;
   52: my %OpResp;
   53: my %maps;
   54: my %mapsort;
   55: my %section;
   56: my %StuBox;
   57: my %DiscFac;
   58: my %DisUp;
   59: my %DisLow;
   60: my $UpCnt;
   61: my $CurMap;
   62: my $CurSec;
   63: my $CurStu;
   64: my @cols;
   65: my @list;
   66: my @students;
   67: my $p_count;
   68: my $Pos;
   69: my $r;
   70: my $OpSel1;
   71: my $OpSel2;
   72: my $OpSel3;
   73: my $OpSel4;
   74: my $GData;
   75: my $cid;
   76: my $firstres;
   77: my $lastres;
   78: my $DiscFlag;
   79: my $HWN;
   80: my $P_Order;
   81: my %foil_to_concept;
   82: my @Concepts;
   83: my %ConceptData;
   84: my %Header = (0,"Homework Sets Order",1,"#Stdnts",2,"Tries",3,"Mod",
   85:               4,"Mean",5,"#YES",6,"#yes",7,"%Wrng",8,"DoDiff",
   86:               9,"S.D.",10,"Skew.",11,"D.F.1st",12,"D.F.2nd");
   87: my %Answer = ();
   88: 
   89: sub InitAnalysis {
   90:     my ($rid, $student)=@_;
   91:     my ($uname,$udom)=split(/\:/,$student);
   92:     $rid=~/(\d+)\.(\d+)/;
   93:     my $symb=&Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.
   94: 	     &Apache::lonnet::declutter($hash{'src_'.$rid});
   95:     my $URI = $hash{'src_'.$rid};
   96:     my $Answ=&Apache::lonnet::ssi($URI,('grade_target' => 'analyze',
   97:                                   'grade_username' => $uname,
   98:                                   'grade_domain' => $udom,
   99:                                   'grade_courseid' => $cid,
  100:                                   'grade_symb' => $symb));
  101: #    my $Answ=&Apache::lonnet::ssi($URI,('grade_target' => 'analyze'));
  102: 
  103:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
  104:     %Answer=();
  105:     %Answer=&Apache::lonnet::str2hash($Answ);
  106: 
  107:     my $parts='';
  108:     foreach my $elm (@{$Answer{"parts"}}) {
  109: 	$parts.="$elm,";
  110:     }
  111:     chop($parts);
  112:     my $conc='';
  113:     foreach my $elm (@{$Answer{"$parts.concepts"}}) {
  114: 	$conc.="$elm@";
  115:     }
  116:     chop($conc);
  117: 
  118:     @Concepts=split(/\@/,$conc);
  119:     foreach my $concept (@{$Answer{"$parts.concepts"}}) {
  120: 	foreach my $foil (@{$Answer{"$parts.concept.$concept"}}) {
  121: 	    $foil_to_concept{$foil} = $concept;
  122: 	}
  123:     }
  124:     return $symb;
  125: }
  126: 
  127: 
  128: sub Interval {
  129:     my ($rid,$part,$symb)=@_;
  130:     my $Int=$ConceptData{"Interval"};
  131:     my $due = &Apache::lonnet::EXT('resource.$part.duedate',$symb)+1;
  132:     my $opn = &Apache::lonnet::EXT('resource.$part.opendate',$symb);
  133:     my $add=int(($due-$opn)/$Int);
  134:     $ConceptData{"Int.0"}=$opn;
  135:     for (my $i=1;$i<$Int;$i++) {
  136: 	$ConceptData{"Int.$i"}=$opn+$i*$add;
  137:     }
  138:     $ConceptData{"Int.$Int"}=$due;     
  139:     for (my $i=0;$i<$Int;$i++) {
  140: 	for (my $n=0; $n<=$#Concepts; $n++ ) {
  141: 	    my $tmp=$Concepts[$n];
  142: 	    $ConceptData{"$tmp.$i.true"}=0;
  143: 	    $ConceptData{"$tmp.$i.false"}=0;
  144: 	}
  145:     }
  146: }
  147: 
  148: 
  149: sub ShowOpGraph {
  150:     my ($InpStr, $Int_No)=@_;
  151:     my ($rid,$part)=split(/\:/,substr($InpStr,8));
  152:     $ConceptData{"Interval"}=$Int_No;
  153:     my $symb=&InitAnalysis($rid,$students[0]);
  154:     &Interval($rid,$part,$symb);
  155:     my $URI = $hash{'src_'.$rid};
  156:     my $Src = $hash{'title_'.$rid};
  157:     $Src =~ s/\ /"_"/eg;
  158:     $r->print('<br><b>'.$URI.'</b>');
  159:     $r->rflush();
  160:     
  161:     &Create_PrgWin();
  162:     &Update_PrgWin("Starting to analyze problem");
  163:     for (my $index=0;$index<=$#students;$index++) {
  164: 	&Update_PrgWin($index);
  165: 	&OpStatus($rid,$students[$index]);
  166:     }
  167:     &Close_PrgWin();
  168: 
  169:     for (my $k=0; $k<$Int_No; $k++ ) {
  170: 	&DrawGraph($k,$Src);
  171:     }
  172: #$Apache::lonxml::debug=1;
  173: #&Apache::lonhomework::showhash(%ConceptData);
  174: #$Apache::lonxml::debug=0;
  175:     my $Answ=&Apache::lonnet::ssi($URI);
  176:     $r->print("<br><b>Here you can see the Problem:</b><br>$Answ");
  177: }
  178: 
  179: 
  180: sub DrawGraph {
  181:     my ($k,$Src)=@_;
  182:     my $Max=0;
  183:     my @data1;
  184:     my @data2;
  185:     my $Correct=0;
  186:     my $Wrong=0;
  187:     # Adjust Data and find the Max 
  188:     for (my $n=0; $n<=$#Concepts; $n++ ) {
  189: 	my $tmp=$Concepts[$n];
  190: 	$data1[$n]=$ConceptData{"$tmp.$k.true"};
  191: 	$Correct+=$data1[$n];
  192: 	$data2[$n]=$ConceptData{"$tmp.$k.false"};
  193: 	$Wrong+=$data2[$n];
  194: 	my $Sum=$data1[$n]+$data2[$n];
  195: 	if ( $Max<$Sum ) {$Max=$Sum;}
  196:     }
  197:     for (my $n=0; $n<=$#Concepts; $n++ ) {
  198: 	if ($data1[$n]+$data2[$n]<$Max) {
  199: 	    $data2[$n]+=$Max-($data1[$n]+$data2[$n]);
  200: 	}
  201:     }
  202:     my $P_No = $#data1+1;
  203: #    $r->print('<br><b>From: ['.localtime($ConceptData{'Int.'.($k-1)}).
  204: #              '] To: ['.localtime($ConceptData{"Int.$k"}).']</b>'); 
  205:     my $Str = "\n".'<table border=2>'.
  206:               "\n".'<tr>'.
  207:               "\n".'<th> # </th>'.
  208: 	      "\n".'<th> Concept </th>'.
  209: 	      "\n".'<th> Correct </th>'.
  210: 	      "\n".'<th> Wrong </th>'.
  211: 	      "\n".'</tr>';
  212: 
  213:     for (my $n=0; $n<=$#Concepts; $n++ ) {
  214: 	$Str .= "\n"."<tr>".
  215: 	        "\n"."<td>".($n+1)."</td>".
  216:                 "\n"."<td bgcolor=#FFFFDD> ".$Concepts[$n]." </td>".
  217:                 "\n"."<td bgcolor=#DDFFDD> ".$data1[$n]." </td>".
  218:                 "\n"."<td bgcolor=#FFDDDD> ".$data2[$n]." </td>".
  219:                 "\n"."</tr>";
  220:     }
  221:     $Str.='<td></td><td><b>From:['.localtime($ConceptData{'Int.'.($k-1)}).
  222:           '] To: ['.localtime($ConceptData{"Int.$k"}).
  223:           "]</b></td><td>$Correct</td><td>$Wrong</td>";
  224: 
  225:     $Str .= "\n".'</table>';
  226: 
  227:     $r->print($Str);
  228: 
  229:     if ( $Max > 1 ) { 
  230: 	$Max += (10 - $Max % 10);
  231: 	$Max = int($Max);
  232:     } else { $Max = 1; }
  233: 
  234:     if ( $Max > 1 ) { 
  235: 	$Max += (10 - $Max % 10);
  236: 	$Max = int($Max);
  237:     } else { $Max = 1; }
  238:     my $Titr=($ConceptData{'Interval'}>1) ? $Src.'_interval_'.$k : $Src;
  239: #    $GData=$Titr.'&Concepts'.'&'.'Answers'.'&'.$Max.'&'.$P_No.'&'.$data1.'&'.$data2;
  240:     $GData="$Titr&Concepts&Answers&$Max&$P_No&".
  241:            (join(',',@data1)).'&'.(join(',',@data2));
  242: 
  243:     $r->print('<br><IMG src="/cgi-bin/graph.gif?'.$GData.'" />');
  244: }
  245: 
  246: 
  247: sub AnalyzeProblem {
  248: # -------------------------------- Selecting the number of intervals
  249:     my $OpSel='';
  250:     my $CurInt = $ENV{'form.interval'};
  251:     if ($CurInt eq '') {$CurMap = '1';}
  252:     my $Ptr = '<b>Select number of intervals</b>'."\n".
  253:        	      '<select name="interval">'."\n";                     	     	     
  254:     for (my $n=1;$n<=7;$n++) {	          
  255: 	$Ptr .= '<option';
  256:         if ($CurInt eq $n) {$Ptr .= ' selected';}     
  257: 	$Ptr .= '>'.$n."</option>"."\n";	     
  258:     }
  259:     $Ptr .= '</select>'."\n";
  260:     $r->print( $Ptr );
  261: 
  262:     $r->print('<br><b> Option Response Problems in this course:</b><br><br>');
  263:     my $Str = "\n".'<table border=2>'.
  264:               "\n".'<tr>'.
  265:               "\n".'<th> # </th>'.
  266: 	      "\n".'<th> Problem Title </th>'.
  267: 	      "\n".'<th> Resouse </th>'.
  268: 	      "\n".'<th> Address </th>'.
  269: 	      "\n".'</tr>';
  270: 
  271:      my $P_No=1;
  272:      foreach (sort keys %OpResp) {
  273: 	 my ($rid,$part)=split(/\:/,$OpResp{$_});
  274: 	 my $Temp = '<a href="'.$hash{'src_'.$rid}.
  275:                     '" target="_blank">'.$hash{'title_'.$rid}.'</a>';
  276: 	 $Str .= "\n"."<tr>".
  277: 	         "\n"."<td> $P_No </td>".
  278:                  "\n"."<td bgcolor=#DDFFDD> ".$Temp." </td>".
  279:                  "\n"."<td bgcolor=#EEFFCC> ".$hash{'src_'.$rid}." </td>".
  280: 	         "\n"."<td> ".'<input type="submit" name="sort" value="'.'Analyze_'.$rid.'" />'.'</td>'.
  281:                  "\n"."</tr>";
  282: 	 $P_No++;
  283:      }
  284:      $Str .= "\n".'</table>';
  285:      $Str .= "\n".'</form>';
  286:      $r->print($Str);
  287:      $r->rflush();	
  288: }
  289: 
  290: 
  291: sub Decide {
  292:     my ($type,$foil,$time)=@_; 
  293:     my $k=0;
  294:     while ($time>$ConceptData{'Int.'.($k+1)} && 
  295:            $k<$ConceptData{'Interval'}) {$k++;}
  296:     $ConceptData{"$foil_to_concept{$foil}.$k.$type"}++;
  297: }
  298: 
  299: 
  300: sub OpStatus {
  301:     my ($rid,$student)=@_;
  302:     my ($uname,$udom)=split(/\:/,$student);
  303:     my $code='U';
  304:     $rid=~/(\d+)\.(\d+)/;
  305:     my $symb=&Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.
  306: 	     &Apache::lonnet::declutter($hash{'src_'.$rid});
  307:     my %reshash=&Apache::lonnet::restore($symb,$cid,$udom,$uname);
  308:     my @True = ();
  309:     my @False = ();
  310:     my $flag=0;
  311:     if ($reshash{'version'}) {
  312:         my $tries=0;
  313: 	&Apache::lonhomework::showhash(%Answer);
  314: 	for (my $version=1;$version<=$reshash{'version'};$version++) {
  315: 	    my $time=$reshash{"$version:timestamp"};
  316: 	   
  317: 	    foreach my $key (sort(split(/\:/,$reshash{$version.':keys'}))) {
  318: 		if (($key=~/\.(\w+)\.(\w+)\.submission$/)) {
  319: 		    my $Id1 = $1; my $Id2 = $2;
  320: 		    #check if this is a repeat submission, if so skip it
  321:           	    if ($reshash{"$version:resource.$Id1.previous"}) { next; }
  322: 		    #if no solved this wasn't a real submission, ignore it
  323: 		    if (!defined($reshash{"$version:resource.$Id1.solved"})) {
  324: 			&Apache::lonxml::debug("skipping ");
  325: 			next;
  326: 		    }
  327: 		    my $Resp = $reshash{"$version:$key"};
  328: 		    my %submission=&Apache::lonnet::str2hash($Resp);
  329: 		    foreach (keys %submission) {
  330: 			my $Ansr = $Answer{"$Id1.$Id2.foil.value.$_"};
  331: 			if ($submission{$_}) {
  332: 			    if ($submission{$_} eq $Ansr) {
  333: 				&Decide("true",$_,$time );
  334: 			    }
  335: 			    else {&Decide("false",$_,$time );}
  336: 			}
  337: 		    }
  338: 	        }	  
  339: 	    }
  340:         }
  341:     }
  342: }
  343: 
  344: 
  345: #------- Processing upperlist and lowerlist according to each problem
  346: sub ProcessDisc {
  347:     my @List = @_;
  348:     @List = sort (@List);
  349:     my $Count = $#List+1;
  350:     my $Prb;
  351:     my @Dis;
  352:     my $Slvd=0;
  353:     my $tmp;
  354:     my $Sum1=0;
  355:     my $Sum2=0;
  356:     my $nIdx=0;
  357:     my $nStud=0;
  358:     my %Proc;
  359:     undef %Proc;
  360:     while ($nIdx<$Count) {
  361: 	($Prb,$tmp)=split(/\=/,$List[$nIdx]);
  362: 	@Dis=split(/\+/,$tmp);
  363: 	my $Temp = $Prb;
  364: 	do {
  365: 	    $nIdx++;
  366: 	    $nStud++;
  367: 	    $Sum1 += $Dis[0];
  368: 	    $Sum2 += $Dis[1];
  369: 	    ($Prb,$tmp)=split(/\=/,$List[$nIdx]);
  370: 	    @Dis=split(/\+/,$tmp);
  371: 	} while ( $Prb eq $Temp && $nIdx < $Count );
  372: #	$Proc{$Temp}=($Sum1/$nStud).':'.$nStud;
  373: 	$Proc{$Temp}=($Sum1/$nStud).':'.($Sum2/$nStud);
  374: #       $r->print("$nIdx) $Temp --> ($nStud) $Proc{$Temp} <br>");
  375: 	$Sum1=0;
  376: 	$Sum2=0;
  377: 	$nStud=0;
  378:     }
  379:     return %Proc;
  380: }
  381: 
  382: 
  383: #------- Creating Discimination factor   
  384: sub Discriminant {
  385:     my $Count=0;
  386:     foreach (keys(%DiscFac)){ 
  387: 	$Count++;
  388:     }
  389:     $UpCnt = int(0.27*$Count);
  390:     my $low=0;
  391:     my $up=$Count-$UpCnt;
  392:     my @UpList=();
  393:     my @LowList=();
  394:     $Count=0;
  395:     foreach my $key (sort(keys(%DiscFac))){ 
  396: 	$Count++;    
  397:         #$r->print("<br>$Count) $key = $DiscFac{$key}");
  398: 	if ($low < $UpCnt || $Count > $up) {
  399: 	    $low++;
  400: 	    my $str=$DiscFac{$key};
  401: 	    foreach(split(/\:/,$str)){
  402: 		if ($_) {
  403: 		    if ($low<$UpCnt){push(@LowList,$_);}
  404: 		    else {push(@UpList,$_);}
  405: 		}
  406: 	    }
  407: 	}
  408:     }
  409:     %DisUp=&ProcessDisc(@UpList);
  410:     %DisLow=&ProcessDisc(@LowList);
  411: }
  412: 
  413:    
  414: sub NumericSort {          
  415:     $a <=> $b;
  416: }
  417: 
  418: # ------ Create different Student Report 
  419: sub StudentReport {
  420:     my ($sname,$sdom)=@_;
  421:     if ( $sname eq 'All Students' ) {
  422: 	$r->print( '<h3><font color=blue>WARNING: 
  423:                     Please select a student</font></h3>' );
  424: 	return;
  425:     }
  426:     my %result = &Apache::lonnet::dump($cid,$sdom,$sname);
  427:     my $ResId;
  428:     my $PrOrd;
  429:     my $Code;
  430:     my $Tries;
  431:     my $TotalTries = 0;
  432:     my $ParCr = 0;
  433:     my $Wrongs;
  434:     my %TempHash;
  435:     my $Version;
  436:     my $LatestVersion;
  437:     my $PtrTry='';
  438:     my $PtrCod='';
  439:     my $SetNo=0;
  440:     my $Str = "\n".'<table border=2>'.
  441:               "\n".'<tr>'.
  442:               "\n".'<th> # </th>'.
  443: 	      "\n".'<th> Set Title </th>'.
  444: 	      "\n".'<th> Results </th>'.
  445: 	      "\n".'<th> Tries </th>'.
  446: 	      "\n".'</tr>';
  447:     my ($temp)=keys(%result);
  448:     unless ($temp=~/^error\:/) {
  449:         foreach my $CurCol (@cols) {
  450: 	    if (!$CurCol){
  451: 		my $Set=&Apache::lonnet::declutter($hash{'map_id_'.$1});
  452: 		if ( $Set ) {
  453: 		    $SetNo++;
  454: 		    $Str .= "\n"."<tr>".
  455: 			    "\n"."<td> $SetNo </td>".
  456:                             "\n"."<td> $Set </td>".
  457:                             "\n"."<td> $PtrCod </td>".
  458:                             "\n"."<td> $PtrTry</td>".
  459:                             "\n"."</tr>";
  460: 		}
  461: 		$PtrTry='';
  462: 		$PtrCod='';
  463: 		next; 
  464: 	    }
  465: 	    ($PrOrd,$ResId)=split(/\:/,$CurCol);
  466:             $ResId=~/(\d+)\.(\d+)/;
  467:             my $Map = &Apache::lonnet::declutter( $hash{'map_id_'.$1} );
  468:             if ( $CurMap ne 'All Maps' ) {
  469: 		my ( $ResMap, $NameMap ) = split(/\=/,$CurMap);
  470: 		if ( $Map ne $ResMap ) { next; }
  471: 	    }
  472: 	    my $meta=$hash{'src_'.$ResId};
  473: 	    my $PartNo = 0;
  474: 	    undef %TempHash;
  475: 	    foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))){
  476: 		if ($_=~/^stores\_(\w+)\_tries$/) {
  477:                     my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
  478: 		    if ( $TempHash{"$Part"} eq '' ) { 
  479: 			$TempHash{"$Part"} = $Part;
  480: 			$TempHash{$PartNo}=$Part;
  481: 			$TempHash{"$Part.Code"} = '-';  
  482: 			$TempHash{"$Part.PrOrd"} = $PrOrd+$PartNo;  
  483: 			$PartNo++;
  484: 		    }
  485: 		}
  486:             }
  487: 
  488:             my $Prob = $Map.'___'.$2.'___'.
  489:                        &Apache::lonnet::declutter( $hash{'src_'.$ResId} );
  490:             $Code='U';
  491:             $Tries = 0;
  492:             $Wrongs = 0;
  493:   	    $LatestVersion = $result{"version:$Prob"};
  494: 	    if ( $LatestVersion ) {
  495: 		for ( my $Version=1; $Version<=$LatestVersion; $Version++ ) {
  496: 		    my $vkeys = $result{"$Version:keys:$Prob"};
  497: 		    my @keys = split(/\:/,$vkeys);		
  498:   
  499: 		    foreach my $Key (@keys) {		  
  500: 			if (($Key=~/\.(\w+)\.solved$/) && ($Key!~/^\d+\:/)) {
  501: 			    my $Part = $1;
  502: 			    $Tries = $result{"$Version:$Prob:resource.$Part.tries"};
  503: 			    $TempHash{"$Part.Tries"} = ($Tries) ? $Tries : 0; 
  504: 			    $TotalTries += $Tries;
  505: 			    my $Val = $result{"$Version:$Prob:resource.$Part.solved"};
  506: 			    if ( $Val eq 'correct_by_student' )
  507:                                 { $Wrongs = $Tries - 1; $Code = 'Y'; } 
  508: 			    elsif ( $Val eq 'correct_by_override' )
  509:                                 { $Wrongs = $Tries - 1; $Code = 'y'; }                        
  510: 			    elsif ( $Val eq 'incorrect_attempted' || 
  511:                                 $Val eq 'incorrect_by_override' )
  512: 		                { $Wrongs = $Tries; $Code = 'N'; }
  513: 			    $TempHash{"$Part.Code"} = $Code;
  514: 			    $TempHash{"$Part.Wrongs"} = $Wrongs;
  515: 			}
  516:      		    }
  517:                 }
  518: 		for ( my $n = 0; $n < $PartNo; $n++ ) {		  
  519: 		    my $part = $TempHash{$n};
  520: 		    if ($PtrTry ne '') {$PtrTry .= ',';}
  521: 		    $PtrTry .= "$TempHash{$part.'.Tries'}";
  522:                     $PtrCod .= "$TempHash{$part.'.Code'}";    
  523: 		}
  524:             }
  525: 	    else { 
  526: 		for(my $n=0; $n<$PartNo; $n++) { 
  527: 		    if ($PtrTry ne '') {$PtrTry .= ',';}
  528: 		    $PtrTry .= "0";
  529:                     $PtrCod .= "-"; 
  530: 		}
  531: 	    }
  532:         }
  533:     }
  534:     $Str .= "\n".'</table>';
  535:     $r->print($Str);
  536:     $r->rflush();
  537: }
  538: 
  539: sub CreateTable {
  540:     my ($Hd, $Hid)=@_;
  541:     if ($ENV{'form.showcsv'}) {
  542: 	if ( $Hd == 1 ) {
  543: 	    $r->print('<br>"'.$hash{'title_'.$Hid}.'","'.$hash{'src_'.$Hid}.'"');
  544: 	}
  545: 	return;
  546:     }
  547:     my $ColNo=0;
  548:     foreach (keys(%Header)){ 
  549: 	$ColNo++;
  550:     } 
  551:     if ( $Hd == 1 ) {
  552: 	$r->print('<br><a href="'.$hash{'src_'.$Hid}.
  553:                   '" target="_blank">'.$hash{'title_'.$Hid}.'</a>');
  554:     }
  555:     my $Result = "\n".'<table border=2>';
  556:     $Result .= '<tr><th>P#</th>'."\n";
  557:     for ( my $nIdx=0; $nIdx < $ColNo; $nIdx++ ) { 
  558: 	$Result .= '<th>'.'<input type="submit" name="sort" value="'.
  559:                    $Header{$nIdx}.'" />'.'</th>'."\n";
  560:     }
  561:     $Result .= "\n".'</tr>'."\n";    
  562:     $r->print( $Result );
  563:     $r->rflush();
  564: }
  565: 
  566: sub CloseTable {
  567:     if ($ENV{'form.showcsv'}) {
  568: 	return;
  569:     }    
  570:     $r->print("\n".'</table>'."\n");
  571:     $r->rflush();
  572: }
  573:  
  574: # ------------------------------------------- Prepare Statistics Table
  575: sub PreStatTable {
  576:     my $CacheDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
  577:                   "_$ENV{'user.domain'}_$cid\_statistics.db";
  578:     my $GraphDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
  579:                   "_$ENV{'user.domain'}_$cid\_graph.db";
  580:     my $OpSel11='';
  581:     my $OpSel12='';
  582:     my $OpSel13='';
  583:     my $Status = $ENV{'form.status'};
  584:     if ( $Status eq 'Any' ) { $OpSel13='selected'; }
  585:     elsif ($Status eq 'Expired' ) { $OpSel12 = 'selected'; }
  586:     else { $OpSel11 = 'selected'; }
  587: 
  588:     my $Ptr = '';
  589:     $Ptr .= '<br><b> Student Status: &nbsp; </b>'."\n".
  590:             '<select name="status">'. 
  591:             '<option '.$OpSel11.' >Active</option>'."\n".
  592:             '<option '.$OpSel12.' >Expired</option>'."\n".
  593: 	    '<option '.$OpSel13.' >Any</option> </select> '."\n";
  594:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  595:     $Ptr .= '<input type=submit name=sort value="Recalculate Statistics"/>'."\n";
  596: 
  597:     $Ptr .= '<br><b> Sorting Type: &nbsp; </b>'."\n".
  598:             '<select name="order"> <option '.$OpSel1.' >Ascending</option>'."\n".
  599: 	    '<option '.$OpSel2.'>Descending</option> </select> '."\n";
  600:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  601:     $Ptr .= '<input type="submit" name="sort" value="DoDiff Graph" />'."\n";
  602:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  603:     $Ptr .= '<input type="submit" name="sort" value="%Wrong Graph" />'."\n";
  604: 
  605:     $Ptr .= '<pre>'.
  606:     '<b>  #Stdnts</b>: Total Number of Students opened the problem.<br>'. 
  607:     '<b>  Tries  </b>: Total Number of Tries for solving the problem.<br>'. 
  608:     '<b>  Max   </b> : Maximunm Number of Tries for solving the problem.<br>'. 
  609:     '<b>  Avg.   </b>: Average Number of the tries. [ Tries / #Stdnts ]<br>'.
  610:     '<b>  #YES   </b>: Number of students solved the problem correctly.<br>'. 
  611:     '<b>  #yes   </b>: Number of students solved the problem by override.<br>'.
  612:     '<b>  %Wrng  </b>: Percentage of students tried to solve the problem but'.
  613:     ' still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]<br>'.
  614: #    '  DoDiff : Degree of Difficulty of the problem. [ Tries/(#YES+#yes+0.1) ]<br>'. Kashy formula
  615:     '<b>  DoDiff </b>: Degree of Difficulty of the problem. [ 1 - ((#YES+#yes) / Tries) ]<br>'. #Gerd formula
  616:     '<b>  S.D.  </b> : Standard Deviation of the tries.'.
  617:     '[ sqrt(sum((Xi - Avg.)^2)) / (#Stdnts-1)'.
  618:     ' where Xi denotes every student\'s tries ]<br>'.
  619:     '<b>  Skew.  </b>: Skewness of the students tries.'.
  620: 	' [ (sqrt( sum((Xi - Avg.)^3) / #Stdnts)) / (S.D.^3) ]<br>'.
  621:     '<b>  Dis.F. </b>: Discrimination Factor: A Standard for '.
  622: 	'evaluating the problem according to a Criterion<br>'.
  623: 	'<b>           [Applied Criterion in %27 Upper Students - '.
  624: 	'Applied the same Criterion in %27 Lower Students]</b><br>'.
  625:     '<b>           1st Criterion</b> for Sorting the Students: '.
  626: 	'<b>Sum of Partial Credit Awarded / Total Number of Tries</b><br>'.
  627:     '<b>           2nd Criterion</b> for Sorting the Students: '.
  628: 	'<b>Total number of Correct Answers / Total Number of Tries</b>'.	
  629:             '</pre>';
  630: 
  631:     $r->print($Ptr);
  632:     
  633:     $r->print('Output CSV format: <input type=checkbox name=showcsv onClick="submit()"');
  634:     if ($ENV{'form.showcsv'}) { $r->print(' checked'); }
  635:     $r->print('>');
  636: 
  637:     $r->rflush();	
  638: 
  639:     if ((-e "$CacheDB")&&($ENV{'form.sort'} ne 'Recalculate Statistics')) {
  640: 	if (tie(%CachData,'GDBM_File',"$CacheDB",&GDBM_READER,0640)) {
  641: 	    tie(%GraphDat,'GDBM_File',$GraphDB,&GDBM_WRCREAT,0640);
  642: 	    &Cache_Statistics();
  643:         }
  644:         else {
  645: 	    $r->print("Unable to tie hash to db file");
  646:         }
  647:     }
  648:     else {
  649: 	if (tie(%CachData,'GDBM_File',$CacheDB,&GDBM_WRCREAT,0640)) {
  650: 	    tie(%GraphDat,'GDBM_File',$GraphDB,&GDBM_WRCREAT,0640);
  651: 	    foreach (keys %DiscFac) {delete $CachData{$_};}
  652: 	    foreach (keys %CachData) {delete $CachData{$_};}
  653: 	    $DiscFlag=0;
  654: 	    &Build_Statistics();
  655: 	}
  656:         else {
  657: 	    $r->print("Unable to tie hash to db file");
  658:         }
  659:     }
  660: 
  661: #    $r->print('Total instances of the problems : '.($p_count*($#students+1)));
  662:     untie(%CachData);
  663:     untie(%GraphDat);
  664: }
  665: 
  666: 
  667: # ------------------------------------- Find the section of student in a course
  668: 
  669: sub usection {
  670:     my ($udom,$unam,$courseid,$ActiveFlag)=@_;
  671:     $courseid=~s/\_/\//g;
  672:     $courseid=~s/^(\w)/\/$1/;
  673:     foreach (split(/\&/,&Apache::lonnet::reply('dump:'.
  674:              $udom.':'.$unam.':roles',
  675:              &Apache::lonnet::homeserver($unam,$udom)))){
  676:         my ($key,$value)=split(/\=/,$_);
  677:         $key=&Apache::lonnet::unescape($key);
  678:         if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
  679:             my $section=$1;
  680:             if ($key eq $courseid.'_st') { $section=''; }
  681: 	    my ($dummy,$end,$start)=split(/\_/,&Apache::lonnet::unescape($value));
  682: 	    if ( $ActiveFlag ne 'Any' ) {
  683: 		my $now=time;
  684: 		my $notactive=0;
  685: 		if ($start) {
  686: 		    if ($now<$start) { $notactive=1; }
  687: 		}
  688: 		if ($end) {
  689: 		    if ($now>$end) { $notactive=1; }
  690: 		}
  691: 		if ((($ActiveFlag eq 'Expired') && $notactive == 1) || 
  692:                     (($ActiveFlag eq 'Active') && $notactive == 0 ) ) {
  693: 		    return $section;
  694: 		}
  695: 		else { return '-1'; } 
  696: 	    }
  697: 	    return $section;
  698:         }
  699:     }
  700:     return '-1';
  701: }
  702: 
  703: 
  704: # ------ Dump the Student's DB file and handling the data for statistics table 
  705: sub ExtractStudentData {
  706:     my $student=shift;
  707:     my ($sname,$sdom) = split( /\:/, $student );
  708:     my %result = &Apache::lonnet::dump($cid,$sdom,$sname);
  709:     my $ResId;
  710:     my $PrOrd;
  711:     my $Dis = '';
  712:     my $Code;
  713:     my $Tries;
  714:     my $ParCr;
  715:     my $TotalTries = 0;
  716:     my $TotalOpend = 0;
  717:     my $ProbSolved = 0;
  718:     my $ProbTot = 0;
  719:     my $TimeTot = 0;
  720:     my $TotParCr = 0;
  721:     my $Wrongs;
  722:     my %TempHash;
  723:     my $Version; 
  724:     my $LatestVersion;
  725:     my $SecLimit;
  726:     my $MapLimit;
  727:     my ($temp)=keys(%result);
  728:     unless ($temp=~/^error\:/) {
  729:         foreach my $CurCol(@cols) {
  730: 	    ($PrOrd,$ResId)=split(/\:/,$CurCol);
  731: 	    if ( !$CurCol ) { next; }
  732:             $ResId=~/(\d+)\.(\d+)/;
  733: 	    my $MapId=$1;
  734: 	    my $PrbId=$2;
  735:             my $MapOrg = $hash{'map_id_'.$MapId};
  736:             my $Map = &Apache::lonnet::declutter($MapOrg);
  737:             if ( $CurMap ne 'All Maps' ) {
  738: 		my ( $ResMap, $NameMap ) = split(/\=/,$CurMap);
  739: 		if ( $Map ne $ResMap ) { next; }
  740: 	    }
  741: 	    my $meta=$hash{'src_'.$ResId};
  742: 	    my $PartNo = 0;
  743: 	    $Dis .= ':';
  744: 	    undef %TempHash;
  745: 
  746: 	    foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) { 
  747: 		if ($_=~/^stores\_(\w+)\_tries$/) {
  748:                     my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
  749: 		    if ( $TempHash{"$Part"} eq '' ) { 
  750: 			$TempHash{"$Part"} = $Part;
  751: 			$TempHash{$PartNo}=$Part;
  752: 			$TempHash{"$Part.Code"} = 'U';  
  753: 			$TempHash{"$Part.PrOrd"} = $PrOrd+$PartNo;
  754: 			$PartNo++;
  755: 		    }
  756:                     #my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
  757: 		}
  758:             }
  759:             &Apache::lonnet::declutter( $hash{'src_'.$ResId} );
  760: 	    my $URI = $hash{'src_'.$ResId};
  761:             my $Prob = $Map.'___'.$PrbId.'___'.
  762:                        &Apache::lonnet::declutter($URI);
  763:             $Code='U';
  764:             $Tries = 0;
  765: 	    $ParCr = 0;
  766:             $Wrongs = 0;
  767:   	    $LatestVersion = $result{"version:$Prob"};        
  768: 	    if ( $LatestVersion ) {
  769: 		for ( my $Version=1; $Version<=$LatestVersion; $Version++ ) {
  770: 		    my $vkeys = $result{"$Version:keys:$Prob"};
  771: 		    my @keys = split(/\:/,$vkeys);		
  772: 		    foreach my $Key (@keys) {		  
  773: 			if (($Key=~/\.(\w+)\.solved$/) && ($Key!~/^\d+\:/)) {
  774: 			    my $Part = $1;
  775: 			    $Tries = $result{"$Version:$Prob:resource.$Part.tries"};
  776: 			    $ParCr = $result{"$Version:$Prob:resource.$Part.awarded"};
  777: 			    my $Time = $result{"$Version:$Prob:timestamp"};
  778: 			    $TempHash{"$Part.Time"} = ($Time) ? $Time : 0;
  779: 			    $TempHash{"$Part.Tries"} = ($Tries) ? $Tries : 0;
  780: 			    $TempHash{"$Part.ParCr"} = ($ParCr) ? $ParCr : 0;        
  781: 			    $TotalTries += $TempHash{"$Part.Tries"};
  782: 			    $TotParCr += $TempHash{"$Part.ParCr"};
  783: 			    my $Val = $result{"$Version:$Prob:resource.$Part.solved"};
  784: 			    if ( $Val eq 'correct_by_student' )
  785:                                { $Wrongs = $Tries - 1; $Code = 'C'; } 
  786: 			    elsif ( $Val eq 'correct_by_override' )
  787:                                { $Wrongs = $Tries - 1; $Code = 'O'; }                        
  788: 			    elsif ( $Val eq 'incorrect_attempted' || 
  789:                                 $Val eq 'incorrect_by_override' )
  790: 		               { $Wrongs = $Tries; $Code = 'I'; }
  791: 			    $TempHash{"$Part.Code"} = $Code;
  792: 			    $TempHash{"$Part.Wrongs"} = $Wrongs;
  793: 			}
  794:      		    }
  795:                 } 
  796: 		for ( my $n = 0; $n < $PartNo; $n++ ) {		  
  797: 		    my $part = $TempHash{$n};
  798: 		    my $Yes = 0;
  799:                     if ( $TempHash{$part.'.Code'} eq 'C' ||
  800:                          $TempHash{$part.'.Code'} eq 'O'  ) 
  801: 		       {$ProbSolved++;$Yes=1;}		
  802: 
  803:  #		    my $ptr = "$hash{'title_'.$ResId}";
  804: 		    my $ptr = $TempHash{$part.'.PrOrd'}.'&'.$ResId;
  805: 
  806: 		    if ( $PartNo > 1 ) {                
  807: 			$ptr .= "*(part $part)";
  808: 			$Dis .= '&';
  809: 		    }
  810: 		    my $Fac = ($TempHash{"$part.Tries"}) ? 
  811:                               ($TempHash{"$part.ParCr"}/$TempHash{"$part.Tries"}) : 0;
  812: 		    my $DisF;
  813: 		    if ( $Fac > 0 &&  $Fac < 1 ) { 
  814: 			$DisF = sprintf( "%.4f", $Fac );
  815: 		    }
  816: 		    else {$DisF = $Fac;}
  817: #		    $DisF .= '+'.$TempHash{"$part.Time"};33333333
  818: 		    $TimeTot += $TempHash{"$part.Time"};
  819: 		    $Dis .= $TempHash{$part.'.PrOrd'}.'='.$DisF.'+'.$Yes;
  820: 		    $ptr .= "&$TempHash{$part.'.Tries'}".
  821: 		            "&$TempHash{$part.'.Wrongs'}".
  822:                             "&$TempHash{$part.'.Code'}";
  823: 		    push (@list, $ptr);
  824: 		    $TotalOpend++;
  825: 		    $ProbTot++;
  826: 		}
  827:             }
  828: 	    #else { 
  829: 		#for(my $n=0; $n<$PartNo; $n++) {
  830: 		#    push (@list, "$TempHash{'0'.'.PrOrd'}.':'.$ResId:0:0:U");
  831: 		#    $ProbTot++; 
  832: 		#}
  833: 	    #}
  834:         }
  835: 	if ( $TotalTries ) {
  836: 	    my $DisFac = ( $TotalTries ) ? ($TotParCr/$TotalTries) : 0;
  837: 	    my $DisFactor = sprintf( "%.4f", $DisFac );
  838: 	    $DiscFac{$DisFactor}=$Dis;
  839: 	    #my $time;
  840: 	    #if ($ProbSolved){
  841: 		#$time = int(($TimeTot/$ProbSolved)-10000000);
  842: 	    #}
  843: 	    #$DiscFac{($DisFactor.':'.$sname.':'.$ProbTot.':'.$TotalOpend.':'.
  844:             #          $TotalTries.':'.$ProbSolved.':'.$time)}=$Dis;
  845: 	}
  846:     }
  847:     #$r->print($sname.' PrCr= '.$TotParCr.' Slvd= '.$ProbSolved.' Tries='.$TotalTries.'<br>');
  848: }
  849: 
  850: 
  851: # ------------------------------------------------------------ Build page table
  852: sub tracetable {
  853:     my ($rid,$beenhere)=@_;
  854:     my $IsMap=0;
  855:     $rid=~/(\d+)\.(\d+)/;
  856:     $maps{&Apache::lonnet::declutter($hash{'map_id_'.$1})}='';#$hash{'title_'.$rid}; 
  857:     #$maps{$HWN}=$hash{'title_'.$rid}; 
  858:     unless ($beenhere=~/\&$rid\&/) {
  859:        $beenhere.=$rid.'&'; 
  860:        if (defined($hash{'is_map_'.$rid})) {
  861: 	   my $cmap=$hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}};
  862:            if ( $cmap eq 'sequence' || $cmap eq 'page' ) { 
  863:                $cols[$#cols+1]=0;
  864: 	       $P_Order++;
  865: 	       $HWN=$P_Order;
  866:                $mapsort{$HWN} = $rid.':';
  867: 	       $IsMap=1;
  868:  
  869:                #$maps{&Apache::lonnet::declutter($hash{'src_'.$rid})}= 
  870:                #      $hash{'title_'.$rid}; 
  871:            }
  872:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
  873:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
  874: 	       my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
  875: 
  876:                &tracetable($hash{'map_start_'.$hash{'src_'.$rid}},
  877: 			   '&'.$frid.'&');
  878: 
  879: 	       $cols[$#cols+1]=($P_Order+1).':'.$frid;
  880:       
  881: 	       my $meta=$hash{'src_'.$frid};
  882: 	       my $PartNo = 0;
  883: 	       my $Part;
  884: 	#       if ($IsMap==0){
  885:                if ($meta) {
  886: 		   if ($meta=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  887: 		       foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
  888: 			   if ($_=~/^stores\_(\w+)\_tries$/) {
  889: 			       $Part=&Apache::lonnet::metadata($meta,$_.'.part');
  890: 			       $P_Order++;
  891: 			       $mapsort{$HWN} .= '&'.$P_Order;
  892: 			       $PartNo++;
  893: 			       #$r->print('<br>'.$PartNo.'---'.$P_Order);
  894: 			   }
  895: 			   foreach my $K(split(/\,/,&Apache::lonnet::metadata($meta,'packages'))) {
  896: 			       if ($K=~/^optionresponse\_($Part)\_(\w+)$/) {
  897: 				   #$r->print('<br>'.$_.'...'.$P_Order.'---'.$Part);
  898: 				   $OpResp{$P_Order}="$frid:$Part";
  899: 			       } 
  900: 			   }
  901: 		       }
  902: 		   }
  903:                }
  904: 	   }
  905: 	 #  }
  906:        } else {
  907: 	   $cols[$#cols+1]=($P_Order+1).':'.$rid;
  908: 	   my $meta=$hash{'src_'.$rid};
  909: 	   my $PartNo = 0;
  910: 	   if ($meta) {
  911: 	       if ($meta=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  912: 		   foreach my $Key(split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
  913: 		       if ($Key=~/^stores\_(\w+)\_tries$/) {
  914: 			   my $Part=&Apache::lonnet::metadata($meta,$Key.'.part');
  915: 			   $P_Order++;
  916: 			   $mapsort{$HWN} .= '&'.$P_Order;
  917: 			   $PartNo++;
  918: 			   foreach (split(/\,/,&Apache::lonnet::metadata($meta,'packages'))) {
  919: 			       if ($_=~/^optionresponse\_($Part)\_(\w+)$/) {
  920: 				   #$r->print('<br>'.$_.'...'.$P_Order.'---'.$Part);
  921: 				   $OpResp{$P_Order}="$rid:$Part";;
  922: 			       } 
  923: 		   
  924: 			   }
  925: 
  926: 		       }
  927: 		   }
  928: 	       }
  929: 	   }       
  930:        }
  931:        if (defined($hash{'to_'.$rid})) {
  932:           foreach (split(/\,/,$hash{'to_'.$rid})){
  933:               &tracetable($hash{'goesto_'.$_},$beenhere);
  934:           }
  935:        }
  936:     }
  937: }
  938: 
  939: sub MySort {          
  940:     if ( $Pos > 0 ) {
  941: 	if ($ENV{'form.order'} eq 'Descending') {$b <=> $a;}
  942: 	else { $a <=> $b; }
  943:     }
  944:     else {
  945: 	if ($ENV{'form.order'} eq 'Descending') {$b cmp $a;}
  946: 	else { $a cmp $b; }
  947:     }
  948: }
  949: 
  950: sub Create_PrgWin {
  951: #----------- Create progress
  952:     $r->print(<<ENDPOP);
  953:     <script>
  954:     popwin=open('','popwin','width=400,height=100');
  955:     popwin.document.writeln('<html><body bgcolor="#88DDFF">'+
  956:       '<title>LON-CAPA Statistics</title>'+
  957:       '<h4>Computation Progress</h4>'+
  958:       '<form name=popremain>'+
  959:       '<input type=text size=35 name=remaining value=Starting></form>'+
  960:       '</body></html>');
  961:     popwin.document.close();
  962:     </script>
  963: ENDPOP
  964: 
  965:     $r->rflush();
  966: }
  967: 
  968: 
  969: sub Update_PrgWin {
  970: #----------- update progress
  971:     my $index = shift;
  972:     $r->print('<script>popwin.document.popremain.remaining.value="'.
  973:               'Computing '.($index+1).'/'.($#students+1).': '.
  974:               $students[$index].'";</script>');
  975:     $r->rflush();
  976: }
  977: 
  978: sub Close_PrgWin {
  979: #--------------------- close Progress Line
  980:     $r->print('<script>popwin.close()</script>');
  981:     $r->rflush(); 
  982: }
  983: 
  984: sub Build_Statistics {
  985:     &Create_PrgWin();
  986: # ---------------------------- Gathering the Data of students' tries
  987:     for (my $index=0;$index<=$#students;$index++) {
  988: 	&Update_PrgWin($index);
  989:         &ExtractStudentData($students[$index]);
  990:     }
  991: 
  992: # -------------------- sorting the Data
  993:     $r->print('<script>popwin.document.popremain.remaining.value="'.
  994:               'Calculating Discrimination Factors...";</script>');
  995: 
  996:     @list = sort (@list);
  997: 
  998: 
  999:     &Discriminant();
 1000: 
 1001:     $OpSel2='';
 1002:     $OpSel1='selected';
 1003:  		   
 1004:     $p_count = 0; 
 1005:     my $nIdx = 0;
 1006:     my $dummy; 
 1007:     my $p_val;
 1008:     my $ResId;
 1009:     my $NoElements = $#list + 1;
 1010: #-------------------------------- loop for data representation
 1011:     foreach (sort keys %mapsort) {
 1012: 	my ($Hid,$pr)=split(/\:/,$mapsort{$_});
 1013: 	my @lpr=split(/\&/,$pr);
 1014: 	&CreateTable(1,$Hid);
 1015: 	for (my $i=1; $i<=$#lpr; $i++) {
 1016: 	    my %storestats=();
 1017: 	    my ($PrOrd,$Prob,$Tries,$Wrongs,$Code)=split(/\&/,$list[$nIdx]);
 1018: 	    my $Temp = $Prob;
 1019: 	    my $MxTries = 0;
 1020: 	    my $TotalTries = 0;
 1021: 	    my $YES = 0;
 1022: 	    my $Incorrect = 0;
 1023: 	    my $Override = 0;
 1024: 	    my $StdNo = 0;
 1025: 	    my @StdLst;
 1026: 	    while ( $PrOrd == $lpr[$i] ) 
 1027: 	    {
 1028: 		$nIdx++;
 1029: 		$StdNo++;
 1030: 		$StdLst[ $StdNo ] = $Tries;
 1031: 		$TotalTries += $Tries;
 1032: 		if ( $MxTries < $Tries ) { $MxTries = $Tries; } 
 1033: 		if ( $Code eq 'C' ){ $YES++; }
 1034: 		elsif( $Code eq 'I' ) { $Incorrect++; }
 1035: 		elsif( $Code eq 'O' ) { $Override++; }
 1036: 		elsif( $Code eq 'U' ) { $StdNo--; }
 1037: 		($PrOrd,$Prob,$Tries,$Wrongs,$Code)=split(/\&/,$list[$nIdx]);
 1038: 	    }	
 1039: 
 1040: 	    $p_count++;
 1041: 	    my $Dummy;
 1042: 	    ($ResId,$Dummy)=split(/\*/,$Temp);
 1043: 
 1044: 	    $Temp = '<a href="'.$hash{'src_'.$ResId}.
 1045:                 '" target="_blank">'.$hash{'title_'.$ResId}.$Dummy.'</a>';
 1046: 
 1047: 	    my $res = &Apache::lonnet::declutter($hash{'src_'.$ResId});
 1048: 	    my $urlres=$res;
 1049: 
 1050: 	    $ResId=~/(\d+)\.(\d+)/;
 1051: 	    my $Map = &Apache::lonnet::declutter( $hash{'map_id_'.$1} );
 1052: 	    $urlres=$Map;
 1053:  
 1054: 	    $res = '<a href="'.$hash{'src_'.$ResId}.'">'.$res.'</a>';
 1055: 	    #$Map = '<a href="'.$Map.'">'.$res.'</a>';
 1056: 
 1057: #------------------------ Compute the Average of Tries about one problem
 1058: 	    my $Average = ($StdNo) ? $TotalTries/$StdNo : 0;
 1059: 
 1060: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___timestamp'}=time;       
 1061: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___stdno'}=$StdNo;
 1062: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___avetries'}=$Average;
 1063:    
 1064: #-------------------------------- Compute percentage of Wrong tries
 1065: 	    my $Wrong = ( $StdNo ) ? 100 * ( $Incorrect / $StdNo ) : 0;
 1066: 
 1067: #-------------------------------- Compute Standard Deviation
 1068: 	    my $StdDev = 0; 
 1069: 	    if ( $StdNo > 1 ) {
 1070: 		for ( my $n = 0; $n < $StdNo; $n++ ) {
 1071: 		    my $Dif = $StdLst[ $n ]-$Average;
 1072: 		    $StdDev += $Dif*$Dif;
 1073: 		} 
 1074: 		$StdDev /= ( $StdNo - 1 );
 1075: 		$StdDev = sqrt( $StdDev );
 1076: 	    }
 1077: 
 1078: #-------------------------------- Compute Degree of Difficulty
 1079: 	    my $DoDiff = 0;
 1080: 	    if( $TotalTries > 0 ) {
 1081: 		$DoDiff = 1 - ( ( $YES + $Override ) / $TotalTries );
 1082: #	    $DoDiff =  ($TotalTries)/($YES + $Override+ 0.1);	    
 1083: 	    }
 1084:        
 1085: 	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___difficulty'}=$DoDiff;
 1086: 
 1087: #-------------------------------- Compute the Skewness
 1088: 	    my $Skewness = 0;
 1089: 	    my $Sum = 0; 
 1090: 	    if ( $StdNo > 0 && $StdDev > 0 ) {
 1091: 		for ( my $n = 0; $n < $StdNo; $n++ ) {
 1092: 		    my $Dif = $StdLst[ $n ]-$Average;
 1093: 		    $Skewness += $Dif*$Dif*$Dif;
 1094: 		} 
 1095: 		$Skewness /= $StdNo;
 1096: 		$Skewness /= $StdDev*$StdDev*$StdDev;
 1097: 	    }
 1098: 
 1099: #--------------------- Compute the Discrimination Factors
 1100: 	    my ($Up1,$Up2)=split(/\:/,$DisUp{$lpr[$i]});
 1101: 	    my ($Lw1,$Lw2)=split(/\:/,$DisLow{$lpr[$i]});
 1102: 	    my $Dis1 = $Up1 - $Lw1;
 1103: 	    my $Dis2 = $Up2 - $Lw2;
 1104: 	    my $_D1 = sprintf("%.2f", $Dis1);
 1105: 	    my $_D2 = sprintf("%.2f", $Dis2);
 1106: 
 1107: #-----------------  Some restition in presenting the float numbers
 1108: 	    my $Avg = sprintf( "%.2f", $Average );
 1109: 	    my $Wrng = sprintf( "%.1f", $Wrong );
 1110: 	    my $SD = sprintf( "%.1f", $StdDev );
 1111: 	    my $DoD = sprintf( "%.2f", $DoDiff );
 1112: 	    my $Sk = sprintf( "%.1f", $Skewness );
 1113: 	    my $join = $lpr[$i].'&'.$Temp.'&'.$StdNo.'&'.
 1114:                        $TotalTries.'&'.$MxTries.'&'.$Avg.'&'.
 1115:                        $YES.'&'.$Override.'&'.$Wrng.'&'.$DoD.'&'.
 1116: 		       $SD.'&'.$Sk.'&'.$_D1.'&'.$_D2.'&'.
 1117:                        $Prob;
 1118: 	    $CachData{($p_count-1)}=$join;
 1119: 
 1120: 	    $urlres=~/^(\w+)\/(\w+)/;
 1121: 	    if ($StdNo) { 
 1122: 		&Apache::lonnet::put('resevaldata',\%storestats,$1,$2); 
 1123: 	    }
 1124: #-------------------------------- Row of statistical table
 1125: 	    if ( $DiscFlag == 0 ) {
 1126: 		&TableRow($join,$i,($p_count-1));
 1127: 	    } 
 1128: 	}
 1129: 	&CloseTable();
 1130:     }
 1131:     &Close_PrgWin();
 1132: }
 1133: 
 1134: sub Cache_Statistics {
 1135:     my @list = ();
 1136:     my $Useful;
 1137:     my $UnUseful;
 1138:     my %myHeader = reverse( %Header );
 1139:     $Pos = $myHeader{$ENV{'form.sort'}};
 1140:     if ($Pos > 0) {$Pos++;}
 1141:     $p_count = 0;
 1142:     foreach my $key( keys %CachData) { 
 1143: 	my @Temp=split(/\&/,$CachData{$key});
 1144: 	if ( $Pos == 0 ) {
 1145: 	    ($UnUseful,$Useful)=split(/\>/,$Temp[$Pos]);
 1146: 	}
 1147: 	else {
 1148: 	    $Useful = $Temp[$Pos];
 1149: 	}   
 1150: 	$list[$p_count]=$Useful.'@'.$CachData{$key};
 1151:         $p_count++;
 1152:     }
 1153: 
 1154:     @list = sort MySort (@list);
 1155: 
 1156:     my $nIdx=0;
 1157: 
 1158:     if ( $Pos == 0 ) {
 1159: 	foreach (sort keys %mapsort) {
 1160: 	    my ($Hid,$pr)=split(/\:/,$mapsort{$_});
 1161: 	    &CreateTable(1,$Hid);
 1162: 	    my @lpr=split(/\&/,$pr);
 1163: 	    for (my $i=1; $i<=$#lpr; $i++) {
 1164: 		my($Pre, $Post) = split(/\@/,$list[$nIdx]); 
 1165: 		#$r->print('<br>'.$Pre.'---'.$Post);
 1166: 		&TableRow($Post,$i,$nIdx);
 1167: 		$nIdx++;
 1168: 	    }
 1169: 	    &CloseTable();
 1170: 	}
 1171:     }
 1172:     else {
 1173: 	&CreateTable(0);
 1174: 	for ( my $nIdx = 0; $nIdx < $p_count; $nIdx++ ) {
 1175: 	    my($Pre, $Post) = split(/\@/,$list[$nIdx]); 
 1176: 	    &TableRow($Post,$nIdx,$nIdx);
 1177: 	} 
 1178: 	&CloseTable();
 1179:     }
 1180: }
 1181: 
 1182: sub TableRow {
 1183:     my ($Str,$Idx,$RealIdx)=@_;
 1184:     my($PrOrd,$Temp,$StdNo,$TotalTries,$MxTries,$Avg,$YES,$Override,
 1185:        $Wrng,$DoD,$SD,$Sk,$_D1,$_D2,$Prob)=split(/\&/,$Str);	
 1186:     if ($ENV{'form.showcsv'}) {
 1187:         my ($ResId,$Dummy)=split(/\*/,$Prob);
 1188:         my $Ptr =  "\n".'<br>'.
 1189:                "\n".'"'.($RealIdx+1).'",'.
 1190:                "\n".'"'.$hash{'title_'.$ResId}.$Dummy.'",'.
 1191:                "\n".'"'.$hash{'src_'.$ResId}.'",'.
 1192:                "\n".'"'.$StdNo.'",'.
 1193:                "\n".'"'.$TotalTries.'",'.
 1194:                "\n".'"'.$MxTries.'",'.
 1195:                "\n".'"'.$Avg.'",'.
 1196:                "\n".'"'.$YES.'",'.
 1197:                "\n".'"'.$Override.'",'.
 1198:                "\n".'"'.$Wrng.'",'.
 1199:                "\n".'"'.$DoD.'",'.
 1200:                "\n".'"'.$SD.'",'.
 1201:                "\n".'"'.$Sk.'",'.
 1202:                "\n".'"'.$_D1.'",'.
 1203: 	       "\n".'"'.$_D2.'"';
 1204:         $r->print("\n".$Ptr);
 1205:     }
 1206:     else{
 1207:         my $Ptr =  "\n".'<tr>'.
 1208:                "\n".'<td>'.($RealIdx+1).'</td>'.
 1209:           #     "\n".'<td>'.$PrOrd.$Temp.'</td>'.
 1210:                "\n".'<td>'.$Temp.'</td>'.
 1211:                "\n".'<td bgcolor="#EEFFCC"> '.$StdNo.'</td>'.
 1212:                "\n".'<td bgcolor="#EEFFCC">'.$TotalTries.'</td>'.
 1213:                "\n".'<td bgcolor="#EEFFCC">'.$MxTries.'</td>'.
 1214:                "\n".'<td bgcolor="#DDFFFF">'.$Avg.'</td>'.
 1215:                "\n".'<td bgcolor="#DDFFFF"> '.$YES.'</td>'.
 1216:                "\n".'<td bgcolor="#DDFFFF"> '.$Override.'</td>'.
 1217:                "\n".'<td bgcolor="#FFDDDD"> '.$Wrng.'</td>'.
 1218:                "\n".'<td bgcolor="#FFDDDD">'.$DoD.'</td>'.
 1219:                "\n".'<td bgcolor="#DDFFDD"> '.$SD.'</td>'.
 1220:                "\n".'<td bgcolor="#DDFFDD"> '.$Sk.'</td>'.
 1221:                "\n".'<td bgcolor="#FFDDFF"> '.$_D1.'</td>'.
 1222: 	       "\n".'<td bgcolor="#FFDDFF"> '.$_D2.'</td>';
 1223:         $r->print("\n".$Ptr.'</tr>' );
 1224:     }
 1225:     $GraphDat{$RealIdx}=$DoD.':'.$Wrng;
 1226: }
 1227: 
 1228: # ------------------------------------------- Prepare data for Graphical chart
 1229: 
 1230: sub GetGraphData {
 1231:     my $ylab = shift;
 1232:     my $Col;
 1233:     my $data='';
 1234:     my $count = 0;
 1235:     my $Max = 0;
 1236:     my $cid=$ENV{'request.course.id'};
 1237:     my $GraphDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
 1238:                   "_$ENV{'user.domain'}_$cid\_graph.db";
 1239:     foreach (keys %GraphDat) {delete $GraphDat{$_};}
 1240:     if (-e "$GraphDB") {
 1241: 	if (tie(%GraphDat,'GDBM_File',"$GraphDB",&GDBM_READER,0640)) {
 1242: 	    if ( $ylab eq 'DoDiff Graph' ) {
 1243: 		$ylab = 'Degree-of-Difficulty';
 1244: 		$Col = 0;
 1245: 	    }
 1246: 	    else {
 1247: 		$ylab = 'Wrong-Percentage';
 1248: 		$Col = 1;
 1249: 	    }
 1250: 	    foreach (sort NumericSort keys %GraphDat) { 
 1251: 		my @Temp=split(/\:/,$GraphDat{$_});
 1252:                 my $inf = $Temp[$Col]; 
 1253: 		if ( $Max < $inf ) {$Max = $inf;}
 1254: 		$data .= $inf.',';
 1255: 		$count++;
 1256: 	    }
 1257: 	    if ( $Max > 1 ) { 
 1258: 		$Max += (10 - $Max % 10);
 1259: 		$Max = int($Max);
 1260: 	    }
 1261: 	    else { $Max = 1; }
 1262:             untie(%GraphDat);
 1263: 	    my $Course = $ENV{'course.'.$cid.'.description'};
 1264: 	    $Course =~ s/\ /"_"/eg;
 1265: 	    $GData=$Course.'&'.'Problems'.'&'.$ylab.'&'.$Max.'&'.$count.'&'.$data;
 1266: 	}
 1267: 	else {
 1268: 	    $r->print("Unable to tie hash to db file");
 1269: 	}
 1270:     }
 1271: }
 1272: 
 1273: 
 1274: sub initial {
 1275: # --------------------------------- Initialize the global varaibles
 1276:   undef @students;
 1277:   undef @cols;
 1278:   undef %maps;
 1279:   undef %section;
 1280:   undef %StuBox;
 1281:   undef @list;
 1282:   undef %CachData;
 1283:   undef %GraphDat;
 1284:   undef %DiscFac;
 1285:   undef %OpResp;
 1286:   undef %ConceptData;
 1287:   undef $CurMap;
 1288:   undef $CurSec;
 1289:   undef $CurStu;
 1290:   undef $p_count;
 1291:   undef $Pos;
 1292:   undef $GData;
 1293:   $DiscFlag=0; 
 1294:   $P_Order=100000;
 1295:   $HWN=$P_Order;
 1296: }
 1297: 
 1298: 
 1299: sub ClassList {
 1300: 
 1301:     &GetStatus();
 1302: 
 1303:     $cid=$ENV{'request.course.id'};
 1304:     my $chome=$ENV{'course.'.$cid.'.home'};
 1305:     my ($cdom,$cnum)=split(/\_/,$cid);
 1306: # ----------------------- Get first and last resource, see if there is anything
 1307:     $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
 1308:     $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
 1309:     if (($firstres) && ($lastres)) {
 1310: 	#my %result = &Apache::lonnet::dump('classlist',$sdom,$sname);
 1311: 	my $classlst=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.':classlist',$chome);
 1312: 	my $StudNo = 0;
 1313: 	my $now=time;
 1314: 	unless ($classlst=~/^error\:/) {
 1315: 	    foreach (sort split(/\&/,$classlst)) {
 1316: 		my ($name,$value)=split(/\=/,$_);
 1317: 		my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
 1318: 		$name=&Apache::lonnet::unescape($name);
 1319: 		my ($sname,$sdom)=split(/\:/,$name);
 1320: 		my $active=1;
 1321: 		my $Status=$ENV{'form.status'};
 1322: 		$Status = ($Status) ? $Status : 'Active';
 1323: 		if ( ( ($end) && $now > $end ) && 
 1324:                      ( ($Status eq 'Active') ) ) { $active=0; }
 1325: 		if ( ($Status eq 'Expired') && 
 1326:                      ($end == 0 || $now < $end) ) { $active=0; }
 1327: 		if ($active) {
 1328: 		    my $thisindex=$#students+1;
 1329: 		    $name=&Apache::lonnet::unescape($name);
 1330: 		    $students[$thisindex]=$name;
 1331: 		    my ($sname,$sdom)=split(/\:/,$name);
 1332: 		    #my %reply=&Apache::lonnet::idrget($sdom,$sname);
 1333: 		    #my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
 1334: 		    #         ':environment:lastname&generation&firstname&middlename',
 1335: 		    #         &Apache::lonnet::homeserver($sname,$sdom));
 1336: 		    my $ssec=&usection($sdom,$sname,$cid,$Status);
 1337: #		    if ($ssec==-1 ) {next;}
 1338: 		    if ($ssec==-1 || $ssec eq 'adm' ) {next;}
 1339: 		    $ssec=($ssec) ? $ssec : '(none)';
 1340: 		    #$ssec=(int($ssec)) ? int($ssec) : $ssec;
 1341: 		    $section{$ssec}=$ssec;
 1342: 		    if ($CurSec eq 'All Sections' || $ssec eq $CurSec) {
 1343: 			$students[$StudNo]=$name;
 1344: 			$StuBox{$sname}=$sdom;
 1345: 		    }
 1346: 		    $StudNo++;
 1347: 	        }
 1348: 	    }
 1349: 	}
 1350: 	else {
 1351: 	    $r->print('<h1>Could not access course data</h1>');
 1352: 	} 
 1353:         $r->print("Total number of students : ".($#students+1));
 1354:         $r->rflush();
 1355: # --------------- Find all assessments and put them into some linear-like order
 1356: 	&tracetable($firstres,'&'.$lastres.'&');
 1357: #    my $c=0;
 1358: #    foreach (sort keys %mapsort) {
 1359: #	$r->print('<br>'.$c.')'.$_.' ... '.$mapsort{$_});
 1360: #	$c++;
 1361: #    }
 1362: #	my $c=1;
 1363: #	foreach (sort keys %OpResp) {
 1364: #	    $r->print('<br>'.$c.')'.$_.' ... '.$OpResp{$_}.' ... '.$hash{'src_'.$OpResp{$_}});
 1365: #	    $c++;
 1366: #	}
 1367: 
 1368:     }
 1369: 
 1370: # ------------------------------------------------------------- End render page 
 1371:     else {
 1372: 	$r->print('<h3>Undefined course sequence</h3>');
 1373:     }
 1374: }
 1375: 
 1376: 
 1377: sub Title {
 1378:     $r->print('<html><head><title>LON-CAPA Statistics</title></head>');
 1379:     $r->print('<body bgcolor="#FFFFFF">'.
 1380:               '<script>window.focus(); window.width=500;window.height=500;</script>'.
 1381:               '<img align=right src=/adm/lonIcons/lonlogos.gif>');
 1382: # ---------------------------------------------------------------- Course title
 1383:     $r->print('<h1> Course : "'.
 1384:               $ENV{'course.'.$ENV{'request.course.id'}.
 1385:               '.description'}.'"</h1><h2>'.localtime().'</h2>');
 1386: # ------------------------------- This is going to take a while, produce output
 1387:     $r->rflush();	
 1388: }
 1389: 
 1390: sub CreateForm {
 1391:     $r->print("\n".'<form name=stat method=post action="/adm/statistics" >');
 1392:     my $content = $ENV{'form.sort'};
 1393:     if (!($ENV{'form.showcsv'}) && ($content eq '' || $content eq 'Return to Menu')) {
 1394: 	my $Ptr = '<h3>';
 1395: 	$Ptr .= '<input type=submit name=sort value="Problem Stats"/>';
 1396: 	$Ptr .= '<br><br>';
 1397: 	$Ptr .= '<input type=submit name=sort value="Problem Analysis"/>';
 1398: 	$Ptr .= '<br><br>';
 1399: 	$Ptr .= '<input type=submit name=sort value="Student Assessment"/>';
 1400: 	$Ptr .= '</h3>';
 1401: 	$r->print( $Ptr );
 1402:     }
 1403:     else {
 1404: 	if ( $content eq 'Student Assessment' || 
 1405: 	     $content eq 'Create Student Report' ) {
 1406: 	    &MapSecOptions();
 1407: 	    &StudentOptions();
 1408: 	    &StudentReport($CurStu,$StuBox{"$CurStu"});
 1409: 	}
 1410: 	elsif ( $content eq 'Problem Analysis' ) {
 1411: 	    &AnalyzeProblem();
 1412: 	}
 1413: 	else {
 1414: 	    &MapSecOptions();
 1415: 	    &PreStatTable();
 1416: 	}
 1417:     }
 1418: }
 1419: 
 1420: 
 1421: sub Menu {
 1422:     &initial();
 1423:     #$Apache::lonxml::debug=1;
 1424:     #&Apache::lonhomework::showhash(%ENV);
 1425:     #$Apache::lonxml::debug=0;
 1426:     &Title();
 1427:     &ClassList();
 1428:     my $InpStr = $ENV{'form.sort'};
 1429:     if ($InpStr=~/^Analyze\_/) {
 1430: 	&ShowOpGraph($InpStr,$ENV{'form.interval'});
 1431:     }
 1432:     elsif ( $InpStr eq 'DoDiff Graph' || $InpStr eq '%Wrong Graph' ) {      
 1433: 	&GetGraphData($InpStr);
 1434:     	$r->print('<IMG src="/cgi-bin/graph.gif?'.$GData.'" />');
 1435:     }
 1436:     else {
 1437: 	&CreateForm();
 1438: 	$r->print("\n".'</form>');
 1439:     }
 1440:     $r->print("\n".'</body>'.
 1441: 	      "\n".'</html>');
 1442:     $r->rflush();
 1443: }
 1444: 
 1445: sub StudentOptions {
 1446:     my $OpSel5='';
 1447:     $CurStu = $ENV{'form.student'};
 1448:     if ( $CurStu eq '' ) { 
 1449:         $CurStu = 'All Students';
 1450:         $OpSel5 = 'selected';
 1451:     }
 1452:     my $Ptr ='';
 1453: # ----------------------------------- Loading the Students Combobox
 1454:     $Ptr .= '<br><b>Select Student</b>'."\n".
 1455:        	    '<select name="student">'."\n". 
 1456: 	    '<option '.$OpSel5.'>All Students</option>';                     	     	     
 1457:     foreach my $key ( sort keys %StuBox ) {	          
 1458: 	$Ptr .= '<option';
 1459: 	if ($CurStu eq $key) {$Ptr .= ' selected';}     
 1460:         $Ptr .= '>'.$key."</option>\n";	     
 1461:     }
 1462:     $Ptr .= '</select>';
 1463:     $Ptr .= '<br><input type="submit" name="sort" value="Create Student Report" />';
 1464:     $r->print( $Ptr );
 1465:     $r->rflush();	
 1466: }
 1467: 
 1468: 
 1469: sub GetStatus {
 1470:     $OpSel1='';
 1471:     $OpSel2='';
 1472:     $OpSel3='';
 1473:     $OpSel4='';
 1474: 
 1475:     if ( $ENV{'form.order'} eq 'Descending' ) { $OpSel2='selected'; }
 1476:     else { $OpSel1 = 'selected'; }
 1477: 
 1478:     my %myHeader = reverse( %Header );
 1479:     $Pos = $myHeader{$ENV{'form.sort'}};
 1480:     if ($Pos == 0) {
 1481: 	$OpSel1 = 'selected';
 1482: 	$ENV{'form.order'}='Ascendig';
 1483:     }
 1484: 
 1485:     $CurMap = $ENV{'form.maps'};
 1486:     if ( $CurMap eq '' ) { 
 1487: 	$CurMap = 'All Maps';
 1488: 	$OpSel3 = 'selected';
 1489:     }
 1490:     $CurSec = $ENV{'form.section'};
 1491:     if ( $CurSec eq '' ) { 
 1492: 	$CurSec = 'All Sections';
 1493:         $OpSel4 = 'selected';
 1494:     }
 1495: }
 1496: 
 1497: 
 1498: sub MapSecOptions {
 1499: # ----------------------------------- Loading the Maps Combobox
 1500:     my $Ptr = '';
 1501:     $Ptr .= '<input type="submit" name="sort" value="Return to Menu" />';
 1502:     $Ptr .= '<br><b> Select &nbsp; Map &nbsp; &nbsp; </b>'."\n".
 1503:        	    '<select name="maps">'."\n". 
 1504: 	    '<option '.$OpSel3.'>All Maps</option>';                     	     	     
 1505:     foreach my $key ( sort keys %maps ) {	          
 1506: 	$Ptr .= '<option';
 1507:         if ($CurMap eq $key) {$Ptr .= ' selected';}	     
 1508: 	$Ptr .= '>'.$key."</option>\n";	     
 1509:     }
 1510:     $Ptr .= '</select>';
 1511:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
 1512: 
 1513: # ----------------------------------- Loading the Sections Combobox
 1514:     $Ptr .= '<br><b>Select Section</b>'."\n".
 1515:        	    '<select name="section">'."\n". 
 1516: 	    '<option '.$OpSel4.'>All Sections</option>';                     	     	     
 1517:     foreach my $key ( sort keys %section ) {	          
 1518: 	$Ptr .= '<option';
 1519:         if ($CurSec eq $key) {$Ptr .= ' selected';}     
 1520: 	$Ptr .= '>'.$key."</option>"."\n";	     
 1521:     }
 1522:     $Ptr .= '</select>'."\n";
 1523: 
 1524:     $r->print( $Ptr );
 1525:     $r->rflush();
 1526: }
 1527: 
 1528: 
 1529: # ================================================================ Main Handler
 1530: 
 1531: sub handler {
 1532:     $r=shift;
 1533: 
 1534:     if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
 1535: # ------------------------------------------- Set document type for header only
 1536: 	if ($r->header_only) {
 1537: 	    if ($ENV{'browser.mathml'}) {
 1538: 		$r->content_type('text/xml');
 1539: 	    } 
 1540: 	    else {
 1541: 		$r->content_type('text/html');
 1542: 	    }
 1543: 	    $r->send_http_header;
 1544: 	    return OK;
 1545: 	}    
 1546: 	my $requrl=$r->uri;
 1547: # ----------------------------------------------------------------- Tie db file
 1548: 
 1549: 	undef %hash;
 1550: 
 1551: 	if ($ENV{'request.course.fn'}) {
 1552: 	    my $fn=$ENV{'request.course.fn'};
 1553: 	    if (-e "$fn.db") {
 1554: 		if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
 1555: # ------------------------------------------------------------------- Hash tied
 1556: 		    $r->content_type('text/html');
 1557: 		    $r->send_http_header;
 1558: 		    &Menu();
 1559: 		}
 1560: 		else {
 1561: 		    $r->content_type('text/html');
 1562: 		    $r->send_http_header;
 1563: 		    $r->print('<html><body>Coursemap undefined.</body></html>');
 1564: 		}
 1565: # ------------------------------------------------------------------ Untie hash
 1566: 		unless (untie(%hash)) {
 1567: 		    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
 1568:                             "Could not untie coursemap $fn (browse).</font>"); 
 1569: 		}
 1570: 
 1571: # -------------------------------------------------------------------- All done
 1572: 		return OK;
 1573: # ----------------------------------------------- Errors, hash could no be tied
 1574: 	    }
 1575: 	} 
 1576: 	else {
 1577: 	    $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
 1578: 	    return HTTP_NOT_ACCEPTABLE; 
 1579: 	}
 1580:     }
 1581:     else {
 1582:         $ENV{'user.error.msg'}=
 1583:         $r->uri.":vgr:0:0:Cannot view grades for complete course";
 1584: 
 1585:         return HTTP_NOT_ACCEPTABLE; 
 1586:     }
 1587: }
 1588: 1;
 1589: __END__

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