--- loncom/interface/statistics/lonstudentsubmissions.pm	2004/03/07 20:41:28	1.7
+++ loncom/interface/statistics/lonstudentsubmissions.pm	2005/02/02 21:19:53	1.31
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lonstudentsubmissions.pm,v 1.7 2004/03/07 20:41:28 matthew Exp $
+# $Id: lonstudentsubmissions.pm,v 1.31 2005/02/02 21:19:53 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -38,19 +38,10 @@ use HTML::Entities();
 use Time::Local();
 use Spreadsheet::WriteExcel();
 
-my @SubmitButtons = ({ name => 'PrevProblem',
-                       text => 'Previous Problem' },
-                     { name => 'NextProblem',
-                       text => 'Next Problem' },
-                     { name => 'break'},
-                     { name => 'ClearCache',
-                       text => 'Clear Caches' },
-                     { name => 'updatecaches',
-                       text => 'Update Student Data' },
-                     { name => 'SelectAnother',
+my @SubmitButtons = ({ name => 'SelectAnother',
                        text => 'Choose a different Problem' },
                      { name => 'Generate',
-                       text => 'Generate Spreadsheet'},
+                       text => 'Generate Report'},
                      );
 
 sub BuildStudentSubmissionsPage {
@@ -75,18 +66,9 @@ sub BuildStudentSubmissionsPage {
         $r->print('<h2>There are no students in the sections selected</h2>');
     }
     #
-    &Apache::loncoursedata::clear_internal_caches();
-    if (exists($ENV{'form.ClearCache'}) || 
-        exists($ENV{'form.updatecaches'}) ||
-        (exists($ENV{'form.firstanalysis'}) &&
-         $ENV{'form.firstanalysis'} ne 'no')) {
-        &Apache::lonstatistics::Gather_Full_Student_Data($r);
-    }
-    if (! exists($ENV{'form.firstanalysis'})) {
-        $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
-    } else {
-        $r->print('<input type="hidden" name="firstanalysis" value="no" />');
-    }
+    my @CacheButtonHTML = 
+        &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status',
+                                   '<h3>'.&mt('Loading student data').'</h3>');
     $r->rflush();
     #
     if (exists($ENV{'form.problemchoice'}) && 
@@ -100,85 +82,546 @@ sub BuildStudentSubmissionsPage {
                 $r->print('&nbsp;'x5);
             }
         }
+        foreach my $html (@CacheButtonHTML) {
+            $r->print($html.('&nbsp;'x5));
+        }
         #
-        $r->print('<hr />');
+        $r->print('<hr />'.$/);
         $r->rflush();
         #
-        # Determine which problem we are to analyze
-        my $current_problem = &Apache::lonstathelpers::get_target_from_id
-            ($ENV{'form.problemchoice'});
-        #
-        my ($prev,$curr,$next) = 
-            &Apache::lonstathelpers::get_prev_curr_next($current_problem,
-                                                        '.',
-                                                        'response',
-                                                        );
-        if (exists($ENV{'form.PrevProblem'}) && defined($prev)) {
-            $current_problem = $prev;
-        } elsif (exists($ENV{'form.NextProblem'}) && defined($next)) {
-            $current_problem = $next;
-        } else {
-            $current_problem = $curr;
+        # Determine which problems we are to analyze
+        my @Symbs = 
+            &Apache::lonstathelpers::get_selected_symbs('problemchoice');
+        foreach my $selected (@Symbs) {
+            $r->print('<input type="hidden" name="problemchoice" value="'.
+                      $selected.'" />'.$/);
         }
         #
-        # Store the current problem choice and send it out in the form
-        $ENV{'form.problemchoice'} = 
-            &Apache::lonstathelpers::make_target_id($current_problem);
-        $r->print('<input type="hidden" name="problemchoice" value="'.
-                  $ENV{'form.problemchoice'}.'" />');
-        #
-        if (! defined($current_problem->{'resource'})) {
+        # Get resource objects
+        my $navmap = Apache::lonnavmaps::navmap->new();
+        if (!defined($navmap)) {
+            $r->print('<h1>'.&mt("Internal error").'</h1>');
+            return;
+        }
+        my %already_seen;
+        my @Problems;
+        foreach my $symb (@Symbs) {
+            my $resource = $navmap->getBySymb($symb);
+            push(@Problems,$resource);
+        }
+        # 
+        if (! scalar(@Problems) || ! defined($Problems[0])) {
             $r->print('resource is undefined');
         } else {
-            my $resource = $current_problem->{'resource'};
-            $r->print('<h1>'.$resource->{'title'}.'</h1>');
-            $r->print('<h3>'.$resource->{'src'}.'</h3>');
-            $r->print(&Apache::lonstathelpers::render_resource($resource));
-            $r->rflush();
-            my %Data = &Apache::lonstathelpers::get_problem_data
-                ($resource->{'src'});
-            my $ProblemData = $Data{$current_problem->{'part'}.
-                                    '.'.
-                                    $current_problem->{'respid'}};
-            &prepare_excel_output($r,$current_problem,
-                                  $ProblemData,\@Students);
+            if (scalar(@Problems) == 1) {
+                my $resource = $Problems[0];
+                $r->print('<h1>'.$resource->title.'</h1>');
+                $r->print('<h3>'.$resource->src.'</h3>');
+                if ($ENV{'form.renderprob'} eq 'true') {
+                    $r->print(
+                              &Apache::lonstathelpers::render_resource({src => $resource->src})
+                              );
+                    $r->rflush();
+                }
+            }
+            if ($ENV{'form.output'} eq 'excel') {
+                &prepare_excel_output($r,\@Problems,\@Students);
+            } elsif ($ENV{'form.output'} eq 'csv') {
+                &prepare_csv_output($r,\@Problems,\@Students);
+            } else {
+                &prepare_html_output($r,\@Problems,\@Students);
+            }
         }
         $r->print('<hr />');
     } else {
         $r->print('<input type="submit" name="Generate" value="'.
-                  &mt('Generate Spreadsheet').'" />');
+                  &mt('Prepare Report').'" />');
         $r->print('&nbsp;'x5);
-        $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
-        $r->print(&Apache::lonstathelpers::ProblemSelector('.'));
+        $r->print('<p>'.
+                  &mt('Computing correct answers greatly increasese the amount of time required to prepare a report.').
+                  '</p>');
+        $r->print('<p>'.
+                  &mt('please select problems and use the <b>Prepare Report</b> button to continue.').
+                  '</p>');
+        $r->print(&Apache::lonstathelpers::MultipleProblemSelector
+                  (undef,'problemchoice','Statistics'));
+    }
+}
+
+##
+## get_extra_response_headers
+##
+sub get_extra_response_headers {
+    my @extra_resp_headers;
+    if ($ENV{'form.correctans'} eq 'true') {
+        push(@extra_resp_headers,'Correct');
+    } 
+    if ($ENV{'form.prob_status'} eq 'true') {
+        push(@extra_resp_headers,'Award Detail'); 
+        push(@extra_resp_headers,'Time');
+        push(@extra_resp_headers,'Attempt');
+        push(@extra_resp_headers,'Awarded');
     }
+    return @extra_resp_headers;
 }
 
