Annotation of loncom/interface/statistics/lonstudentsubmissions.pm, revision 1.56

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: #
1.56    ! bisitz      3: # $Id: lonstudentsubmissions.pm,v 1.55 2009/12/16 13:01:01 bisitz Exp $
1.1       matthew     4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: package Apache::lonstudentsubmissions;
                     28: 
                     29: use strict;
1.40      albertel   30: use Apache::lonnet;
1.1       matthew    31: use Apache::loncommon();
                     32: use Apache::lonhtmlcommon();
                     33: use Apache::loncoursedata();
                     34: use Apache::lonstatistics;
                     35: use Apache::lonlocal;
                     36: use Apache::lonstathelpers;
                     37: use HTML::Entities();
                     38: use Time::Local();
                     39: use Spreadsheet::WriteExcel();
1.45      www        40: use lib '/home/httpd/lib/perl/';
                     41: use LONCAPA;
                     42:   
1.1       matthew    43: 
1.18      matthew    44: my @SubmitButtons = ({ name => 'SelectAnother',
1.1       matthew    45:                        text => 'Choose a different Problem' },
                     46:                      { name => 'Generate',
1.16      matthew    47:                        text => 'Generate Report'},
1.1       matthew    48:                      );
                     49: 
                     50: sub BuildStudentSubmissionsPage {
                     51:     my ($r,$c)=@_;
                     52:     #
                     53:     my %Saveable_Parameters = ('Status' => 'scalar',
                     54:                                'Section' => 'array',
                     55:                                'NumPlots' => 'scalar',
                     56:                                );
                     57:     &Apache::loncommon::store_course_settings('student_submissions',
                     58:                                               \%Saveable_Parameters);
                     59:     &Apache::loncommon::restore_course_settings('student_submissions',
                     60:                                                 \%Saveable_Parameters);
                     61:     #
                     62:     &Apache::lonstatistics::PrepareClasslist();
                     63:     #
                     64:     $r->print(&CreateInterface());
                     65:     #
                     66:     my @Students = @Apache::lonstatistics::Students;
                     67:     #
                     68:     if (@Students < 1) {
1.50      bisitz     69:         $r->print('<div class="LC_warning">'
                     70:                  .&mt('There are no students in the sections selected.')
                     71:                  .'</div>');
1.1       matthew    72:     }
                     73:     #
1.11      matthew    74:     my @CacheButtonHTML = 
1.19      matthew    75:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status',
1.50      bisitz     76:                                    '<div class="LC_info">'.&mt('Loading student data...').'</div>');
1.1       matthew    77:     $r->rflush();
                     78:     #
1.40      albertel   79:     if (exists($env{'form.problemchoice'}) && 
                     80:         ! exists($env{'form.SelectAnother'})) {
1.1       matthew    81:         foreach my $button (@SubmitButtons) {
                     82:             if ($button->{'name'} eq 'break') {
                     83:                 $r->print("<br />\n");
                     84:             } else {
                     85:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
                     86:                           'value="'.&mt($button->{'text'}).'" />');
                     87:                 $r->print('&nbsp;'x5);
                     88:             }
                     89:         }
1.11      matthew    90:         foreach my $html (@CacheButtonHTML) {
                     91:             $r->print($html.('&nbsp;'x5));
                     92:         }
1.1       matthew    93:         #
1.18      matthew    94:         $r->print('<hr />'.$/);
1.1       matthew    95:         $r->rflush();
                     96:         #
1.18      matthew    97:         # Determine which problems we are to analyze
                     98:         my @Symbs = 
                     99:             &Apache::lonstathelpers::get_selected_symbs('problemchoice');
                    100:         foreach my $selected (@Symbs) {
                    101:             $r->print('<input type="hidden" name="problemchoice" value="'.
                    102:                       $selected.'" />'.$/);
                    103:         }
1.1       matthew   104:         #
1.18      matthew   105:         # Get resource objects
                    106:         my $navmap = Apache::lonnavmaps::navmap->new();
                    107:         if (!defined($navmap)) {
1.50      bisitz    108:             $r->print('<div class="LC_error">'.&mt("Internal error").'</div>');
1.18      matthew   109:             return;
                    110:         }
                    111:         my %already_seen;
                    112:         my @Problems;
                    113:         foreach my $symb (@Symbs) {
                    114:             my $resource = $navmap->getBySymb($symb);
                    115:             push(@Problems,$resource);
1.1       matthew   116:         }
1.25      matthew   117:         # 
1.37      matthew   118:         $r->print('<h4>'.
                    119:                   &Apache::lonstatistics::section_and_enrollment_description().
                    120:                   '</h4>');
1.18      matthew   121:         if (! scalar(@Problems) || ! defined($Problems[0])) {
1.1       matthew   122:             $r->print('resource is undefined');
                    123:         } else {
1.18      matthew   124:             if (scalar(@Problems) == 1) {
                    125:                 my $resource = $Problems[0];
                    126:                 $r->print('<h1>'.$resource->title.'</h1>');
                    127:                 $r->print('<h3>'.$resource->src.'</h3>');
1.40      albertel  128:                 if ($env{'form.renderprob'} eq 'true') {
1.36      matthew   129:                     $r->print(&Apache::lonstathelpers::render_resource($resource));
1.18      matthew   130:                     $r->rflush();
                    131:                 }
                    132:             }
1.40      albertel  133:             if ($env{'form.output'} eq 'excel') {
1.21      matthew   134:                 &prepare_excel_output($r,\@Problems,\@Students);
1.40      albertel  135:             } elsif ($env{'form.output'} eq 'csv') {
1.22      matthew   136:                 &prepare_csv_output($r,\@Problems,\@Students);
1.21      matthew   137:             } else {
                    138:                 &prepare_html_output($r,\@Problems,\@Students);
                    139:             }
1.1       matthew   140:         }
                    141:         $r->print('<hr />');
                    142:     } else {
                    143:         $r->print('<input type="submit" name="Generate" value="'.
1.17      matthew   144:                   &mt('Prepare Report').'" />');
1.1       matthew   145:         $r->print('&nbsp;'x5);
1.19      matthew   146:         $r->print('<p>'.
                    147:                   &mt('Computing correct answers greatly increasese the amount of time required to prepare a report.').
                    148:                   '</p>');
                    149:         $r->print('<p>'.
1.50      bisitz    150:                   &mt('Please select problems and use the [_1]Prepare Report[_2] button to continue.','<b>','</b>').
1.19      matthew   151:                   '</p>');
1.18      matthew   152:         $r->print(&Apache::lonstathelpers::MultipleProblemSelector
1.19      matthew   153:                   (undef,'problemchoice','Statistics'));
1.18      matthew   154:     }
                    155: }
                    156: 
