File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.35: download - view: text, annotated - select for diffs
Wed Feb 23 19:41:27 2005 UTC (19 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Add 'Part ' to the display of part names/ids.

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

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