--- loncom/interface/lonstatistics.pm	2002/05/13 16:41:46	1.17
+++ loncom/interface/lonstatistics.pm	2002/05/31 16:02:11	1.26
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # (Publication Handler
 #
-# $Id: lonstatistics.pm,v 1.17 2002/05/13 16:41:46 minaeibi Exp $
+# $Id: lonstatistics.pm,v 1.26 2002/05/31 16:02:11 minaeibi Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -30,7 +30,8 @@
 # 5/5,7/9,7/25/1,8/11,9/13,9/26,10/5,10/9,10/22,10/26 Behrouz Minaei
 # 11/1,11/4,11/16,12/14,12/16,12/18,12/20,12/31 Behrouz Minaei
 # YEAR=2002
-# 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
+# 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
+# 5/12,5/14,5/15,5/19,5/26 Behrouz Minaei
 #
 ###
 
@@ -77,13 +78,62 @@ my $lastres;
 my $DiscFlag;
 my $HWN;
 my $P_Order;
+my %color;
 my %foil_to_concept;
 my @Concepts;
 my %ConceptData;
 my %Header = (0,"Homework Sets Order",1,"#Stdnts",2,"Tries",3,"Mod",
               4,"Mean",5,"#YES",6,"#yes",7,"%Wrng",8,"DoDiff",
               9,"S.D.",10,"Skew.",11,"D.F.1st",12,"D.F.2nd");