1.30      matthew   157: ##
1.31      matthew   158: ## get_extra_response_headers
                    159: ##
                    160: sub get_extra_response_headers {
                    161:     my @extra_resp_headers;
1.40      albertel  162:     if ($env{'form.correctans'} eq 'true') {
1.31      matthew   163:         push(@extra_resp_headers,'Correct');
                    164:     } 
1.40      albertel  165:     if ($env{'form.prob_status'} eq 'true') {
1.31      matthew   166:         push(@extra_resp_headers,'Award Detail'); 
                    167:         push(@extra_resp_headers,'Time');
                    168:         push(@extra_resp_headers,'Attempt');
                    169:         push(@extra_resp_headers,'Awarded');
                    170:     }
                    171:     return @extra_resp_headers;
                    172: }
                    173: 
                    174: ##
1.30      matthew   175: ## get_headers:
                    176: ##     return the proper headers for the given response 
                    177: sub get_headers {
                    178:     my ($prob,$partid,$respid,$resptype,$analysis,$output,$purpose,
                    179:         @basic_headers) = @_;
                    180:     my @headers;
                    181:     if ($resptype eq 'essay' && $purpose eq 'display' &&
                    182:         ($output eq 'html')) {# || scalar(@{$prob->parts})!=1)) {
                    183:         @headers = ();
                    184:     } elsif ($resptype =~ /^(option|match|rank)$/) {
                    185:         my $prefix = '_';
                    186:         if ($purpose eq 'display') {
                    187:             $prefix = '';
                    188:         }
                    189:         my @foils = 
                    190:             map { 
                    191:                 $prefix.$_; 
                    192:             } sort(keys(%{$analysis->{$partid.'.'.$respid}->{'_Foils'}}));
                    193:         if (scalar(@basic_headers) && $basic_headers[0] eq 'Correct') {
                    194:             @foils = map { ($_ , $_.' Correct') } @foils;
                    195:             shift(@basic_headers);  # Get rid of 'Correct'
                    196:         }
                    197:         @headers = (@foils,@basic_headers);
1.42      albertel  198:     } elsif (lc($resptype) eq 'task') {
                    199:         @headers = ('Grader','Status',@basic_headers,'Submission');
1.30      matthew   200:     } else {
                    201:         @headers = ('Submission',@basic_headers);
                    202:     }
                    203:     return @headers;
                    204: }
                    205: 
1.18      matthew   206: #########################################################
                    207: #########################################################
