--- loncom/interface/statistics/lonproblemstatistics.pm	2002/08/12 18:21:42	1.18
+++ loncom/interface/statistics/lonproblemstatistics.pm	2003/02/25 20:47:47	1.40
@@ -1,7 +1,6 @@
 # The LearningOnline Network with CAPA
-# (Publication Handler
 #
-# $Id: lonproblemstatistics.pm,v 1.18 2002/08/12 18:21:42 albertel Exp $
+# $Id: lonproblemstatistics.pm,v 1.40 2003/02/25 20:47:47 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -30,12 +29,12 @@
 # 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 Behrouz Minaei
-# 5/12,5/14,5/15,5/19,5/26,7/16,7/25,7/29,8/5  Behrouz Minaei
+# 1/22,2/1,2/6,2/25,3/2,3/26,4/7,5/6 Behrouz Minaei
+# 5/12,5/26,7/16,7/29,8/5,10/31  Behrouz Minaei
 #
 ###
 
-package Apache::lonproblemstatistics; 
+package Apache::lonproblemstatistics;
 
 use strict;
 use Apache::lonnet();
@@ -44,654 +43,406 @@ use Apache::loncoursedata;
 use GDBM_File;
 
 
-sub BuildProblemStatisticsPage {
+sub InitializeProblemStatistics {
     my ($cacheDB, $students, $courseID, $c, $r)=@_;
     my %cache;
 
     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
-        return '<html><body>Unable to tie database.</body></html>';
+        $r->print('Unable to tie database1.');
+        return ('ERROR', undef);
     }
 
-    my $Ptr = '';
-    $Ptr .= '<table border="0"><tbody>';
-    $Ptr .= '<tr><td align="right"><b>Select Map</b></td>'."\n";
-    $Ptr .= '<td align="left">';
-    $Ptr .= &Apache::lonhtmlcommon::MapOptions(\%cache, 'ProblemStatistics',
-                                               'Statistics');
-    $Ptr .= '</td></tr>'."\n";
-    $Ptr .= '<tr><td align="right"><b>Sorting Type:</b></td>'."\n";
-    $Ptr .= '<td align="left">'."\n";
-    $Ptr .= &Apache::lonhtmlcommon::AscendOrderOptions(
-                                            $cache{'ProblemStatisticsAscend'}, 
-                                            'ProblemStatistics',
-                                            'Statistics');
-    $Ptr .= '</td></tr>'."\n";
-    $Ptr .= &ProblemStatisticsButtons($cache{'DisplayFormat'});
-    $Ptr .= '</table>';
-    $Ptr .= &ProblemStatisticsLegend();
-    $r->print($Ptr);
-    $r->rflush();
-
-    untie(%cache);
-    &Create_PrgWin($r);
-    my $count=0;
-    foreach (@$students) {
-        &Update_PrgWin(scalar(@$students),$count,$_,$r);
-        my $courseData = 
-            &Apache::loncoursedata::DownloadCourseInformation($_, $courseID);
-        last if ($c->aborted());
-        if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
-            &Apache::loncoursedata::ProcessStudentData(\%cache, 
-                                                       $courseData, $_);
-            untie(%cache);
+    # Remove students who don't have the proper section.
+    my @sectionsSelected = split(':',$cache{'sectionsSelected'});
+    for(my $studentIndex=((scalar @$students)-1); $studentIndex>=0;
+        $studentIndex--) {
+        my $value = $cache{$students->[$studentIndex].':section'};
+        my $found = 0;
+        foreach (@sectionsSelected) {
+            if($_ eq 'none') {
+                if($value eq '' || !defined($value) || $value eq ' ') {
+                    $found = 1;
+                    last;
+                }
+            } else {
+                if($value eq $_) {
+                    $found = 1;
+                    last;
+                }
+            }
+        }
+        if($found == 0) {
+            splice(@$students, $studentIndex, 1);
         }
-        $count++;
     }
-    &Close_PrgWin($r);
-    if($c->aborted()) { return; }
 
-    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
-        return '<html><body>Unable to tie database.</body></html>';
+    my $isNotCached = 0;
+    my $lastStatus = (defined($cache{'StatisticsLastStatus'})) ?
+                     $cache{'StatisticsLastStatus'} : 'Nothing';
+    my $whichStudents = join(':::',sort(@$students));
+    if(!defined($cache{'StatisticsCached'}) ||
+       $lastStatus ne $cache{'Status'} ||
+       $whichStudents ne $cache{'StatisticsWhichStudents'}) {
+        $isNotCached = 1;
     }
 
-    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", 13, "Disc.");
-    my $color=&setbgcolor(0);
-
-    my $state=$ENV{'form.ProblemStatisticsHeading'}; 
+    untie(%cache);
+    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
+        $r->print('Unable to tie database.2');
+        return ('ERROR', undef);
+    }
+    if($isNotCached && defined($cache{'StatisticsCached'})) {
+        my @statkeys = split(':::', $cache{'StatisticsKeys'});
+        delete $cache{'StatisticsKeys'};
+        delete $cache{'StatisticsCached'};
+        foreach(@statkeys) {
+            delete $cache{$_};
+        }
+    }
 
-    my $TempCache;
+    untie(%cache);
+    if($isNotCached) {
+        &Apache::loncoursedata::DownloadStudentCourseDataSeparate($students,
+                                                                  'true',
+                                                                  $cacheDB,
+                                                                  'true',
+                                                                  'true',
+                                                                  $courseID,
+                                                                  $r, $c);
+    }
+    if($c->aborted()) { return ('ERROR', undef); }
 
-    if ($state) {
-	$TempCache=&CacheStatisticsTable($state,\%cache,\%Header,
-					 $r,$color);
-    } else {    
-	my %discriminant=();
-	my @list=();
-	my %Discuss=&Apache::loncoursedata::LoadDiscussion($courseID);
-	my $index=0;
-	foreach (@$students) {
-	    $index++;
-	    &ExtractStudentData(\%cache, $_, \@list,\%Discuss, $r,
-                                \%discriminant);
-	}
-	my ($upper, $lower) = &Discriminant(\%discriminant,$r);
-	$TempCache= &BuildStatisticsTable(\%cache, $upper, $lower, 
-					   \@list, \%Header, $students,
-					   $r, $color);
+    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
+        $r->print('Unable to tie database.3');
+        return ('ERROR', undef);
+    }
+    my $problemData;
+    if($isNotCached) {
+        ($problemData) = &ExtractStudentData(\%cache, $students);
+        &CalculateStatistics($problemData, \%cache, $courseID);
     }
     untie(%cache);
 
-    foreach (keys %$TempCache) {
-        last if ($c->aborted());
-        if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
-	    $cache{$_}=$TempCache->{$_};
-
-            untie(%cache);
+    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
+        $r->print('Unable to tie database.4');
+        return ('ERROR', undef);
+    }
+    if($isNotCached) {
+        foreach(keys(%$problemData)) {
+            $cache{$_} = $problemData->{$_};
         }
+        $cache{'StatisticsKeys'} = join(':::', keys(%$problemData));
+        $cache{'StatisticsCached'} = 'true';
+        $cache{'StatisticsLastStatus'} = $cache{'Status'};
+        $cache{'StatisticsWhichStudents'} = $whichStudents;
+    }
+    untie(%cache);
+
+    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
+        $r->print('Unable to tie database.5');
+        return ('ERROR', undef);
     }
-    if($c->aborted()) { return; }
+
+    my $orderedProblems = &SortProblems(\%cache,
+                                        $cache{'ProblemStatisticsSort'},
+                                        $cache{'SortProblems'},
+                                        $cache{'ProblemStatisticsAscend'});
     untie(%cache);
+
+    return ('OK', $orderedProblems);
 }
 