-my @shown;
+my %Answer = ();
+
+
+sub Activity {
+    my $file="/home/minaeibi/activity.log";
+    my $userid='adamsde1';
+    $r->print("<br>Using $file");
+    $r->rflush();
+    open(FILEID, "<$file");
+    my $line;
+    my @allaccess;
+    my $Count=0;
+    while ($line=<FILEID>) {
+	my ($time,$machine,$what)=split(':',$line);
+	$what=&Apache::lonnet::unescape($what);
+	my @accesses=split('&',$what);
+	foreach my $access (@accesses) {
+	    my ($date,$resource,$who,$domain,$post,@posts)=split(':',$access);
+	    if ($who ne $userid) { next; }
+	    if (!$resource) { next; }
+	    my $res=&Apache::lonnet::unescape($resource);
+	    if (($res =~ /\.(problem|htm|html)/)) {
+		$Count++;
+		$r->print("<br>$Count) ".localtime($date).": $who --> $res");
+#	        if ($post) { 
+#		    $Count++;
+#		    $r->print("<br><b>$Count) Sent data ".join(':',
+#                              &Apache::lonnet::unescape(@posts)).'</b>');
+#		}
+		$r->rflush();
+	    }
+	##    push (@allaccess,unescape($access));
+	    #print $machine;
+	}
+    }
+#    @allaccess=sort(@allaccess);
+#    $Count=0;
+#    foreach my $access (@allaccess) {
+#	my ($date,$resource,$who,$domain,$post,@posts)=split(':',$access);
+#	$Count++;
+#	$r->print("<br>$Count) $date: $who --> $resource");
+#	$r->rflush();
+#	if ($post) { 
+#	    $r->print("<br><b>Sent data ".join(':',unescape(@posts)).'</b>');
+#	}
+#    }
+}
+
+
 
 sub InitAnalysis {
     my ($rid, $student)=@_;
@@ -100,7 +150,7 @@ sub InitAnalysis {
 #    my $Answ=&Apache::lonnet::ssi($URI,('grade_target' => 'analyze'));
 
     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
-    my %Answer=();
+    %Answer=();
     %Answer=&Apache::lonnet::str2hash($Answ);
 
     my $parts='';
@@ -115,17 +165,10 @@ sub InitAnalysis {
     chop($conc);
 
     @Concepts=split(/\@/,$conc);
-    my $show='';
-    foreach my $elm (@{$Answer{"$parts.shown"}}) {
-	$show.="$elm@";
-    }
-    chop($show);
-    @shown=split(/\@/,$show);
-#    $r->print("<br> shown:".$show);#    $r->rflush();
     foreach my $concept (@{$Answer{"$parts.concepts"}}) {
 	foreach my $foil (@{$Answer{"$parts.concept.$concept"}}) {
 	    $foil_to_concept{$foil} = $concept;
-	    $ConceptData{$foil} = $Answer{"$parts.foil.value.$foil"};
+	    #$ConceptData{$foil} = $Answer{"$parts.foil.value.$foil"};
 	}
     }
     return $symb;
@@ -138,7 +181,6 @@ sub Interval {
     my $due = &Apache::lonnet::EXT('resource.$part.duedate',$symb)+1;
     my $opn = &Apache::lonnet::EXT('resource.$part.opendate',$symb);
     my $add=int(($due-$opn)/$Int);
-#$r->print("<br> $opn, to $due add=$add  int=$Int");#$r->rflush();
     $ConceptData{"Int.0"}=$opn;
     for (my $i=1;$i<$Int;$i++) {
 	$ConceptData{"Int.$i"}=$opn+$i*$add;
@@ -155,39 +197,20 @@ sub Interval {
 
 
 sub ShowOpGraph {
-
     my ($InpStr, $Int_No)=@_;
-
-    $r->print(<<ENDPOP);
-    <script language="JavaScript">
-
-    function display(name) {
-	document.forms.displayform.elements.dis.value=name;
-	window.status=name;
-    }
-    function cleardisplay() {
-	document.forms.displayform.elements.dis.value='';
-	window.status='No Concept in particular';
-    }
-
-    </script>
-ENDPOP
-
     my ($rid,$part)=split(/\:/,substr($InpStr,8));
     $ConceptData{"Interval"}=$Int_No;
+    #Initialize the option response true answers
     my $symb=&InitAnalysis($rid,$students[0]);
+    #compute the intervals
     &Interval($rid,$part,$symb);
     my $URI = $hash{'src_'.$rid};
     my $Src = $hash{'title_'.$rid};
     $Src =~ s/\ /"_"/eg;
     $r->print('<br><b>'.$URI.'</b>');
-    for (my $n=1; $n<=$#Concepts+1; $n++ ) {
-	my $tmp=$Concepts[$n-1];
-	$tmp =~ s/</" less than "/eg;
-	$r->print("<br><b>Concept $n</b>:$tmp");
-    }
     $r->rflush();
     
+    #Java script Progress window
     &Create_PrgWin();
     &Update_PrgWin("Starting to analyze problem");
     for (my $index=0;$index<=$#students;$index++) {
@@ -196,10 +219,12 @@ ENDPOP
     }
     &Close_PrgWin();
 
+    $r->print('<br>');
     for (my $k=0; $k<$Int_No; $k++ ) {
- 	my $data1=''; 
-	my $data2='';
-	&DrawGraph(&AdjustData($k).'+'.$Src.'+'.($k+1));
+	&DrawGraph($k,$Src);
+    }
+    for (my $k=0; $k<$Int_No; $k++ ) {
+	&DrawTable($k);
     }
 #$Apache::lonxml::debug=1;
 #&Apache::lonhomework::showhash(%ConceptData);
@@ -208,94 +233,101 @@ ENDPOP
     $r->print("<br><b>Here you can see the Problem:</b><br>$Answ");
 }
 
-sub AdjustData {
+
+sub DrawTable {
     my $k=shift;
     my $Max=0;
     my @data1;
     my @data2;
+    my $Correct=0;
+    my $Wrong=0;
     for (my $n=0; $n<=$#Concepts; $n++ ) {
 	my $tmp=$Concepts[$n];
 	$data1[$n]=$ConceptData{"$tmp.$k.true"};
+	$Correct+=$data1[$n];
 	$data2[$n]=$ConceptData{"$tmp.$k.false"};
+	$Wrong+=$data2[$n];
 	my $Sum=$data1[$n]+$data2[$n];
 	if ( $Max<$Sum ) {$Max=$Sum;}
-	$ConceptData{"$tmp.true"}+=$data1[$n];
-	$ConceptData{"$tmp.false"}+=$data2[$n];
     }
     for (my $n=0; $n<=$#Concepts; $n++ ) {
 	if ($data1[$n]+$data2[$n]<$Max) {
 	    $data2[$n]+=$Max-($data1[$n]+$data2[$n]);
 	}
     }
-    return join(',',@data1).'+'.
-           join(',',@data2).'+'.
-           $Max.'+'.($#data1+1);
+    my $P_No = $#data1+1;
+#    $r->print('<br><b>From: ['.localtime($ConceptData{'Int.'.($k-1)}).
+#              '] To: ['.localtime($ConceptData{"Int.$k"}).']</b>'); 
+    my $Str = "\n".'<table border=2>'.
+              "\n".'<tr>'.
+              "\n".'<th> # </th>'.
+	      "\n".'<th> Concept </th>'.
+	      "\n".'<th> Correct </th>'.
+	      "\n".'<th> Wrong </th>'.
+	      "\n".'</tr>';
+
+    for (my $n=0; $n<=$#Concepts; $n++ ) {
+	$Str .= "\n"."<tr>".
+	        "\n"."<td>".($n+1)."</td>".
+                "\n".'<td bgcolor='.$color{"yellow"}.'> '.$Concepts[$n]." </td>".
+                "\n".'<td bgcolor='.$color{"green"}.'> '.$data1[$n]." </td>".
+                "\n".'<td bgcolor='.$color{"red"}.'> '.$data2[$n]." </td>".
+                "\n"."</tr>";
+    }
+    $Str.='<td></td><td><b>From:['.localtime($ConceptData{'Int.'.$k}).
+          '] To: ['.localtime($ConceptData{'Int.'.($k+1)}-1).
+          "]</b></td><td>$Correct</td><td>$Wrong</td>";
+
+    $Str .= "\n".'</table>';
+
+    $r->print($Str);
+#$Apache::lonxml::debug=1;
+#&Apache::lonhomework::showhash(%ConceptData);
+#$Apache::lonxml::debug=0;
 }
 
 
 sub DrawGraph {
-    my $data=shift;
-    my($data1,$data2,$Max,$P_No,$Src,$k)=split(/\+/,$data);
+    my ($k,$Src)=@_;
+    my $Max=0;
+    my @data1;
+    my @data2;
 
-    my $Str="\n".'<table border=1>'.
-            "\n".'<tr>'.
-	    "\n".'<th> Correct Answers </th>'.
-	    "\n".'<th> Wrong Answers </th>'.
-	    "\n".'<th> From </th>'.
-	    "\n".'<th> To </th>'.
-	    "\n".'</tr>'.
-	    "\n"."<tr>".
-	    "\n"."<td> $data1 </td>".
-            "\n"."<td> $data2 </td>".
-            "\n"."<td> ".localtime($ConceptData{'Int.'.($k-1)})." </td>".
-            "\n"."<td> ".localtime($ConceptData{'Int.'.$k}-1)." </td>".
-            "\n"."</tr></table>";
-    $r->print($Str);
-#    $r->print('<br><b>Correct Answers:</b> '.$data1.
-#              '<br><b>Wrong Answers: </b>'.$data2); 
-#    $r->print('<br><b>From: </b>'.localtime($ConceptData{'Int.'.($k-1)}).
-#              '<br><b>To: </b>'.localtime($ConceptData{"Int.$k"})); 
-
-#   if ( $Max > 1 ) { 
-#	$Max += (10 - $Max % 10);
-#	$Max = int($Max);
-#   }
-#   else { $Max = 1; }
-    my $Titr=($ConceptData{'Interval'}>1) ? $Src.'_interval_'.$k : $Src;
-    $GData=$Titr.'&'.'Concepts'.'&'.'Answers'.'&'.$Max.'&'.$P_No.'&'.$data1.'&'.$data2;
-    if($ConceptData{'Interval'}>1){
-	$r->print('<br><IMG src="/cgi-bin/graph.gif?'.$GData.'" />');
-	return;
+    # Adjust Data and find the Max 
+    for (my $n=0; $n<=$#Concepts; $n++ ) {
+	my $tmp=$Concepts[$n];
+	$data1[$n]=$ConceptData{"$tmp.$k.true"};
+	$data2[$n]=$ConceptData{"$tmp.$k.false"};
+	my $Sum=$data1[$n]+$data2[$n];
+	if ( $Max<$Sum ) {$Max=$Sum;}
     }
-    $r->print('<br>Move your mouse over a bar to find out the concept'); 
-    my $ptr='';
-    $ptr.="\n".'<form method="post" action="" name="displayform"> <input type=text name="dis" size=80> </form>';
-    $ptr.="\n".'<p><img src=/cgi-bin/graph.gif?'.$GData.
-          ' usemap="#Map" border=1>'.
-          "\n".'<map name="Map">';
-    my $gap=12;
-    my $Size=340;
-    my $barsize=($P_No) ? int($Size/$P_No) : 1;
-    for (my $i=0; $i<$P_No; $i++) {
-	my $x1=80+$gap*$i+($i*$barsize);
-	my $x2=$x1+$barsize;
-	my $y1=25;
-	my $y2=350;
-	my $j=$i+1;
-	$ptr.="\n".'<area shape="rect" coords='.$x1.','.$y1.','.$x2.','.$y2.' onMouseOver="display('."'Concept $j: $Concepts[$i]'".'); " href="javascript:alert('."'Concept $j: $Concepts[$i]'".');">';
-    } 
-    $ptr.="\n".'<area shape="default" onMouseOver="cleardisplay(); " href="javascript:alert('.'No Concept  in particular'.');">';
-    $ptr.="\n".'</map></p>';
-    $r->print('<br>'.$ptr.'<br>');
+    for (my $n=0; $n<=$#Concepts; $n++ ) {
+	if ($data1[$n]+$data2[$n]<$Max) {
+	    $data2[$n]+=$Max-($data1[$n]+$data2[$n]);
+	}
+    }
+    my $P_No = $#data1+1;
+
+    if ( $Max > 1 ) { 
+	$Max += (10 - $Max % 10);
+	$Max = int($Max);
+    } else { $Max = 1; }
+
+    my $Titr=($ConceptData{'Interval'}>1) ? $Src.'_interval_'.($k+1) : $Src;
+#    $GData=$Titr.'&Concepts'.'&'.'Answers'.'&'.$Max.'&'.$P_No.'&'.$data1.'&'.$data2;
+    $GData="$Titr&Concepts&Answers&$Max&$P_No&".
+           (join(',',@data1)).'&'.(join(',',@data2));
+
+    $r->print('<IMG src="/cgi-bin/graph.gif?'.$GData.'" border=1/>');
 }
 
 
 sub AnalyzeProblem {
-# -------------------------------- Selecting the number of intervals
+    # selecting the number of intervals
     my $OpSel='';
     my $CurInt = $ENV{'form.interval'};
     if ($CurInt eq '') {$CurMap = '1';}
-    my $Ptr = '<b>Select number of intervals</b>'."\n".
+    my $Ptr = '<br><b>Select number of intervals</b>'."\n".
        	      '<select name="interval">'."\n";                     	     	     
     for (my $n=1;$n<=7;$n++) {	          
 	$Ptr .= '<option';
@@ -305,12 +337,13 @@ sub AnalyzeProblem {
     $Ptr .= '</select>'."\n";
     $r->print( $Ptr );
 
+    #the table of option response problems
     $r->print('<br><b> Option Response Problems in this course:</b><br><br>');
     my $Str = "\n".'<table border=2>'.
               "\n".'<tr>'.
               "\n".'<th> # </th>'.
 	      "\n".'<th> Problem Title </th>'.
-	      "\n".'<th> Resouse </th>'.
+	      "\n".'<th> Resource </th>'.
 	      "\n".'<th> Address </th>'.
 	      "\n".'</tr>';
 
@@ -321,8 +354,8 @@ sub AnalyzeProblem {
                     '" target="_blank">'.$hash{'title_'.$rid}.'</a>';
 	 $Str .= "\n"."<tr>".
 	         "\n"."<td> $P_No </td>".
-                 "\n"."<td bgcolor=#DDFFDD> ".$Temp." </td>".
-                 "\n"."<td bgcolor=#EEFFCC> ".$hash{'src_'.$rid}." </td>".
+                 "\n"."<td bgcolor=".$color{"green"}."> ".$Temp." </td>".
+                 "\n"."<td bgcolor=".$color{"yellow"}."> ".$hash{'src_'.$rid}." </td>".
 	         "\n"."<td> ".'<input type="submit" name="sort" value="'.'Analyze_'.$rid.'" />'.'</td>'.
                  "\n"."</tr>";
 	 $P_No++;
@@ -335,6 +368,7 @@ sub AnalyzeProblem {
 
 
 sub Decide {
+    #deciding the true or false answer belongs to each interval
     my ($type,$foil,$time)=@_; 
     my $k=0;
     while ($time>$ConceptData{'Int.'.($k+1)} && 
@@ -343,6 +377,7 @@ sub Decide {
 }
 
 
+#restore the student submissions and finding the result
 sub OpStatus {
     my ($rid,$student)=@_;
     my ($uname,$udom)=split(/\:/,$student);
@@ -354,47 +389,32 @@ sub OpStatus {
     my @True = ();
     my @False = ();
     my $flag=0;
-    @shown=();
     if ($reshash{'version'}) {
+        my $tries=0;
+	&Apache::lonhomework::showhash(%Answer);
 	for (my $version=1;$version<=$reshash{'version'};$version++) {
 	    my $time=$reshash{"$version:timestamp"};
-	    foreach (sort(split(/\:/,$reshash{$version.':keys'}))) {
-		if (($_=~/\.(\w+)\.(\w+)\.submission$/)) {
-		    #my $Id1=$1; my $Id2=$2;
-		    my $Resp = $reshash{$_};
+	   
+	    foreach my $key (sort(split(/\:/,$reshash{$version.':keys'}))) {
+		if (($key=~/\.(\w+)\.(\w+)\.submission$/)) {
+		    my $Id1 = $1; my $Id2 = $2;
+		    #check if this is a repeat submission, if so skip it
+          	    if ($reshash{"$version:resource.$Id1.previous"}) { next; }
+		    #if no solved this wasn't a real submission, ignore it
+		    if (!defined($reshash{"$version:resource.$Id1.solved"})) {
+			&Apache::lonxml::debug("skipping ");
+			next;
+		    }
+		    my $Resp = $reshash{"$version:$key"};
 		    my %submission=&Apache::lonnet::str2hash($Resp);
 		    foreach (keys %submission) {
-			my $Ansr = $ConceptData{"$_"};
-			#my $Ansr = $Answer{"$Id1.$Id2.foil.value.$_"};
-			#$r->print("<br>shown: ".join(',',@shown)." size=$#shown");     
+			my $Ansr = $Answer{"$Id1.$Id2.foil.value.$_"};
 			if ($submission{$_}) {
-			    #if($#shown==-1){
-				#my @str=split(/\&/,$Resp);
-				#my $garb;
-				#for(my $j=0;$j<=$#str;$j++){
-				#    ($shown[$j],$garb)=split(/\=/,$str[$j]);
-				#}
-				##$r->print("<br>shown: ".join(',',@shown)." size=$#shown");     
-			    #}
 			    if ($submission{$_} eq $Ansr) {
 				&Decide("true",$_,$time );
 			    }
 			    else {&Decide("false",$_,$time );}
 			}
-			#else {
-			#    if ($#shown==-1 && $flag==0) {
-			#	$flag++;
-			#	&InitAnalysis($rid,$student);
-			#    }
-			#    my @erl=split(/\:/,$_);
-			#    for (my $i=0;$i<=$#shown; $i++){
-			#	my $Ans=$Answer{"$Id1.$Id2.foil.value.$shown[$i]"};
-			#	if ($erl[$i] eq $Ans) {
-			#	    &Decide("true",$shown[$i],$time);
-			#	}
-			#	else {&Decide("false",$shown[$i],$time);}
-			#    }
-			#}
 		    }
 	        }	  
 	    }
@@ -506,7 +526,7 @@ sub StudentReport {
 	      "\n".'<th> Tries </th>'.
 	      "\n".'</tr>';
     my ($temp)=keys(%result);
-    unless ($temp=~/^error\:/) {
+    unless ($temp=~/^(con_lost|error|no_such_host)/i) {
         foreach my $CurCol (@cols) {
 	    if (!$CurCol){
 		my $Set=&Apache::lonnet::declutter($hash{'map_id_'.$1});
@@ -598,11 +618,17 @@ sub StudentReport {
 }
 
 sub CreateTable {
+    my ($Hd, $Hid)=@_;
+    if ($ENV{'form.showcsv'}) { 
+	if ( $Hd == 1 ) {
+	    $r->print('<br>"'.$hash{'title_'.$Hid}.'","'.$hash{'src_'.$Hid}.'"');
+	}
+	return;
+    }
     my $ColNo=0;
     foreach (keys(%Header)){ 
 	$ColNo++;
     } 
-    my ($Hd, $Hid)=@_;
     if ( $Hd == 1 ) {
 	$r->print('<br><a href="'.$hash{'src_'.$Hid}.
                   '" target="_blank">'.$hash{'title_'.$Hid}.'</a>');
@@ -619,12 +645,16 @@ sub CreateTable {
 }
 
 sub CloseTable {
+    if ($ENV{'form.showcsv'}) {
+	return;
+    }    
     $r->print("\n".'</table>'."\n");
     $r->rflush();
 }
  
 # ------------------------------------------- Prepare Statistics Table
 sub PreStatTable {
+
     my $CacheDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
                   "_$ENV{'user.domain'}_$cid\_statistics.db";
     my $GraphDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
@@ -657,7 +687,7 @@ sub PreStatTable {
     $Ptr .= '<pre>'.
     '<b>  #Stdnts</b>: Total Number of Students opened the problem.<br>'. 
     '<b>  Tries  </b>: Total Number of Tries for solving the problem.<br>'. 
-    '<b>  Mod   </b> : Maximunm Number of Tries for solving the problem.<br>'. 
+    '<b>  Mod    </b>: Maximunm Number of Tries for solving the problem.<br>'. 
     '<b>  Mean   </b>: Average Number of the tries. [ Tries / #Stdnts ]<br>'.
     '<b>  #YES   </b>: Number of students solved the problem correctly.<br>'. 
     '<b>  #yes   </b>: Number of students solved the problem by override.<br>'.
@@ -681,6 +711,11 @@ sub PreStatTable {
             '</pre>';
 
     $r->print($Ptr);
+    
+    $r->print('Output CSV format: <input type=checkbox name=showcsv onClick="submit()"');
+    if ($ENV{'form.showcsv'}) { $r->print(' checked'); }
+    $r->print('>');
+
     $r->rflush();	
 
     if ((-e "$CacheDB")&&($ENV{'form.sort'} ne 'Recalculate Statistics')) {
@@ -717,15 +752,18 @@ sub usection {
     my ($udom,$unam,$courseid,$ActiveFlag)=@_;
     $courseid=~s/\_/\//g;
     $courseid=~s/^(\w)/\/$1/;
-    foreach (split(/\&/,&Apache::lonnet::reply('dump:'.
-             $udom.':'.$unam.':roles',
-             &Apache::lonnet::homeserver($unam,$udom)))){
-        my ($key,$value)=split(/\=/,$_);
-        $key=&Apache::lonnet::unescape($key);
+
+    my %result=&Apache::lonnet::dump('roles',$udom,$unam);
+    my ($checkForResult)=keys(%result);
+    if ($checkForResult=~/^(con_lost|error|no_such_host)/i) {
+	return -1;
+    }
+    foreach my $key (keys (%result)) {
+	my $value=$result{$key};
         if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
             my $section=$1;
             if ($key eq $courseid.'_st') { $section=''; }
-	    my ($dummy,$end,$start)=split(/\_/,&Apache::lonnet::unescape($value));
+	    my ($dummy,$end,$start)=split(/\_/,$value);
 	    if ( $ActiveFlag ne 'Any' ) {
 		my $now=time;
 		my $notactive=0;
@@ -772,7 +810,7 @@ sub ExtractStudentData {
     my $SecLimit;
     my $MapLimit;
     my ($temp)=keys(%result);
-    unless ($temp=~/^error\:/) {
+    unless ($temp=~/^(con_lost|error|no_such_host)/i) {
         foreach my $CurCol(@cols) {
 	    ($PrOrd,$ResId)=split(/\:/,$CurCol);
 	    if ( !$CurCol ) { next; }
@@ -895,6 +933,7 @@ sub ExtractStudentData {
 }
 
 
+
 # ------------------------------------------------------------ Build page table
 sub tracetable {
     my ($rid,$beenhere)=@_;
@@ -1042,7 +1081,6 @@ sub Build_Statistics {
 
     @list = sort (@list);
 
-
     &Discriminant();
 
     $OpSel2='';
@@ -1182,6 +1220,7 @@ sub Cache_Statistics {
     my @list = ();
     my $Useful;
     my $UnUseful;
+#    $r->print('<input type="hidden" name="show" value="excel" />'."\n"); 
     my %myHeader = reverse( %Header );
     $Pos = $myHeader{$ENV{'form.sort'}};
     if ($Pos > 0) {$Pos++;}
@@ -1230,23 +1269,45 @@ sub TableRow {
     my ($Str,$Idx,$RealIdx)=@_;
     my($PrOrd,$Temp,$StdNo,$TotalTries,$MxTries,$Avg,$YES,$Override,
        $Wrng,$DoD,$SD,$Sk,$_D1,$_D2,$Prob)=split(/\&/,$Str);	
-    my $Ptr =  "\n".'<tr>'.
+    if ($ENV{'form.showcsv'}) {
+        my ($ResId,$Dummy)=split(/\*/,$Prob);
+        my $Ptr =  "\n".'<br>'.
+               "\n".'"'.($RealIdx+1).'",'.
+               "\n".'"'.$hash{'title_'.$ResId}.$Dummy.'",'.
+               "\n".'"'.$hash{'src_'.$ResId}.'",'.
+               "\n".'"'.$StdNo.'",'.
+               "\n".'"'.$TotalTries.'",'.
+               "\n".'"'.$MxTries.'",'.
+               "\n".'"'.$Avg.'",'.
+               "\n".'"'.$YES.'",'.
+               "\n".'"'.$Override.'",'.
+               "\n".'"'.$Wrng.'",'.
+               "\n".'"'.$DoD.'",'.
+               "\n".'"'.$SD.'",'.
+               "\n".'"'.$Sk.'",'.
+               "\n".'"'.$_D1.'",'.
+	       "\n".'"'.$_D2.'"';
+        $r->print("\n".$Ptr);
+    }
+    else{
+        my $Ptr =  "\n".'<tr>'.
                "\n".'<td>'.($RealIdx+1).'</td>'.
           #     "\n".'<td>'.$PrOrd.$Temp.'</td>'.
                "\n".'<td>'.$Temp.'</td>'.
-               "\n".'<td bgcolor="#EEFFCC"> '.$StdNo.'</td>'.
-               "\n".'<td bgcolor="#EEFFCC">'.$TotalTries.'</td>'.
-               "\n".'<td bgcolor="#EEFFCC">'.$MxTries.'</td>'.
-               "\n".'<td bgcolor="#DDFFFF">'.$Avg.'</td>'.
-               "\n".'<td bgcolor="#DDFFFF"> '.$YES.'</td>'.
-               "\n".'<td bgcolor="#DDFFFF"> '.$Override.'</td>'.
-               "\n".'<td bgcolor="#FFDDDD"> '.$Wrng.'</td>'.
-               "\n".'<td bgcolor="#FFDDDD">'.$DoD.'</td>'.
-               "\n".'<td bgcolor="#DDFFDD"> '.$SD.'</td>'.
-               "\n".'<td bgcolor="#DDFFDD"> '.$Sk.'</td>'.
-               "\n".'<td bgcolor="#FFDDFF"> '.$_D1.'</td>'.
-	       "\n".'<td bgcolor="#FFDDFF"> '.$_D2.'</td>';
-    $r->print("\n".$Ptr.'</tr>' );
+               "\n".'<td bgcolor='.$color{"yellow"}.'> '.$StdNo.'</td>'.
+               "\n".'<td bgcolor='.$color{"yellow"}.'>'.$TotalTries.'</td>'.
+               "\n".'<td bgcolor='.$color{"yellow"}.'>'.$MxTries.'</td>'.
+               "\n".'<td bgcolor='.$color{"gb"}.'>'.$Avg.'</td>'.
+               "\n".'<td bgcolor='.$color{"gb"}.'> '.$YES.'</td>'.
+               "\n".'<td bgcolor='.$color{"gb"}.'> '.$Override.'</td>'.
+               "\n".'<td bgcolor='.$color{"red"}.'> '.$Wrng.'</td>'.
+               "\n".'<td bgcolor='.$color{"red"}.'> '.$DoD.'</td>'.
+               "\n".'<td bgcolor='.$color{"green"}.'> '.$SD.'</td>'.
+               "\n".'<td bgcolor='.$color{"green"}.'> '.$Sk.'</td>'.
+               "\n".'<td bgcolor='.$color{"purple"}.'> '.$_D1.'</td>'.
+	       "\n".'<td bgcolor='.$color{"purple"}.'> '.$_D2.'</td>';
+        $r->print("\n".$Ptr.'</tr>' );
+    }
     $GraphDat{$RealIdx}=$DoD.':'.$Wrng;
 }
 
@@ -1298,72 +1359,102 @@ sub GetGraphData {
 
 sub initial {
 # --------------------------------- Initialize the global varaibles
-  undef @students;
-  undef @cols;
-  undef %maps;
-  undef %section;
-  undef %StuBox;
-  undef @list;
-  undef %CachData;
-  undef %GraphDat;
-  undef %DiscFac;
-  undef %OpResp;
-  undef %ConceptData;
-  undef $CurMap;
-  undef $CurSec;
-  undef $CurStu;
-  undef $p_count;
-  undef $Pos;
-  undef $GData;
-  $DiscFlag=0; 
-  $P_Order=100000;
-  $HWN=$P_Order;
+    undef @students;
+    undef @cols;
+    undef %maps;
+    undef %section;
+    undef %StuBox;
+    undef @list;
+    undef %CachData;
+    undef %GraphDat;
+    undef %DiscFac;
+    undef %OpResp;
+    undef %ConceptData;
+    undef $CurMap;
+    undef $CurSec;
+    undef $CurStu;
+    undef $p_count;
+    undef $Pos;
+    undef $GData;
+    $DiscFlag=0; 
+    $P_Order=100000;
+    $HWN=$P_Order;
+    &setbgcolor(0);
+}
+
+# For loading the colored table for display or un-colored for print
+sub setbgcolor {
+    my $PrintTable=shift;
+    undef %color;
+    if ($PrintTable){
+	$color{"gb"}="#FFFFFF";
+	$color{"red"}="#FFFFFF";
+	$color{"yellow"}="#FFFFFF";
+	$color{"green"}="#FFFFFF";
+	$color{"purple"}="#FFFFFF";
+    } else {
+	$color{"gb"}="#DDFFFF";
+	$color{"red"}="#FFDDDD";
+	$color{"yellow"}="#EEFFCC";
+	$color{"green"}="#DDFFDD";
+	$color{"purple"}="#FFDDFF";
+    }
 }
 
+#	my $CacheDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
+#                  "_$ENV{'user.domain'}_$cid\_classlist.db";
+#	if (-e "$CacheDB") {
+#	    if (tie(%students,'GDBM_File',"$CacheDB",&GDBM_READER,0640)) {
+#		&CachClassList();
+#	    }
+#            else {
+#	        $r->print("Unable to tie hash to db file");
+#            }
+#        }
+#        else {
+#	    if (tie(%students,'GDBM_File',$CacheDB,&GDBM_WRCREAT,0640)) {
+#	        &MakeClassList();
+#	    }
+#            else {
+#	        $r->print("Unable to tie hash to db file");
+#            }
+#        }
+#        untie(%students);
 
 sub ClassList {
 
     &GetStatus();
 
     $cid=$ENV{'request.course.id'};
-    my $chome=$ENV{'course.'.$cid.'.home'};
     my ($cdom,$cnum)=split(/\_/,$cid);
 # ----------------------- Get first and last resource, see if there is anything
     $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
     $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
     if (($firstres) && ($lastres)) {
-# ----------------------------------------------------------------- Render page
-	my $classlst=&Apache::lonnet::reply
-                 ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
+
 	my $StudNo = 0;
 	my $now=time;
-	unless ($classlst=~/^error\:/) {
-	    foreach (sort split(/\&/,$classlst)) {
-		my ($name,$value)=split(/\=/,$_);
-		my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
-		$name=&Apache::lonnet::unescape($name);
-		my ($sname,$sdom)=split(/\:/,$name);
+	my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
+
+	my ($checkForError)=keys(%classlist);
+	if($checkForError=~/^(con_lost|error|no_such_host)/i) {
+	    $r->print('<h1>Could not access course data</h1>');
+	} else {
+	    foreach my $name (sort keys (%classlist)) {
+		my $value=$classlist{$name};
+		my ($end,$start)=split(/\:/,$value);
 		my $active=1;
 		my $Status=$ENV{'form.status'};
 		$Status = ($Status) ? $Status : 'Active';
 		if ( ( ($end) && $now > $end ) && 
-                     ( ($Status eq 'Active') ) ) { $active=0; }
+		     ( ($Status eq 'Active') ) ) { $active=0; }
 		if ( ($Status eq 'Expired') && 
-                     ($end == 0 || $now < $end) ) { $active=0; }
-#//new
-#		$Status = 'Any';
-#end new
+		     ($end == 0 || $now < $end) ) { $active=0; }
 		if ($active) {
 		    my $thisindex=$#students+1;
-		    $name=&Apache::lonnet::unescape($name);
 		    $students[$thisindex]=$name;
 		    my ($sname,$sdom)=split(/\:/,$name);
-		    #my %reply=&Apache::lonnet::idrget($sdom,$sname);
-		    #my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
-		    #         ':environment:lastname&generation&firstname&middlename',
-		    #         &Apache::lonnet::homeserver($sname,$sdom));
 		    my $ssec=&usection($sdom,$sname,$cid,$Status);
-#		    if ($ssec==-1 ) {next;}
 		    if ($ssec==-1 || $ssec eq 'adm' ) {next;}
 		    $ssec=($ssec) ? $ssec : '(none)';
 		    #$ssec=(int($ssec)) ? int($ssec) : $ssec;
@@ -1373,12 +1464,10 @@ sub ClassList {
 			$StuBox{$sname}=$sdom;
 		    }
 		    $StudNo++;
-	        }
+		}
 	    }
 	}
-	else {
-	    $r->print('<h1>Could not access course data</h1>');
-	} 
+
         $r->print("Total number of students : ".($#students+1));
         $r->rflush();
 # --------------- Find all assessments and put them into some linear-like order
@@ -1416,10 +1505,12 @@ sub Title {
     $r->rflush();	
 }
 
+
 sub CreateForm {
     $r->print("\n".'<form name=stat method=post action="/adm/statistics" >');
     my $content = $ENV{'form.sort'};
-    if ($content eq '' || $content eq 'Return to Menu') {
+    if (!($ENV{'form.showcsv'}) && 
+        ($content eq '' || $content eq 'Return to Menu')) {
 	my $Ptr = '<h3>';
 	$Ptr .= '<input type=submit name=sort value="Problem Stats"/>';
 	$Ptr .= '<br><br>';
@@ -1427,9 +1518,11 @@ sub CreateForm {
 	$Ptr .= '<br><br>';
 	$Ptr .= '<input type=submit name=sort value="Student Assessment"/>';
 	$Ptr .= '</h3>';
+	#$Ptr .= '<input type=submit name=sort value="Activity Log"/>';
 	$r->print( $Ptr );
     }
     else {
+	&ClassList();
 	if ( $content eq 'Student Assessment' || 
 	     $content eq 'Create Student Report' ) {
 	    &MapSecOptions();
@@ -1449,10 +1542,20 @@ sub CreateForm {
 
 sub Menu {
     &initial();
+#    my $fn=$ENV{'request.course.fn'};
+#    $r->print( '<br>'.$fn.'<br>' );
+
+#    $Apache::lonxml::debug=1;
+#    &Apache::lonhomework::showhash(%ENV);
+#    $Apache::lonxml::debug=0;
+
     &Title();
-    &ClassList();
     my $InpStr = $ENV{'form.sort'};
-    if ($InpStr=~/^Analyze\_/) {
+    if ($InpStr eq 'Activity Log') {
+	&Activity();
+    }    
+    elsif ($InpStr=~/^Analyze\_/) {
+	&ClassList();
 	&ShowOpGraph($InpStr,$ENV{'form.interval'});
     }
     elsif ( $InpStr eq 'DoDiff Graph' || $InpStr eq '%Wrong Graph' ) {      
@@ -1468,6 +1571,7 @@ sub Menu {
     $r->rflush();
 }
 
+
 sub StudentOptions {
     my $OpSel5='';
     $CurStu = $ENV{'form.student'};
@@ -1524,7 +1628,7 @@ sub GetStatus {
 sub MapSecOptions {
 # ----------------------------------- Loading the Maps Combobox
     my $Ptr = '<br>';
-    $Ptr .= '<br><input type="submit" name="sort" value="Return to Menu" />';
+    $Ptr .= '<input type="submit" name="sort" value="Return to Menu" />';
     $Ptr .= '<br><b> Select &nbsp; Map &nbsp; &nbsp; </b>'."\n".
        	    '<select name="maps">'."\n". 
 	    '<option '.$OpSel3.'>All Maps</option>';