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

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 03 Feb 2005 00:25:03 -0000


matthew		Wed Feb  2 19:25:03 2005 EDT

  Modified files:              
    /loncom/interface/statistics	lonstudentsubmissions.pm 
  Log:
  Consolidate some code between Excel and CSV output.  Also removed debugging
  code which caused CSV output to be generated for at most 10 students.
  
  
Index: loncom/interface/statistics/lonstudentsubmissions.pm
diff -u loncom/interface/statistics/lonstudentsubmissions.pm:1.32 loncom/interface/statistics/lonstudentsubmissions.pm:1.33
--- loncom/interface/statistics/lonstudentsubmissions.pm:1.32	Wed Feb  2 18:05:26 2005
+++ loncom/interface/statistics/lonstudentsubmissions.pm	Wed Feb  2 19:25:01 2005
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lonstudentsubmissions.pm,v 1.32 2005/02/02 23:05:26 matthew Exp $
+# $Id: lonstudentsubmissions.pm,v 1.33 2005/02/03 00:25:01 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -741,8 +741,10 @@
                                                          $partid,
                                                          $respid);
                         my @response_data = 
-                            &excel_response_data(\@headers,$prob,$partid,
-                                                 $respid,$response,$resptype);
+                            &compile_response_data(\@headers,$response,
+                                                   $prob,$partid,$respid,
+                                                   $resptype,
+                                                   \&excel_format_item);
                         $worksheet->write_row($rows_output++,$cols_output,
                                               \@response_data);
                         $cols_output+=scalar(@response_data);
@@ -782,13 +784,18 @@
     return;
 }
 
-sub excel_response_data {
-    my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
+sub compile_response_data {
+    my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
     if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
         return ();
     }
+    if (ref($format) ne 'CODE') {
+        $format = sub { return $_[0]; };
+    }
     #
-    my $submission = &HTML::Entities::decode($response->{'Submission'});
+    my $submission = 
+        &HTML::Entities::decode
+        (&Apache::lonnet::unescape($response->{'Submission'}));
     return () if (! defined($submission) || $submission eq '');
     $submission =~ s/\\\"/\"/g;
     $submission =~ s/\\\'/\'/g;
@@ -825,16 +832,15 @@
                 } elsif (exists($submission{$header})) {
                     $option = $submission{$header};
                 }
-                push(@values,&excel_format_item($option,$header));
+                push(@values,&{$format}($option,$header));
             } else {
                 # A normal column
-                push(@values,
-                     &excel_format_item($response->{$original_header},
+                push(@values,&{$format}($response->{$original_header},
                                         $original_header));
             }
         }
     } else {
-        @values = map { &excel_format_item($response->{$_},$_); } @$headers;
+        @values = map { &{$format}($response->{$_},$_); } @$headers;
     }
     return @values;
 }
@@ -842,7 +848,7 @@
 sub excel_format_item {
     my ($item,$type) = @_;
     if ($type eq 'Time') {
-        &Apache::lonstathelpers::calc_serial($item);
+        $item = &Apache::lonstathelpers::calc_serial($item);
     } else {
         if ($item =~ m/^=/) {
             $item = ' '.$item;
@@ -941,9 +947,7 @@
     }
     #
     # Main loop
-    my $mycount = 10;
     foreach my $student (@$students) {
-        last if ($mycount-- <0);
         last if ($c->aborted());
         my @rows;
         foreach my $prob (@$problems) {
@@ -973,7 +977,8 @@
                                                          $partid,$respid);
                         my @data = &compile_response_data(\@headers,$response,
                                                           $prob,$partid,
-                                                          $respid,$resptype);
+                                                          $respid,$resptype,
+                                                          \&csv_format_item);
                         my $resp_start_idx =
                             $start_col{$prob->symb}->{$partid}->{$respid};
                         for (my $k=0;$k<=$#data;$k++) {
@@ -1010,64 +1015,11 @@
     return;
 }
 
-sub compile_response_data {
-    my ($headers,$response,$prob,$partid,$respid,$resptype) = @_;
-    if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
-        return ();
-    }
-    #
-    my $submission = 
-        &HTML::Entities::decode
-        (&Apache::lonnet::unescape($response->{'Submission'}));
-    return () if (! defined($submission) || $submission eq '');
-    $response->{'Submission'} = $submission;
-    my @values;
-    if ($resptype =~ /^(option|match|rank)$/) {
-        my %submission = 
-            map { 
-                my ($foil,$value) = split('=',&Apache::lonnet::unescape($_));
-                ($foil,$value);
-            } split('&',$response->{'Submission'});
-        my %correct;
-        if (exists($response->{'Correct'})) {
-            %correct = 
-                map { 
-                    my ($foil,$value)=split('=',&Apache::lonnet::unescape($_));
-                    ($foil,$value);
-                } split('&',$response->{'Correct'});
-        }
-        #
-        foreach my $original_header (@$headers) {
-            if ($original_header =~ /^_/) {
-                # '_' denotes a foil column
-                my ($header) = ($original_header =~ m/^_(.*)$/);
-                my $option = '';
-                if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
-                    if (exists($correct{$foil})) {
-                        $option = $correct{$foil};
-                    }
-                } elsif (exists($submission{$header})) {
-                    $option = $submission{$header};
-                }
-                push(@values,&csv_format_item($option,$header));
-            } else {
-                # A normal column
-                push(@values,
-                     &csv_format_item($response->{$original_header},
-                                        $original_header));
-            }
-        }
-    } else {
-        @values = map { &csv_format_item($response->{$_},$_); } @$headers;
-    }
-    return @values;
-}
-
 sub csv_format_item {
     my ($item,$type) = @_;
     if ($type eq 'Time') {
         $item = localtime($item);
-    } 
+    }
     $item =&Apache::loncommon::csv_translate($item); 
     return $item;
 }