[LON-CAPA-cvs] cvs: loncom /interface/statistics lonproblemstatistics.pm

matthew lon-capa-cvs@mail.lon-capa.org
Wed, 26 Mar 2003 15:19:16 -0000


matthew		Wed Mar 26 10:19:16 2003 EDT

  Modified files:              
    /loncom/interface/statistics	lonproblemstatistics.pm 
  Log:
  Implemented sorting of statistics.  Still more to do but it works well.
  
  
Index: loncom/interface/statistics/lonproblemstatistics.pm
diff -u loncom/interface/statistics/lonproblemstatistics.pm:1.41 loncom/interface/statistics/lonproblemstatistics.pm:1.42
--- loncom/interface/statistics/lonproblemstatistics.pm:1.41	Tue Mar 25 18:00:40 2003
+++ loncom/interface/statistics/lonproblemstatistics.pm	Wed Mar 26 10:19:16 2003
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lonproblemstatistics.pm,v 1.41 2003/03/25 23:00:40 matthew Exp $
+# $Id: lonproblemstatistics.pm,v 1.42 2003/03/26 15:19:16 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -286,16 +286,21 @@
 sub output_html_ungrouped {
     my ($r) = @_;
     #
-    my $sortby = $ENV{'form.sortby'};
-    if (! defined($sortby) || $sortby eq '') {
-        $sortby = 'sequence';
-    }
-    #
     my $show_container = 0;
     #$r->print(&ProblemStatisticsLegend());
     my @Header = ("Title","Part","#Stdnts","Tries","Mod",
                   "Mean","#YES","#yes","%Wrng","DoDiff",
                   "S.D.","Skew.");#,"D.F.1st","D.F.2nd");
+    #
+    my $sortby = undef;
+    foreach (@Header) {
+        if ($ENV{'form.sortby'} eq $_) {
+            $sortby = $_;
+        }
+    }
+    if (! defined($sortby) || $sortby eq '') {
+        $sortby = 'Container';
+    }
     # #FFFFE6 #EEFFCC #DDFFFF FFDDDD #DDFFDD #FFDDFF
     my @Sequences = &Apache::lonstatistics::Sequences_with_Assess();
     if (@Sequences > 1) {
@@ -307,14 +312,21 @@
     $r->print('<table border="0" cellpadding="3">'."\n");
     my $Str = '';
     foreach (@Header) {
-        $Str .= '<th>'.
+        if (/^(Part)$/) {  # Do not allow sorting on this field
+            $Str .= '<th>'.$_.'</th>';
+        } else {
+            $Str .= '<th>'.
      '<a href="javascript:document.Statistics.sortby.value='."'$_'".
          ';document.Statistics.submit();">'.
-         $_.'</a></th>';
+             $_.'</a></th>';
+        }
     }
     $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
     $r->rflush();
     #
+    # Compile the data
+    my %Statshash;
+    my @Statsarray;
     foreach my $sequence (@Sequences) {
         next if ($sequence->{'num_assess'}<1);
         foreach my $resource (@{$sequence->{'contents'}}) {
@@ -324,28 +336,93 @@
                     $SKEW) = &Apache::loncoursedata::get_problem_statistics
                         (undef,$resource->{'symb'},$part,
                          $ENV{'request.course.id'});
-                if ($part == 0) {
-                    $part = '&nbsp;';
-                }
                 my $wrongpercent = 0;
                 if (defined($num) && $num > 0) {
                     $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10;
                 }
-                if ($show_container) {
-                    $r->print('<tr>'.
-                              '<td bgcolor="#FFFFE6">'.$sequence->{'title'}.
-                              '</td>'.
-                              &statistics_html_table_data
-                              ($resource,$part,$num,$tries,$mod,$mean,$Solved,
-                               $solved,$wrongpercent,$DegOfDiff,$STD,$SKEW).
-                              "</tr>\n");
-                } else {
-                    $r->print('<tr>'.&statistics_html_table_data
-                              ($resource,$part,$num,$tries,$mod,$mean,$Solved,
-                               $solved,$wrongpercent,$DegOfDiff,$STD,$SKEW).
-                              "</tr>\n");
+                my $key = $resource->{'symb'}.':'.$part;
+                $Statshash{$key} = 
+                    {
+                        'sequence' => $sequence,
+                        'resource' => $resource,
+                        'Title' => $resource->{'title'},
+                        'Part'  => $part,
+                        '#Stdnts' => $num,
+                        'Tries' => $tries,
+                        'Mod' => $mod,
+                        'Mean' => $mean,
+                        '#YES' => $Solved,
+                        '#yes' => $solved,
+                        '%Wrng' => $wrongpercent,
+                        'DoDiff' => $DegOfDiff,
+                        'S.D.' => $STD,
+                        'Skew' => $SKEW,
+                    };
+                push (@Statsarray,$Statshash{$key});
+            }
+        }
+    }
+    #
+    # Sort the data
+    if ($sortby eq 'Container') {
+        foreach my $sequence (@Sequences) {
+            next if ($sequence->{'num_assess'}<1);
+            foreach my $resource (@{$sequence->{'contents'}}) {
+                next if ($resource->{'type'} ne 'assessment');
+                foreach my $part (@{$resource->{'parts'}}) {
+                    my $key = $resource->{'symb'}.':'.$part;
+                    $r->print('<tr>');
+                    if ($show_container) {
+                        $r->print('<td bgcolor="#FFFFE6">'
+                                  .$sequence->{'title'}.'</td>');
+                    }
+                    $r->print(&stats_row_from_hash($Statshash{$key}));
+                    $r->print("</tr>\n");
+                }
+            }
+        }
+    } else {
+        # $sortby is already defined, so we can charge ahead
+        my @OutputOrder;
+        if ($sortby =~ /^(title|part)$/i) {
+            # Alpha comparison
+            @OutputOrder = sort {
+                $b->{$sortby} cmp $a->{$sortby} ||
+                    $b->{'Title'} cmp $a->{'Title'} ||
+                        $b->{'Part'} cmp $a->{'Part'};
+            } @Statsarray;
+        } else {
+            # Numerical comparison
+            @OutputOrder = sort {
+                my $retvalue = 0;
+                if ($b->{$sortby} eq 'nan') {
+                    if ($a->{$sortby} ne 'nan') {
+                        $retvalue = -1;
+                    } else {
+                        $retvalue = 0;
+                    }
+                }
+                if ($a->{$sortby} eq 'nan') {
+                    if ($b->{$sortby} ne 'nan') {
+                        $retvalue = 1;
+                    }
+                }
+                if ($retvalue eq '0') {
+                    $retvalue = $b->{$sortby} <=> $a->{$sortby} ||
+                                $b->{'Title'} <=> $a->{'Title'} ||
+                                $b->{'Part'}  <=> $a->{'Part'};
                 }
+                $retvalue;
+            } @Statsarray;
+        }
+        foreach my $row (@OutputOrder) {
+            $r->print('<tr>');
+            if ($show_container) {
+                $r->print('<td bgcolor="#FFFFE6">'
+                          .$row->{'sequence'}->{'title'}.'</td>');
             }
+            $r->print(&stats_row_from_hash($row));
+            $r->print("</tr>\n");
         }
     }
     $r->print("</table>\n");
@@ -353,6 +430,20 @@
     $r->rflush();
     #
     return;
+}
+
+sub stats_row_from_hash {
+    my ($data) = @_;
+    if (ref($data) ne 'HASH') {
+        my %Tmp = @_;
+        $data = \%Tmp;
+    }
+    return &statistics_html_table_data($data->{'resource'},$data->{'Part'},
+                                       $data->{'#Stdnts'}, $data->{'Tries'},
+                                       $data->{'Mod'},     $data->{'Mean'},
+                                       $data->{'#YES'},    $data->{'#yes'},
+                                       $data->{"\%Wrng"},  $data->{'DoDiff'},
+                                       $data->{'S.D.'},    $data->{'Skew'});
 }
 
 ###############################################