1.21      matthew   208: ##
                    209: ##    HTML Output Routines
                    210: ##
                    211: #########################################################
                    212: #########################################################
                    213: sub prepare_html_output {
                    214:     my ($r,$problems,$students) = @_;
                    215:     my $c = $r->connection();
                    216:     #
                    217:     # Set a flag for the case when there is just one problem
                    218:     my $single_response = 0;
                    219:     if (scalar(@$problems) == 1 &&
                    220:         $problems->[0]->countResponses == 1) {
                    221:         $single_response = 1;
                    222:     }
                    223:     #
                    224:     # Compute the number of columns per response
1.31      matthew   225:     my @extra_resp_headers = &get_extra_response_headers();
1.21      matthew   226:     #
                    227:     # Create the table header
1.38      matthew   228:     my @student_columns = ('username','domain','id','section');
1.21      matthew   229:     #
                    230:     my %headers;
                    231:     my $student_column_count = scalar(@student_columns);
                    232:     $headers{'problem'} = qq{<th colspan="$student_column_count">\&nbsp;</th>};
                    233:     foreach (@student_columns) {
                    234:         $headers{'student'}.= '<th>'.ucfirst($_).'</th>';
                    235:     }
                    236:     #
                    237:     # we put the headers into the %headers hash
                    238:     my $total_col = scalar(@student_columns);
                    239:     my $nonempty_part_headers = 0;
1.30      matthew   240:     #
                    241:     my %problem_analysis;
1.21      matthew   242:     foreach my $prob (@$problems) {
1.31      matthew   243:         my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
1.30      matthew   244:         $problem_analysis{$prob->src}=\%analysis;
                    245:         #
1.21      matthew   246:         my $prob_span = 0;
                    247:         my $single_part = 0;
                    248:         if (scalar(@{$prob->parts}) == 1) {
                    249:             $single_part = 1;
                    250:         }
                    251:         foreach my $partid (@{$prob->parts}) {
                    252:             my $part_span = 0;
                    253:             my $responses = [$prob->responseIds($partid)];
                    254:             my $resptypes = [$prob->responseType($partid)];
                    255:             for (my $i=0;$i<scalar(@$responses);$i++) {
1.30      matthew   256:                 my $respid = $responses->[$i];
                    257:                 my @headers = &get_headers($prob,$partid,$respid,
                    258:                                            $resptypes->[$i],
                    259:                                            $problem_analysis{$prob->src},
                    260:                                            'html','display',
                    261:                                            @extra_resp_headers);
                    262:                 if (scalar(@headers)>0) {
                    263:                     $total_col += scalar(@headers);
                    264:                     $part_span += scalar(@headers);
1.21      matthew   265:                     $headers{'response'} .=
1.30      matthew   266:                         '<th colspan="'.scalar(@headers).'">'.
1.21      matthew   267:                         &mt('Response [_1]',$responses->[$i]).'</th>';
1.52      raeburn   268:                     $headers{'student'}.= '<th><span class="LC_nobreak">'.
                    269:                                           join('</span></th><th><span class="LC_nobreak">',
1.30      matthew   270:                                                       @headers).
1.51      bisitz    271:                                                           '</span></th>';
1.21      matthew   272:                 }
                    273:             }
1.41      matthew   274:             if ($part_span == 0) {
                    275:                 next;
                    276:             }
1.21      matthew   277:             if (! $single_part) {
                    278:                 my $tmpname = $partid;
                    279:                 if ($partid =~/^\d+$/) {
1.24      matthew   280:                     $tmpname = $prob->part_display($partid);
1.21      matthew   281:                 }
1.35      matthew   282:                 if ($tmpname !~ /^part/) {
                    283:                     $tmpname = 'Part '.$tmpname;
                    284:                 }
1.21      matthew   285:                 $headers{'part'} .= qq{<th colspan="$part_span">$tmpname</th>};
                    286:                 $nonempty_part_headers = 1;
                    287:             } else {
1.53      bisitz    288:                 $headers{'part'} .= qq{<th colspan="$part_span">&nbsp;</th>};
1.21      matthew   289:             }
                    290:             $prob_span += $part_span;
                    291:         }
1.23      matthew   292:         my $title = $prob->compTitle;
1.21      matthew   293:         if ($prob_span > 0) {
                    294:             $headers{'problem'}.= qq{<th colspan="$prob_span">$title</th>};
                    295:         } elsif ($single_response) {
                    296:             $prob_span = scalar(@student_columns);
                    297:             $headers{'problem'} = qq{<th colspan="$prob_span">$title</th>};
                    298:         }
                    299:     }
                    300:     if (exists($headers{'part'})) {
                    301:         $headers{'part'} = qq{<th colspan="$student_column_count">\&nbsp;</th>}.
                    302:             $headers{'part'};
                    303:     }
                    304:     if (exists($headers{'response'})) {
                    305:         $headers{'response'}=
                    306:             qq{<th colspan="$student_column_count">\&nbsp;</th>}.
                    307:             $headers{'response'};
                    308:     }
                    309:     my $full_header = $/.'<table>'.$/;
                    310:     $full_header .= '<tr align="left">'.$headers{'problem'}.'</tr>'.$/;
                    311:     if ($nonempty_part_headers) {
                    312:         $full_header .= '<tr align="left">'.$headers{'part'}.'</tr>'.$/;
                    313:     }
                    314:     $full_header .= '<tr align="left">'.$headers{'response'}.'</tr>'.$/;
                    315:     $full_header .= '<tr align="left">'.$headers{'student'}.'</tr>'.$/;
                    316:     #
                    317:     # Main loop
                    318:     my $count;
                    319:     $r->print($/.$full_header.$/);
                    320:     my $row_class = 'odd';   # css 
                    321:     foreach my $student (@$students) {
                    322:         my $student_row_data;
                    323:         if ($count++ >= 30) {
                    324:             $r->print('</table>'.$/.$full_header.$/);
                    325:             $count = 0;
                    326:         }
                    327:         last if ($c->aborted());
                    328:         foreach my $field (@student_columns) {
                    329:             $student_row_data .= 
                    330:                 '<td valign="top">'.$student->{$field}.'</td>';
                    331:         }
                    332:         #
                    333:         # Figure out what it is we need to output for this student
                    334:         my @essays;
1.30      matthew   335:         my %prob_data;
1.21      matthew   336:         my $maxrow;
                    337:         foreach my $prob (@$problems) {
1.30      matthew   338:             $prob_data{$prob->symb}={};
1.21      matthew   339:             foreach my $partid (@{$prob->parts}) {
                    340:                 my @responses = $prob->responseIds($partid);
                    341:                 my @response_type = $prob->responseType($partid);
                    342:                 for (my $i=0;$i<=$#responses;$i++) {
1.30      matthew   343:                     my $respid  = $responses[$i];
1.21      matthew   344:                     my $results = 
                    345:                         &Apache::loncoursedata::get_response_data_by_student
                    346:                         ($student,$prob->symb(),$respid);
1.30      matthew   347:                     my $resptype = $response_type[$i];
                    348:                     my @headers = &get_headers($prob,$partid,$respid,
                    349:                                                $resptype,
                    350:                                                $problem_analysis{$prob->src},
                    351:                                                'html','normal',
                    352:                                                @extra_resp_headers);
                    353:                     my $width = scalar(@headers);
1.41      matthew   354:                     next if ($width < 1);
1.30      matthew   355:                     my $resp_data;
1.41      matthew   356:                     $resp_data->{'fake'} = qq{<td colspan="$width">&nbsp;</td>};
1.21      matthew   357:                     if (! defined($results)) {
                    358:                         $results = [];
                    359:                     }
1.30      matthew   360:                     # 
1.21      matthew   361:                     if (scalar(@$results) > $maxrow && $resptype ne 'essay') {
                    362:                         $maxrow = scalar(@$results);
                    363:                     }
                    364:                     for (my $j=scalar(@$results)-1;$j>=0;$j--) {
1.40      albertel  365:                         if ($env{'form.all_sub'} ne 'true') {
1.21      matthew   366:                             next if ($j ne scalar(@$results)-1);
                    367:                         }
1.30      matthew   368:                         my $response = &hashify_response($results->[$j],
                    369:                                                          $prob,
                    370:                                                          $student,
                    371:                                                          $partid,
                    372:                                                          $respid);
1.21      matthew   373:                         if ($resptype eq 'essay') {
                    374:                             push(@essays,
1.30      matthew   375:                                  &html_essay_results(\@headers,
1.21      matthew   376:                                                      $prob,$partid,$respid,
                    377:                                                      $response,
                    378:                                                      $single_response).
                    379:                                  '</td>');
1.42      albertel  380: 			} elsif (lc($resptype) eq 'task') {
                    381: 			    my $results = 
                    382: 				&html_task_results(\@headers,
                    383: 						   $prob,$partid,$respid,
                    384: 						   $response,$resptype);
                    385: 			    if ($results) {
                    386: 				push(@{$resp_data->{'real'}},$results);
                    387: 			    }
1.21      matthew   388:                         } else {
                    389:                             push(@{$resp_data->{'real'}},
1.30      matthew   390:                                  &html_non_essay_results(\@headers,
                    391:                                                          $prob,$partid,$respid,
                    392:                                                          $response,$resptype));
1.21      matthew   393:                         }
1.30      matthew   394:                     }
                    395:                     $prob_data{$prob->symb}->{$partid}->{$respid}=$resp_data;
1.21      matthew   396:                 } # end of $i loop
                    397:             } # end of partid loop
                    398:         } # end of prob loop
                    399:         #
                    400:         # if there is no data, skip this student.
                    401:         next if (! $maxrow && ! scalar(@essays));
                    402:         #
                    403:         # Go through the problem data and output a row.
                    404:         if ($row_class eq 'even') {
                    405:             $row_class = 'odd'; 
                    406:         } else {
                    407:             $row_class = 'even'; 
                    408:         }
                    409:         my $printed_something;
                    410:         for (my $rows_output = 0;$rows_output<$maxrow;$rows_output++) {
                    411:             my $html;
                    412:             my $no_data = 1;
                    413:             foreach my $prob (@$problems) {
                    414:                 foreach my $partid (@{$prob->parts}) {
                    415:                     my @responses     = $prob->responseIds($partid);
                    416:                     my @response_type = $prob->responseType($partid);
                    417:                     for (my $i=0;$i<=$#responses;$i++) {
                    418:                         my $respid   = $responses[$i];
                    419:                         my $resp_data = 
1.30      matthew   420:                             $prob_data{$prob->symb}->{$partid}->{$respid};
1.21      matthew   421:                         next if ($response_type[$i] eq 'essay');
                    422:                         if (defined($resp_data->{'real'}->[$rows_output])) {
                    423:                             $html .= $resp_data->{'real'}->[$rows_output];
                    424:                             $no_data = 0;
                    425:                         } else {
                    426:                             $html .= $resp_data->{'fake'};
                    427:                         }
                    428:                     }
                    429:                 }
                    430:             }
                    431:             if (! $no_data) {
                    432:                 $r->print(qq{<tr class="$row_class">$student_row_data$html</tr>}.$/);
                    433:                 $printed_something=1;
                    434:             }
                    435:         }
                    436:         if (@essays) {
                    437:             my $tr = qq{<tr class="$row_class">};
                    438:             my $td = qq{<td  valign="top" class="essay" colspan="$total_col">};
                    439:             if (! $printed_something) {
                    440:                 $r->print($tr.$student_row_data.'</tr>'.$/);
                    441:             }
                    442:             $r->print($tr.$td.
                    443:                       join('</td></tr>'.$/.$tr.$td,@essays).'</td></tr>'.$/);
                    444:             undef(@essays);
                    445:         }
                    446:     } # end of student loop
                    447:     return;
                    448: }
                    449: 
