File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.19: download - view: text, annotated - select for diffs
Thu Sep 16 21:54:22 2004 UTC (19 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
lonstathelpers: removed $ResponseTypes input from MultipleProblemSelector
Added parameter $update_message to manage_caches.
lonstudentsubmissions:Removed useless calls to get_problem_data, added
warning about the amount of time it takes to compute correct answers,
new_excel_output: determine number of columns we will use in the excel
  spreadsheet and abort if it's too many, added support for correct answer
  printing
added &get_problem_data which is not currently used and probably won't be
Excel output is the only working output still.

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

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