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

matthew lon-capa-cvs@mail.lon-capa.org
Wed, 18 Feb 2004 17:33:12 -0000


This is a MIME encoded message

--matthew1077125592
Content-Type: text/plain

matthew		Wed Feb 18 12:33:12 2004 EDT

  Modified files:              
    /loncom/interface/statistics	lonproblemanalysis.pm 
  Log:
  First working pass at excel output of student submissions and the correct
  answer for a response.  More needs to be done.
  
  
--matthew1077125592
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20040218123312.txt"

Index: loncom/interface/statistics/lonproblemanalysis.pm
diff -u loncom/interface/statistics/lonproblemanalysis.pm:1.68 loncom/interface/statistics/lonproblemanalysis.pm:1.69
--- loncom/interface/statistics/lonproblemanalysis.pm:1.68	Mon Feb 16 15:50:03 2004
+++ loncom/interface/statistics/lonproblemanalysis.pm	Wed Feb 18 12:33:12 2004
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lonproblemanalysis.pm,v 1.68 2004/02/16 20:50:03 matthew Exp $
+# $Id: lonproblemanalysis.pm,v 1.69 2004/02/18 17:33:12 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -159,8 +159,13 @@
                                        $ProblemData,
                                        \@Students);
             } elsif ($current_problem->{'resptype'} eq 'numerical') {
-                &NumericalResponseAnalysis($r,$current_problem,
-                                           $ProblemData,\@Students);
+#                if (exists($ENV{'form.ExcelOutput'})) {
+                    &prepare_excel_output($r,$current_problem,
+                                          $ProblemData,\@Students);
+#                } else {
+#                    &NumericalResponseAnalysis($r,$current_problem,
+#                                               $ProblemData,\@Students);
+#                }
             } else {
                 $r->print('<h2>This analysis is not supported</h2>');
             }
@@ -176,6 +181,114 @@
     }
 }
 
+
+#########################################################
+#########################################################
+##
+##      Excel output of student answers and correct answers
+##
+#########################################################
+#########################################################
+sub prepare_excel_output {
+    my ($r,$problem,$ProblemData,$Students) = @_;
+    my ($resource,$respid) = ($problem->{'resource'},
+                              $problem->{'respid'});
+    $r->print('<h2>'.
+              &mt('Preparing Excel spreadsheet of student responses').
+              '</h2>');
+    #
+    &GetStudentAnswers($r,$problem,$Students);
+    #
+    my @Columns = ( 'username','domain','attempt','time',
+                    'submission','correct', 'grading');
+    #
+    # Create excel worksheet
+    my $filename = '/prtspool/'.
+        $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+        time.'_'.rand(1000000000).'.xls';
+    my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
+    if (! defined($workbook)) {
+        $r->log_error("Error creating excel spreadsheet $filename: $!");
+        $r->print('<p>'.&mt("Unable to create new Excel file.  ".
+                            "This error has been logged.  ".
+                            "Please alert your LON-CAPA administrator").
+                  '</p>');
+        return undef;
+    }
+    #
+    $workbook->set_tempdir('/home/httpd/perl/tmp');
+    #
+    my $format = &Apache::loncommon::define_excel_formats($workbook);
+    my $worksheet  = $workbook->addworksheet('Student Submission Data');
+    #
+    # Put on the standard headers and whatnot
+    my $rows_output=0;
+    $worksheet->write($rows_output++,0,$resource->{'title'},$format->{'h1'});
+    $worksheet->write($rows_output++,0,$resource->{'src'},$format->{'h3'});
+    $rows_output++;
+    $worksheet->write_row($rows_output++,0,\@Columns,$format->{'bold'});
+    #
+    # Populate the worksheet with the student data
+    foreach my $student (@$Students) {
+        # For each attempt
+        #     write the username, domain, attempt number, timestamp,
+        #     submission, correct answer, grading
+        my $results = &Apache::loncoursedata::get_response_data_by_student
+            ($student,$resource->{'symb'},$respid);
+        if (! defined($results) || ref($results) ne 'ARRAY') {
+            my %row;
+            $row{'username'} = $student->{'username'};
+            $row{'domain'}   = $student->{'domain'};
+            $row{'correct'} = $student->{'answer'};
+            my $cols_output = 0;
+            foreach my $col (@Columns) {
+                if (! exists($row{$col})) {
+                    $cols_output++;
+                    next;
+                }
+                $worksheet->write($rows_output,$cols_output++,$row{$col});
+            }
+            $rows_output++;
+        } else {
+            foreach my $response (@$results) {
+                my %row_format;
+                my %row;
+                #
+                # Time is handled differently
+                $row{'time'} = &calc_serial(
+                     $response->[&Apache::loncoursedata::RDs_timestamp()]);
+                $row_format{'time'}=$format->{'date'};
+                #
+                $row{'username'} = $student->{'username'};
+                $row{'domain'}   = $student->{'domain'};
+                $row{'attempt'}  = $response->[
+                     &Apache::loncoursedata::RDs_tries()];
+                $row{'submission'} = $response->[
+                     &Apache::loncoursedata::RDs_submission()];
+                $row{'correct'} = $student->{'answer'};
+                $row{'grading'} = $response->[
+                     &Apache::loncoursedata::RDs_awarddetail()];
+                my $cols_output = 0;
+                foreach my $col (@Columns) {
+                    $worksheet->write($rows_output,$cols_output++,$row{$col},
+                                      $row_format{$col});
+                }
+                $rows_output++;
+            }
+        } # End of else clause on if (! defined($results) ....
+    }
+    #
+    # Close the excel file
+    $workbook->close();
+    #
+    # Write a link to allow them to download it
+    $r->print('<p><a href="'.$filename.'">'.
+              &mt('Your Excel spreadsheet.').
+              '</a></p>'."\n");
+
+}
+
+
 #########################################################
 #########################################################
 ##