1.30      matthew   450: sub hashify_response {
                    451:     my ($response,$prob,$student,$partid,$respid) =@_;
                    452:     my $resp_hash = {};
1.40      albertel  453:     if ($env{'form.correctans'} eq 'true') {
1.30      matthew   454:         $resp_hash->{'Correct'} = 
                    455:             &Apache::lonstathelpers::get_student_answer
                    456:             ($prob,$student->{'username'},$student->{'domain'},
                    457:              $partid,$respid);
                    458:     }
                    459:     $resp_hash->{'Submission'} = 
                    460:         $response->[&Apache::loncoursedata::RDs_submission()];
                    461:     $resp_hash->{'Time'} = 
                    462:         $response->[&Apache::loncoursedata::RDs_timestamp()];
                    463:     $resp_hash->{'Attempt'} =
                    464:         $response->[&Apache::loncoursedata::RDs_tries()];
                    465:     $resp_hash->{'Awarded'} = 
                    466:         $response->[&Apache::loncoursedata::RDs_awarded()];
1.42      albertel  467:     if ($prob->is_task()) {
                    468: 	$resp_hash->{'Grader'} = 
                    469: 	    $response->[&Apache::loncoursedata::RDs_response_eval_2()];
                    470: 	if ($resp_hash->{'Attempt'} eq '0') {
                    471: 	    $resp_hash->{'Attempt'} = '';
                    472: 	}
                    473: 	$resp_hash->{'Award Detail'} = 
                    474: 	    $response->[&Apache::loncoursedata::RDs_part_award()];
                    475: 	$resp_hash->{'Status'} = 
                    476: 	    $response->[&Apache::loncoursedata::RDs_response_eval()];
                    477:     } else {
                    478: 	$resp_hash->{'Award Detail'} = 
                    479: 	    $response->[&Apache::loncoursedata::RDs_awarddetail()];
                    480:     }
                    481: 
1.30      matthew   482:     return $resp_hash;
                    483: }
                    484: 
1.21      matthew   485: #####################################################
                    486: ##
                    487: ##     HTML helper routines
                    488: ##
                    489: #####################################################
                    490: sub html_essay_results {
1.30      matthew   491:     my ($headers,$prob,$partid,$respid,$response,$single_response)=@_;
                    492:     if (! ref($headers) || ref($headers) ne 'ARRAY') {
                    493:         return '';
1.21      matthew   494:     }
1.30      matthew   495:     # Start of telling them what problem, part, and response
1.21      matthew   496:     my $Str;
                    497:     if (! $single_response) {
1.23      matthew   498:         my $id = $prob->compTitle;
1.21      matthew   499:         if (defined($partid) && $partid ne '0') {
1.24      matthew   500:             $id .= ' '.$prob->part_display($partid);
1.21      matthew   501:         }
                    502:         if (defined($respid)) {
                    503:             $id .= ' '.$respid;
                    504:         }
1.51      bisitz    505:         $Str .= '<span class="LC_nobreak">'.$id.'</span>'.('&nbsp;'x4);
1.21      matthew   506:     }
1.30      matthew   507:     #
                    508:     shift(@$headers); # Get rid of the Submission header
                    509:     my $correct = '';
                    510:     if ($headers->[0] eq 'Correct') {
                    511:         $correct = &html_format_essay_sub($response->{'Correct'});
                    512:         shift(@$headers);
                    513:     }
1.51      bisitz    514:     $Str .= '<span class="LC_nobreak">'.
1.30      matthew   515:         join('',
                    516:              map {
                    517:                  ('&nbsp;'x4).&mt($_.': [_1]',$response->{$_});
1.51      bisitz    518:              } @$headers).'</span>';
1.30      matthew   519:     if (@$headers || ! $single_response) {
                    520:         $Str .= '<br />';
1.21      matthew   521:     }
1.30      matthew   522:     $Str .= &html_format_essay_sub($response->{'Submission'});
                    523:     #
1.21      matthew   524:     if (defined($correct) && $correct !~ /^\s*$/) {
                    525:         $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
                    526:     }
                    527:     return $Str;
                    528: }
                    529: 
1.30      matthew   530: sub html_format_essay_sub {
                    531:     my ($submission) = @_;
                    532:     return '' if (! defined($submission) || $submission eq '');
                    533:     $submission = &HTML::Entities::decode($submission);
                    534:     $submission =~ s/\\\"/\"/g;
                    535:     $submission =~ s/\\\'/\'/g;
                    536:     $submission =~ s|\\r\\n|$/|g;
                    537:     $submission = &HTML::Entities::encode($submission,'<>&"');
                    538:     $submission =~ s|$/\s*$/|$/</p><p>$/|g;
                    539:     $submission =~ s|\\||g;
                    540:     $submission = '<p>'.$submission.'</p>';
                    541:     return $submission;
1.21      matthew   542: }
                    543: 
1.42      albertel  544: sub html_task_results {
                    545:     my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
                    546:     if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
                    547:         return '';
                    548:     }
                    549: 
                    550:     my @values;
                    551:     @values = map { $response->{$_}; } @$headers;
                    552: 
                    553:     my $td = '<td valign="top">';
                    554:     my $str = $td.join('</td>'.$td,@values).'</td>';
                    555:     return $str;
                    556: }
                    557: 
1.30      matthew   558: sub html_non_essay_results {
                    559:     my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
                    560:     if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
                    561:         return '';
                    562:     }
                    563:     # 
1.45      www       564:     my $submission = &HTML::Entities::decode(&unescape($response->{'Submission'})); 
1.21      matthew   565:     return '' if (! defined($submission) || $submission eq '');
1.25      matthew   566:     $submission =~ s/\\\"/\"/g;
                    567:     $submission =~ s/\\\'/\'/g;
1.30      matthew   568:     if ($resptype eq 'radiobutton') {
1.25      matthew   569:         $submission = &HTML::Entities::encode($submission,'<>&"');
1.21      matthew   570:         $submission =~ s/=([^=])$//;
1.51      bisitz    571:         $submission = '<span class="LC_nobreak">'.$submission.'</span>';
1.30      matthew   572:     }
                    573:     $response->{'Submission'} = $submission;
                    574:     #
                    575:     my @values;
                    576:     if ($resptype =~ /^(option|match|rank)$/) {
                    577:         my %submission = 
                    578:             map { 
1.45      www       579:                 my ($foil,$value) = split('=',&unescape($_));
1.30      matthew   580:                 ($foil,$value);
                    581:             } split('&',$response->{'Submission'});
                    582:         my %correct;
                    583:         if (exists($response->{'Correct'})) {
                    584:             %correct = 
                    585:                 map { 
1.45      www       586:                     my ($foil,$value)=split('=',&unescape($_));
1.30      matthew   587:                     ($foil,$value);
                    588:                 } split('&',$response->{'Correct'});
                    589:         }
                    590:         #
                    591:         foreach my $original_header (@$headers) {
                    592:             if ($original_header =~ /^_/) {
                    593:                 # '_' denotes a foil column
                    594:                 my ($header) = ($original_header =~ m/^_(.*)$/);
                    595:                 my $option = '';
                    596:                 if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
                    597:                     if (exists($correct{$foil})) {
                    598:                         $option = $correct{$foil};
                    599:                     }
                    600:                 } elsif (exists($submission{$header})) {
                    601:                     $option = $submission{$header};
                    602:                 }
                    603:                 push(@values,&HTML::Entities::encode($option));
1.38      matthew   604:             } elsif ($original_header eq 'Time') {
                    605:                 push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
1.30      matthew   606:             } else {
                    607:                 # A normal column
                    608:                 push(@values,$response->{$original_header});
                    609:             }
                    610:         }
