File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.34: download - view: text, annotated - select for diffs
Wed Feb 23 01:21:46 2005 UTC (19 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fixed bug with excel output where an extra blank row was output for each
student.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentsubmissions.pm,v 1.34 2005/02/23 01:21:46 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:                 $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:         }
  277:         my $title = $prob->compTitle;
  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;
  320:         my %prob_data;
  321:         my $maxrow;
  322:         foreach my $prob (@$problems) {
  323:             $prob_data{$prob->symb}={};
  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++) {
  328:                     my $respid  = $responses[$i];
  329:                     my $results = 
  330:                         &Apache::loncoursedata::get_response_data_by_student
  331:                         ($student,$prob->symb(),$respid);
  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>};
  341:                     if (! defined($results)) {
  342:                         $results = [];
  343:                     }
  344:                     # 
  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:                         }
  352:                         my $response = &hashify_response($results->[$j],
  353:                                                          $prob,
  354:                                                          $student,
  355:                                                          $partid,
  356:                                                          $respid);
  357:                         if ($resptype eq 'essay') {
  358:                             push(@essays,
  359:                                  &html_essay_results(\@headers,
  360:                                                      $prob,$partid,$respid,
  361:                                                      $response,
  362:                                                      $single_response).
  363:                                  '</td>');
  364:                         } else {
  365:                             push(@{$resp_data->{'real'}},
  366:                                  &html_non_essay_results(\@headers,
  367:                                                          $prob,$partid,$respid,
  368:                                                          $response,$resptype));
  369:                         }
  370:                     }
  371:                     $prob_data{$prob->symb}->{$partid}->{$respid}=$resp_data;
  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 = 
  396:                             $prob_data{$prob->symb}->{$partid}->{$respid};
  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: 
  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: 
  448: #####################################################
  449: ##
  450: ##     HTML helper routines
  451: ##
  452: #####################################################
  453: sub html_essay_results {
  454:     my ($headers,$prob,$partid,$respid,$response,$single_response)=@_;
  455:     if (! ref($headers) || ref($headers) ne 'ARRAY') {
  456:         return '';
  457:     }
  458:     # Start of telling them what problem, part, and response
  459:     my $Str;
  460:     if (! $single_response) {
  461:         my $id = $prob->compTitle;
  462:         if (defined($partid) && $partid ne '0') {
  463:             $id .= ' '.$prob->part_display($partid);
  464:         }
  465:         if (defined($respid)) {
  466:             $id .= ' '.$respid;
  467:         }
  468:         $Str .= '<nobr>'.$id.'</nobr>'.('&nbsp;'x4);
  469:     }
  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 />';
  484:     }
  485:     $Str .= &html_format_essay_sub($response->{'Submission'});
  486:     #
  487:     if (defined($correct) && $correct !~ /^\s*$/) {
  488:         $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
  489:     }
  490:     return $Str;
  491: }
  492: 
  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;
  505: }
  506: 
  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'});
  514:     return '' if (! defined($submission) || $submission eq '');
  515:     $submission =~ s/\\\"/\"/g;
  516:     $submission =~ s/\\\'/\'/g;
  517:     if ($resptype eq 'radiobutton') {
  518:         $submission = &HTML::Entities::encode($submission,'<>&"');
  519:         $submission =~ s/=([^=])$//;
  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:         }
  558:     } else {
  559:         @values = map { $response->{$_}; } @$headers;
  560:     }
  561:     my $td = '<td valign="top">';
  562:     my $str = $td.join('</td>'.$td,@values).'</td>';
  563:     return $str;
  564: }
  565: 
  566: 
  567: #########################################################
  568: #########################################################
  569: ##
  570: ##    Excel Output Routines
  571: ##
  572: #########################################################
  573: #########################################################
  574: sub prepare_excel_output {
  575:     my ($r,$Problems,$Students) = @_;
  576:     my $c = $r->connection();
  577:     #
  578:     #
  579:     # Determine the number of columns in the spreadsheet
  580:     my $columncount = 3; # username, domain, id
  581:     my @extra_resp_headers = &get_extra_response_headers();
  582:     my $lastprob;
  583:     my %problem_analysis;
  584:     foreach my $prob (@$Problems) {
  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:         }
  599:         last if ($columncount > 255);
  600:         $lastprob = $prob;
  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>'.$/.
  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>'.$/.
  606:                   '<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'</p>');
  607:         $r->rflush();
  608:         return;
  609:     }
  610:     #
  611:     # Print out a message telling them what we are doing
  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) {
  660:         $worksheet->write($header_row,$cols_output++,ucfirst($_),
  661:                           $format->{'bold'});
  662:     }
  663:     # Problem headers
  664:     my %start_col;
  665:     foreach my $prob (@$Problems) {
  666:         my $title = $prob->compTitle;
  667:         $worksheet->write($title_row,$cols_output,
  668:                           $title,$format->{'h3'});
  669:         foreach my $partid (@{$prob->parts}) {
  670:             $worksheet->write($partid_row,$cols_output,
  671:                               $prob->part_display($partid));
  672:             my $responses = [$prob->responseIds($partid)];
  673:             my $resptypes = [$prob->responseType($partid)];
  674:             for (my $i=0;$i<scalar(@$responses);$i++) {
  675:                 $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
  676:                     $cols_output;
  677:                 $worksheet->write($respid_row,$cols_output,
  678:                                   $resptypes->[$i].', '.$responses->[$i]);
  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);
  690:                 }
  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');
  700:     my $max_row = $rows_output;
  701:     foreach my $student (@$Students) {
  702:         last if ($c->aborted());
  703:         $cols_output = 0;
  704:         my $student_row = $max_row;
  705:         foreach my $field (@StudentColumns) {
  706:             $worksheet->write($student_row,$cols_output++,
  707:                               $student->{$field});
  708:         }
  709:         my $last_student_col = $cols_output-1;
  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);
  720:                     my @headers = &get_headers($prob,$partid,$respid,
  721:                                                $resptype,
  722:                                                $problem_analysis{$prob->src},
  723:                                                'excel','normal',
  724:                                                @extra_resp_headers);
  725: 
  726:                     if (! defined($results)) {
  727:                         $results = [];
  728:                     }
  729:                     #
  730:                     $rows_output = $student_row;
  731:                     #
  732:                     my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
  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:                         }
  738:                         my $response = &hashify_response($results->[$j],
  739:                                                          $prob,
  740:                                                          $student,
  741:                                                          $partid,
  742:                                                          $respid);
  743:                         my @response_data = 
  744:                             &compile_response_data(\@headers,$response,
  745:                                                    $prob,$partid,$respid,
  746:                                                    $resptype,
  747:                                                    \&excel_format_item);
  748:                         $worksheet->write_row($rows_output++,$cols_output,
  749:                                               \@response_data);
  750:                         $cols_output+=scalar(@response_data);
  751:                         if ($rows_output > $max_row) {
  752:                             $max_row = $rows_output;
  753:                         }
  754:                     }
  755:                 }
  756:             }
  757:         }
  758:         # Fill in the remaining rows with the students data
  759:         for (my $row = $student_row+1;$row<$max_row;$row++) {
  760:             my $cols = 0;
  761:             foreach my $field (@StudentColumns) {
  762:                 $worksheet->write($row,$cols++,
  763:                                   $student->{$field});
  764:             }
  765:         }
  766:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  767:                                                  'last student');
  768:     }
  769:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  770:     #
  771:     # Close the excel file
  772:     $workbook->close();
  773:     #
  774:     # Write a link to allow them to download it
  775:     $r->print('<p><a href="'.$filename.'">'.
  776:               &mt('Your Excel spreadsheet.').
  777:               '</a></p>'."\n");
  778:     $r->print('<script>'.
  779:               'window.document.Statistics.stats_status.value="'.
  780:               'Done compiling spreadsheet.  See link below to download.'.
  781:               '";</script>');
  782:     $r->rflush();
  783:     return;
  784: }
  785: 
  786: sub compile_response_data {
  787:     my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
  788:     if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
  789:         return ();
  790:     }
  791:     if (ref($format) ne 'CODE') {
  792:         $format = sub { return $_[0]; };
  793:     }
  794:     #
  795:     my $submission = 
  796:         &HTML::Entities::decode
  797:         (&Apache::lonnet::unescape($response->{'Submission'}));
  798:     return () if (! defined($submission) || $submission eq '');
  799:     $submission =~ s/\\\"/\"/g;
  800:     $submission =~ s/\\\'/\'/g;
  801:     if ($resptype eq 'radiobutton') {
  802:         $submission =~ s/=([^=])$//;
  803:     }
  804:     $response->{'Submission'} = $submission;
  805:     #
  806:     my @values;
  807:     if ($resptype =~ /^(option|match|rank)$/) {
  808:         my %submission = 
  809:             map { 
  810:                 my ($foil,$value) = split('=',&Apache::lonnet::unescape($_));
  811:                 ($foil,$value);
  812:             } split('&',$response->{'Submission'});
  813:         my %correct;
  814:         if (exists($response->{'Correct'})) {
  815:             %correct = 
  816:                 map { 
  817:                     my ($foil,$value)=split('=',&Apache::lonnet::unescape($_));
  818:                     ($foil,$value);
  819:                 } split('&',$response->{'Correct'});
  820:         }
  821:         #
  822:         foreach my $original_header (@$headers) {
  823:             if ($original_header =~ /^_/) {
  824:                 # '_' denotes a foil column
  825:                 my ($header) = ($original_header =~ m/^_(.*)$/);
  826:                 my $option = '';
  827:                 if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
  828:                     if (exists($correct{$foil})) {
  829:                         $option = $correct{$foil};
  830:                     }
  831:                 } elsif (exists($submission{$header})) {
  832:                     $option = $submission{$header};
  833:                 }
  834:                 push(@values,&{$format}($option,$header));
  835:             } else {
  836:                 # A normal column
  837:                 push(@values,&{$format}($response->{$original_header},
  838:                                         $original_header));
  839:             }
  840:         }
  841:     } else {
  842:         @values = map { &{$format}($response->{$_},$_); } @$headers;
  843:     }
  844:     return @values;
  845: }
  846: 
  847: sub excel_format_item {
  848:     my ($item,$type) = @_;
  849:     if ($type eq 'Time') {
  850:         $item = &Apache::lonstathelpers::calc_serial($item);
  851:     } else {
  852:         if ($item =~ m/^=/) {
  853:             $item = ' '.$item;
  854:         }
  855:         $item =~ s/\\r//g;
  856:         $item =~ s/\\n/\n/g;
  857:         $item =~ s/(\s*$|^\s*)//g;
  858:         $item =~ s/\\\'/\'/g;
  859:     }
  860:     return $item;
  861: }
  862: 
  863: #########################################################
  864: #########################################################
  865: ##
  866: ##      CSV output of student answers
  867: ##
  868: #########################################################
  869: #########################################################
  870: sub prepare_csv_output {
  871:     my ($r,$problems,$students) = @_;
  872:     my $c = $r->connection();
  873:     #
  874:     $r->print('<h2>'.
  875:               &mt('Generating CSV report of student responses').'</h2>');
  876:     #
  877:     # Progress window
  878:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  879:         ($r,'CSV File Compilation Status',
  880:          'CSV File Compilation Progress', 
  881:          scalar(@$students),'inline',undef,'Statistics','stats_status');
  882:     
  883:     $r->rflush();
  884:     #
  885:     # Open a file
  886:     my $outputfile;
  887:     my $filename = '/prtspool/'.
  888:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  889:             time.'_'.rand(1000000000).'.csv';
  890:     unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
  891:         $r->log_error("Couldn't open $filename for output $!");
  892:         $r->print("Problems occured in writing the csv file.  ".
  893:                   "This error has been logged.  ".
  894:                   "Please alert your LON-CAPA administrator.");
  895:         $outputfile = undef;
  896:     }
  897:     #
  898:     # Compute the number of columns per response
  899:     my @extra_resp_headers = &get_extra_response_headers();
  900:     #
  901:     # Create the table header
  902:     my @student_columns = ('username','domain','id');
  903:     #
  904:     my %headers;
  905:     push(@{$headers{'student'}},@student_columns);
  906:     # Pad for the student data
  907:     foreach my $row ('problem','part','response') {
  908:         $headers{$row}=[map {''} @student_columns];
  909:     }
  910:     #
  911:     # we put the headers into the %headers hash
  912:     my %problem_analysis;
  913:     my %start_col;
  914:     my $max_column = scalar(@student_columns);
  915:     foreach my $prob (@$problems) {
  916:         my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
  917:         $problem_analysis{$prob->src}=\%analysis;
  918:         $headers{'problem'}->[$max_column] = $prob->compTitle;
  919:         foreach my $partid (@{$prob->parts}) {
  920:             $headers{'part'}->[$max_column] = $prob->part_display($partid);
  921:             my $responses = [$prob->responseIds($partid)];
  922:             my $resptypes = [$prob->responseType($partid)];
  923:             for (my $i=0;$i<scalar(@$responses);$i++) {
  924:                 my @headers = &get_headers($prob,$partid,$responses->[$i],
  925:                                            $resptypes->[$i],
  926:                                            $problem_analysis{$prob->src},
  927:                                            'csv','display',
  928:                                            @extra_resp_headers);
  929:                 $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
  930:                     $max_column;
  931:                 $headers{'response'}->[$max_column]=
  932:                     &mt('Response [_1]',$responses->[$i]);
  933:                 for (my $j=0;$j<=$#headers;$j++) {
  934:                     $headers{'student'}->[$max_column+$j]=$headers[$j];
  935:                 }
  936:                 $max_column += scalar(@headers);
  937:             }
  938:         }
  939:     }
  940:     foreach my $row ('problem','part','response','student') {
  941:         print $outputfile '"'.
  942:             join('","',
  943:                  map { 
  944:                      &Apache::loncommon::csv_translate($_); 
  945:                  } @{$headers{$row}}).'"'.$/;
  946:     }
  947:     #
  948:     # Main loop
  949:     foreach my $student (@$students) {
  950:         last if ($c->aborted());
  951:         my @rows;
  952:         foreach my $prob (@$problems) {
  953:             foreach my $partid (@{$prob->parts}) {
  954:                 my @responses = $prob->responseIds($partid);
  955:                 my @response_type = $prob->responseType($partid);
  956:                 for (my $i=0;$i<=$#responses;$i++) {
  957:                     my $respid   = $responses[$i];
  958:                     my $resptype = $response_type[$i];
  959:                     my @headers = &get_headers($prob,$partid,$respid,$resptype,
  960:                                                $problem_analysis{$prob->src},
  961:                                                'csv','normal',
  962:                                                @extra_resp_headers);
  963:                     my $results = 
  964:                         &Apache::loncoursedata::get_response_data_by_student
  965:                         ($student,$prob->symb(),$respid);
  966:                     if (! defined($results)) {
  967:                         $results = [];
  968:                     }
  969:                     for (my $j=0; $j<scalar(@$results);$j++) {
  970:                         if ($ENV{'form.all_sub'} ne 'true') {
  971:                             next if ($j != 0);
  972:                         }
  973:                         my $idx = scalar(@$results) - $j - 1;
  974:                         my $response = &hashify_response($results->[$idx],
  975:                                                          $prob,$student,
  976:                                                          $partid,$respid);
  977:                         my @data = &compile_response_data(\@headers,$response,
  978:                                                           $prob,$partid,
  979:                                                           $respid,$resptype,
  980:                                                           \&csv_format_item);
  981:                         my $resp_start_idx =
  982:                             $start_col{$prob->symb}->{$partid}->{$respid};
  983:                         for (my $k=0;$k<=$#data;$k++) {
  984:                             $rows[$j]->[$resp_start_idx + $k] = $data[$k];
  985:                         }
  986:                     }
  987:                 }
  988:             }
  989:         }
  990:         foreach my $row (@rows) {
  991:             print $outputfile '"'.join('","',
  992:                                        map { $student->{$_}; }
  993:                                        @student_columns).'"';
  994:             for (my $i=scalar(@student_columns);$i<$max_column;$i++) {
  995:                 my $value = &Apache::loncommon::csv_translate($row->[$i]);
  996:                 $value ||='';
  997:                 print $outputfile ',"'.$value.'"';
  998:             }
  999:             print $outputfile $/;
 1000:         }
 1001:         undef(@rows);
 1002:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 1003:                                                  'last student');
 1004:     }
 1005:     close($outputfile);
 1006:     #
 1007:     # Close the progress window
 1008:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1009:     #
 1010:     # Tell the user where to get their csv file
 1011:     $r->print('<br />'.
 1012:               '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
 1013:     $r->rflush();
 1014:     return;
 1015: }
 1016: 
 1017: sub csv_format_item {
 1018:     my ($item,$type) = @_;
 1019:     if ($type eq 'Time') {
 1020:         $item = localtime($item);
 1021:     }
 1022:     $item =&Apache::loncommon::csv_translate($item); 
 1023:     return $item;
 1024: }
 1025: 
 1026: #########################################################
 1027: #########################################################
 1028: ##
 1029: ##   Generic Interface Routines
 1030: ##
 1031: #########################################################
 1032: #########################################################
 1033: sub CreateInterface {
 1034:     ##
 1035:     ## Output Selection
 1036:     my $output_selector = $/.'<select name="output">'.$/;
 1037:     foreach ('HTML','Excel','CSV') {
 1038:         $output_selector .= '    <option value="'.lc($_).'"';
 1039:         if ($ENV{'form.output'} eq lc($_)) {
 1040:             $output_selector .= ' selected ';
 1041:         }
 1042:         $output_selector .='>'.&mt($_).'</option>'.$/;
 1043:     } 
 1044:     $output_selector .= '</select>'.$/;
 1045:     ##
 1046:     ## Environment variable initialization
 1047:     my $Str = '';
 1048:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
 1049:         (undef,'Student Submission Reports');
 1050:     $Str .= '<p>';
 1051:     $Str .= '<table cellspacing="5">'."\n";
 1052:     $Str .= '<tr>';
 1053:     $Str .= '<th>'.&mt('Sections').'</th>';
 1054:     $Str .= '<th>'.&mt('Enrollment Status').'</th>';
 1055:     $Str .= '<th>'.&mt('Output as [_1]',$output_selector).'</th>';
 1056:     $Str .= '</tr>'."\n";
 1057:     #
 1058:     $Str .= '<tr><td align="center">'."\n";
 1059:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1060:     $Str .= '</td>';
 1061:     #
 1062:     $Str .= '<td align="center">';
 1063:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1064:     $Str .= '</td>';
 1065:     #
 1066:     # Render problem checkbox
 1067:     my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
 1068:     if (exists($ENV{'form.renderprob'}) && $ENV{'form.renderprob'} eq 'true') {
 1069:         $prob_checkbox .= 'checked ';
 1070:     }
 1071:     $prob_checkbox .= 'value="true" />';
 1072:     #
 1073:     # Compute correct answers checkbox
 1074:     my $ans_checkbox = '<input type="checkbox" name="correctans" ';
 1075:     if (exists($ENV{'form.correctans'}) && $ENV{'form.correctans'} eq 'true') {
 1076:         $ans_checkbox .= 'checked ';
 1077:     }
 1078:     $ans_checkbox .= 'value="true" />';
 1079:     #
 1080:     # Show all submissions checkbox
 1081:     my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
 1082:     if (exists($ENV{'form.all_sub'}) && 
 1083:         $ENV{'form.all_sub'} eq 'true') {
 1084:         $all_sub_checkbox .= 'checked ';
 1085:     }
 1086:     $all_sub_checkbox.= 'value="true" />';
 1087:     #
 1088:     # problem status checkbox
 1089:     my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
 1090:     if (exists($ENV{'form.prob_status'}) && 
 1091:         $ENV{'form.prob_status'} eq 'true') {
 1092:         $prob_status_checkbox .= 'checked ';
 1093:     }
 1094:     $prob_status_checkbox .= 'value="true" />';
 1095:     #
 1096:     $Str .= '<td align="right" halign="top">'.
 1097:         '<label><b>'.
 1098:         &mt('Show problem [_1]',$prob_checkbox).'</b></label><br />'.
 1099:         '<label><b>'.
 1100:         &mt('Show correct answers [_1]',$ans_checkbox).'</b></label><br />'.
 1101:         '<label><b>'.
 1102:         &mt('Show all submissions [_1]',$all_sub_checkbox).
 1103:         '</b></label><br />'.
 1104:         '<label><b>'.
 1105:         &mt('Show problem grading [_1]',$prob_status_checkbox).
 1106:         '</b></label><br />'.
 1107:         '</td>';
 1108:     #
 1109:     $Str .= '</tr>'."\n";
 1110:     $Str .= '</table>'."\n";
 1111:     #
 1112:     $Str .= '<nobr>'.&mt('Status: [_1]',
 1113:                          '<input type="text" '.
 1114:                          'name="stats_status" size="60" value="" />').
 1115:             '</nobr>'.'</p>';    
 1116:     ##
 1117:     return $Str;
 1118: }
 1119: 
 1120: 1;
 1121: 
 1122: __END__

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