+##
+## get_headers:
+##     return the proper headers for the given response 
+sub get_headers {
+    my ($prob,$partid,$respid,$resptype,$analysis,$output,$purpose,
+        @basic_headers) = @_;
+    my @headers;
+    if ($resptype eq 'essay' && $purpose eq 'display' &&
+        ($output eq 'html')) {# || scalar(@{$prob->parts})!=1)) {
+        @headers = ();
+    } elsif ($resptype =~ /^(option|match|rank)$/) {
+        my $prefix = '_';
+        if ($purpose eq 'display') {
+            $prefix = '';
+        }
+        my @foils = 
+            map { 
+                $prefix.$_; 
+            } sort(keys(%{$analysis->{$partid.'.'.$respid}->{'_Foils'}}));
+        if (scalar(@basic_headers) && $basic_headers[0] eq 'Correct') {
+            @foils = map { ($_ , $_.' Correct') } @foils;
+            shift(@basic_headers);  # Get rid of 'Correct'
+        }
+        @headers = (@foils,@basic_headers);
+    } else {
+        @headers = ('Submission',@basic_headers);
+    }
+    return @headers;
+}
 
 #########################################################
 #########################################################
 ##
-##      Excel output of student answers and correct answers
+##    HTML Output Routines
+##
+#########################################################
+#########################################################
+sub prepare_html_output {
+    my ($r,$problems,$students) = @_;
+    my $c = $r->connection();
+    #
+    # Set a flag for the case when there is just one problem
+    my $single_response = 0;
+    if (scalar(@$problems) == 1 &&
+        $problems->[0]->countResponses == 1) {
+        $single_response = 1;
+    }
+    #
+    # Compute the number of columns per response
+    my @extra_resp_headers = &get_extra_response_headers();
+    #
+    # Create the table header
+    my @student_columns = ('username','domain','id');
+    #
+    my %headers;
+    my $student_column_count = scalar(@student_columns);
+    $headers{'problem'} = qq{<th colspan="$student_column_count">\&nbsp;</th>};
+    foreach (@student_columns) {
+        $headers{'student'}.= '<th>'.ucfirst($_).'</th>';
+    }
+    #
+    # we put the headers into the %headers hash
+    my $total_col = scalar(@student_columns);
+    my $nonempty_part_headers = 0;
+    #
+    my %problem_analysis;
+    foreach my $prob (@$problems) {
+        my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
+        $problem_analysis{$prob->src}=\%analysis;
+        #
+        my $prob_span = 0;
+        my $single_part = 0;
+        if (scalar(@{$prob->parts}) == 1) {
+            $single_part = 1;
+        }
+        foreach my $partid (@{$prob->parts}) {
+            my $part_span = 0;
+            my $responses = [$prob->responseIds($partid)];
+            my $resptypes = [$prob->responseType($partid)];
+            for (my $i=0;$i<scalar(@$responses);$i++) {
+                my $respid = $responses->[$i];
+                my @headers = &get_headers($prob,$partid,$respid,
+                                           $resptypes->[$i],
+                                           $problem_analysis{$prob->src},
+                                           'html','display',
+                                           @extra_resp_headers);
+                if (scalar(@headers)>0) {
+                    $total_col += scalar(@headers);
+                    $part_span += scalar(@headers);
+                    $headers{'response'} .=
+                        '<th colspan="'.scalar(@headers).'">'.
+                        &mt('Response [_1]',$responses->[$i]).'</th>';
+                    $headers{'student'}.= '<th>'.join('</th><th><nobr>',
+                                                      @headers).
+                                                          '</nobr></th>';
+                }
+            }
+            if (! $single_part) {
+                my $tmpname = $partid;
+                if ($partid =~/^\d+$/) {
+                    $tmpname = $prob->part_display($partid);
+                }
+                $headers{'part'} .= qq{<th colspan="$part_span">$tmpname</th>};
+                $nonempty_part_headers = 1;
+            } else {
+                $headers{'part'} .= qq{<th colspan="$part_span">&nbsp</th>};
+            }
+            $prob_span += $part_span;
+        }
+        my $title = $prob->compTitle;
+        if ($prob_span > 0) {
+            $headers{'problem'}.= qq{<th colspan="$prob_span">$title</th>};
+        } elsif ($single_response) {
+            $prob_span = scalar(@student_columns);
+            $headers{'problem'} = qq{<th colspan="$prob_span">$title</th>};
+        }
+    }
+    if (exists($headers{'part'})) {
+        $headers{'part'} = qq{<th colspan="$student_column_count">\&nbsp;</th>}.
+            $headers{'part'};
+    }
+    if (exists($headers{'response'})) {
+        $headers{'response'}=
+            qq{<th colspan="$student_column_count">\&nbsp;</th>}.
+            $headers{'response'};
+    }
+    my $full_header = $/.'<table>'.$/;
+    $full_header .= '<tr align="left">'.$headers{'problem'}.'</tr>'.$/;
+    if ($nonempty_part_headers) {
+        $full_header .= '<tr align="left">'.$headers{'part'}.'</tr>'.$/;
+    }
+    $full_header .= '<tr align="left">'.$headers{'response'}.'</tr>'.$/;
+    $full_header .= '<tr align="left">'.$headers{'student'}.'</tr>'.$/;
+    #
+    # Main loop
+    my $count;
+    $r->print($/.$full_header.$/);
+    my $row_class = 'odd';   # css 
+    foreach my $student (@$students) {
+        my $student_row_data;
+        if ($count++ >= 30) {
+            $r->print('</table>'.$/.$full_header.$/);
+            $count = 0;
+        }
+        last if ($c->aborted());
+        foreach my $field (@student_columns) {
+            $student_row_data .= 
+                '<td valign="top">'.$student->{$field}.'</td>';
+        }
+        #
+        # Figure out what it is we need to output for this student
+        my @essays;
+        my %prob_data;
+        my $maxrow;
+        foreach my $prob (@$problems) {
+            $prob_data{$prob->symb}={};
+            foreach my $partid (@{$prob->parts}) {
+                my @responses = $prob->responseIds($partid);
+                my @response_type = $prob->responseType($partid);
+                for (my $i=0;$i<=$#responses;$i++) {
+                    my $respid  = $responses[$i];
+                    my $results = 
+                        &Apache::loncoursedata::get_response_data_by_student
+                        ($student,$prob->symb(),$respid);
+                    my $resptype = $response_type[$i];
+                    my @headers = &get_headers($prob,$partid,$respid,
+                                               $resptype,
+                                               $problem_analysis{$prob->src},
+                                               'html','normal',
+                                               @extra_resp_headers);
+                    my $width = scalar(@headers);
+                    my $resp_data;
+                    $resp_data->{'fake'} = qq{<td colspan="$width">&nbsp</td>};
+                    if (! defined($results)) {
+                        $results = [];
+                    }
+                    # 
+                    if (scalar(@$results) > $maxrow && $resptype ne 'essay') {
+                        $maxrow = scalar(@$results);
+                    }
+                    for (my $j=scalar(@$results)-1;$j>=0;$j--) {
+                        if ($ENV{'form.all_sub'} ne 'true') {
+                            next if ($j ne scalar(@$results)-1);
+                        }
+                        my $response = &hashify_response($results->[$j],
+                                                         $prob,
+                                                         $student,
+                                                         $partid,
+                                                         $respid);
+                        if ($resptype eq 'essay') {
+                            push(@essays,
+                                 &html_essay_results(\@headers,
+                                                     $prob,$partid,$respid,
+                                                     $response,
+                                                     $single_response).
+                                 '</td>');
+                        } else {
+                            push(@{$resp_data->{'real'}},
+                                 &html_non_essay_results(\@headers,
+                                                         $prob,$partid,$respid,
+                                                         $response,$resptype));
+                        }
+                    }
+                    $prob_data{$prob->symb}->{$partid}->{$respid}=$resp_data;
+                } # end of $i loop
+            } # end of partid loop
+        } # end of prob loop
+        #
+        # if there is no data, skip this student.
+        next if (! $maxrow && ! scalar(@essays));
+        #
+        # Go through the problem data and output a row.
+        if ($row_class eq 'even') {
+            $row_class = 'odd'; 
+        } else {
+            $row_class = 'even'; 
+        }
+        my $printed_something;
+        for (my $rows_output = 0;$rows_output<$maxrow;$rows_output++) {
+            my $html;
+            my $no_data = 1;
+            foreach my $prob (@$problems) {
+                foreach my $partid (@{$prob->parts}) {
+                    my @responses     = $prob->responseIds($partid);
+                    my @response_type = $prob->responseType($partid);
+                    for (my $i=0;$i<=$#responses;$i++) {
+                        my $respid   = $responses[$i];
+                        my $resp_data = 
+                            $prob_data{$prob->symb}->{$partid}->{$respid};
+                        next if ($response_type[$i] eq 'essay');
+                        if (defined($resp_data->{'real'}->[$rows_output])) {
+                            $html .= $resp_data->{'real'}->[$rows_output];
+                            $no_data = 0;
+                        } else {
+                            $html .= $resp_data->{'fake'};
+                        }
+                    }
+                }
+            }
+            if (! $no_data) {
+                $r->print(qq{<tr class="$row_class">$student_row_data$html</tr>}.$/);
+                $printed_something=1;
+            }
+        }
+        if (@essays) {
+            my $tr = qq{<tr class="$row_class">};
+            my $td = qq{<td  valign="top" class="essay" colspan="$total_col">};
+            if (! $printed_something) {
+                $r->print($tr.$student_row_data.'</tr>'.$/);
+            }
+            $r->print($tr.$td.
+                      join('</td></tr>'.$/.$tr.$td,@essays).'</td></tr>'.$/);
+            undef(@essays);
+        }
+    } # end of student loop
+    return;
+}
+
+sub hashify_response {
+    my ($response,$prob,$student,$partid,$respid) =@_;
+    my $resp_hash = {};
+    if ($ENV{'form.correctans'} eq 'true') {
+        $resp_hash->{'Correct'} = 
+            &Apache::lonstathelpers::get_student_answer
+            ($prob,$student->{'username'},$student->{'domain'},
+             $partid,$respid);
+    }
+    $resp_hash->{'Submission'} = 
+        $response->[&Apache::loncoursedata::RDs_submission()];
+    $resp_hash->{'Award Detail'} = 
+        $response->[&Apache::loncoursedata::RDs_awarddetail()];
+    $resp_hash->{'Time'} = 
+        $response->[&Apache::loncoursedata::RDs_timestamp()];
+    $resp_hash->{'Attempt'} =
+        $response->[&Apache::loncoursedata::RDs_tries()];
+    $resp_hash->{'Awarded'} = 
+        $response->[&Apache::loncoursedata::RDs_awarded()];
+    return $resp_hash;
+}
+
+#####################################################
+##
+##     HTML helper routines
+##
+#####################################################
+sub html_essay_results {
+    my ($headers,$prob,$partid,$respid,$response,$single_response)=@_;
+    if (! ref($headers) || ref($headers) ne 'ARRAY') {
+        return '';
+    }
+    # Start of telling them what problem, part, and response
+    my $Str;
+    if (! $single_response) {
+        my $id = $prob->compTitle;
+        if (defined($partid) && $partid ne '0') {
+            $id .= ' '.$prob->part_display($partid);
+        }
+        if (defined($respid)) {
+            $id .= ' '.$respid;
+        }
+        $Str .= '<nobr>'.$id.'</nobr>'.('&nbsp;'x4);
+    }
+    #
+    shift(@$headers); # Get rid of the Submission header
+    my $correct = '';
+    if ($headers->[0] eq 'Correct') {
+        $correct = &html_format_essay_sub($response->{'Correct'});
+        shift(@$headers);
+    }
+    $Str .= '<nobr>'.
+        join('',
+             map {
+                 ('&nbsp;'x4).&mt($_.': [_1]',$response->{$_});
+             } @$headers).'</nobr>';
+    if (@$headers || ! $single_response) {
+        $Str .= '<br />';
+    }
+    $Str .= &html_format_essay_sub($response->{'Submission'});
+    #
+    if (defined($correct) && $correct !~ /^\s*$/) {
+        $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
+    }
+    return $Str;
+}
+
+sub html_format_essay_sub {
+    my ($submission) = @_;
+    return '' if (! defined($submission) || $submission eq '');
+    $submission = &HTML::Entities::decode($submission);
+    $submission =~ s/\\\"/\"/g;
+    $submission =~ s/\\\'/\'/g;
+    $submission =~ s|\\r\\n|$/|g;
+    $submission = &HTML::Entities::encode($submission,'<>&"');
+    $submission =~ s|$/\s*$/|$/</p><p>$/|g;
+    $submission =~ s|\\||g;
+    $submission = '<p>'.$submission.'</p>';
+    return $submission;
+}
+
+sub html_non_essay_results {
+    my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
+    if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
+        return '';
+    }
+    # 
+    my $submission = &HTML::Entities::decode($response->{'Submission'});
+    return '' if (! defined($submission) || $submission eq '');
+    $submission =~ s/\\\"/\"/g;
+    $submission =~ s/\\\'/\'/g;
+    if ($resptype eq 'radiobutton') {
+        $submission = &HTML::Entities::encode($submission,'<>&"');
+        $submission =~ s/=([^=])$//;
+        $submission = '<nobr>'.$submission.'</nobr>';
+    }
+    $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,&HTML::Entities::encode($option));
+            } else {
+                # A normal column
+                push(@values,$response->{$original_header});
+            }
+        }
+    } else {
+        @values = map { $response->{$_}; } @$headers;
+    }
+    my $td = '<td valign="top">';
+    my $str = $td.join('</td>'.$td,@values).'</td>';
+    return $str;
+}
+
+
+#########################################################
+#########################################################
+##
+##    Excel Output Routines
 ##
 #########################################################
 #########################################################
 sub prepare_excel_output {
-    my ($r,$problem,$ProblemData,$Students) = @_;
-    my ($resource,$respid,$partid) = ($problem->{'resource'},
-                                      $problem->{'respid'},
-                                      $problem->{'part'});
-    $r->print('<h2>'.
-              &mt('Preparing Excel spreadsheet of student responses').
-              '</h2>');
+    my ($r,$Problems,$Students) = @_;
+    my $c = $r->connection();
     #
-    &GetStudentAnswers($r,$problem,$Students);
     #
-    my @Columns = ( 'username','domain','attempt','time',
-                    'submission','correct', 'grading','awarded','weight',
-                    'score');
-    my $awarded_col = 7;
-    my $weight_col  = 8;
+    # Determine the number of columns in the spreadsheet
+    my $columncount = 3; # username, domain, id
+    my @extra_resp_headers = &get_extra_response_headers();
+    my $lastprob;
+    my %problem_analysis;
+    foreach my $prob (@$Problems) {
+        my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
+        $problem_analysis{$prob->src}=\%analysis;
+        foreach my $partid (@{$prob->parts}) {
+            my $responses = [$prob->responseIds($partid)];
+            my $resptypes = [$prob->responseType($partid)];
+            for (my $i=0;$i<scalar(@$responses);$i++) {
+                my @headers = &get_headers($prob,$partid,$responses->[$i],
+                                           $resptypes->[$i],
+                                           $problem_analysis{$prob->src},
+                                           'excel','display',
+                                           @extra_resp_headers);
+                $columncount += scalar(@headers);
+            }
+        }
+        last if ($columncount > 255);
+        $lastprob = $prob;
+    }
+    if ($columncount > 255) {
+        $r->print('<h1>'.&mt('Unable to complete request').'</h1>'.$/.
+                  '<p>'.&mt('LON-CAPA is unable to produce your Excel spreadsheet because your selections will result in more than 255 columns.  Excel allows only 255 columns in a spreadsheet.').'</p>'.$/.
+                  '<p>'.&mt('Consider selecting fewer problems to generate reports on, or reducing the number of items per problem.  Or use HTML or CSV output.').'</p>'.$/.
+                  '<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'</p>');
+        $r->rflush();
+        return;
+    }
+    #
+    # Print out a message telling them what we are doing
+    if (scalar(@$Problems) > 1) {
+        $r->print('<h2>'.
+                  &mt('Preparing Excel spreadsheet of student responses to [_1] problems',
+                      scalar(@$Problems)).
+                  '</h2>');
+    } else {
+        $r->print('<h2>'.
+                  &mt('Preparing Excel spreadsheet of student responses').
+                  '</h2>');
+    }
+    $r->rflush();
     #
-    # Create excel worksheet
+    # Create the excel spreadsheet
     my $filename = '/prtspool/'.
         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
         time.'_'.rand(1000000000).'.xls';
@@ -197,85 +640,132 @@ sub prepare_excel_output {
     my $format = &Apache::loncommon::define_excel_formats($workbook);
     my $worksheet  = $workbook->addworksheet('Student Submission Data');
     #
-    # Make sure we get new weight data instead of data on a 10 minute delay
-    &Apache::lonnet::clear_EXT_cache_status();
-    #
-    # 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'});
+    # Add headers to the worksheet
+    my $rows_output = 0;
+    $worksheet->write($rows_output++,0,
+                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
+                      $format->{'h1'});
     $rows_output++;
-    $worksheet->write_row($rows_output++,0,\@Columns,$format->{'bold'});
+    my $cols_output = 0;
+    my $title_row  = $rows_output++;
+    my $partid_row = $rows_output++;
+    my $respid_row = $rows_output++;
+    my $header_row = $rows_output++;
+    $worksheet->write($title_row ,0,'Problem Title',$format->{'bold'});
+    $worksheet->write($partid_row,0,'Part ID',$format->{'bold'});
+    $worksheet->write($respid_row,0,'Response ID',$format->{'bold'});
+    # Student headers
+    my @StudentColumns = ('username','domain','id');
+    foreach (@StudentColumns) {
+        $worksheet->write($header_row,$cols_output++,ucfirst($_),
+                          $format->{'bold'});
+    }
+    # Problem headers
+    my %start_col;
+    foreach my $prob (@$Problems) {
+        my $title = $prob->compTitle;
+        $worksheet->write($title_row,$cols_output,
+                          $title,$format->{'h3'});
+        foreach my $partid (@{$prob->parts}) {
+            $worksheet->write($partid_row,$cols_output,
+                              $prob->part_display($partid));
+            my $responses = [$prob->responseIds($partid)];
+            my $resptypes = [$prob->responseType($partid)];
+            for (my $i=0;$i<scalar(@$responses);$i++) {
+                $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
+                    $cols_output;
+                $worksheet->write($respid_row,$cols_output,
+                                  $resptypes->[$i].', '.$responses->[$i]);
+                my @headers = &get_headers($prob,$partid,$responses->[$i],
+                                           $resptypes->[$i],
+                                           $problem_analysis{$prob->src},
+                                           'excel','display',
+                                           @extra_resp_headers);
+                foreach my $text (@headers) {
+                    if ($text eq 'Time') {
+                        $worksheet->set_column($cols_output,$cols_output,undef,
+                                               $format->{'date'});
+                    } 
+                    $worksheet->write($header_row,$cols_output++,$text);
+                }
+            }
+        }
+    }
     #
     # Populate the worksheet with the student data
+    my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
+        ($r,'Excel File Compilation Status',
+         'Excel File Compilation Progress', 
+         scalar(@$Students),'inline',undef,'Statistics','stats_status');
+    my $max_row = $rows_output;
     foreach my $student (@$Students) {
-        my $results = &Apache::loncoursedata::get_response_data_by_student
-            ($student,$resource->{'symb'},$respid);
-        my %row;
-        $row{'username'} = $student->{'username'};
-        $row{'domain'}   = $student->{'domain'};
-        $row{'correct'} = $student->{'answer'};
-        $row{'weight'} = &Apache::lonnet::EXT
-            ('resource.'.$partid.'.weight',$resource->{'symb'},
-             undef,undef,undef);
-        if (! defined($results) || ref($results) ne 'ARRAY') {
-            $row{'score'} = '='.
-                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
-                    ($rows_output,$awarded_col)
-                .'*'.
-                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
-                    ($rows_output,$weight_col);
-            my $cols_output = 0;
-            foreach my $col (@Columns) {
-                if (! exists($row{$col})) {
-                    $cols_output++;
-                    next;
+        last if ($c->aborted());
+        $cols_output = 0;
+        my $student_row = $max_row;
+        foreach my $field (@StudentColumns) {
+            $worksheet->write($student_row,$cols_output++,
+                              $student->{$field});
+        }
+        my $last_student_col = $cols_output-1;
+        foreach my $prob (@$Problems) {
+            foreach my $partid (@{$prob->parts}) {
+                my @Response = $prob->responseIds($partid);
+                my @ResponseType = $prob->responseType($partid);
+                for (my $i=0;$i<=$#Response;$i++) {
+                    my $respid   = $Response[$i];
+                    my $resptype = $ResponseType[$i];
+                    my $results = 
+                        &Apache::loncoursedata::get_response_data_by_student
+                        ($student,$prob->symb(),$respid);
+                    my @headers = &get_headers($prob,$partid,$respid,
+                                               $resptype,
+                                               $problem_analysis{$prob->src},
+                                               'excel','normal',
+                                               @extra_resp_headers);
+
+                    if (! defined($results)) {
+                        $results = [];
+                    }
+                    #
+                    $rows_output = $student_row;
+                    #
+                    my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
+                    for (my $j=scalar(@$results)-1;$j>=0;$j--) {
+                        $cols_output = $response_start_col;
+                        if ($ENV{'form.all_sub'} ne 'true') {
+                            next if ($j ne scalar(@$results)-1);
+                        }
+                        my $response = &hashify_response($results->[$j],
+                                                         $prob,
+                                                         $student,
+                                                         $partid,
+                                                         $respid);
+                        my @response_data = 
+                            &excel_response_data(\@headers,$prob,$partid,
+                                                 $respid,$response,$resptype);
+                        $worksheet->write_row($rows_output++,$cols_output,
+                                              \@response_data);
+                        $cols_output+=scalar(@response_data);
+                        if ($rows_output > $max_row) {
+                            $max_row = $rows_output;
+                        }
+                    }
                 }
-                $worksheet->write($rows_output,$cols_output++,$row{$col});
             }
-            $rows_output++;
-        } else {
-            foreach my $response (@$results) {
-                delete($row{'time'});
-                delete($row{'attempt'});
-                delete($row{'submission'});
-                delete($row{'awarded'});
-                delete($row{'grading'});
-                delete($row{'score'});
-                my %row_format;
-                #
-                # Time is handled differently
-                $row{'time'} = &Apache::lonstathelpers::calc_serial
-                    ($response->[&Apache::loncoursedata::RDs_timestamp()]);
-                $row_format{'time'}=$format->{'date'};
-                #
-                $row{'attempt'}  = $response->[
-                     &Apache::loncoursedata::RDs_tries()];
-                $row{'submission'} = $response->[
-                     &Apache::loncoursedata::RDs_submission()];
-                if ($row{'submission'} =~ m/^=/) {
-                    # This will be interpreted as a formula.  That is bad!
-                    $row{'submission'} = " ".$row{'submission'};
-                }
-                $row{'grading'} = $response->[
-                     &Apache::loncoursedata::RDs_awarddetail()];
-                $row{'awarded'} = $response->[
-                     &Apache::loncoursedata::RDs_awarded()];
-                $row{'score'} = '='.
-                    &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
-                        ($rows_output,$awarded_col)
-                    .'*'.
-                    &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
-                        ($rows_output,$weight_col);
-                my $cols_output = 0;
-                foreach my $col (@Columns) {
-                    $worksheet->write($rows_output,$cols_output++,$row{$col},
-                                      $row_format{$col});
-                }
-                $rows_output++;
+        }
+        # Fill in the remaining rows with the students data
+        for (my $row = $student_row+1;$row<=$max_row;$row++) {
+            my $cols = 0;
+            foreach my $field (@StudentColumns) {
+                $worksheet->write($row,$cols++,
+                                  $student->{$field});
             }
-        } # End of else clause on if (! defined($results) ....
+        }
+        $rows_output++;
+        &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
+                                                 'last student');
     }
+    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
     #
     # Close the excel file
     $workbook->close();
@@ -284,36 +774,260 @@ sub prepare_excel_output {
     $r->print('<p><a href="'.$filename.'">'.
               &mt('Your Excel spreadsheet.').
               '</a></p>'."\n");
+    $r->print('<script>'.
+              'window.document.Statistics.stats_status.value="'.
+              'Done compiling spreadsheet.  See link below to download.'.
+              '";</script>');
+    $r->rflush();
+    return;
+}
+
+sub excel_response_data {
+    my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
+    if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
+        return ();
+    }
+    #
+    my $submission = &HTML::Entities::decode($response->{'Submission'});
+    return '' if (! defined($submission) || $submission eq '');
+    $submission =~ s/\\\"/\"/g;
+    $submission =~ s/\\\'/\'/g;
+    if ($resptype eq 'radiobutton') {
+        $submission =~ s/=([^=])$//;
+    }
+    $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,&excel_format_item($option,$header));
+            } else {
+                # A normal column
+                push(@values,
+                     &excel_format_item($response->{$original_header},
+                                        $original_header));
+            }
+        }
+    } else {
+        @values = map { &excel_format_item($response->{$_},$_); } @$headers;
+    }
+    return @values;
 }
 
