--- loncom/interface/lonstatistics.pm	2002/05/16 01:27:05	1.19
+++ loncom/interface/lonstatistics.pm	2002/05/29 17:58:45	1.22
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # (Publication Handler
 #
-# $Id: lonstatistics.pm,v 1.19 2002/05/16 01:27:05 minaeibi Exp $
+# $Id: lonstatistics.pm,v 1.22 2002/05/29 17:58:45 stredwic Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -31,7 +31,7 @@
 # 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 Behrouz Minaei
-# 5/12, 5/14, 5/15 Behrouz Minaei
+# 5/12,5/14,5/15,5/19,5/26 Behrouz Minaei
 #
 ###
 
@@ -86,6 +86,51 @@ my %Header = (0,"Homework Sets Order",1,
               9,"S.D.",10,"Skew.",11,"D.F.1st",12,"D.F.2nd");
 my %Answer = ();
 
+
+sub unescape {
+    my $str=shift;
+    $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
+    return $str;
+}
+
+sub Activity {
+    my $file="/home/minaeibi/minaei";
+    my $userid='billskat';
+    $r->print("<br>Using $file");
+    $r->rflush();
+    open(FILEID, "<$file");
+    my $line;
+    my @allaccess;
+    while ($line=<FILEID>) {
+	my ($time,$machine,$what)=split(':',$line);
+	#$r->print("time=$time machine=$machine:-> $what\n");
+	#$r->rflush();
+	$what=&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; }
+	    push (@allaccess,unescape($access));
+	    #print $machine;
+	}
+    }
+    @allaccess=sort(@allaccess);
+    my $Count=0;
+    foreach my $access (@allaccess) {
+	my ($date,$resource,$who,$domain,$post,@posts)=split(':',$access);
+	$Count++;
+#	$r->print("$Count ".localtime($date)." $who $resource\n");
+	$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)=@_;
     my ($uname,$udom)=split(/\:/,$student);
