File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.17: download - view: text, annotated - select for diffs
Mon May 13 16:41:46 2002 UTC (22 years ago) by minaeibi
Branches: MAIN
CVS tags: HEAD
Fixed bugs in loading the graph of DoDiff and %wrong

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

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