-sub GetStudentAnswers {
-    my ($r,$problem,$Students) = @_;
-    my %Answers;
-    my ($resource,$partid,$respid) = ($problem->{'resource'},
-                                      $problem->{'part'},
-                                      $problem->{'respid'});
-    # Open progress window
+sub excel_format_item {
+    my ($item,$type) = @_;
+    if ($type eq 'Time') {
+        &Apache::lonstathelpers::calc_serial($item);
+    } else {
+        if ($item =~ m/^=/) {
+            $item = ' '.$item;
+        }
+        $item =~ s/\\r//g;
+        $item =~ s/\\n/\n/g;
+        $item =~ s/(\s*$|^\s*)//g;
+        $item =~ s/\\\'/\'/g;
+    }
+    return $item;
+}
+
+#########################################################
+#########################################################
+##
+##      CSV output of student answers
+##
+#########################################################
+#########################################################
+sub prepare_csv_output {
+    my ($r,$problems,$students) = @_;
+    my $c = $r->connection();
+    #
+    $r->print('<h2>'.
+              &mt('Generating CSV report of student responses').'</h2>');
+    #
+    # Progress window
     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
-        ($r,'Student Answer Compilation Status',
-         'Student Answer Compilation Progress', scalar(@$Students));
-    $r->print("<table>\n");
+        ($r,'CSV File Compilation Status',
+         'CSV File Compilation Progress', 
+         scalar(@$students),'inline',undef,'Statistics','stats_status');
+    
     $r->rflush();
-    foreach my $student (@$Students) {
-        my $sname = $student->{'username'};
-        my $sdom = $student->{'domain'};
-        my $answer = &Apache::lonstathelpers::analyze_problem_as_student
-            ($resource,$sname,$sdom,$partid,$respid);
+    #
+    # Open a file
+    my $outputfile;
+    my $filename = '/prtspool/'.
+        $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+            time.'_'.rand(1000000000).'.csv';
+    unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
+        $r->log_error("Couldn't open $filename for output $!");
+        $r->print("Problems occured in writing the csv file.  ".
+                  "This error has been logged.  ".
+                  "Please alert your LON-CAPA administrator.");
+        $outputfile = undef;
+    }
+    #
+    # Compute the number of columns per response
+    my @extra_resp_headers;
+    if ($ENV{'form.correctans'} eq 'true') {
+        push(@extra_resp_headers,'Correct');
+    } 
+    if ($ENV{'form.prob_status'} eq 'true') {
+        push(@extra_resp_headers,'Award Detail'); 
+        push(@extra_resp_headers,'Time');
+        push(@extra_resp_headers,'Attempt');
+        push(@extra_resp_headers,'Awarded');
+    }
+    my $response_multiplier = scalar(@extra_resp_headers);
+    #
+    # Create the table header
+    my @student_columns = ('username','domain','id');
+    #
+    my %headers;
+    push(@{$headers{'student'}},@student_columns);
+    # Pad for the student data
+    foreach my $row ('problem','part','response') {
+        foreach (@student_columns) {
+            push(@{$headers{$row}},'');
+        }
+    }
+    #
+    # we put the headers into the %headers hash
+    my $prob_start_idx = 0;
+    foreach my $prob (@$problems) {
+        $headers{'problem'}->[$prob_start_idx] = $prob->compTitle;
+        my $part_start_idx = $prob_start_idx;
+        foreach my $partid (@{$prob->parts}) {
+            $headers{'part'}->[$part_start_idx] = $prob->part_display($partid);
+            my $responses = [$prob->responseIds($partid)];
+            for (my $i=0;$i<scalar(@$responses);$i++) {
+                my $resp_idx = $prob_start_idx + $response_multiplier * $i;
+                $headers{'response'}->[$resp_idx]=
+                    &mt('Response [_1]',$responses->[$i]);
+                for (my $j=0;$j<=$#extra_resp_headers;$j++) {
+                    $headers{'student'}->[$resp_idx+$j]=$extra_resp_headers[$j];
+                }
+            }
+            $part_start_idx += scalar(@$responses)*$response_multiplier;
+        }
+        $prob_start_idx += $prob->countResponses * $response_multiplier;
+    }
+    foreach my $row ('problem','part','response','student') {
+        print $outputfile '"'.join('","',map { ''; } @student_columns).'","'.
+            join('","',
+                 map { 
+                     &Apache::loncommon::csv_translate($_); 
+                 } @{$headers{$row}}).'"'.$/;
+    }
+    #
+    # Main loop
+    foreach my $student (@$students) {
+        last if ($c->aborted());
+        my @rows;
+        my $prob_start_idx = 0;
+        foreach my $prob (@$problems) {
+            my $part_start_idx = 0;
+            foreach my $partid (@{$prob->parts}) {
+                my @responses = $prob->responseIds($partid);
+                my @response_type = $prob->responseType($partid);
+                for (my $i=0;$i<=$#responses;$i++) {
+                    my $resp_start_idx = $response_multiplier * $i;
+                    my $respid   = $responses[$i];
+                    my $results = 
+                        &Apache::loncoursedata::get_response_data_by_student
+                        ($student,$prob->symb(),$respid);
+                    if (! defined($results)) {
+                        $results = [];
+                    }
+                    for (my $j=0; $j<scalar(@$results);$j++) {
+                        if ($ENV{'form.all_sub'} ne 'true') {
+                            next if ($j != 0);
+                        }
+                        my $idx = scalar(@$results) - $j - 1;
+                        my $response = $results->[$idx];
+                        my @data = &compile_response_data($response,$student,
+                                                          $prob,$partid,
+                                                          $respid);
+                        for (my $k=0;$k<=$#data;$k++) {
+                            $rows[$j]->[$prob_start_idx + $part_start_idx +
+                                        $resp_start_idx + $k] = $data[$k];
+                        }
+                    }
+                }
+                $part_start_idx += scalar(@responses)*$response_multiplier;
+            }
+            $prob_start_idx += $prob->countResponses * $response_multiplier;
+        }
+        foreach my $row (@rows) {
+            print $outputfile '"'.join('","',
+                                       map { $student->{$_}; }
+                                       @student_columns).'"';
+                                           
+            for (my $i=0;$i<$prob_start_idx;$i++) {
+                my $value = &Apache::loncommon::csv_translate($row->[$i]);
+                $value ||='';
+                print $outputfile ',"'.$value.'"';
+            }
+            print $outputfile $/;
+        }
+        undef(@rows);
         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
-                                                 &mt('last student'));
-        $student->{'answer'} = $answer;
+                                                 'last student');
     }