1.25      matthew   611:     } else {
1.30      matthew   612:         @values = map { $response->{$_}; } @$headers;
1.21      matthew   613:     }
1.30      matthew   614:     my $td = '<td valign="top">';
                    615:     my $str = $td.join('</td>'.$td,@values).'</td>';
                    616:     return $str;
1.21      matthew   617: }
1.18      matthew   618: 
1.30      matthew   619: 
1.21      matthew   620: #########################################################
                    621: #########################################################
                    622: ##
                    623: ##    Excel Output Routines
                    624: ##
                    625: #########################################################
                    626: #########################################################
                    627: sub prepare_excel_output {
1.19      matthew   628:     my ($r,$Problems,$Students) = @_;
1.18      matthew   629:     my $c = $r->connection();
                    630:     #
1.19      matthew   631:     #
                    632:     # Determine the number of columns in the spreadsheet
                    633:     my $columncount = 3; # username, domain, id
1.31      matthew   634:     my @extra_resp_headers = &get_extra_response_headers();
1.20      matthew   635:     my $lastprob;
1.31      matthew   636:     my %problem_analysis;
1.19      matthew   637:     foreach my $prob (@$Problems) {
1.31      matthew   638:         my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
                    639:         $problem_analysis{$prob->src}=\%analysis;
                    640:         foreach my $partid (@{$prob->parts}) {
                    641:             my $responses = [$prob->responseIds($partid)];
                    642:             my $resptypes = [$prob->responseType($partid)];
                    643:             for (my $i=0;$i<scalar(@$responses);$i++) {
                    644:                 my @headers = &get_headers($prob,$partid,$responses->[$i],
                    645:                                            $resptypes->[$i],
                    646:                                            $problem_analysis{$prob->src},
                    647:                                            'excel','display',
                    648:                                            @extra_resp_headers);
                    649:                 $columncount += scalar(@headers);
                    650:             }
                    651:         }
1.19      matthew   652:         last if ($columncount > 255);
1.20      matthew   653:         $lastprob = $prob;
1.19      matthew   654:     }
                    655:     if ($columncount > 255) {
                    656:         $r->print('<h1>'.&mt('Unable to complete request').'</h1>'.$/.
                    657:                   '<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>'.$/.
1.20      matthew   658:                   '<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>'.$/.
1.23      matthew   659:                   '<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'</p>');
1.19      matthew   660:         $r->rflush();
                    661:         return;
                    662:     }
                    663:     #
                    664:     # Print out a message telling them what we are doing
1.18      matthew   665:     if (scalar(@$Problems) > 1) {
                    666:         $r->print('<h2>'.
                    667:                   &mt('Preparing Excel spreadsheet of student responses to [_1] problems',
                    668:                       scalar(@$Problems)).
                    669:                   '</h2>');
                    670:     } else {
                    671:         $r->print('<h2>'.
                    672:                   &mt('Preparing Excel spreadsheet of student responses').
                    673:                   '</h2>');
                    674:     }
                    675:     $r->rflush();
                    676:     #
                    677:     # Create the excel spreadsheet
1.36      matthew   678:     my ($workbook,$filename,$format) = 
                    679:         &Apache::loncommon::create_workbook($r);
                    680:     return if (! defined($workbook));
1.18      matthew   681:     my $worksheet  = $workbook->addworksheet('Student Submission Data');
                    682:     #
                    683:     # Add headers to the worksheet
                    684:     my $rows_output = 0;
                    685:     $worksheet->write($rows_output++,0,
1.40      albertel  686:                     $env{'course.'.$env{'request.course.id'}.'.description'},
1.18      matthew   687:                       $format->{'h1'});
                    688:     $rows_output++;
                    689:     my $cols_output = 0;
                    690:     my $title_row  = $rows_output++;
                    691:     my $partid_row = $rows_output++;
                    692:     my $respid_row = $rows_output++;
                    693:     my $header_row = $rows_output++;
                    694:     $worksheet->write($title_row ,0,'Problem Title',$format->{'bold'});
                    695:     $worksheet->write($partid_row,0,'Part ID',$format->{'bold'});
                    696:     $worksheet->write($respid_row,0,'Response ID',$format->{'bold'});
                    697:     # Student headers
1.38      matthew   698:     my @StudentColumns = ('username','domain','id','section');
1.18      matthew   699:     foreach (@StudentColumns) {
1.19      matthew   700:         $worksheet->write($header_row,$cols_output++,ucfirst($_),
                    701:                           $format->{'bold'});
1.18      matthew   702:     }
                    703:     # Problem headers
1.31      matthew   704:     my %start_col;
1.18      matthew   705:     foreach my $prob (@$Problems) {
1.23      matthew   706:         my $title = $prob->compTitle;
1.18      matthew   707:         $worksheet->write($title_row,$cols_output,
                    708:                           $title,$format->{'h3'});
                    709:         foreach my $partid (@{$prob->parts}) {
1.24      matthew   710:             $worksheet->write($partid_row,$cols_output,
                    711:                               $prob->part_display($partid));
1.18      matthew   712:             my $responses = [$prob->responseIds($partid)];
                    713:             my $resptypes = [$prob->responseType($partid)];
                    714:             for (my $i=0;$i<scalar(@$responses);$i++) {
1.31      matthew   715:                 $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
                    716:                     $cols_output;
1.18      matthew   717:                 $worksheet->write($respid_row,$cols_output,
                    718:                                   $resptypes->[$i].', '.$responses->[$i]);
1.31      matthew   719:                 my @headers = &get_headers($prob,$partid,$responses->[$i],
                    720:                                            $resptypes->[$i],
                    721:                                            $problem_analysis{$prob->src},
                    722:                                            'excel','display',
                    723:                                            @extra_resp_headers);
                    724:                 foreach my $text (@headers) {
                    725:                     if ($text eq 'Time') {
                    726:                         $worksheet->set_column($cols_output,$cols_output,undef,
                    727:                                                $format->{'date'});
                    728:                     } 
                    729:                     $worksheet->write($header_row,$cols_output++,$text);
1.20      matthew   730:                 }
1.18      matthew   731:             }
                    732:         }
                    733:     }
                    734:     #
                    735:     # Populate the worksheet with the student data
                    736:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                    737:         ($r,'Excel File Compilation Status',
                    738:          'Excel File Compilation Progress', 
                    739:          scalar(@$Students),'inline',undef,'Statistics','stats_status');
1.20      matthew   740:     my $max_row = $rows_output;
1.18      matthew   741:     foreach my $student (@$Students) {
                    742:         last if ($c->aborted());
                    743:         $cols_output = 0;
1.20      matthew   744:         my $student_row = $max_row;
1.18      matthew   745:         foreach my $field (@StudentColumns) {
1.20      matthew   746:             $worksheet->write($student_row,$cols_output++,
1.18      matthew   747:                               $student->{$field});
                    748:         }
1.20      matthew   749:         my $last_student_col = $cols_output-1;
1.18      matthew   750:         foreach my $prob (@$Problems) {
                    751:             foreach my $partid (@{$prob->parts}) {
                    752:                 my @Response = $prob->responseIds($partid);
                    753:                 my @ResponseType = $prob->responseType($partid);
                    754:                 for (my $i=0;$i<=$#Response;$i++) {
                    755:                     my $respid   = $Response[$i];
                    756:                     my $resptype = $ResponseType[$i];
                    757:                     my $results = 
                    758:                         &Apache::loncoursedata::get_response_data_by_student
                    759:                         ($student,$prob->symb(),$respid);
1.31      matthew   760:                     my @headers = &get_headers($prob,$partid,$respid,
                    761:                                                $resptype,
                    762:                                                $problem_analysis{$prob->src},
                    763:                                                'excel','normal',
                    764:                                                @extra_resp_headers);
                    765: 
1.20      matthew   766:                     if (! defined($results)) {
                    767:                         $results = [];
                    768:                     }
                    769:                     #
                    770:                     $rows_output = $student_row;
                    771:                     #
1.31      matthew   772:                     my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
1.20      matthew   773:                     for (my $j=scalar(@$results)-1;$j>=0;$j--) {
                    774:                         $cols_output = $response_start_col;
1.40      albertel  775:                         if ($env{'form.all_sub'} ne 'true') {
1.20      matthew   776:                             next if ($j ne scalar(@$results)-1);
                    777:                         }
1.31      matthew   778:                         my $response = &hashify_response($results->[$j],
                    779:                                                          $prob,
                    780:                                                          $student,
                    781:                                                          $partid,
                    782:                                                          $respid);
                    783:                         my @response_data = 
1.33      matthew   784:                             &compile_response_data(\@headers,$response,
                    785:                                                    $prob,$partid,$respid,
                    786:                                                    $resptype,
                    787:                                                    \&excel_format_item);
1.31      matthew   788:                         $worksheet->write_row($rows_output++,$cols_output,
                    789:                                               \@response_data);
                    790:                         $cols_output+=scalar(@response_data);
1.20      matthew   791:                         if ($rows_output > $max_row) {
                    792:                             $max_row = $rows_output;
                    793:                         }
1.19      matthew   794:                     }
1.18      matthew   795:                 }
                    796:             }
                    797:         }
1.28      matthew   798:         # Fill in the remaining rows with the students data
1.34      matthew   799:         for (my $row = $student_row+1;$row<$max_row;$row++) {
1.28      matthew   800:             my $cols = 0;
                    801:             foreach my $field (@StudentColumns) {
                    802:                 $worksheet->write($row,$cols++,
                    803:                                   $student->{$field});
                    804:             }
                    805:         }
1.18      matthew   806:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                    807:                                                  'last student');
                    808:     }
                    809:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    810:     #
                    811:     # Close the excel file
                    812:     $workbook->close();
                    813:     #
                    814:     # Write a link to allow them to download it
                    815:     $r->print('<p><a href="'.$filename.'">'.
                    816:               &mt('Your Excel spreadsheet.').
                    817:               '</a></p>'."\n");
                    818:     $r->print('<script>'.
                    819:               'window.document.Statistics.stats_status.value="'.
                    820:               'Done compiling spreadsheet.  See link below to download.'.
                    821:               '";</script>');
                    822:     $r->rflush();
                    823:     return;
                    824: }
                    825: 
