File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.61: download - view: text, annotated - select for diffs
Tue Aug 24 14:05:19 2010 UTC (13 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, HEAD
- Bug 6119.
 Must exceed threshold for number of responders to anonymous survey items
 before displaying anonymized submission data.
- Fix typo.
- Missing &mt().

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

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