+sub BuildProblemStatisticsPage {
+    my ($cacheDB, $students, $courseID, $c, $r)=@_;
 
-#---- Problem Statistics Web Page ---------------------------------------
+    my @Header = ("Homework Sets Order","#Stdnts","Tries","Mod",
+                  "Mean","#YES","#yes","%Wrng","DoDiff",
+                  "S.D.","Skew.","D.F.1st","D.F.2nd");
+    my $color=&setbgcolor(0);
+    my %cache;
 
-sub CreateProblemStatisticsTableHeading {
-    my ($displayFormat,$sequenceSource,$sequenceTitle,$headings,$r)=@_;
-    if($displayFormat eq 'Display CSV Format') {
-        $r->print('<br>"'.$sequenceTitle.'","');
-        $r->print($sequenceSource.'"');
-	return;
-    }
-    if ($sequenceSource eq 'Sorted by: ') {
-	$r->print('<br><b>'.$sequenceSource.$sequenceTitle.'</b>');
-    } else {
-        $r->print('<br><a href="'.$sequenceSource.
-		  '" target="_blank">'.$sequenceTitle.'</a>');
+    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
+        $r->print('Unable to tie database.6');
+        return;
     }
-    my $Result = "\n".'<table border=2><tr><th>P#</th>'."\n";
-    for(my $nIndex=0; $nIndex < (scalar (keys %$headings)); $nIndex++) { 
-	$Result .= '<th>'.'<input type="submit" name="';
-        $Result .= 'ProblemStatisticsHeading" value="';
-        $Result .= $headings->{$nIndex}.'" />'.'</th>'."\n";
+    my $Ptr = '';
+    $Ptr .= '<table border="0" cellspacing="5"><tbody>';
+    $Ptr .= '<tr><td align="right"><b>Select Map</b></td>'."\n";
+    $Ptr .= '<td align="left">';
+    $Ptr .= &Apache::lonhtmlcommon::MapOptions(\%cache, 'Statistics',
+                                               'Statistics');
+    $Ptr .= '</td></tr>'."\n";
+    $Ptr .= '<tr><td align="right"><b>Sorting Type:</b></td>'."\n";
+    $Ptr .= '<td align="left">'."\n";
+    $Ptr .= &Apache::lonhtmlcommon::AscendOrderOptions(
+                                           $cache{'ProblemStatisticsAscend'},
+                                           'ProblemStatistics',
+                                           'Statistics');
+    $Ptr .= '</td></tr>'."\n";
+    $Ptr .= '<tr><td align="right"><b>Select Sections</b>';
+    $Ptr .= '</td>'."\n";
+    $Ptr .= '<td align="left">'."\n";
+    my @sections = split(':',$cache{'sectionList'});
+    my @sectionsSelected = split(':',$cache{'sectionsSelected'});
+    $Ptr .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
+    $Ptr .= '</td></tr>'."\n";
+    $Ptr .= &ProblemStatisticsButtons($cache{'DisplayFormat'},
+                                      $cache{'DisplayLegend'},
+                                      $cache{'SortProblems'});
+    $Ptr .= '</table>';
+    if($cache{'DisplayLegend'} eq 'Show Legend') {
+        $Ptr .= &ProblemStatisticsLegend();
     }
-    $Result .= "\n".'</tr>'."\n";    
-    $r->print($Result);
+    $r->print($Ptr);
     $r->rflush();
-}
+    untie(%cache);
 
-sub CloseTable {
-    my ($cache,$r)=@_;
-    if($cache->{'DisplayFormat'} eq 'Display CSV Format') {
-	return;
-    }    
-    $r->print("\n".'</table>'."\n");
-    $r->rflush();
+    my ($result, $orderedProblems) =
+        &InitializeProblemStatistics($cacheDB, $students, $courseID, $c, $r);
+    if($result ne 'OK') {
+        return;
+    }
+
+    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
+        $r->print('Unable to tie database.6');
+        return;
+    }
+    &BuildStatisticsTable(\%cache, $cache{'DisplayFormat'},
+                          $cache{'SortProblems'}, $orderedProblems,
+                          \@Header, $r, $color);
+    untie(%cache);
+
+    return;
 }
 
+sub BuildGraphicChart {
+    my ($graph,$cacheDB,$courseDescription,$students,$courseID,$r,$c)=@_;
+    my %cache;
+    my $max;
 
-# Create progress
-sub Create_PrgWin {
-    my ($r)=@_;
-    $r->print(<<ENDPOP);
-    <script>
-    popwin=open('','popwin','width=400,height=100');
-    popwin.document.writeln('<html><body bgcolor="#88DDFF">'+
-      '<title>LON-CAPA Statistics</title>'+
-      '<h4>Computation Progress</h4>'+
-      '<form name=popremain>'+
-      '<input type=text size=35 name=remaining value=Starting></form>'+
-      '</body></html>');
-    popwin.document.close();
-    </script>
-ENDPOP
+    my $title = '';
+    if($graph eq 'DoDiffGraph') {
+	$title = 'Degree-of-Difficulty';
+    } else {
+	$title = 'Wrong-Percentage';
+    }
 
-    $r->rflush();
-}
+    my $currentSequence = -1;
+    my $sortProblems = 'Sort Within Sequence';
 
-# update progress
-sub Update_PrgWin {
-    my ($totalStudents,$index,$name,$r)=@_;
-    $r->print('<script>popwin.document.popremain.remaining.value="'.
-              'Computing '.$index.'/'.$totalStudents.': '.
-              $name.'";</script>');
-    $r->rflush();
-}
+    my ($result, $orderedProblems) =
+        &InitializeProblemStatistics($cacheDB, $students, $courseID, $c, $r);
+    if($result ne 'OK') {
+        return;
+    }
 
-# close Progress Line
-sub Close_PrgWin {
-    my ($r)=@_;
-    $r->print('<script>popwin.close()</script>');
-    $r->rflush(); 
-}
+    my @values = ();
 
- 
-# ------ Dump the Student's DB file and handling the data for statistics table 
-sub ExtractStudentData {
-    my ($cache,$name,$list,$Discuss,$r,$discriminant)=@_;
-    my $totalTries = 0;
-    my $totalAwarded = 0;
-    my $spent=0;
-    my $spent_yes=0;
-    my $TotDiscuss=0;
-    my $TotalOpend = 0;
-    my $ProbSolved = 0;
-    my $ProbTot = 0;
-    my $TotFirst = 0;
-    my $TimeTot = 0;
-    my $Discussed=0;
-    my $discrim='';
-    my $tempSequenceOrder=100;
-
-#$Apache::lonxml::debug=1;
-#&Apache::lonhomework::showhash(%$cache);
-#$Apache::lonxml::debug=0;
+    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
+        return 'Unable to tie database.7';
+    }
 
-    foreach my $sequence (split(':', $cache->{'orderedSequences'})) {
-	my $tempProblemOrder=100;
-        if($cache->{'ProblemStatisticsMaps'} ne 'All Maps'  &&
-           $cache->{'ProblemStatisticsMaps'} ne $cache->{$sequence.':title'}) {
-            next;
+    foreach(@$orderedProblems) {
+        my ($sequence,$problem,$part)=split(':', $_);
+        if($cache{'StatisticsMaps'} ne 'All Maps'  &&
+           $cache{'StatisticsMaps'} ne $cache{$sequence.':title'}) {
+             next;
         }
-	$tempSequenceOrder++;
 
-        foreach my $problemID (split(':', $cache->{$sequence.':problems'})) {
-            my $problem = $cache->{$problemID.':problem'};
-            my $LatestVersion = $cache->{$name.':version:'.$problem};
-	    # Output dashes for all the parts of this problem if there
-            # is no version information about the current problem.
-            #if(!$LatestVersion) {
-            #    foreach my $part (split(/\:/,$cache->{$sequence.':'.
-            #                                          $problemID.
-            #                                          ':parts'})) {
-            #        $codes    .= "-,";
-            #        $attempts .= "0,"; 
-            #    }
-            #    next;
-            #}
-
-            my %partData=undef;
-	    $partData{'count'}=0;
-            # Initialize part data, display skips correctly
-            # Skip refers to when a student made no submissions on that
-            # part/problem.
-            foreach my $part (split(/\:/,$cache->{$sequence.':'.
-                                                  $problemID.
-                                                  ':parts'})) {
-		$tempProblemOrder++;
-		$partData{'count'}++;
-                $partData{$part.':order'}=$tempProblemOrder;
-                $partData{$part.':tries'}=0;
-                $partData{$part.':code'}='-';
+        if( $currentSequence == -1 ||
+            ($sortProblems eq 'Sort Within Sequence' &&
+            $currentSequence != $sequence)) {
+	    if($currentSequence != -1) {
+		&DrawGraph(\@values,$courseDescription,$title,$max,$r);
+	    }
+            if($sortProblems eq 'Sort Within Sequence') {
+                $r->print('<br><b>'.$cache{$sequence.':title'}.'</b>'."\n");
             }
 
-            # Looping through all the versions of each part, starting with the
-            # oldest version.  Basically, it gets the most recent 
-            # set of grade data for each part.
-	    for(my $Version=1; $Version<=$LatestVersion; $Version++) {
-		foreach my $part (split(/\:/,$cache->{$sequence.':'.
-                                                      $problemID.
-                                                      ':parts'})) {
-
-                    if(!defined($cache->{$name.":$Version:$problem".
-                                               ":resource.$part.solved"})) {
-                        # No grade for this submission, so skip
-                        next;
-                    }
-
-                    my $tries=0;
-                    my $time=0;
-                    my $awarded=0;
-		    $Discussed=0;
-                    my $code='-';
-
-                    $awarded = $cache->{"$name:$Version:$problem:resource.".
-                                        "$part.awarded"};
-                    $partData{$part.':awarded'} = ($awarded) ? $awarded : 0;
-                    $totalAwarded += $awarded;
+            $currentSequence = $sequence;
+            @values = ();
+	    $max=0;
+        }
+        my $data = 0;
+        if($graph eq 'DoDiffGraph') {
+            $data = sprintf("%.2f", $cache{$_.':degreeOfDifficulty'}),
+        } else {
+            $data = sprintf("%.1f", $cache{$_.':percentWrong'}),
+        }
+        if($max < $data) {
+            $max = $data;
+        }
+        push(@values, $data);
+    }
+    untie(%cache);
 
-                    $tries = $cache->{"$name:$Version:$problem".
-                                      ":resource.$part.tries"};
-                    $partData{$part.':tries'} = ($tries) ? $tries : 0;
-                    $partData{$part.':wrong'} = $partData{$part.':tries'};
-                    $totalTries += $tries;
+    &DrawGraph(\@values,$courseDescription,$title,$max,$r);
 
-                    my $val = $cache->{$name.":$Version:$problem".
-                                       ":resource.$part.solved"};
-                    if    ($val eq 'correct_by_student')   {$code = 'C';} 
-                    elsif ($val eq 'correct_by_override')  {$code = 'O';}
-                    elsif ($val eq 'incorrect_attempted')  {$code = 'I';} 
-                    elsif ($val eq 'incorrect_by_override'){$code = 'I';}
-                    elsif ($val eq 'excused')              {$code = 'x';}
-                    $partData{$part.':code'}=$code;
-                    if($partData{$part.':wrong'} ne 0 && 
-                       ($code eq 'C' || $code eq 'O')) {
-                        $partData{$part.':wrong'}--;
-                    }
-                }
-            }
+    return;
+}
 
-            # Loop through all the parts for the current problem in the 
-            # correct order and prepare the output
-	    my $partCounter=0;
-            foreach (split(/\:/,$cache->{$sequence.':'.$problemID.
-                                         ':parts'})) {
-		$partCounter++;
-                my $Yes = 0;
-                if($partData{$_.':code'} eq 'C' || 
-                   $partData{$_.':code'} eq 'O') {
-                    $Yes=1;
-                }
-                my $pOrder=$partData{$_.':order'};
-                my $ptr = $tempSequenceOrder.':'.$pOrder.':'.$problemID;
 
-                if($partData{'count'} > 1) {
-                    $ptr .= "*(part $_)";
-                }
-                $discrim .= '&';
+sub DrawGraph {
+    my ($values,$courseDescription,$title,$Max,$r)=@_;
+    my $sendValues = join(',', @$values);
+    my $sendCount = scalar(@$values);
+    $r->print("<br>The Maximum Value is: $Max");
+    if ( $Max > 1 ) {
+	if ($Max % 10) {
+            if ( int($Max) < $Max ) {
+	    	$Max++;
+		$Max = int($Max);
+	    }
+	}
+    #(10 - $Max % 10);
+    } else { $Max = 1; }
 
-		my ($pr_no,$dod)=split('&',$ptr);
-#		my $DoDiff=$DoDiff->{$dod};
-#               $r->print('<br>'.$name.'---'.$ptr.'==='.$DoDiff);
-
-                my $Fac = ($partData{$_.':tries'}) ? 
-                    ($partData{$_.':awarded'}/$partData{$_.':tries'}) : 0;
-                my $DisF;
-                if($Fac > 0 &&  $Fac < 1) { 
-                    $DisF = sprintf( "%.4f", $Fac );
-                } else {
-                    $DisF = $Fac;
-                }
+    my @GData = ('','Problem_number',$title,$Max,$sendCount,$sendValues);
 
-                if ($Discuss->{"$name:$problem"}) {
-		    $TotDiscuss++;
-                    $Discussed=1;
-                }
-                my $time = $cache->{"$name:$LatestVersion:$problem:timestamp"};
-                $discrim .= $tempSequenceOrder.'@'.$pOrder.'='.$DisF.'+'.$Yes;
-                $ptr .= '&'.$partData{$_.':tries'}.
-                        '&'.$partData{$_.':wrong'}.
-                        '&'.$partData{$_.':code'};
-                push (@$list, $ptr."&$Discussed");
-#                $r->print('<br>'.$_.$name.'---'.$ptr);
-		
-####		if ($DoDiff>0.85) {
+#    $r->print('</form>'."\n");
+    $r->print('<br>'."\n");
+    $r->print('<IMG src="/cgi-bin/graph.png?'.
+              (join('&', @GData)).'" border="1" />');
+#    $r->print('<form>'."\n");
+    $r->print('<br>'."\n");
+}
 
-                $TimeTot += $time;
+#---- Problem Statistics Web Page ---------------------------------------
 
-                if ($Yes==1 && $partData{$_.':tries'}==1) {
-		    $TotFirst++;
-                }
-#		my $Acts= $Activity->{$name.':'.$problem};
-#		if ($Acts) {
-#		    my $Pt=&ProcAct( $Acts, $time );
-		    #my ($spe,$beg) = split(/\+/,$Pt);
-#                    my $spe= $Pt;
-#		    if ($Yes==1) {$spent_yes += $spe;}
-#		    $spent += $spe;
-		    #$Beg += $beg;
-#                   $r->print('<br>'.$name.'---'.$problem.'---'.$spe);
-#		}
-		$TotalOpend++;
-		$ProbTot++;
+sub CreateProblemStatisticsTableHeading {
+    my ($headings,$r)=@_;
 
-                $tempProblemOrder++;
-            }
-        }
-    }
-    my $pstr;
-    if($totalTries) {
-	my $DisFac = ($totalAwarded/$totalTries);
-	my $DisFactor = sprintf( "%.4f", $DisFac );
-        my $TS = sprintf( "%.2f", $spent );
-        my $TS_yes = sprintf( "%.2f", $spent_yes );
-	$pstr=$DisFactor.':'.$name.':'.$ProbTot.':'.$TotalOpend.':'.
-              $totalTries.':'.$ProbSolved.':'.$TotFirst.':'.
-              $TS_yes.':'.$TS.':'.$TotDiscuss;
-	(%$discriminant)->{$pstr}=$discrim;
+    my $Str='';
+    $Str .= '<tr>'."\n";
+    $Str .= '<th bgcolor="#ffffe6">P#</th>'."\n";
+    foreach(@$headings) {
+	$Str .= '<th bgcolor="#ffffe6">';
+        $Str .= '<a href="/adm/statistics?reportSelected=';
+        $Str .= &Apache::lonnet::escape('Problem Statistics');
+        $Str .= '&ProblemStatisticsSort=';
+        $Str .= &Apache::lonnet::escape($_).'">'.$_.'</a>&nbsp</th>'."\n";
     }
-}
+    $Str .= "\n".'</tr>'."\n";
 
-sub NumericSort {
-    $a <=> $b;
-}
-
-sub OrderedSort  {
-    if ($ENV{'form.order'} eq 'Descending') {
-	$b <=> $a;
-    } else { 
-	$a <=> $b;
-    }
+    return $Str;
 }
 
-
-
 sub BuildStatisticsTable {
-    my ($cache,$upper,$lower,$list,$headings,$students,$r,$color)=@_;
-    my $NoElements = scalar @$list;
-    my @list=sort(@$list);
-
-#6666666
-#    my $file="/home/httpd/perl/tmp/183d.txt";
-#    open(OUT, ">$file");
-#6666666
-##     &Apache::lonstatistics::Create_PrgWin($r);
-##777777
-##    my (%Activity) = &LoadActivityLog();
-##    $r->print('<script>popwin.document.popremain.remaining.value="'.
-##              'Loading Discussion...";</script>');
-##    my ($doDiffFile) = &LoadDoDiffFile();
-
-##777777
-##    $Str .= &Classify($discriminantFactor, $students);
-
-    my $p_count = 0;
-    my $dummy;
-    my $p_val;
-    my $ResId;
-    my %TempCache;
-    my $cIdx=0;
+    my ($cache,$displayFormat,$sortProblems,$orderedProblems,$headings,
+        $r,$color)=@_;
 
-    foreach my $sequence (split(':', $cache->{'orderedSequences'})) {
-        if($cache->{'ProblemStatisticsMaps'} ne 'All Maps'  &&
-           $cache->{'ProblemStatisticsMaps'} ne $cache->{$sequence.':title'}) {
+    my $count = 1;
+    my $currentSequence = -1;
+    foreach(@$orderedProblems) {
+        my ($sequence,$problem,$part)=split(':', $_);
+        if($cache->{'StatisticsMaps'} ne 'All Maps'  &&
+           $cache->{'StatisticsMaps'} ne $cache->{$sequence.':title'}) {
             next;
         }
-        &CreateProblemStatisticsTableHeading($cache->{'DisplayFormat'}, 
-                                             $cache->{$sequence.':source'},
-                                             $cache->{$sequence.':title'}, 
-                                             $headings,$r);
-	my ($tar,$Tries,$Wrongs,$Code,$Disc)=split(/\&/,
-                                                   $list[$cIdx]);
-        my ($SqOrd,$PrOrd,$Prob)=split(/\:/,$tar);
-	$sequence+=100;
-	while ($SqOrd==$sequence && $cIdx<$NoElements) {
-	    my %storestats=();
-	    my $pOrd=$PrOrd;
-	    my $Temp = $Prob;
-	    my $MxTries = 0;
-	    my $TotalTries = 0;
-	    my $YES = 0;
-	    my $Incorrect = 0;
-	    my $Override = 0;
-	    my $StdNo = 0;
-	    my $DiscNo=0;
-	    my @StdLst;
-	    while ($pOrd==$PrOrd && $cIdx<$NoElements)
-	    {
-		$cIdx++;
-		$StdNo++;
-		$StdLst[ $StdNo ] = $Tries;
-		$TotalTries += $Tries;
-		if ( $MxTries < $Tries ) { $MxTries = $Tries; } 
-		if ( $Code eq 'C' ){ $YES++; }
-		elsif( $Code eq 'I' ) { $Incorrect++; }
-		elsif( $Code eq 'O' ) { $Override++; }
-		elsif( $Code eq '-' ) { $StdNo--; }
-		($tar,$Tries,$Wrongs,$Code,$Disc)=split(/\&/,
-                                                     $list[$cIdx]);
-	        ($SqOrd,$PrOrd,$Prob)=split(/\:/,$tar);
-	    }
 
-	    $p_count++;
-	    my $Dummy;
-	    ($ResId,$Dummy)=split(/\*/,$Temp);
-	    $Temp = '<a href="'.$cache->{$ResId.':source'}.
-                '" target="_blank">'.$cache->{$ResId.':title'}.$Dummy.'</a>';
-
-#	    my $urlres = $cache->{$sequence.':source'});
-#check with Gerd
-	    #symb of the problem (already decluttered and cleaned)
-	    my $urlres = $cache->{$ResId.':problem'};
-
-#------------------------ Compute the Average of Tries about one problem
-	    my $Average = ($StdNo) ? $TotalTries/$StdNo : 0;
-
-	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___timestamp'}=time;
-	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___stdno'}=$StdNo;
-	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___avetries'}=$Average;
-
-#-------------------------------- Compute percentage of Wrong tries
-	    my $Wrong = ( $StdNo ) ? 100 * ( $Incorrect / $StdNo ) : 0;
-
-#-------------------------------- Compute Standard Deviation
-	    my $StdDev = 0; 
-	    if ( $StdNo > 1 ) {
-		for ( my $n = 0; $n < $StdNo; $n++ ) {
-		    my $Dif = $StdLst[ $n ]-$Average;
-		    $StdDev += $Dif*$Dif;
-		} 
-		$StdDev /= ( $StdNo - 1 );
-		$StdDev = sqrt( $StdDev );
-	    }
+        if($currentSequence == -1 ||
+           ($sortProblems eq 'Sort Within Sequence' &&
+            $currentSequence != $sequence)) {
+            if($displayFormat ne 'Display CSV Format') {
+                if($currentSequence ne -1) {
+                    $r->print('</table>');
+                    $r->print('</td></tr></table><br>');
+                }
+                if($sortProblems eq 'Sort Within Sequence') {
+                    $r->print('<b>'.$cache->{$sequence.':title'}.'</b>');
+                }
+                $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
+                $r->print('<table border="0" cellpadding="3">'."\n");
+                $r->print(&CreateProblemStatisticsTableHeading($headings, $r));
+            } else {
+                if($sortProblems eq 'Sort Within Sequence') {
+                    $r->print('"'.$cache->{$sequence.':title'}.'"');
+                }
+                $r->print('<br>');
+            }
+            $currentSequence = $sequence;
+        }
 
-#-------------------------------- Compute Degree of Difficulty
-	    my $DoDiff = 0;
-	    if( $TotalTries > 0 ) {
-		$DoDiff = 1 - ( ( $YES + $Override ) / $TotalTries );
-#	    $DoDiff =  ($TotalTries)/($YES + $Override+ 0.1);	    
-	    }
-       
-	    $storestats{$ENV{'request.course.id'}.'___'.$urlres.'___difficulty'}=$DoDiff;
+        my $ref = '<a href="'.$cache->{$problem.':source'}.
+                  '" target="_blank">'.$cache->{$problem.':title'}.'</a>';
+        my $title = $cache->{$problem.':title'};
+        if($part != 0) {
+            $title .= ' Part '.$part;
+        }
+        my $source = $cache->{$problem.':source'};
+        my $tableData = join('&', $ref, $title, $source,
+                       $cache->{$_.':studentCount'},
+                       $cache->{$_.':totalTries'},
+                       $cache->{$_.':maxTries'},
+                       $cache->{$_.':mean'},
+                       $cache->{$_.':correct'},
+                       $cache->{$_.':correctByOverride'},
+                       $cache->{$_.':percentWrong'},
+                       $cache->{$_.':degreeOfDifficulty'},
+                       $cache->{$_.':standardDeviation'},
+                       $cache->{$_.':skewness'},
+                       $cache->{$_.':discriminationFactor1'},
+                       $cache->{$_.':discriminationFactor2'});
 
-#-------------------------------- Compute the Skewness
-	    my $Skewness = 0;
-	    my $Sum = 0; 
-	    if ( $StdNo > 0 && $StdDev > 0 ) {
-		for ( my $n = 0; $n < $StdNo; $n++ ) {
-		    my $Dif = $StdLst[ $n ]-$Average;
-		    $Skewness += $Dif*$Dif*$Dif;
-		} 
-		$Skewness /= $StdNo;
-		$Skewness /= $StdDev*$StdDev*$StdDev;
-	    }
+        &TableRow($displayFormat,$tableData,$count,$r,$color);
 
-#--------------------- Compute the Discrimination Factors
-            my ($Up1,$Up2)=split(/\:/,$upper->{$sequence.'@'.$pOrd});
-	    my ($Lw1,$Lw2)=split(/\:/,$lower->{$sequence.'@'.$pOrd});
-
-	    my $Dis1 = $Up1 - $Lw1;
-	    my $Dis2 = $Up2 - $Lw2;
-	    my $_D1 = sprintf("%.2f", $Dis1);
-	    my $_D2 = sprintf("%.2f", $Dis2);
-
-#-----------------  Some restition in presenting the float numbers
-	    my $Avg = sprintf( "%.2f", $Average );
-	    my $Wrng = sprintf( "%.1f", $Wrong );
-	    my $SD = sprintf( "%.1f", $StdDev );
-	    my $DoD = sprintf( "%.2f", $DoDiff );
-	    my $Sk = sprintf( "%.1f", $Skewness );
-	    my $join = $sequence.'@'.$pOrd.'&'.$Temp.'&'.$StdNo.'&'.
-                       $TotalTries.'&'.$MxTries.'&'.$Avg.'&'.
-                       $YES.'&'.$Override.'&'.$Wrng.'&'.$DoD.'&'.
-		       $SD.'&'.$Sk.'&'.$_D1.'&'.$_D2.'&'.
-                       $DiscNo.'&'.$Prob;
-
-	    $TempCache{'CacheTable:'.($p_count-1)}=$join;
-
-#6666666
-#	    $r->print('<br>'.$out.'&'.$DoD);
-#            print (OUT $out.'@'.$DoD.'&');
-#6666666
-
-#check with Gerd
-	    $urlres=~/^(\w+)\/(\w+)/;
-	    if ($StdNo) {
-		&Apache::lonnet::put('nohist_resevaldata',\%storestats,
-				     $1,$2);
-	    }
-#-------------------------------- Row of statistical table
-            &TableRow($cache,$join,$cIdx,($p_count-1),$r,$color,
-                      \%TempCache);
-	}
-	$TempCache{'ProblemCount'}=$p_count;
-	&CloseTable($cache,$r);
+        $count++;
     }
-###    &Close_PrgWin();
-#6666666
-#    close( OUT );
-#666666
-    return \%TempCache;
-}
-
-
-sub CacheStatisticsTable {
-    my ($state,$cache,$headings,$r,$color)=@_;
-    my @list = (); 
-    my %TempCache;
-    my %myHeader = reverse( %$headings );
-    my $pos = $myHeader{$state};
-    if ($pos > 0) {$pos++;}
-    my $p_count = $cache->{'ProblemCount'};
-
-    for ( my $k=0; $k<$p_count;$k++) {
-	my $key=$cache->{'CacheTable:'.$k};
-	my @Temp=split(/\&/,$key);
-	$list[$k]=$Temp[$pos].'+'.$key;
-    }
-    
-    if ($pos>0) {
-        @list = sort OrderedSort (@list);
+    if($displayFormat ne 'Display CSV Format') {
+        $r->print('</table>'."\n");
+        $r->print('</td></tr></table>');
     } else {
-        @list = sort (@list);
-    }
-    my $cIdx=0;
-
-    if ( $pos == 0 ) {
-	foreach my $sequence (split(':', $cache->{'orderedSequences'})) {
-	    if($cache->{'ProblemStatisticsMaps'} ne 'All Maps'  &&
-	       $cache->{'ProblemStatisticsMaps'} ne $cache->{$sequence.':title'}) {
-		next;
-	    }
-	    if ($cIdx==$p_count) {
-		return \%TempCache;
-	    }
-	    &CreateProblemStatisticsTableHeading($cache->{'DisplayFormat'}, 
-                                             $cache->{$sequence.':source'},
-                                             $cache->{$sequence.':title'}, 
-                                             $headings,$r);
-
-	    my ($tar)=split(/\&/,$list[$cIdx]);
-	    $tar=~s/\+//eg;
-	    my ($SqOrd)=split(/\@/,$tar);
-	    $sequence+=100;
-	    while ($SqOrd==$sequence && $cIdx<$p_count) {
-		my($Pre, $Post) = split(/\+/,$list[$cIdx]); 
-		&TableRow($cache,$Post,$cIdx,$cIdx,$r,$color,\%TempCache);
-		$cIdx++;
-		my ($tar)=split(/\&/,$list[$cIdx]);
-		$tar=~s/\+//eg;
-		($SqOrd)=split(/\@/,$tar);
-	    }
-	    &CloseTable($cache,$r);
-	}
-    }
-    else {
-        &CreateProblemStatisticsTableHeading($cache->{'DisplayFormat'}, 
-                                             'Sorted by: ',
-					     $headings->{$pos-1},
-                                             $headings,$r);
-	for ( my $nIndex = 0; $nIndex < $p_count; $nIndex++ ) {
-	    my($Pre, $Post) = split(/\+/,$list[$nIndex]);
-	    &TableRow($cache,$Post,$nIndex,$nIndex,$r,$color,\%TempCache);
-	} 
-	&CloseTable($cache,$r);
+        $r->print('<br>');
     }
 
-    return \%TempCache;
+    return;
 }
 
-
 sub TableRow {
-    my ($cache,$Str,$Idx,$RealIdx,$r,$color,$GraphDat)=@_;
-    my($PrOrd,$Temp,$StdNo,$TotalTries,$MxTries,$Avg,$YES,$Override,
-       $Wrng,$DoD,$SD,$Sk,$_D1,$_D2,$DiscNo,$Prob)=split(/\&/,$Str);	
+    my ($displayFormat,$Str,$RealIdx,$r,$color)=@_;
+    my($ref,$title,$source,$StdNo,$TotalTries,$MxTries,$Avg,$YES,$Override,
+       $Wrng,$DoD,$SD,$Sk,$_D1,$_D2)=split(/\&/,$Str);	
     my $Ptr;
-    if($cache->{'DisplayFormat'} eq 'Display CSV Format') {
-        my ($ResId,$Dummy)=split(/\*/,$Prob);
-        $Ptr="\n".'<br>'.
-             "\n".'"'.($RealIdx+1).'",'.
-             "\n".'"'.$cache->{$ResId.':title'}.$Dummy.'",'.
-             "\n".'"'.$cache->{$ResId.':source'}.'",'.
-             "\n".'"'.$StdNo.'",'.
-             "\n".'"'.$TotalTries.'",'.
-             "\n".'"'.$MxTries.'",'.
-             "\n".'"'.$Avg.'",'.
-             "\n".'"'.$YES.'",'.
-             "\n".'"'.$Override.'",'.
-             "\n".'"'.$Wrng.'",'.
-             "\n".'"'.$DoD.'",'.
-             "\n".'"'.$SD.'",'.
-             "\n".'"'.$Sk.'",'.
-             "\n".'"'.$_D1.'",'.
-	     "\n".'"'.$_D2.'"'.
-	     "\n".'"'.$DiscNo.'"';
+    if($displayFormat eq 'Display CSV Format') {
+        $Ptr='"'.$RealIdx.'",'."\n".
+             '"'.$title.'",'."\n".
+             '"'.$source.'",'."\n".
+             '"'.$StdNo.'",'."\n".
+             '"'.$TotalTries.'",'."\n".
+             '"'.$MxTries.'",'."\n".
+             '"'.$Avg.'",'."\n".
+             '"'.$YES.'",'."\n".
+             '"'.$Override.'",'."\n".
+             '"'.$Wrng.'",'."\n".
+             '"'.$DoD.'",'."\n".
+             '"'.$SD.'",'."\n".
+             '"'.$Sk.'",'."\n".
+             '"'.$_D1.'",'."\n".
+             '"'.$_D2.'"'."\n".
+             "<br>\n";
 
         $r->print("\n".$Ptr);
     } else {
-        $Ptr="\n".'<tr>'.
-             "\n".'<td>'.($RealIdx+1).'</td>'.
-             "\n".'<td>'.$Temp.'</td>'.
-             "\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>'.
-             "\n".'<td bgcolor='.$color->{"yellow"}.'> '.$DiscNo.'</td>';
-        $r->print("\n".$Ptr.'</tr>' );
+        $Ptr='<tr>'."\n".
+             '<td bgcolor="#ffffe6">'.$RealIdx.'</td>'."\n".
+             '<td bgcolor="#ffffe6">'.$ref.'</td>'."\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>'."\n";
+        $r->print($Ptr.'</tr>'."\n");
     }
-    $GraphDat->{'GraphGif:'.$RealIdx}=$DoD.':'.$Wrng;
-}
 
+    return;
+}
 
 # For loading the colored table for display or un-colored for print
 sub setbgcolor {
@@ -715,15 +466,29 @@ sub setbgcolor {
 }
 
 sub ProblemStatisticsButtons {
-    my ($displayFormat)=@_;
+    my ($displayFormat, $displayLegend, $sortProblems)=@_;
 
     my $Ptr = '<tr><td></td><td align="left">';
     $Ptr .= '<input type="submit" name="DoDiffGraph" ';
-    $Ptr .= 'value="DoDiff Graph" />'."\n";
-    $Ptr .= '&nbsp;&nbsp;&nbsp;';
+    $Ptr .= 'value="Plot Degree of Difficulty" />'."\n";
+    $Ptr .= '</td><td align="left">';
     $Ptr .= '<input type="submit" name="PercentWrongGraph" ';
-    $Ptr .= 'value="%Wrong Graph" />'."\n";
-    $Ptr .= '&nbsp;&nbsp;&nbsp;';
+    $Ptr .= 'value="Plot Percent Wrong" />'."\n";
+    $Ptr .= '</td></tr><tr><td></td><td>'."\n";
+    $Ptr .= '<input type="submit" name="SortProblems" ';
+    if($sortProblems eq 'Sort All Problems') {
+        $Ptr .= 'value="Sort Within Sequence" />'."\n";
+    } else {
+        $Ptr .= 'value="Sort All Problems" />'."\n";
+    }
+    $Ptr .= '</td><td align="left">';
+    $Ptr .= '<input type="submit" name="DisplayLegend" ';
+    if($displayLegend eq 'Show Legend') {
+        $Ptr .= 'value="Hide Legend" />'."\n";
+    } else {
+        $Ptr .= 'value="Show Legend" />'."\n";
+    }
+    $Ptr .= '</td><td align="left">';
     $Ptr .= '<input type="submit" name="DisplayCSVFormat" ';
     if($displayFormat eq 'Display CSV Format') {
         $Ptr .= 'value="Display Table Format" />'."\n";
@@ -740,16 +505,16 @@ sub ProblemStatisticsLegend {
     $Ptr = '<table border="0">';
     $Ptr .= '<tr><td>';
     $Ptr .= '<b>#Stdnts</b></td>';
-    $Ptr .= '<td>Total Number of Students opened the problem.';
+    $Ptr .= '<td>Total number of students attempted the problem.';
     $Ptr .= '</td></tr><tr><td>';
     $Ptr .= '<b>Tries</b></td>';
-    $Ptr .= '<td>Total Number of Tries for solving the problem.';
+    $Ptr .= '<td>Total number of tries for solving the problem.';
     $Ptr .= '</td></tr><tr><td>';
     $Ptr .= '<b>Mod</b></td>';
-    $Ptr .= '<td>Maximunm Number of Tries for solving the problem.';
+    $Ptr .= '<td>Largest number of tries for solving the problem by a student.';
     $Ptr .= '</td></tr><tr><td>';
     $Ptr .= '<b>Mean</b></td>';
-    $Ptr .= '<td>Average Number of the tries. [ Tries / #Stdnts ]';
+    $Ptr .= '<td>Average number of tries. [ Tries / #Stdnts ]';
     $Ptr .= '</td></tr><tr><td>';
     $Ptr .= '<b>#YES</b></td>';
     $Ptr .= '<td>Number of students solved the problem correctly.';
@@ -757,9 +522,9 @@ sub ProblemStatisticsLegend {
     $Ptr .= '<b>#yes</b></td>';
     $Ptr .= '<td>Number of students solved the problem by override.';
     $Ptr .= '</td></tr><tr><td>';
-    $Ptr .= '<b>%Wrng</b></td>';
-    $Ptr .= '<td>Percentage of students tried to solve the problem ';
-    $Ptr .= 'but still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]';
+    $Ptr .= '<b>%Wrong</b></td>';
+    $Ptr .= '<td>Percentage of students who tried to solve the problem ';
+    $Ptr .= 'but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]';
     $Ptr .= '</td></tr><tr><td>';
     $Ptr .= '<b>DoDiff</b></td>';
     $Ptr .= '<td>Degree of Difficulty of the problem.  ';
@@ -777,8 +542,8 @@ sub ProblemStatisticsLegend {
     $Ptr .= '<b>Dis.F.</b></td>';
     $Ptr .= '<td>Discrimination Factor: A Standard for evaluating the ';
     $Ptr .= 'problem according to a Criterion<br>';
-    $Ptr .= '<b>[Applied Criterion in %27 Upper Students - ';
-    $Ptr .= 'Applied the same Criterion in %27 Lower Students]</b><br>';
+    $Ptr .= '<b>[Criterion to group students into %27 Upper Students - ';
+    $Ptr .= 'and %27 Lower Students]</b><br>';
     $Ptr .= '<b>1st Criterion</b> for Sorting the Students: ';
     $Ptr .= '<b>Sum of Partial Credit Awarded / Total Number of Tries</b><br>';
     $Ptr .= '<b>2nd Criterion</b> for Sorting the Students: ';
@@ -791,128 +556,304 @@ sub ProblemStatisticsLegend {
     return $Ptr;
 }
 
-#------- Processing upperlist and lowerlist according to each problem
-sub ProcessDiscriminant {
-    my ($List,$r) = @_;
-    my @sortedList = sort (@$List);
-    my $Count = scalar @sortedList;
-    my $Problem;
-    my @Dis;
-    my $Slvd=0;
-    my $tmp;
-    my $Sum1=0;
-    my $Sum2=0;
-    my $nIndex=0;
-    my $nStudent=0;
-    my %Proc=undef;
-    while ($nIndex<$Count) {
-#        $r->print("<br> $nIndex) $sortedList[$nIndex]");
-	($Problem,$tmp)=split(/\=/,$sortedList[$nIndex]);
-	@Dis=split(/\+/,$tmp);
-	my $Temp = $Problem;
-	do {
-	    $nIndex++;
-	    $nStudent++;
-	    $Sum1 += $Dis[0];
-	    $Sum2 += $Dis[1];
-	    ($Problem,$tmp)=split(/\=/,$sortedList[$nIndex]);
-	    @Dis=split(/\+/,$tmp);
-	} while ( $Problem eq $Temp && $nIndex < $Count );
-	$Proc{$Temp}=($Sum1/$nStudent).':'.($Sum2/$nStudent);
-#        $r->print("<br> $nIndex) $Temp --> ($nStudent) $Proc{$Temp}");
-	$Sum1=0;
-	$Sum2=0;
-	$nStudent=0;
-    }
-
-    return %Proc;
-}
-
-#------- Creating Discimination factor   
-sub Discriminant {
-    my ($discriminant,$r)=@_;
-    my @discriminantKeys=keys(%$discriminant);
-    my $Count = scalar @discriminantKeys;
-
-    my $UpCnt = int(0.27*$Count);
-    my $low=0;
-    my $up=$Count-$UpCnt;
-    my @UpList=();
-    my @LowList=();
-
-    $Count=0;
-    foreach my $key (sort(@discriminantKeys)) { 
-	$Count++;    
-	if($low < $UpCnt || $Count > $up) {
-            $low++;
-            my $str=$discriminant->{$key};
-            foreach(split(/\&/,$str)){
-                if($_) {
-                    if($low<$UpCnt) { push(@LowList,$_); }
-                    else            { push(@UpList,$_);  }
+sub ExtractStudentData {
+    my ($cache, $students)=@_;
+
+    my @problemList=();
+    my %problemData;
+    foreach my $sequence (split(':', $cache->{'orderedSequences'})) {
+        foreach my $problemID (split(':', $cache->{$sequence.':problems'})) {
+            foreach my $part (split(/\:/,$cache->{$sequence.':'.
+                                                  $problemID.
+                                                  ':parts'})) {
+                my $id = $sequence.':'.$problemID.':'.$part;
+                push(@problemList, $id);
+                my $totalTries = 0;
+                my $totalAwarded = 0;
+                my $correct = 0;
+                my $correctByOverride = 0;
+                my $studentCount = 0;
+                my $maxTries = 0;
+                my $totalFirst = 0;
+                my @studentTries=();
+                foreach(@$students) {
+                    my $code = $cache->{"$_:$problemID:$part:code"};
+
+                    if(defined($cache->{$_.':error'}) || $code eq ' ' ||
+                       $cache->{"$_:$problemID:NoVersion"} eq 'true') {
+                        next;
+                    }
+
+                    $studentCount++;
+                    my $tries =  $cache->{"$_:$problemID:$part:tries"};
+                    if($maxTries < $tries) {
+                        $maxTries = $tries;
+                    }
+                    $totalTries += $tries;
+                    push(@studentTries, $tries);
+
+                    my $awarded = $cache->{"$_:$problemID:$part:awarded"};
+                    $totalAwarded += $awarded;
+
+                    if($code eq '*') {
+                        $correct++;
+                        if($tries == 1) {
+                            $totalFirst++;
+                        }
+                    } elsif($code eq '+') {
+                        $correctByOverride++;
+                    }
                 }
+
+                my $studentTriesJoined = join(':::', @studentTries);
+                $problemData{$id.':sequenceTitle'} =
+                    $cache->{$sequence.':title'};
+                $problemData{$id.':studentCount'} = $studentCount;
+                $problemData{$id.':totalTries'} = $totalTries;
+                $problemData{$id.':studentTries'} = $studentTriesJoined;
+                $problemData{$id.':totalAwarded'} = $totalAwarded;
+                $problemData{$id.':correct'} = $correct;
+                $problemData{$id.':correctByOverride'} = $correctByOverride;
+                $problemData{$id.':wrong'} = $studentCount - 
+                                             ($correct + $correctByOverride);
+                $problemData{$id.':maxTries'} = $maxTries;
+                $problemData{$id.':totalFirst'} = $totalFirst;
             }
         }
     }
-    my %DisUp =  &ProcessDiscriminant(\@UpList,$r);
-    my %DisLow = &ProcessDiscriminant(\@LowList,$r);
 
-    return (\%DisUp, \%DisLow);
-}   
+    my @upperStudents1=();
+    my @lowerStudents1=();
+    my @upperStudents2=();
+    my @lowerStudents2=();
+    my $upperCount = int(0.27*scalar(@$students));
+    # Discriminant Factor criterion 1
+    my $sortedStudents = &SortDivideByTries($students,$cache,':totalAwarded');
+
+    for(my $i=0; $i<$upperCount; $i++) {
+        push(@lowerStudents1, $sortedStudents->[$i]);
+        push(@upperStudents1, $sortedStudents->[(scalar(@$students)-$i-1)]);
+    }
+
+    $problemData{'studentsUpperListCriterion1'}=join(':::', @upperStudents1);
+    $problemData{'studentsLowerListCriterion1'}=join(':::', @lowerStudents1);
+
+    # Discriminant Factor criterion 2
+    $sortedStudents = &SortDivideByTries($students, $cache, ':totalSolved');
+
+    for(my $i=0; $i<$upperCount; $i++) {
+        push(@lowerStudents2, $sortedStudents->[$i]);
+        push(@upperStudents2, $sortedStudents->[(scalar(@$students)-$i-1)]);
+    }
+    $problemData{'studentsUpperListCriterion2'}=join(':::', @upperStudents2);
+    $problemData{'studentsLowerListCriterion2'}=join(':::', @lowerStudents2);
+
+    $problemData{'problemList'} = join(':::', @problemList);
+
+    return \%problemData;
+}
+
+sub SortDivideByTries {
+    my ($toSort, $data, $sortOn)=@_;
+    my @orderedData = sort { ($data->{$a.':totalTries'}) ?
+                             ($data->{$a.$sortOn}/$data->{$a.':totalTries'}):0
+                             <=>
+                             ($data->{$b.':totalTries'}) ?
+                             ($data->{$b.$sortOn}/$data->{$b.':totalTries'}):0
+                           } @$toSort;
+
+    return \@orderedData;
+}
+
+sub SortProblems {
+    my ($problemData,$sortBy,$sortProblems,$ascend)=@_;
+
+    my @problems = split(':::', $problemData->{'problemList'});
+    if($sortBy eq "Homework Sets Order") {
+        return \@problems;
+    }
+
+    my $data;
+
+    if   ($sortBy eq "#Stdnts") { $data = ':studentCount'; }
+    elsif($sortBy eq "Tries")   { $data = ':totalTries'; }
+    elsif($sortBy eq "Mod")     { $data = ':maxTries'; }
+    elsif($sortBy eq "Mean")    { $data = ':mean'; }
+    elsif($sortBy eq "#YES")    { $data = ':correct'; }
+    elsif($sortBy eq "#yes")    { $data = ':correctByOverride'; }
+    elsif($sortBy eq "%Wrng")   { $data = ':percentWrong'; }
+    elsif($sortBy eq "DoDiff")  { $data = ':degreeOfDifficulty'; }
+    elsif($sortBy eq "S.D.")    { $data = ':standardDeviation'; }
+    elsif($sortBy eq "Skew.")   { $data = ':skewness'; }
+    elsif($sortBy eq "D.F.1st") { $data = ':discriminationFactor1'; }
+    elsif($sortBy eq "D.F.2nd") { $data = ':discriminationFactor2'; }
+    else                        { return \@problems; }
+
+    my %temp;
+    my @sequenceList=();
+    foreach(@problems) {
+        my ($sequence) = split(':', $_);
+
+        my @array=();
+        my $tempArray;
+        if(defined($temp{$sequence})) {
+            $tempArray = $temp{$sequence};
+        } else {
+            push(@sequenceList, $sequence);
+            $tempArray = \@array;
+            $temp{$sequence} = $tempArray;
+        }
 
-#---- END Problem Statistics Web Page ----------------------------------------
+        push(@$tempArray, $_);
+    }
 
-#---- Problem Statistics Graph Web Page --------------------------------------
+    my @orderedProblems;
+    if($sortProblems eq "Sort Within Sequence") {
+        foreach(keys(%temp)) {
+            my $tempArray = $temp{$_};
+            my @tempOrder =
+                sort { $problemData->{$a.$data} <=> $problemData->{$b.$data} }
+            @$tempArray;
+            $temp{$_} = \@tempOrder;
+        }
+        foreach(@sequenceList) {
+            my $tempArray = $temp{$_};
+            @orderedProblems = (@orderedProblems, @$tempArray);
+        }
+    } else {
+        @orderedProblems = 
+            sort { $problemData->{$a.$data} <=> $problemData->{$b.$data} }
+        @problems;
+    }
 
-# ------------------------------------------- Prepare data for Graphical chart
+    if($ascend eq 'Descending') {
+        @orderedProblems = reverse(@orderedProblems);
+    }
 
-sub BuildGraphicChart {
-    my ($ylab,$r,$cacheDB)=@_;
-    my %cache;
-    my $Col;
-    my $data='';
-    my $count = 0;
-    my $Max = 0;
+    return \@orderedProblems;
+}
 
-    unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
-        return '<html><body>Unable to tie database.</body></html>';
-    }
-   
-    my $p_count = $cache{'ProblemCount'};
+sub CalculateStatistics {
+    my ($data, $cache, $courseID)=@_;
 
-    for ( my $k=0; $k<$p_count;$k++) {
-        my @Temp=split(/\:/,$cache{'GraphGif:'.$k});
-        my $inf = $Temp[$Col]; 
-	if ( $Max < $inf ) {$Max = $inf;}
-      	$data .= $inf.',';
-       	$count++;
-    }
-    untie(%cache);
-#    $r->print("<br>count=$p_count >>data= $data");
+    my @problems = split(':::', $data->{'problemList'});
+    foreach(@problems) {
+        # Mean
+        my $mean = ($data->{$_.':studentCount'}) ? 
+            ($data->{$_.':totalTries'} / $data->{$_.':studentCount'}) : 0;
+        $data->{$_.':mean'} = sprintf("%.2f", $mean);
 
-    if ( $Max > 1 ) { 
-	$Max += (10 - $Max % 10);
-      	$Max = int($Max);
-    } else { $Max = 1; }
+        # %Wrong
+        my $pw = ($data->{$_.':studentCount'}) ?
+            (($data->{$_.':wrong'} / $data->{$_.':studentCount'}) * 100.0) : 
+            100.0;
+        $data->{$_.':percentWrong'} = sprintf("%.1f", $pw);
 
-    my $cid=$ENV{'request.course.id'};
+        # Degree of Difficulty
+        my $dod = ($data->{$_.':totalTries'}) ?
+            (1 - (($data->{$_.':correct'} + $data->{$_.':correctByOverride'}) /
+                  $data->{$_.':totalTries'})) : 0;
 
-    if ( $ylab eq 'DoDiff Graph' ) {
-	$ylab = 'Degree-of-Difficulty';
-	$Col = 0;
-    } else {
-	$ylab = 'Wrong-Percentage';
-	$Col = 1;
-    }
-    my $Course = $ENV{'course.'.$cid.'.description'};
-    $Course =~ s/\ /"_"/eg;
-    my $GData=$Course.'&'.'Problems#'.'&'.$ylab.'&'.
-	      $Max.'&'.$count.'&'.$data;
+        $data->{$_.':degreeOfDifficulty'} = sprintf("%.2f", $dod);
+
+        # Factor in mean
+        my @studentTries = split(':::', $data->{$_.':studentTries'});
+        foreach(my $index=0; $index < scalar(@studentTries); $index++) {
+            $studentTries[$index] -= $mean;
+        }
+        my $sumSquared = 0;
+        my $sumCubed = 0;
+        foreach(@studentTries) {
+            my $squared = ($_ * $_);
+            my $cubed = ($squared * $_);
+            $sumSquared += $squared;
+            $sumCubed += $cubed;
+        }
+
+        # Standard deviation
+        my $standardDeviation;
+        if($data->{$_.':studentCount'} - 1 > 0) {
+            $standardDeviation = (sqrt($sumSquared)) / 
+                                 ($data->{$_.':studentCount'} - 1);
+        } else {
+            $standardDeviation =  0.0;
+        }
+        $data->{$_.':standardDeviation'} = sprintf("%.1f", $standardDeviation);
+
+        # Skewness
+        my $skew;
+        if($standardDeviation > 0.0999 && $data->{$_.':studentCount'} > 0) {
+            $skew = (((sqrt($sumSquared)) / $data->{$_.':studentCount'}) / 
+                     ($standardDeviation * 
+                      $standardDeviation * 
+                      $standardDeviation));
+        } else {
+            $skew = 0.0;
+        }
+
+        $data->{$_.':skewness'} = sprintf("%.1f", $skew);
 
-    $r->print('<IMG src="/cgi-bin/graph.gif?'.$GData.'" />');
+        # Discrimination Factor 1
+        my ($sequence, $problem, $part) = split(':', $_);
+
+        my @upper1 = split(':::', $data->{'studentsUpperListCriterion1'});
+        my @lower1 = split(':::', $data->{'studentsLowerListCriterion1'});
+
+        my $upper1Sum=0;
+        foreach my $name (@upper1) {
+            $upper1Sum += $cache->{"$name:$problem:$part:awarded"};
+        }
+        $upper1Sum = (scalar(@upper1)) ? ($upper1Sum/(scalar(@upper1))) : 0;
+
+        my $lower1Sum=0;
+        foreach my $name (@lower1) {
+            $lower1Sum += $cache->{"$name:$problem:$part:awarded"};
+        }
+        $lower1Sum = (scalar(@lower1)) ? ($lower1Sum/(scalar(@lower1))) : 0;
+
+        my $df1 = $upper1Sum - $lower1Sum;
+        $data->{$_.':discriminationFactor1'} = sprintf("%.2f", $df1);
+
+        # Discrimination Factor 2
+        my @upper2 = split(':::', $data->{'studentsUpperListCriterion2'});
+        my @lower2 = split(':::', $data->{'studentsLowerListCriterion2'});
+
+        my $upper2Sum=0;
+        foreach my $name (@upper2) {
+            $upper2Sum += $cache->{"$name:$problem:$part:awarded"};
+        }
+        $upper2Sum = (scalar(@upper2)) ? ($upper2Sum/(scalar(@upper2))) : 0;
+
+        my $lower2Sum=0;
+        foreach my $name (@lower2) {
+            $lower2Sum += $cache->{"$name:$problem:$part:awarded"};
+        }
+        $lower2Sum = (scalar(@lower2)) ? ($lower2Sum/(scalar(@lower2))) : 0;
+
+        my $df2 = $upper2Sum - $lower2Sum;
+        $data->{$_.':discriminationFactor2'} = sprintf("%.2f", $df2);
+
+        my %storestats;
+        my $Average = ($data->{$_.':studentCount'}) ? 
+            $data->{$_.':totalTries'}/$data->{$_.':studentCount'} : 0;
+        $storestats{$courseID.'___'.$cache->{$sequence.':source'}.
+                        '___timestamp'}=time;
+        $storestats{$courseID.'___'.$cache->{$sequence.':source'}.
+                        '___stdno'}=$data->{$_.':studentCount'};
+        $storestats{$courseID.'___'.$cache->{$sequence.':source'}.
+                        '___avetries'}=$Average;
+        $storestats{$courseID.'___'.$cache->{$sequence.':source'}.
+                        '___difficulty'}=$data->{$_.':degreeOfDifficulty'};
+        $cache->{$sequence.':source'} =~ /^(\w+)\/(\w+)/;
+        if($data->{$_.':studentCount'}) { 
+            &Apache::lonnet::put('nohist_resevaldata',\%storestats,$1,$2);
+        }
+    }
 
     return;
 }
 
+#---- END Problem Statistics Web Page ----------------------------------------
+
 1;
 __END__