1.33      matthew   826: sub compile_response_data {
                    827:     my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
1.31      matthew   828:     if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
                    829:         return ();
                    830:     }
1.33      matthew   831:     if (ref($format) ne 'CODE') {
                    832:         $format = sub { return $_[0]; };
                    833:     }
1.31      matthew   834:     #
1.33      matthew   835:     my $submission = 
                    836:         &HTML::Entities::decode
1.45      www       837:         (&unescape($response->{'Submission'}));
1.42      albertel  838:     if (!$prob->is_task()) {
                    839: 	return () if (! defined($submission) || $submission eq '');
                    840:     }
1.31      matthew   841:     $submission =~ s/\\\"/\"/g;
                    842:     $submission =~ s/\\\'/\'/g;
                    843:     if ($resptype eq 'radiobutton') {
                    844:         $submission =~ s/=([^=])$//;
1.20      matthew   845:     }
1.31      matthew   846:     $response->{'Submission'} = $submission;
                    847:     #
                    848:     my @values;
                    849:     if ($resptype =~ /^(option|match|rank)$/) {
                    850:         my %submission = 
                    851:             map { 
1.45      www       852:                 my ($foil,$value) = split('=',&unescape($_));
1.31      matthew   853:                 ($foil,$value);
                    854:             } split('&',$response->{'Submission'});
                    855:         my %correct;
                    856:         if (exists($response->{'Correct'})) {
                    857:             %correct = 
                    858:                 map { 
1.45      www       859:                     my ($foil,$value)=split('=',&unescape($_));
1.31      matthew   860:                     ($foil,$value);
                    861:                 } split('&',$response->{'Correct'});
                    862:         }
                    863:         #
                    864:         foreach my $original_header (@$headers) {
                    865:             if ($original_header =~ /^_/) {
                    866:                 # '_' denotes a foil column
                    867:                 my ($header) = ($original_header =~ m/^_(.*)$/);
                    868:                 my $option = '';
                    869:                 if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
                    870:                     if (exists($correct{$foil})) {
                    871:                         $option = $correct{$foil};
                    872:                     }
                    873:                 } elsif (exists($submission{$header})) {
                    874:                     $option = $submission{$header};
                    875:                 }
1.33      matthew   876:                 push(@values,&{$format}($option,$header));
1.31      matthew   877:             } else {
                    878:                 # A normal column
1.33      matthew   879:                 push(@values,&{$format}($response->{$original_header},
1.31      matthew   880:                                         $original_header));
                    881:             }
                    882:         }
                    883:     } else {
1.33      matthew   884:         @values = map { &{$format}($response->{$_},$_); } @$headers;
1.20      matthew   885:     }
1.31      matthew   886:     return @values;
1.20      matthew   887: }
                    888: 
1.31      matthew   889: sub excel_format_item {
                    890:     my ($item,$type) = @_;
                    891:     if ($type eq 'Time') {
1.33      matthew   892:         $item = &Apache::lonstathelpers::calc_serial($item);
1.31      matthew   893:     } else {
                    894:         if ($item =~ m/^=/) {
                    895:             $item = ' '.$item;
                    896:         }
                    897:         $item =~ s/\\r//g;
                    898:         $item =~ s/\\n/\n/g;
                    899:         $item =~ s/(\s*$|^\s*)//g;
                    900:         $item =~ s/\\\'/\'/g;
1.18      matthew   901:     }
1.31      matthew   902:     return $item;
1.1       matthew   903: }
                    904: 
                    905: #########################################################
                    906: #########################################################