-    $r->print("</table>\n");
-    $r->rflush();
-    # close progress window
+    close($outputfile);
+    #
+    # Close the progress window
     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
+    #
+    # Tell the user where to get their csv file
+    $r->print('<br />'.
+              '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
+    $r->rflush();
     return;
 }
 
+sub compile_response_data {
+    my ($response,$student,$prob,$partid,$respid) = @_;
+    my @rowdata;
+    push(@rowdata,&Apache::lonnet::unescape($response->[&Apache::loncoursedata::RDs_submission()]));
+    if ($ENV{'form.correctans'} eq 'true') {
+        my $correct = &Apache::lonstathelpers::get_student_answer
+            ($prob,$student->{'username'},$student->{'domain'},
+             $partid,$respid);
+        push(@rowdata,$correct);
+    }
+    if ($ENV{'form.prob_status'}) {
+        push(@rowdata,$response->[&Apache::loncoursedata::RDs_awarddetail()]);
+        push(@rowdata,&Apache::lonlocal::locallocaltime
+             ($response->[&Apache::loncoursedata::RDs_timestamp()]));
+        push(@rowdata,$response->[&Apache::loncoursedata::RDs_tries()]);
+        push(@rowdata,$response->[&Apache::loncoursedata::RDs_awarded()]);
+    }
+    return @rowdata;
+}
 
 #########################################################
 #########################################################