@@ -174,6 +219,9 @@ sub ShowOpGraph {
     for (my $k=0; $k<$Int_No; $k++ ) {
 	&DrawGraph($k,$Src);
     }
+    for (my $k=0; $k<$Int_No; $k++ ) {
+	&DrawTable($k);
+    }
 #$Apache::lonxml::debug=1;
 #&Apache::lonhomework::showhash(%ConceptData);
 #$Apache::lonxml::debug=0;
@@ -182,14 +230,13 @@ sub ShowOpGraph {
 }
 
 
-sub DrawGraph {
-    my ($k,$Src)=@_;
+sub DrawTable {
+    my $k=shift;
     my $Max=0;
     my @data1;
     my @data2;
     my $Correct=0;
     my $Wrong=0;
-    # Adjust Data and find the Max 
     for (my $n=0; $n<=$#Concepts; $n++ ) {
 	my $tmp=$Concepts[$n];
 	$data1[$n]=$ConceptData{"$tmp.$k.true"};
@@ -223,17 +270,39 @@ sub DrawGraph {
                 "\n"."<td bgcolor=#FFDDDD> ".$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;
+}
 
-#    $Str.='<td></td><td><b>From:['.localtime($ConceptData{'Int.$k'}).
-#          '] To: ['.localtime($ConceptData{'Int.'.($k+1)}).
-#          "]</b></td><td>$Correct</td><td>$Wrong</td>";#
 
-    $Str .= "\n".'</table>';
+sub DrawGraph {
+    my ($k,$Src)=@_;
+    my $Max=0;
+    my @data1;
+    my @data2;
 
-#    $r->print($Str);
+    # 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;}
+    }
+    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);
@@ -255,7 +324,7 @@ sub AnalyzeProblem {
     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';
@@ -271,7 +340,7 @@ sub AnalyzeProblem {
               "\n".'<tr>'.
               "\n".'<th> # </th>'.
 	      "\n".'<th> Problem Title </th>'.
-	      "\n".'<th> Resouse </th>'.
+	      "\n".'<th> Resource </th>'.
 	      "\n".'<th> Address </th>'.
 	      "\n".'</tr>';
 
@@ -547,7 +616,7 @@ sub StudentReport {
 
 sub CreateTable {
     my ($Hd, $Hid)=@_;
-    if ($ENV{'form.showcsv'}) {
+    if ($ENV{'form.showcsv'}) { 
 	if ( $Hd == 1 ) {
 	    $r->print('<br>"'.$hash{'title_'.$Hid}.'","'.$hash{'src_'.$Hid}.'"');
 	}
@@ -615,8 +684,8 @@ 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>  Max   </b> : Maximunm Number of Tries for solving the problem.<br>'. 
-    '<b>  Avg.   </b>: Average Number of the tries. [ Tries / #Stdnts ]<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>'.
     '<b>  %Wrng  </b>: Percentage of students tried to solve the problem but'.
@@ -624,10 +693,10 @@ sub PreStatTable {
 #    '  DoDiff : Degree of Difficulty of the problem. [ Tries/(#YES+#yes+0.1) ]<br>'. Kashy formula
     '<b>  DoDiff </b>: Degree of Difficulty of the problem. [ 1 - ((#YES+#yes) / Tries) ]<br>'. #Gerd formula
     '<b>  S.D.  </b> : Standard Deviation of the tries.'.
-    '[ sqrt(sum((Xi - Avg.)^2)) / (#Stdnts-1)'.
+    '[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1)'.
     ' where Xi denotes every student\'s tries ]<br>'.
     '<b>  Skew.  </b>: Skewness of the students tries.'.
-	' [ (sqrt( sum((Xi - Avg.)^3) / #Stdnts)) / (S.D.^3) ]<br>'.
+	' [ (sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3) ]<br>'.
     '<b>  Dis.F. </b>: Discrimination Factor: A Standard for '.
 	'evaluating the problem according to a Criterion<br>'.
 	'<b>           [Applied Criterion in %27 Upper Students - '.
@@ -680,15 +749,14 @@ 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);
+    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;
@@ -858,6 +926,7 @@ sub ExtractStudentData {
 }
 
 
+
 # ------------------------------------------------------------ Build page table
 sub tracetable {
     my ($rid,$beenhere)=@_;
@@ -1144,9 +1213,8 @@ sub Cache_Statistics {
     my @list = ();
     my $Useful;
     my $UnUseful;
+#    $r->print('<input type="hidden" name="show" value="excel" />'."\n"); 
     my %myHeader = reverse( %Header );
-    #&Apache::lonnet::delenv('form_');
-    #&Apache::lonnet::delenv('test'); 
     $Pos = $myHeader{$ENV{'form.sort'}};
     if ($Pos > 0) {$Pos++;}
     $p_count = 0;
@@ -1332,7 +1400,6 @@ 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'}};
@@ -1347,43 +1414,39 @@ sub ClassList {
 #	my ($temp)=keys(%students);
 #	unless ($temp=~/^error\:/) {
 #	    foreach my $KeyPoint(sort keys(%students)) {
-	my $classlst=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.':classlist',$chome);
+
+
+	my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
+
 	my $StudNo = 0;
 	my $now=time;
-	unless ($classlst=~/^error\:/) {
-	    foreach my $KeyPoint(sort split(/\&/,$classlst)) {
-		my ($name,$value)=split(/\=/,$KeyPoint);
-		my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
-		my $active=1;
-		my $Status=$ENV{'form.status'};
-		$Status = ($Status) ? $Status : 'Active';
-		if ( ( ($end) && $now > $end ) && 
-                     ( ($Status eq 'Active') ) ) { $active=0; }
-		
-		if ( ($Status eq 'Expired') && 
-                     ($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 $ssec=&usection($sdom,$sname,$cid,$Status);
-		    if ($ssec==-1 || $ssec eq 'adm' ) {next;}
-		    $ssec=($ssec) ? $ssec : '(none)';
-		    #$ssec=(int($ssec)) ? int($ssec) : $ssec;
-		    $section{$ssec}=$ssec;
-		    if ($CurSec eq 'All Sections' || $ssec eq $CurSec) {
-			$students[$StudNo]=$name;
-			$StuBox{$sname}=$sdom;
-		    }
-		    $StudNo++;
-	        }
+	foreach my $name (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; }
+	    if ( ($Status eq 'Expired') && 
+		 ($end == 0 || $now < $end) ) { $active=0; }
+	    if ($active) {
+		my $thisindex=$#students+1;
+		$students[$thisindex]=$name;
+		my ($sname,$sdom)=split(/\:/,$name);
+		my $ssec=&usection($sdom,$sname,$cid,$Status);
+		if ($ssec==-1 || $ssec eq 'adm' ) {next;}
+		$ssec=($ssec) ? $ssec : '(none)';
+		#$ssec=(int($ssec)) ? int($ssec) : $ssec;
+		$section{$ssec}=$ssec;
+		if ($CurSec eq 'All Sections' || $ssec eq $CurSec) {
+		    $students[$StudNo]=$name;
+		    $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
@@ -1408,74 +1471,6 @@ sub ClassList {
 }
 
 
-sub ClassListnew {
-
-    &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)) {
-
-	my %students = &Apache::lonnet::dump('classlist',$cdom,$cnum);
-	my $StudNo = 0;
-	my $now=time;
-	my ($temp)=keys(%students);
-	unless ($temp=~/^error\:/) {
-	    foreach (sort keys(%students)) {
-		my ($name,$value)=split(/\=/,$_);
-		my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
-		$name=&Apache::lonnet::unescape($name);
-		my ($sname,$sdom)=split(/\:/,$name);
-		my $active=1;
-		my $Status=$ENV{'form.status'};
-		$Status = ($Status) ? $Status : 'Active';
-		if ( ( ($end) && $now > $end ) && 
-                     ( ($Status eq 'Active') ) ) { $active=0; }
-		if ( ($Status eq 'Expired') && 
-                     ($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;
-		    $section{$ssec}=$ssec;
-		    if ($CurSec eq 'All Sections' || $ssec eq $CurSec) {
-			$students[$StudNo]=$name;
-			$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
-	&tracetable($firstres,'&'.$lastres.'&');
-    }
-
-# ------------------------------------------------------------- End render page 
-    else {
-	$r->print('<h3>Undefined course sequence</h3>');
-    }
-}
-
-
 sub Title {
     $r->print('<html><head><title>LON-CAPA Statistics</title></head>');
     $r->print('<body bgcolor="#FFFFFF">'.
@@ -1493,8 +1488,8 @@ sub Title {
 sub CreateForm {
     $r->print("\n".'<form name=stat method=post action="/adm/statistics" >');
     my $content = $ENV{'form.sort'};
-    if (!($ENV{'form.showcsv'}) && #|| !($ENV{'csv'})) && 
-         ($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>';
@@ -1502,6 +1497,8 @@ 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"/>';
+	#$Ptr .= '</h3>';
 	$r->print( $Ptr );
     }
     else {
@@ -1530,7 +1527,10 @@ sub Menu {
 #    $Apache::lonxml::debug=0;
     &Title();
     my $InpStr = $ENV{'form.sort'};
-    if ($InpStr=~/^Analyze\_/) {
+    if ($InpStr eq 'Activity Log') {
+	&Activity();
+    }    
+    elsif ($InpStr=~/^Analyze\_/) {
 	&ClassList();
 	&ShowOpGraph($InpStr,$ENV{'form.interval'});
     }
@@ -1547,6 +1547,7 @@ sub Menu {
     $r->rflush();
 }
 
+
 sub StudentOptions {
     my $OpSel5='';
     $CurStu = $ENV{'form.student'};