1.17      matthew   907: ##
                    908: ##      CSV output of student answers
                    909: ##
                    910: #########################################################
                    911: #########################################################
                    912: sub prepare_csv_output {
1.22      matthew   913:     my ($r,$problems,$students) = @_;
1.17      matthew   914:     my $c = $r->connection();
                    915:     #
                    916:     $r->print('<h2>'.
                    917:               &mt('Generating CSV report of student responses').'</h2>');
                    918:     #
                    919:     # Progress window
                    920:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                    921:         ($r,'CSV File Compilation Status',
                    922:          'CSV File Compilation Progress', 
1.22      matthew   923:          scalar(@$students),'inline',undef,'Statistics','stats_status');
                    924:     
1.17      matthew   925:     $r->rflush();
                    926:     #
                    927:     # Open a file
                    928:     my $outputfile;
                    929:     my $filename = '/prtspool/'.
1.40      albertel  930:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.17      matthew   931:             time.'_'.rand(1000000000).'.csv';
                    932:     unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
                    933:         $r->log_error("Couldn't open $filename for output $!");
1.55      bisitz    934:         $r->print(
                    935:             '<p class="LC_error">'
                    936:            .&mt('Problems occurred in writing the CSV file.')
                    937:            .' '.&mt('This error has been logged.')
                    938:            .' '.&mt('Please alert your LON-CAPA administrator.')
                    939:            .'</p>'
                    940:         );
1.17      matthew   941:         $outputfile = undef;
                    942:     }
                    943:     #
1.22      matthew   944:     # Compute the number of columns per response
1.32      matthew   945:     my @extra_resp_headers = &get_extra_response_headers();
1.22      matthew   946:     #
                    947:     # Create the table header
1.38      matthew   948:     my @student_columns = ('username','domain','id','section');
1.17      matthew   949:     #
1.22      matthew   950:     my %headers;
                    951:     push(@{$headers{'student'}},@student_columns);
                    952:     # Pad for the student data
                    953:     foreach my $row ('problem','part','response') {
1.32      matthew   954:         $headers{$row}=[map {''} @student_columns];
1.22      matthew   955:     }
                    956:     #
                    957:     # we put the headers into the %headers hash
1.32      matthew   958:     my %problem_analysis;
                    959:     my %start_col;
                    960:     my $max_column = scalar(@student_columns);
1.22      matthew   961:     foreach my $prob (@$problems) {
1.32      matthew   962:         my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
                    963:         $problem_analysis{$prob->src}=\%analysis;
                    964:         $headers{'problem'}->[$max_column] = $prob->compTitle;
1.22      matthew   965:         foreach my $partid (@{$prob->parts}) {
1.32      matthew   966:             $headers{'part'}->[$max_column] = $prob->part_display($partid);
1.22      matthew   967:             my $responses = [$prob->responseIds($partid)];
1.32      matthew   968:             my $resptypes = [$prob->responseType($partid)];
1.22      matthew   969:             for (my $i=0;$i<scalar(@$responses);$i++) {
1.32      matthew   970:                 my @headers = &get_headers($prob,$partid,$responses->[$i],
                    971:                                            $resptypes->[$i],
                    972:                                            $problem_analysis{$prob->src},
                    973:                                            'csv','display',
                    974:                                            @extra_resp_headers);
                    975:                 $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
                    976:                     $max_column;
                    977:                 $headers{'response'}->[$max_column]=
1.22      matthew   978:                     &mt('Response [_1]',$responses->[$i]);
1.32      matthew   979:                 for (my $j=0;$j<=$#headers;$j++) {
                    980:                     $headers{'student'}->[$max_column+$j]=$headers[$j];
1.22      matthew   981:                 }
1.32      matthew   982:                 $max_column += scalar(@headers);
1.17      matthew   983:             }
                    984:         }
                    985:     }
1.22      matthew   986:     foreach my $row ('problem','part','response','student') {
1.32      matthew   987:         print $outputfile '"'.
1.22      matthew   988:             join('","',
                    989:                  map { 
                    990:                      &Apache::loncommon::csv_translate($_); 
                    991:                  } @{$headers{$row}}).'"'.$/;
1.17      matthew   992:     }
                    993:     #
1.22      matthew   994:     # Main loop
                    995:     foreach my $student (@$students) {
1.27      matthew   996:         last if ($c->aborted());
1.22      matthew   997:         my @rows;
                    998:         foreach my $prob (@$problems) {
                    999:             foreach my $partid (@{$prob->parts}) {
                   1000:                 my @responses = $prob->responseIds($partid);
                   1001:                 my @response_type = $prob->responseType($partid);
                   1002:                 for (my $i=0;$i<=$#responses;$i++) {
                   1003:                     my $respid   = $responses[$i];
1.32      matthew  1004:                     my $resptype = $response_type[$i];
                   1005:                     my @headers = &get_headers($prob,$partid,$respid,$resptype,
                   1006:                                                $problem_analysis{$prob->src},
                   1007:                                                'csv','normal',
                   1008:                                                @extra_resp_headers);
1.22      matthew  1009:                     my $results = 
                   1010:                         &Apache::loncoursedata::get_response_data_by_student
                   1011:                         ($student,$prob->symb(),$respid);
                   1012:                     if (! defined($results)) {
                   1013:                         $results = [];
                   1014:                     }
                   1015:                     for (my $j=0; $j<scalar(@$results);$j++) {
1.40      albertel 1016:                         if ($env{'form.all_sub'} ne 'true') {
1.22      matthew  1017:                             next if ($j != 0);
                   1018:                         }
                   1019:                         my $idx = scalar(@$results) - $j - 1;
1.32      matthew  1020:                         my $response = &hashify_response($results->[$idx],
                   1021:                                                          $prob,$student,
                   1022:                                                          $partid,$respid);
                   1023:                         my @data = &compile_response_data(\@headers,$response,
1.22      matthew  1024:                                                           $prob,$partid,
1.33      matthew  1025:                                                           $respid,$resptype,
                   1026:                                                           \&csv_format_item);
1.32      matthew  1027:                         my $resp_start_idx =
                   1028:                             $start_col{$prob->symb}->{$partid}->{$respid};
1.22      matthew  1029:                         for (my $k=0;$k<=$#data;$k++) {
1.32      matthew  1030:                             $rows[$j]->[$resp_start_idx + $k] = $data[$k];
1.22      matthew  1031:                         }
                   1032:                     }
                   1033:                 }
1.17      matthew  1034:             }
1.22      matthew  1035:         }
                   1036:         foreach my $row (@rows) {
                   1037:             print $outputfile '"'.join('","',
                   1038:                                        map { $student->{$_}; }
                   1039:                                        @student_columns).'"';
1.32      matthew  1040:             for (my $i=scalar(@student_columns);$i<$max_column;$i++) {
1.22      matthew  1041:                 my $value = &Apache::loncommon::csv_translate($row->[$i]);
                   1042:                 $value ||='';
                   1043:                 print $outputfile ',"'.$value.'"';
1.17      matthew  1044:             }
1.22      matthew  1045:             print $outputfile $/;
1.17      matthew  1046:         }
1.22      matthew  1047:         undef(@rows);
1.17      matthew  1048:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                   1049:                                                  'last student');
                   1050:     }
                   1051:     close($outputfile);
                   1052:     #
                   1053:     # Close the progress window
                   1054:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                   1055:     #
