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

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

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