@@ -246,33 +359,21 @@
          'Student Answer Compilation Progress', scalar(@$Students));
     $r->print("<table>\n");
     $r->rflush();
-    my ($min,$max);
     foreach my $student (@$Students) {
         my $sname = $student->{'username'};
         my $sdom = $student->{'domain'};
-        my $answer = analyze_problem_as_student($resource,
-                                                $sname,$sdom,
-                                                $partid,$respid);
-        if (! defined($min) || $min > $answer) {
-            $min = $answer;
-        }
-        if (! defined($max) || $max < $answer) {
-            $max = $answer;
-        }
-#        $r->print('<tr>'.
-#                  '<td>'.$sname.'</td>'.
-#                  '<td>'.$sdom.'</td>'.
-#                  '<td>'.$answer.'</td>'.
-#                  '</tr>'."\n");
+        my $answer = &analyze_problem_as_student($resource,
+                                                 $sname,$sdom,
+                                                 $partid,$respid);
         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                                                  &mt('last student'));
         $student->{'answer'} = $answer;
     }
     $r->print("</table>\n");
     $r->rflush();
-    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
     # close progress window
-    return ($max,$min);
+    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
+    return;
 }
 
 sub build_student_data_worksheet {
@@ -999,22 +1100,11 @@
     }
     #
     $workbook->set_tempdir('/home/httpd/perl/tmp');
-    #
-    # Define some potentially useful formats
-    my $format;
-    $format->{'header'} = $workbook->add_format(bold      => 1, 
-                                                bottom    => 1,
-                                                align     => 'center');
-    $format->{'bold'} = $workbook->add_format(bold=>1);
-    $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
-    $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
-    $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
-    $format->{'date'} = $workbook->add_format(num_format=>
-                                              'mmm d yyyy hh:mm AM/PM');
+    my $format = &Apache::loncommon::define_excel_formats($workbook);
     #
     # Create and populate main worksheets
     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
-    my $student_data_sheet  = $workbook->addworksheet('Student Data');
+    my $student_data_sheet = &build_student_data_worksheet($workbook,$format);
     my $response_data_sheet = $workbook->addworksheet('Response Data');
     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
                        $response_data_sheet) {
@@ -1028,10 +1118,6 @@
     if ($result ne 'okay') {
         # Do something useful
     }
-    $result = &OR_build_student_data_worksheet($student_data_sheet,$format);
-    if ($result ne 'okay') {
-        # Do something useful
-    }
     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
                                              $PerformanceData,$Foils,
                                              $ORdata);
@@ -1118,32 +1204,6 @@
         $worksheet->write($rows_output++,0,$string);
     }
     return 'okay';
-}
-
-sub OR_build_student_data_worksheet {
-    my ($worksheet,$format) = @_;
-    my $rows_output = 3;
-    my $cols_output = 0;
-    $worksheet->write($rows_output++,0,'Student Data',$format->{'h3'});
-    my @Headers = ('full name','username','domain','section',
-                   "student\nnumber",'identifier');
-    $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
-    my @Students = @Apache::lonstatistics::Students;
-    my $studentrows = &Apache::loncoursedata::get_student_data(\@Students);
-    my %ids;
-    foreach my $row (@$studentrows) {
-        my ($mysqlid,$student) = @$row;
-        $ids{$student}=$mysqlid;
-    }
-    foreach my $student (@Students) {
-        my $name_domain = $student->{'username'}.':'.$student->{'domain'};
-        $worksheet->write_row($rows_output++,0,
-                          [$student->{'fullname'},
-                           $student->{'username'},$student->{'domain'},
-                           $student->{'section'},$student->{'id'},
-                           $ids{$name_domain}]);
-    }
-    return;
 }
 
 sub OR_build_response_data_worksheet {

--matthew1077125592--