1.48      bisitz   1056:     # Tell the user where to get their CSV file
1.17      matthew  1057:     $r->print('<br />'.
1.48      bisitz   1058:               '<a href="'.$filename.'">'.&mt('Your CSV file.').'</a>'."\n");
1.17      matthew  1059:     $r->rflush();
                   1060:     return;
                   1061: }
                   1062: 
1.32      matthew  1063: sub csv_format_item {
                   1064:     my ($item,$type) = @_;
                   1065:     if ($type eq 'Time') {
                   1066:         $item = localtime($item);
1.33      matthew  1067:     }
1.32      matthew  1068:     $item =&Apache::loncommon::csv_translate($item); 
                   1069:     return $item;
1.15      matthew  1070: }
                   1071: 
1.1       matthew  1072: #########################################################
                   1073: #########################################################
                   1074: ##
                   1075: ##   Generic Interface Routines
                   1076: ##
                   1077: #########################################################
                   1078: #########################################################
                   1079: sub CreateInterface {
                   1080:     ##
1.16      matthew  1081:     ## Output Selection
1.19      matthew  1082:     my $output_selector = $/.'<select name="output">'.$/;
1.22      matthew  1083:     foreach ('HTML','Excel','CSV') {
1.19      matthew  1084:         $output_selector .= '    <option value="'.lc($_).'"';
1.40      albertel 1085:         if ($env{'form.output'} eq lc($_)) {
1.19      matthew  1086:             $output_selector .= ' selected ';
1.16      matthew  1087:         }
1.19      matthew  1088:         $output_selector .='>'.&mt($_).'</option>'.$/;
1.16      matthew  1089:     } 
1.19      matthew  1090:     $output_selector .= '</select>'.$/;
1.16      matthew  1091:     ##
1.1       matthew  1092:     ## Environment variable initialization
                   1093:     my $Str = '';
1.43      albertel 1094:     $Str .= &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports');
1.11      matthew  1095:     $Str .= '<p>';
1.50      bisitz   1096:     $Str .= &Apache::loncommon::start_data_table();
                   1097:     $Str .= &Apache::loncommon::start_data_table_header_row();
1.16      matthew  1098:     $Str .= '<th>'.&mt('Sections').'</th>';
1.44      raeburn  1099:     $Str .= '<th>'.&mt('Groups').'</th>';
1.46      raeburn  1100:     $Str .= '<th>'.&mt('Access Status').'</th>';
1.50      bisitz   1101:     $Str .= '<th>'.&mt('Options').'</th>';
                   1102:     $Str .= '<th>'.&mt('Output Format').'</th>';
                   1103:     $Str .= &Apache::loncommon::end_data_table_header_row();
1.11      matthew  1104:     #
1.50      bisitz   1105:     $Str .= &Apache::loncommon::start_data_table_row();
                   1106:     $Str .= '<td align="center">'."\n";
1.1       matthew  1107:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
                   1108:     $Str .= '</td>';
                   1109:     #
1.44      raeburn  1110:     $Str .= '<td align="center">'."\n";
                   1111:     $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
                   1112:     $Str .= '</td>';
                   1113:     #
1.1       matthew  1114:     $Str .= '<td align="center">';
                   1115:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
                   1116:     $Str .= '</td>';
1.6       matthew  1117:     #
1.15      matthew  1118:     # Render problem checkbox
                   1119:     my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
1.40      albertel 1120:     if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
1.50      bisitz   1121:         $prob_checkbox .= 'checked="checked" ';
1.15      matthew  1122:     }
                   1123:     $prob_checkbox .= 'value="true" />';
                   1124:     #
                   1125:     # Compute correct answers checkbox
                   1126:     my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1.40      albertel 1127:     if (exists($env{'form.correctans'}) && $env{'form.correctans'} eq 'true') {
1.50      bisitz   1128:         $ans_checkbox .= 'checked="checked" ';
1.13      matthew  1129:     }
1.15      matthew  1130:     $ans_checkbox .= 'value="true" />';
                   1131:     #
1.19      matthew  1132:     # Show all submissions checkbox
                   1133:     my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
1.40      albertel 1134:     if (exists($env{'form.all_sub'}) && 
                   1135:         $env{'form.all_sub'} eq 'true') {
1.50      bisitz   1136:         $all_sub_checkbox .= 'checked="checked" ';
1.15      matthew  1137:     }
1.19      matthew  1138:     $all_sub_checkbox.= 'value="true" />';
1.15      matthew  1139:     #
1.20      matthew  1140:     # problem status checkbox
                   1141:     my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
1.40      albertel 1142:     if (exists($env{'form.prob_status'}) && 
                   1143:         $env{'form.prob_status'} eq 'true') {
1.50      bisitz   1144:         $prob_status_checkbox .= 'checked="checked" ';
1.15      matthew  1145:     }
1.20      matthew  1146:     $prob_status_checkbox .= 'value="true" />';
1.15      matthew  1147:     #
1.56    ! bisitz   1148:     $Str .=
        !          1149:         '<td valign="top">'
        !          1150:        .'<label>'
        !          1151:        .$prob_checkbox.&mt('Show problem')
        !          1152:        .'</label><br />'
        !          1153:        .'<label>'
        !          1154:        .' '.$ans_checkbox.&mt('Show correct answers')
        !          1155:        .'</label><br />'
        !          1156:        .'<label>'
        !          1157:        .$all_sub_checkbox.&mt('Show all submissions')
        !          1158:        .'</label><br />'
        !          1159:        .'<label>'
        !          1160:        .$prob_status_checkbox.&mt('Show problem grading')
        !          1161:        .'</label>'
        !          1162:        .'</td>';
1.13      matthew  1163:     #
1.50      bisitz   1164:     $Str .= '<td align="center" valign="top">'.$output_selector.'</td>';
                   1165:     #
                   1166:     $Str .= &Apache::loncommon::end_data_table_row();
                   1167:     $Str .= &Apache::loncommon::end_data_table();
1.1       matthew  1168:     #
1.49      bisitz   1169:     $Str .= '<p><span class="LC_nobreak">'
                   1170:            .&mt('Status: [_1]',
                   1171:                     '<input type="text" name="stats_status"'
                   1172:                    .' size="60" value="" readonly="readonly" />')
                   1173:            .'</span></p>';
1.52      raeburn  1174:     $Str .= '</p>';
1.11      matthew  1175:     ##
1.1       matthew  1176:     return $Str;
                   1177: }
                   1178: 
                   1179: 1;
                   1180: 
                   1181: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>