File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.41: download - view: text, annotated - select for diffs
Tue Apr 12 21:49:49 2005 UTC (19 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, HEAD
Two bugs with HTML output.
Skip columns with a width of '0' as it _really_ messes up firefox.
Some responses were not being unescaped.

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

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