@@ -324,18 +1038,29 @@ sub GetStudentAnswers {
 #########################################################
 sub CreateInterface {
     ##
+    ## Output Selection
+    my $output_selector = $/.'<select name="output">'.$/;
+    foreach ('HTML','Excel','CSV') {
+        $output_selector .= '    <option value="'.lc($_).'"';
+        if ($ENV{'form.output'} eq lc($_)) {
+            $output_selector .= ' selected ';
+        }
+        $output_selector .='>'.&mt($_).'</option>'.$/;
+    } 
+    $output_selector .= '</select>'.$/;
+    ##
     ## Environment variable initialization
     my $Str = '';
     $Str .= &Apache::lonhtmlcommon::breadcrumbs
         (undef,'Student Submission Reports');
+    $Str .= '<p>';
     $Str .= '<table cellspacing="5">'."\n";
     $Str .= '<tr>';
-    $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
-    $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
-    $Str .= '<td align="center">&nbsp;</td>';
+    $Str .= '<th>'.&mt('Sections').'</th>';
+    $Str .= '<th>'.&mt('Enrollment Status').'</th>';
+    $Str .= '<th>'.&mt('Output as [_1]',$output_selector).'</th>';
     $Str .= '</tr>'."\n";
-    ##
-    ## 
+    #
     $Str .= '<tr><td align="center">'."\n";
     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
     $Str .= '</td>';
@@ -344,31 +1069,60 @@ sub CreateInterface {
     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
     $Str .= '</td>';
     #
-    $Str .= '<td></td>';
+    # Render problem checkbox
+    my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
+    if (exists($ENV{'form.renderprob'}) && $ENV{'form.renderprob'} eq 'true') {
+        $prob_checkbox .= 'checked ';
+    }
+    $prob_checkbox .= 'value="true" />';
+    #
+    # Compute correct answers checkbox
+    my $ans_checkbox = '<input type="checkbox" name="correctans" ';
+    if (exists($ENV{'form.correctans'}) && $ENV{'form.correctans'} eq 'true') {
+        $ans_checkbox .= 'checked ';
+    }
+    $ans_checkbox .= 'value="true" />';
+    #
+    # Show all submissions checkbox
+    my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
+    if (exists($ENV{'form.all_sub'}) && 
+        $ENV{'form.all_sub'} eq 'true') {
+        $all_sub_checkbox .= 'checked ';
+    }
+    $all_sub_checkbox.= 'value="true" />';
+    #
+    # problem status checkbox
+    my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
+    if (exists($ENV{'form.prob_status'}) && 
+        $ENV{'form.prob_status'} eq 'true') {
+        $prob_status_checkbox .= 'checked ';
+    }
+    $prob_status_checkbox .= 'value="true" />';
+    #
+    $Str .= '<td align="right" halign="top">'.
+        '<label><b>'.
+        &mt('Show problem [_1]',$prob_checkbox).'</b></label><br />'.
+        '<label><b>'.
+        &mt('Show correct answers [_1]',$ans_checkbox).'</b></label><br />'.
+        '<label><b>'.
+        &mt('Show all submissions [_1]',$all_sub_checkbox).
+        '</b></label><br />'.
+        '<label><b>'.
+        &mt('Show problem grading [_1]',$prob_status_checkbox).
+        '</b></label><br />'.
+        '</td>';
     #
-    my $only_seq_with_assessments = sub { 
-        my $s=shift;
-        if ($s->{'num_assess'} < 1) { 
-            return 0;
-        } else { 
-            return 1;
-        }
-    };
-    ##
-    ##
     $Str .= '</tr>'."\n";
     $Str .= '</table>'."\n";
     #
-    # We do this to make sure the sequence information is initialized
-    &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
-                                              $only_seq_with_assessments);
-    
-    #
+    $Str .= '<nobr>'.&mt('Status: [_1]',
+                         '<input type="text" '.
+                         'name="stats_status" size="60" value="" />').
+            '</nobr>'.'</p>';    
+    ##
     return $Str;
 }
 
-
-
 1;
 
 __END__