File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.79: download - view: text, annotated - select for diffs
Fri Mar 12 21:06:32 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Check for aborted connections when compiling results for students.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemanalysis.pm,v 1.79 2004/03/12 21:06:32 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::lonproblemanalysis;
   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 Apache::lonstudentsubmissions();
   38: use HTML::Entities();
   39: use Time::Local();
   40: use Spreadsheet::WriteExcel();
   41: 
   42: my $plotcolors = ['#33ff00', 
   43:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
   44:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
   45:                   ]; 
   46: 
   47: my @SubmitButtons = ({ name => 'PrevProblemAnalysis',
   48:                        text => 'Previous Problem' },
   49:                      { name => 'ProblemAnalysis',
   50:                        text => 'Analyze Problem Again' },
   51:                      { name => 'NextProblemAnalysis',
   52:                        text => 'Next Problem' },
   53:                      { name => 'break'},
   54:                      { name => 'ClearCache',
   55:                        text => 'Clear Caches' },
   56:                      { name => 'updatecaches',
   57:                        text => 'Update Student Data' },
   58:                      { name => 'SelectAnother',
   59:                        text => 'Choose a different Problem' },
   60:                      { name => 'ExcelOutput',
   61:                        text => 'Produce Excel Output' });
   62: 
   63: 
   64: sub BuildProblemAnalysisPage {
   65:     my ($r,$c)=@_;
   66:     #
   67:     my %Saveable_Parameters = ('Status' => 'scalar',
   68:                                'Section' => 'array',
   69:                                'NumPlots' => 'scalar',
   70:                                'AnalyzeAs' => 'scalar',
   71:                                'AnalyzeOver' => 'scalar',
   72:                                );
   73:     &Apache::loncommon::store_course_settings('problem_analysis',
   74:                                               \%Saveable_Parameters);
   75:     &Apache::loncommon::restore_course_settings('problem_analysis',
   76:                                                 \%Saveable_Parameters);
   77:     #
   78:     &Apache::lonstatistics::PrepareClasslist();
   79:     #
   80:     $r->print(&CreateInterface());
   81:     #
   82:     my @Students = @Apache::lonstatistics::Students;
   83:     #
   84:     if (@Students < 1) {
   85:         $r->print('<h2>There are no students in the sections selected</h2>');
   86:     }
   87:     #
   88:     &Apache::loncoursedata::clear_internal_caches();
   89:     if (exists($ENV{'form.ClearCache'}) || 
   90:         exists($ENV{'form.updatecaches'}) ||
   91:         (exists($ENV{'form.firstanalysis'}) &&
   92:          $ENV{'form.firstanalysis'} ne 'no')) {
   93:         &Apache::lonstatistics::Gather_Full_Student_Data($r);
   94:     }
   95:     if (! exists($ENV{'form.firstanalysis'})) {
   96:         $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
   97:     } else {
   98:         $r->print('<input type="hidden" name="firstanalysis" value="no" />');
   99:     }
  100:     $r->rflush();
  101:     #
  102:     my $problem_types = '(option|radiobutton|numerical)';
  103:     if (exists($ENV{'form.problemchoice'}) && 
  104:         ! exists($ENV{'form.SelectAnother'})) {
  105:         foreach my $button (@SubmitButtons) {
  106:             if ($button->{'name'} eq 'break') {
  107:                 $r->print("<br />\n");
  108:             } else {
  109:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
  110:                           'value="'.&mt($button->{'text'}).'" />');
  111:                 $r->print('&nbsp;'x5);
  112:             }
  113:         }
  114:         #
  115:         $r->print('<hr />');
  116:         $r->rflush();
  117:         #
  118:         # Determine which problem we are to analyze
  119:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
  120:             ($ENV{'form.problemchoice'});
  121:         #
  122:         my ($prev,$curr,$next) = 
  123:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  124:                                                         $problem_types,
  125:                                                         'response',
  126:                                                         );
  127:         if (exists($ENV{'form.PrevProblemAnalysis'}) && defined($prev)) {
  128:             $current_problem = $prev;
  129:         } elsif (exists($ENV{'form.NextProblemAnalysis'}) && defined($next)) {
  130:             $current_problem = $next;
  131:         } else {
  132:             $current_problem = $curr;
  133:         }
  134:         #
  135:         # Store the current problem choice and send it out in the form
  136:         $ENV{'form.problemchoice'} = 
  137:             &Apache::lonstathelpers::make_target_id($current_problem);
  138:         $r->print('<input type="hidden" name="problemchoice" value="'.
  139:                   $ENV{'form.problemchoice'}.'" />');
  140:         #
  141:         if (! defined($current_problem->{'resource'})) {
  142:             $r->print('resource is undefined');
  143:         } else {
  144:             my $resource = $current_problem->{'resource'};
  145:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  146:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  147:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  148:             $r->rflush();
  149:             my %Data = &Apache::lonstathelpers::get_problem_data
  150:                 ($resource->{'src'});
  151:             my $ProblemData = $Data{$current_problem->{'part'}.
  152:                                     '.'.
  153:                                     $current_problem->{'respid'}};
  154:             if ($current_problem->{'resptype'} eq 'option') {
  155:                 &OptionResponseAnalysis($r,$current_problem,
  156:                                         $ProblemData,
  157:                                         \@Students);
  158:             } elsif ($current_problem->{'resptype'} eq 'radiobutton') {
  159:                 &RadioResponseAnalysis($r,$current_problem,
  160:                                        $ProblemData,
  161:                                        \@Students);
  162:             } elsif ($current_problem->{'resptype'} eq 'numerical') {
  163: #                if (exists($ENV{'form.ExcelOutput'})) {
  164: #                    &Apache::lonstudentsubmissions::prepare_excel_output
  165: #                        ($r,$current_problem,$ProblemData,\@Students);
  166: #                } else {
  167:                     &NumericalResponseAnalysis($r,$current_problem,
  168:                                                $ProblemData,\@Students);
  169: #                }
  170:             } else {
  171:                 $r->print('<h2>This analysis is not supported</h2>');
  172:             }
  173:         }
  174:         $r->print('<hr />');
  175:     } else {
  176:         $r->print('<input type="submit" name="ProblemAnalysis" value="'.
  177:                   &mt('Analyze Problem').'" />');
  178:         $r->print('&nbsp;'x5);
  179:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
  180:         $r->print(&Apache::lonstathelpers::ProblemSelector
  181:                   ($problem_types));
  182:     }
  183: }
  184: 
  185: 
  186: #########################################################
  187: #########################################################
  188: ##
  189: ##      Numerical Response Routines
  190: ##
  191: #########################################################
  192: #########################################################
  193: sub NumericalResponseAnalysis {
  194:     my ($r,$problem,$ProblemData,$Students) = @_;
  195:     my $c = $r->connection();
  196:     my ($resource,$respid) = ($problem->{'resource'},
  197:                               $problem->{'respid'});
  198:     my $analysis_html;
  199:     my $PerformanceData = 
  200:         &Apache::loncoursedata::get_response_data
  201:         ($Students,$resource->{'symb'},$respid);
  202:     if (! defined($PerformanceData) || 
  203:         ref($PerformanceData) ne 'ARRAY' ) {
  204:         $analysis_html = '<h2>'.
  205:             &mt('There is no submission data for this resource').
  206:             '</h2>';
  207:         $r->print($analysis_html);
  208:         return;
  209:     }
  210:     #
  211:     # This next call causes all the waiting around that people complain about
  212:     my ($max,$min) = &GetStudentAnswers($r,$problem,$Students);
  213:     return if ($c->aborted());
  214:     #
  215:     # Collate the data
  216:     my %Data;
  217:     foreach my $student (@$Students) {
  218:         my $answer = $student->{'answer'};
  219:         $Data{$answer}++;
  220:     }
  221:     my @Labels = sort {$a <=> $b } keys(%Data);
  222:     my @PlotData = @Data{@Labels};
  223:     #
  224:     my $width  = 500; 
  225:     my $height = 100;
  226:     my $plot = &one_dimensional_plot($r,500,100,scalar(@$Students),
  227:                                      \@Labels,\@PlotData);
  228:     $r->print($plot);
  229:     return;
  230: }
  231: 
  232: sub one_dimensional_plot {
  233:     my ($r,$width,$height,$N,$Labels,$Data)=@_;
  234:     #
  235:     # Compute data -> image scaling factors
  236:     my $min = $Labels->[0];
  237:     my $max = $Labels->[-1];
  238:     my $h_scale = ($width-10)/($max-$min);
  239:     #
  240:     my $max_y = 0;
  241:     foreach (@$Data) {
  242:         $max_y = $_ if ($max_y < $_);
  243:     }
  244:     my $ticscale = 5;
  245:     if ($max_y * $ticscale > $height/2) {
  246:         $ticscale = int($height/2/$max_y);
  247:         $ticscale = 1 if ($ticscale < 1);
  248:     }
  249:     #
  250:     # Create the plot
  251:     my $plot = 
  252:         qq{<drawimage width="$width" height="$height" bgcolor="transparent" >};
  253:     for (my $idx=0;$idx<scalar(@$Labels);$idx++) {
  254:         my $xloc = 5+$h_scale*($Labels->[$idx] - $min);
  255:         my $top    = $height/2-$Data->[$idx]*$ticscale;
  256:         my $bottom = $height/2+$Data->[$idx]*$ticscale;
  257:         $plot .= 
  258:             &line($xloc,$top,$xloc,$bottom,'888888',1);
  259:     }
  260:     #
  261:     # Put the scale on last to ensure it is on top of the data.
  262:     if ($min < 0 && $max > 0) {
  263:         my $circle_x = 5+$h_scale*abs($min);  # '0' in data coordinates
  264:         my $r = 4;
  265:         $plot .= &line(5,$height/2,$circle_x-$r,$height/2,'000000',1);
  266:         $plot .= &circle($circle_x,$height/2,$r+1,'000000');
  267:         $plot .= &line($circle_x+$r,$height/2,$width-5,$height/2,'000000',1);
  268:     } else {
  269:         $plot .= &line(5,$height/2,$width-5,$height/2,'000000',1);
  270:     }
  271:     $plot .= '</drawimage>';
  272:     my $plotresult =  &Apache::lonxml::xmlparse($r,'web',$plot);
  273:     
  274:     my $title = 'Distribution of correct answers';
  275:     my $result = '<table>'.
  276:         '<tr><td colspan="3" align="center">'.
  277:         '<font size="+2">'.$title.' (N='.$N.')'.
  278:         '</font>'.
  279:         '</td></tr>'.
  280:         '<tr>'.
  281:         '<td valign="center">'.$min.'</td>'.
  282:         '<td>'.$plotresult.'</td>'.
  283:         '<td valign="center">'.$max.'</td>'.
  284:         '</tr>'.
  285:         '<tr><td colspan="3" align="center">'.
  286:         'Maximum Number of Coinciding Values: '.$max_y.
  287:         '</td></tr>'.
  288:         '</table>';
  289:     return $result;
  290: }
  291: 
  292: ##
  293: ## Helper subroutines for <drawimage>.  
  294: ## These should probably go somewhere more suitable soon.
  295: sub line {
  296:     my ($x1,$y1,$x2,$y2,$color,$thickness) = @_;
  297:     return qq{<line x1="$x1" y1="$y1" x2="$x2" y2="$y2" color="$color" thickness="$thickness" />$/};
  298: }
  299: 
  300: sub rectangle {
  301:     my ($x1,$y1,$x2,$y2,$color,$thickness,$filled) = @_;
  302:     return qq{<rectangle x1="$x1" y1="$y1" x2="$x2" y2="$y2" color="$color" thickness="$thickness" filled="$filled" />};
  303: }
  304: 
  305: sub arc {
  306:     my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=@_;
  307:     return qq{<arc x="$x" y="$y" width="$width" height="$height" start="$start" end="$end" color="$color" thickness="$thickness" filled="$filled" />};
  308: }
  309: 
  310: sub circle {
  311:     my ($x,$y,$radius,$color,$thickness,$filled)=@_;
  312:     return &arc($x,$y,$radius,$radius,0,360,$color,$thickness,$filled);
  313: }
  314: 
  315: sub GetStudentAnswers {
  316:     my ($r,$problem,$Students) = @_;
  317:     my $c = $r->connection();
  318:     my %Answers;
  319:     my ($resource,$partid,$respid) = ($problem->{'resource'},
  320:                                       $problem->{'part'},
  321:                                       $problem->{'respid'});
  322:     # Open progress window
  323:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  324:         ($r,'Student Answer Compilation Status',
  325:          'Student Answer Compilation Progress', scalar(@$Students));
  326:     $r->print("<table>\n");
  327:     $r->rflush();
  328:     foreach my $student (@$Students) {
  329:         last if ($c->aborted());
  330:         my $sname = $student->{'username'};
  331:         my $sdom = $student->{'domain'};
  332:         my $answer = &Apache::lonstathelpers::analyze_problem_as_student
  333:             ($resource,$sname,$sdom,$partid,$respid);
  334:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  335:                                                  &mt('last student'));
  336:         $student->{'answer'} = $answer;
  337:     }
  338:     return if ($c->aborted());
  339:     $r->print("</table>\n");
  340:     $r->rflush();
  341:     # close progress window
  342:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  343:     return;
  344: }
  345: 
  346: sub build_student_data_worksheet {
  347:     my ($workbook,$format) = @_;
  348:     my $rows_output = 3;
  349:     my $cols_output = 0;
  350:     my $worksheet  = $workbook->addworksheet('Student Data');
  351:     $worksheet->write($rows_output++,0,'Student Data',$format->{'h3'});
  352:     my @Headers = ('full name','username','domain','section',
  353:                    "student\nnumber",'identifier');
  354:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
  355:     my @Students = @Apache::lonstatistics::Students;
  356:     my $studentrows = &Apache::loncoursedata::get_student_data(\@Students);
  357:     my %ids;
  358:     foreach my $row (@$studentrows) {
  359:         my ($mysqlid,$student) = @$row;
  360:         $ids{$student}=$mysqlid;
  361:     }
  362:     foreach my $student (@Students) {
  363:         my $name_domain = $student->{'username'}.':'.$student->{'domain'};
  364:         $worksheet->write_row($rows_output++,0,
  365:                           [$student->{'fullname'},
  366:                            $student->{'username'},$student->{'domain'},
  367:                            $student->{'section'},$student->{'id'},
  368:                            $ids{$name_domain}]);
  369:     }
  370:     return $worksheet;
  371: }
  372: 
  373: #########################################################
  374: #########################################################
  375: ##
  376: ##      Radio Response Routines
  377: ##
  378: #########################################################
  379: #########################################################
  380: sub RadioResponseAnalysis {
  381:     my ($r,$problem,$ProblemData,$Students) = @_;
  382:     my ($resource,$respid) = ($problem->{'resource'},
  383:                               $problem->{'respid'});
  384:     my $analysis_html;
  385:     my $PerformanceData = 
  386:         &Apache::loncoursedata::get_response_data
  387:         ($Students,$resource->{'symb'},$respid);
  388:     if (! defined($PerformanceData) || 
  389:         ref($PerformanceData) ne 'ARRAY' ) {
  390:         $analysis_html = '<h2>'.
  391:             &mt('There is no submission data for this resource').
  392:             '</h2>';
  393:         $r->print($analysis_html);
  394:         return;
  395:     }
  396:     if (exists($ENV{'form.ExcelOutput'})) {
  397:         $analysis_html .= &RR_Excel_output($r,$problem->{'resource'},
  398:                                            $PerformanceData,$ProblemData);
  399:     } elsif ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  400:         $analysis_html .= &RR_Tries_Analysis($r,$problem->{'resource'},
  401:                                              $PerformanceData,$ProblemData);
  402:     } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  403:         $analysis_html .= &RR_Time_Analysis($r,$problem->{'resource'},
  404:                                             $PerformanceData,$ProblemData);
  405:     } else {
  406:         $analysis_html .= '<h2>'.
  407:            &mt('The analysis you have selected is not supported at this time').
  408:            '</h2>';
  409:     }
  410:     $r->print($analysis_html);
  411: }
  412: 
  413: sub RR_Excel_output   { 
  414:     my ($r,$PerformanceData,$ProblemData) = @_;
  415:     return '<h1>No!</h1>';
  416: }
  417: 
  418: sub RR_Tries_Analysis { 
  419:     my ($r,$resource,$PerformanceData,$ProblemData) = @_;
  420:     my $analysis_html;
  421:     my $mintries = 1;
  422:     my $maxtries = $ENV{'form.NumPlots'};
  423:     my ($table,$Foils,$Concepts) = &build_foil_index($ProblemData);
  424:     if ((! defined($Concepts)) || ((@$Concepts < 2) && 
  425:                                    ($ENV{'form.AnalyzeAs'} ne 'Foils'))) {
  426:         $table = '<h3>'.
  427:             &mt('Not enough data for concept analysis.  '.
  428:                 'Performing Foil Analysis').
  429:             '</h3>'.$table;
  430:         $ENV{'form.AnalyzeAs'} = 'Foils';
  431:     }
  432:     $analysis_html .= $table;
  433:     my @TryData = &RR_tries_data_analysis($r,$PerformanceData);
  434: #    if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  435:         $analysis_html .= &RR_Tries_Foil_Analysis($mintries,$maxtries,$Foils,
  436:                                                  \@TryData,$ProblemData);
  437: #    } else {
  438: #        $analysis_html = &RR_Tries_Concept_Analysis($mintries,$maxtries,
  439: #                                                    $Concepts,
  440: #                                                    \@TryData,
  441: #                                                    $ProblemData);
  442: #    }
  443:     return $analysis_html;
  444: }
  445: 
  446: sub RR_tries_data_analysis {
  447:     my ($r,$Attempt_data) = @_;
  448:     my @TryData;
  449:     foreach my $attempt (@$Attempt_data) {
  450:         my %Attempt = &hashify_attempt($attempt);
  451:         my ($answer,undef) = split('=',$Attempt{'submission'});
  452:         $TryData[$Attempt{'tries'}]->{$answer}++;
  453:     }
  454:     return @TryData;
  455: }
  456: 
  457: sub RR_Time_Analysis  { 
  458:     my ($r,$PerformanceData,$ProblemData) = @_;
  459:     my $html;
  460:     return $html;
  461: }
  462: 
  463: sub RR_Tries_Foil_Analysis {
  464:     my ($min,$max,$Foils,$TryData,$ProblemData) = @_;
  465:     my $html;
  466:     #
  467:     # Compute the data neccessary to make the plots
  468:     for (my $try=$min;$try<=$max;$try++) {
  469:         my @PlotData_Correct; 
  470:         my @PlotData_Incorrect;
  471:         next if ($try > scalar(@{$TryData}));
  472:         next if (! defined($TryData->[$try]));
  473:         my %DataSet = %{$TryData->[$try]};
  474:         my $total = 0;
  475:         foreach my $foilid (@$Foils) {
  476:             $total += $DataSet{$foilid};
  477:         }
  478:         foreach my $foilid (@$Foils) {
  479:             if ($total == 0) {
  480:                 push (@PlotData_Correct,0);
  481:                 push (@PlotData_Incorrect,0);
  482:             } else {
  483:                 if ($ProblemData->{'_Foils'}->{$foilid}->{'value'} eq 'true') {
  484:                     push (@PlotData_Correct,
  485:                           int(100*$DataSet{$foilid}/$total));
  486:                     push (@PlotData_Incorrect,0);
  487:                 } else {
  488:                     push (@PlotData_Correct,0);
  489:                     push (@PlotData_Incorrect,
  490:                           int(100*$DataSet{$foilid}/$total));
  491:                 }
  492:             }
  493:         }
  494:         my $title='Attempt '.$try;
  495:         my $xlabel = $total.' Submissions';
  496:         $html.=  &Apache::loncommon::DrawBarGraph($title,
  497:                                                   $xlabel,
  498:                                                   'Percent Choosing',
  499:                                                   100,
  500:                                                   ['#33ff00','#ff3300'],
  501:                                                   undef,
  502:                                                   \@PlotData_Correct,
  503:                                                   \@PlotData_Incorrect);
  504:     }
  505:     return $html;
  506: }
  507: 
  508: sub RR_Tries_Concept_Analysis {
  509:     my ($min,$max,$Concepts,$ResponseData,$ProblemData) = @_;
  510:     my $html;
  511:     return $html;
  512: }
  513: 
  514: sub RR_Time_Foil_Analysis {
  515:     my ($min,$max,$Foils,$ResponseData,$ProblemData) = @_;
  516:     my $html;
  517:     return $html;
  518: }
  519: 
  520: sub RR_Time_Concept_Analysis {
  521:     my ($min,$max,$Concepts,$ResponseData,$ProblemData) = @_;
  522:     my $html;
  523:     return $html;
  524: }
  525: 
  526: 
  527: sub get_Radio_problem_data {
  528:     my ($url) = @_;
  529:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
  530:     (my $garbage,$Answ)=split('_HASH_REF__',$Answ,2);
  531:     my %Answer = &Apache::lonnet::str2hash($Answ);
  532:     my %Partdata;
  533:     foreach my $part (@{$Answer{'parts'}}) {
  534:         while (my($key,$value) = each(%Answer)) {
  535: #            if (ref($value) eq 'ARRAY') {
  536: #                &Apache::lonnet::logthis('is ref part:'.$part.' '.$key.'='.join(',',@$value));
  537: #            } else {
  538: #                &Apache::lonnet::logthis('notref part:'.$part.' '.$key.'='.$value);
  539: #            }                
  540:             next if ($key !~ /^$part/);
  541:             $key =~ s/^$part\.//;
  542:             if ($key eq 'foils') {
  543:                 $Partdata{$part}->{'_Foils'}=$value;
  544:             } elsif ($key eq 'options') {
  545:                 $Partdata{$part}->{'_Options'}=$value;
  546:             } elsif ($key eq 'shown') {
  547:                 $Partdata{$part}->{'_Shown'}=$value;
  548:             } elsif ($key =~ /^foil.value.(.*)$/) {
  549:                 $Partdata{$part}->{$1}->{'value'}=$value;
  550:             } elsif ($key =~ /^foil.text.(.*)$/) {
  551:                 $Partdata{$part}->{$1}->{'text'}=$value;
  552:             }
  553:         }
  554:     }
  555:     return %Partdata;
  556: }
  557: 
  558: #########################################################
  559: #########################################################
  560: ##
  561: ##      Option Response Routines
  562: ##
  563: #########################################################
  564: #########################################################
  565: sub OptionResponseAnalysis {
  566:     my ($r,$problem,$ProblemData,$Students) = @_;
  567:     my ($resource,$respid) = ($problem->{'resource'},
  568:                               $problem->{'respid'});
  569:     # Note: part data is not needed.
  570:     my $PerformanceData = 
  571:         &Apache::loncoursedata::get_response_data
  572:         ($Students,$resource->{'symb'},$respid);
  573:     if (! defined($PerformanceData) || 
  574:         ref($PerformanceData) ne 'ARRAY' ) {
  575:         $r->print('<h2>'.
  576:                   &mt('There is no student data for this problem.').
  577:                   '</h2>');
  578:     }  else {
  579:         $r->rflush();
  580:         if (exists($ENV{'form.ExcelOutput'})) {
  581:             my $result = &OR_excel_sheet($r,$resource,
  582:                                          $PerformanceData,
  583:                                          $ProblemData);
  584:             $r->print($result);
  585:             $r->rflush();
  586:         } else {
  587:             if ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  588:                 my $analysis_html = &OR_tries_analysis($r,
  589:                                                     $PerformanceData,
  590:                                                     $ProblemData);
  591:                 $r->print($analysis_html);
  592:                 $r->rflush();
  593:             } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  594:                 my $analysis_html = &OR_time_analysis($PerformanceData,
  595:                                                    $ProblemData);
  596:                 $r->print($analysis_html);
  597:                 $r->rflush();
  598:             } else {
  599:                 $r->print('<h2>'.
  600:                           &mt('The analysis you have selected is '.
  601:                               'not supported at this time').
  602:                           '</h2>');
  603:             }
  604:         }
  605:     }
  606: }
  607: 
  608: #########################################################
  609: #
  610: #       Option Response:  Tries Analysis
  611: #
  612: #########################################################
  613: sub OR_tries_analysis {
  614:     my ($r,$PerformanceData,$ORdata) = @_;
  615:     my $mintries = 1;
  616:     my $maxtries = $ENV{'form.NumPlots'};
  617:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  618:     if (! defined($Concepts) || 
  619:         ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils'))) {
  620:         $table = '<h3>'.
  621:             &mt('Not enough data for concept analysis.  '.
  622:                 'Performing Foil Analysis').
  623:             '</h3>'.$table;
  624:         $ENV{'form.AnalyzeAs'} = 'Foils';
  625:     }
  626:     my %ResponseData = &OR_analyze_by_tries($r,$PerformanceData,
  627:                                                      $mintries,$maxtries);
  628:     my $analysis = '';
  629:     if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  630:         $analysis = &OR_Tries_Foil_Analysis($mintries,$maxtries,$Concepts,
  631:                                          \%ResponseData,$ORdata);
  632:     } else {
  633:         $analysis = &OR_Tries_Concept_Analysis($mintries,$maxtries,
  634:                                             $Concepts,\%ResponseData,$ORdata);
  635:     }
  636:     $table .= $analysis;
  637:     return $table;
  638: }
  639: 
  640: sub OR_Tries_Foil_Analysis {
  641:     my ($mintries,$maxtries,$Concepts,$respdat,$ORdata) = @_;
  642:     my %ResponseData = %$respdat;
  643:     #
  644:     # Compute the data neccessary to make the plots
  645:     my @PlotData; 
  646:     foreach my $concept (@$Concepts) {
  647:         foreach my $foilid (@{$concept->{'foils'}}) {
  648:             for (my $try=$mintries;$try<=$maxtries;$try++) {
  649:                 if ($ResponseData{$foilid}->[$try]->{'_total'} == 0) {
  650:                     push(@{$PlotData[$try]->{'_correct'}},0);
  651:                 } else {
  652:                     push(@{$PlotData[$try]->{'_correct'}},
  653:                          100*$ResponseData{$foilid}->[$try]->{'_correct'}/
  654:                          $ResponseData{$foilid}->[$try]->{'_total'});
  655:                 }
  656:                 foreach my $option (@{$ORdata->{'_Options'}}) {
  657:                     push(@{$PlotData[$try]->{'_total'}},
  658:                          $ResponseData{$foilid}->[$try]->{'_total'});
  659:                     if ($ResponseData{$foilid}->[$try]->{'_total'} == 0) {
  660:                         push (@{$PlotData[$try]->{$option}},0);
  661:                     } else {
  662:                         if ($ResponseData{$foilid}->[$try]->{'_total'} ==
  663:                             $ResponseData{$foilid}->[$try]->{'_correct'}) {
  664:                             push(@{$PlotData[$try]->{$option}},0);
  665:                         } else {
  666:                             push (@{$PlotData[$try]->{$option}},
  667:                                   100 * 
  668:                                   $ResponseData{$foilid}->[$try]->{$option} / 
  669:                                   ($ResponseData{$foilid}->[$try]->{'_total'} 
  670:                                    - 
  671:                                    $ResponseData{$foilid}->[$try]->{'_correct'}
  672:                                    ));
  673:                         }
  674:                     }
  675:                 } # End of foreach my $option
  676:             }
  677:         } # End of foreach my $foilid
  678:     } # End of foreach my $concept
  679:     # 
  680:     # Build a table for the plots
  681:     my $analysis_html = "<table>\n";
  682:     my $optionkey = &build_option_index($ORdata);
  683:     for (my $try=$mintries;$try<=$maxtries;$try++) {
  684:         my $count = $ResponseData{'_total'}->[$try];
  685:         my $title = 'Submission '.$try.' (N='.$count.')';
  686:         my @Datasets;
  687:         foreach my $option ('_correct',@{$ORdata->{'_Options'}}) {
  688:             next if (! exists($PlotData[$try]->{$option}));
  689:             push(@Datasets,$PlotData[$try]->{$option});
  690:         }
  691:         #
  692:         # Put a blank in the data set between concepts
  693:         for (my $set =0;$set<=$#Datasets;$set++) {
  694:             my @Data = @{$Datasets[$set]};
  695:             my $idx = 0;
  696:             foreach my $concept (@{$Concepts}) {
  697:                 foreach my $foilid (@{$concept->{'foils'}}) {
  698:                     $Datasets[$set]->[$idx++]=shift(@Data);
  699:                 }
  700:                 if ($concept->{'name'} ne $Concepts->[-1]->{'name'}) {
  701:                     $Datasets[$set]->[$idx++] = 0;
  702:                 }
  703:             }
  704:         }
  705:         #
  706:         # Set up the labels needed for the bar graph
  707:         my @Labels;
  708:         my $idx = 1;
  709:         foreach my $concept (@{$Concepts}) {
  710:             foreach my $foilid (@{$concept->{'foils'}}) {
  711:                 push(@Labels,$idx++);
  712:             }
  713:             push(@Labels,'');
  714:         }
  715:         #
  716:         my $correctgraph = &Apache::loncommon::DrawBarGraph
  717:             ($title,'Foil Number','Percent Correct',
  718:              100,$plotcolors,\@Labels,$Datasets[0]);
  719:         $analysis_html.= '<tr><td>'.$correctgraph.'</td>';
  720:         
  721:         #
  722:         #
  723:         next if (! defined($Datasets[0]));
  724:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
  725:             $Datasets[0]->[$i]=0;
  726:         }
  727:         $count = $ResponseData{'_total'}->[$try] - 
  728:                                            $ResponseData{'_correct'}->[$try];
  729:         $title = 'Submission '.$try.' (N='.$count.')';
  730:         my $incorrectgraph = &Apache::loncommon::DrawBarGraph
  731:             ($title,'Foil Number','% Option Chosen Incorrectly',
  732:              100,$plotcolors,\@Labels,@Datasets);
  733:         $analysis_html.= '<td>'.$incorrectgraph.'</td>';
  734:         $analysis_html.= '<td>'.$optionkey."<td></tr>\n";
  735:     }
  736:     $analysis_html .= "</table>\n";
  737:     return $analysis_html;
  738: }
  739: 
  740: sub OR_Tries_Concept_Analysis {
  741:     my ($mintries,$maxtries,$Concepts,$respdat,$ORdata) = @_;
  742:     my %ResponseData = %$respdat;
  743:     my $analysis_html = "<table>\n";
  744:     #
  745:     # Compute the data neccessary to make the plots
  746:     my @PlotData;
  747:     # Concept analysis
  748:     #
  749:     # Note: we do not bother with characterizing the students incorrect
  750:     # answers at the concept level because an incorrect answer for one foil
  751:     # may be a correct answer for another foil.
  752:     my %ConceptData;
  753:     foreach my $concept (@{$Concepts}) {
  754:         for (my $i=$mintries;$i<=$maxtries;$i++) {
  755:             #
  756:             # Gather the per-attempt data
  757:             my $cdata = $ConceptData{$concept}->[$i];
  758:             foreach my $foilid (@{$concept->{'foils'}}) {
  759:                 $cdata->{'_correct'} += 
  760:                     $ResponseData{$foilid}->[$i]->{'_correct'};
  761:                 $cdata->{'_total'}   += 
  762:                     $ResponseData{$foilid}->[$i]->{'_total'};
  763:             }
  764:             push (@{$PlotData[$i]->{'_total'}},$cdata->{'_total'});
  765:             if ($cdata->{'_total'} == 0) {
  766:                 push (@{$PlotData[$i]->{'_correct'}},0);
  767:             } else {
  768:                 push (@{$PlotData[$i]->{'_correct'}},
  769:                       100*$cdata->{'_correct'}/$cdata->{'_total'});
  770:                 }
  771:         }
  772:     }
  773:     # Build a table for the plots
  774:     for (my $i=$mintries;$i<=$maxtries;$i++) {
  775:         my $minstu = $PlotData[$i]->{'_total'}->[0];
  776:         my $maxstu = $PlotData[$i]->{'_total'}->[0];
  777:         foreach my $count (@{$PlotData[$i]->{'_total'}}) {
  778:             if ($minstu > $count) {
  779:                 $minstu = $count;
  780:             }
  781:             if ($maxstu < $count) {
  782:                 $maxstu = $count;
  783:             }
  784:         }
  785:         $maxstu = 0 if (! defined($maxstu));
  786:         $minstu = 0 if (! defined($minstu));
  787:         my $title;
  788:         my $count = $ResponseData{'_total'}->[$i];
  789:         if ($count == 0) {
  790:             $count = 'no submissions';
  791:         } elsif ($count == 1) {
  792:             $count = '1 submission';
  793:         } else {
  794:             $count = $count.' submissions';
  795:         }
  796:         $title = 'Attempt '.$i.', '.$count;
  797:         my $graphlink = &Apache::loncommon::DrawBarGraph
  798:             ($title,'Concept Number','Percent Correct',
  799:              100,$plotcolors,undef,$PlotData[$i]->{'_correct'});
  800:         $analysis_html.= '<tr><td>'.$graphlink."</td></tr>\n";
  801:     }
  802:     $analysis_html .= "</table>\n";
  803:     return $analysis_html;
  804: }
  805: 
  806: sub OR_analyze_by_tries {
  807:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
  808:     my %Trydata;
  809:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
  810:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
  811:     foreach my $row (@$PerformanceData) {
  812:         next if (! defined($row));
  813:         my $tries = &get_tries_from_row($row);
  814:         my %Row   = &Process_OR_Row($row);
  815:         next if (! %Row);
  816:         while (my ($foilid,$href) = each(%Row)) {
  817:             if (! ref($href)) { 
  818:                 $Trydata{$foilid}->[$tries] += $href;
  819:                 next;
  820:             }
  821:             while (my ($option,$value) = each(%$href)) {
  822:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
  823:             }
  824:         }
  825:     }
  826:     return %Trydata;
  827: }
  828: 
  829: #########################################################
  830: #
  831: #     Option Response: Time Analysis
  832: #
  833: #########################################################
  834: sub OR_time_analysis {
  835:     my ($PerformanceData,$ORdata) = @_;
  836:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  837:     if ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils')) {
  838:         $table = '<h3>'.
  839:             &mt('Not enough data for concept analysis.  '.
  840:                 'Performing Foil Analysis').
  841:             '</h3>'.$table;
  842:         $ENV{'form.AnalyzeAs'} = 'Foils';
  843:     }
  844:     my $num_plots = $ENV{'form.NumPlots'};
  845:     my $num_data = scalar(@$PerformanceData)-1;
  846:     my $percent = sprintf('%2f',100/$num_plots);
  847:     #
  848:     $table .= "<table>\n";
  849:     for (my $i=0;$i<$num_plots;$i++) {
  850:         ##
  851:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  852:             ('startdate_'.$i);
  853:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  854:             ('enddate_'.$i);
  855:         if (! defined($starttime) || ! defined($endtime)) {
  856:             my $sec_in_day = 86400;
  857:             my $last_sub_time = &get_time_from_row($PerformanceData->[-1]);
  858:             my ($sday,$smon,$syear);
  859:             (undef,undef,undef,$sday,$smon,$syear) = 
  860:                 localtime($last_sub_time - $sec_in_day*$i);
  861:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
  862:             $endtime = $starttime + $sec_in_day;
  863:             if ($i == ($num_plots -1 )) {
  864:                 $starttime = &get_time_from_row($PerformanceData->[0]);
  865:             }
  866:         }
  867:         my $startdateform = &Apache::lonhtmlcommon::date_setter
  868:             ('Statistics','startdate_'.$i,$starttime);
  869:         my $enddateform = &Apache::lonhtmlcommon::date_setter
  870:             ('Statistics','enddate_'.$i,$endtime);
  871:         #
  872:         my $begin_index;
  873:         my $end_index;
  874:         my $j;
  875:         while (++$j < scalar(@$PerformanceData)) {
  876:             last if (&get_time_from_row($PerformanceData->[$j]) 
  877:                                                               > $starttime);
  878:         }
  879:         $begin_index = $j;
  880:         while (++$j < scalar(@$PerformanceData)) {
  881:             last if (&get_time_from_row($PerformanceData->[$j]) > $endtime);
  882:         }
  883:         $end_index = $j;
  884:         ##
  885:         my $interval = {
  886:             begin_index => $begin_index,
  887:             end_index   => $end_index,
  888:             startdateform => $startdateform,
  889:             enddateform   => $enddateform,
  890:         };
  891:         if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  892:             $table .= &OR_Foil_Time_Analysis($PerformanceData,$ORdata,$Foils,
  893:                                           $interval);
  894:         } else {
  895:             $table .= &OR_Concept_Time_Analysis($PerformanceData,$ORdata,
  896:                                              $Concepts,$interval);
  897:         }
  898:     }
  899:     #
  900:     return $table;
  901: }
  902: 
  903: sub OR_Foil_Time_Analysis {
  904:     my ($PerformanceData,$ORdata,$Foils,$interval) = @_;
  905:     my $analysis_html;
  906:     my $foilkey = &build_option_index($ORdata);
  907:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  908:                                     $interval->{'end_index'});
  909:     my %TimeData;
  910:     #
  911:     # Compute the number getting the foils correct or incorrects
  912:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  913:         my $row = $PerformanceData->[$j];
  914:         next if (! defined($row));
  915:         my %Row = &Process_OR_Row($row);
  916:         while (my ($foilid,$href) = each(%Row)) {
  917:             if (! ref($href)) {
  918:                 $TimeData{$foilid} += $href;
  919:                 next;
  920:             }
  921:             while (my ($option,$value) = each(%$href)) {
  922:                 $TimeData{$foilid}->{$option}+=$value;
  923:             }
  924:         }
  925:     }
  926:     my @Plotdata;
  927:     foreach my $foil (@$Foils) {
  928:         my $total = $TimeData{$foil}->{'_total'};
  929:         if ($total == 0) {
  930:             push(@{$Plotdata[0]},0);
  931:         } else {
  932:             push(@{$Plotdata[0]},
  933:                  100 * $TimeData{$foil}->{'_correct'} / $total);
  934:         }
  935:         my $total_incorrect = $total - $TimeData{$foil}->{'_correct'};
  936:         my $optionidx = 1;
  937:         foreach my $option (@{$ORdata->{'_Options'}}) {
  938:             if ($total_incorrect == 0) {
  939:                 push(@{$Plotdata[$optionidx]},0);
  940:             } else {
  941:                 push(@{$Plotdata[$optionidx]},
  942:                      100 * $TimeData{$foil}->{$option} / $total_incorrect);
  943:             }
  944:         } continue {
  945:             $optionidx++;
  946:         }
  947:     }
  948:     #
  949:     # Create the plot
  950:     my $count = $end_index-$begin_index;
  951:     my $title;
  952:     if ($count == 0) {
  953:         $title = 'no submissions';
  954:     } elsif ($count == 1) {
  955:         $title = 'one submission';
  956:     } else {
  957:         $title = $count.' submissions';
  958:     }
  959:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
  960:                                                        'Foil Number',
  961:                                                        'Percent Correct',
  962:                                                        100,
  963:                                                        $plotcolors,
  964:                                                        undef,
  965:                                                        $Plotdata[0]);
  966:     for (my $j=0; $j< scalar(@{$Plotdata[0]});$j++) {
  967:         $Plotdata[0]->[$j]=0;
  968:     }
  969:     $count = $end_index-$begin_index-$TimeData{'_correct'};
  970:     if ($count == 0) {
  971:         $title = 'no submissions';
  972:     } elsif ($count == 1) {
  973:         $title = 'one submission';
  974:     } else {
  975:         $title = $count.' submissions';
  976:     }
  977:     my $incorrectplot = &Apache::loncommon::DrawBarGraph($title,
  978:                                                  'Foil Number',
  979:                                                  'Incorrect Option Choice',
  980:                                                  100,
  981:                                                  $plotcolors,
  982:                                                  undef,
  983:                                                  @Plotdata);        
  984:     $analysis_html.='<tr>'.
  985:         '<td>'.$correctplot.'</td>'.
  986:         '<td>'.$incorrectplot.'</td>'.
  987:         '<td align="left" valign="top">'.$foilkey.'</td>'."</tr>\n";
  988:     $analysis_html.= '<tr>'.'<td colspan="3">'.
  989:         '<b>Start Time</b>:'.
  990:         ' &nbsp;'.$interval->{'startdateform'}.'<br />'.
  991:         '<b>End Time</b>&nbsp;&nbsp;: '.
  992:         '&nbsp;'.$interval->{'enddateform'}.'<br />'.
  993: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.
  994: #        ("&nbsp;"x3).$interval->{'titleform'}.
  995:         '</td>'.
  996:         "</tr>\n";
  997:     return $analysis_html;
  998: }
  999: 
 1000: sub OR_Concept_Time_Analysis {
 1001:     my ($PerformanceData,$ORdata,$Concepts,$interval) = @_;
 1002:     my $analysis_html;
 1003:     ##
 1004:     ## Determine starttime, endtime, startindex, endindex
 1005:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
 1006:                                     $interval->{'end_index'});
 1007:     my %TimeData;
 1008:     #
 1009:     # Compute the number getting the foils correct or incorrects
 1010:     for (my $j=$begin_index;$j<=$end_index;$j++) {
 1011:         my $row = $PerformanceData->[$j];
 1012:         next if (! defined($row));
 1013:         my %Row = &Process_OR_Row($row);
 1014:         while (my ($foilid,$href) = each(%Row)) {
 1015:             if (! ref($href)) {
 1016:                 $TimeData{$foilid} += $href;
 1017:                 next;
 1018:             }
 1019:             while (my ($option,$value) = each(%$href)) {
 1020:                 $TimeData{$foilid}->{$option}+=$value;
 1021:             }
 1022:         }
 1023:     }
 1024:     #
 1025:     # Put the data in plottable form
 1026:     my @Plotdata;
 1027:     foreach my $concept (@$Concepts) {
 1028:         my ($total,$correct);
 1029:         foreach my $foil (@{$concept->{'foils'}}) {
 1030:             $total += $TimeData{$foil}->{'_total'};
 1031:             $correct += $TimeData{$foil}->{'_correct'};
 1032:         }
 1033:         if ($total == 0) {
 1034:             push(@Plotdata,0);
 1035:         } else {
 1036:             push(@Plotdata,100 * $correct / $total);
 1037:         }
 1038:     }
 1039:     #
 1040:     # Create the plot
 1041:     my $title = ($end_index - $begin_index).' submissions';
 1042:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
 1043:                                                     'Concept Number',
 1044:                                                     'Percent Correct',
 1045:                                                     100,
 1046:                                                     $plotcolors,
 1047:                                                     undef,
 1048:                                                     \@Plotdata);
 1049:     $analysis_html.='<tr>'.
 1050:         '<td>'.$correctplot.'</td>'.
 1051:         '<td align="left" valign="top">'.
 1052:         '<b>Start Time</b>: &nbsp;'.$interval->{'startdateform'}.'<br />'.
 1053:         '<b>End Time</b>&nbsp;&nbsp;: '.
 1054:            '&nbsp;'.$interval->{'enddateform'}.'<br />'.
 1055: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.("&nbsp;"x3).
 1056: #            $interval->{'titleform'}.
 1057:         '</td>'.
 1058:         "</tr>\n";
 1059:     return $analysis_html;
 1060: }
 1061: 
 1062: #########################################################
 1063: #########################################################
 1064: ##
 1065: ##             Excel output 
 1066: ##
 1067: #########################################################
 1068: #########################################################
 1069: sub OR_excel_sheet {
 1070:     my ($r,$resource,$PerformanceData,$ORdata) = @_;
 1071:     my $response = '';
 1072:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
 1073:     #
 1074:     # Create excel worksheet
 1075:     my $filename = '/prtspool/'.
 1076:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1077:         time.'_'.rand(1000000000).'.xls';
 1078:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1079:     if (! defined($workbook)) {
 1080:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1081:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
 1082:                             "This error has been logged.  ".
 1083:                             "Please alert your LON-CAPA administrator").
 1084:                   '</p>');
 1085:         return undef;
 1086:     }
 1087:     #
 1088:     $workbook->set_tempdir('/home/httpd/perl/tmp');
 1089:     my $format = &Apache::loncommon::define_excel_formats($workbook);
 1090:     #
 1091:     # Create and populate main worksheets
 1092:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
 1093:     my $student_data_sheet = &build_student_data_worksheet($workbook,$format);
 1094:     my $response_data_sheet = $workbook->addworksheet('Response Data');
 1095:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
 1096:                        $response_data_sheet) {
 1097:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
 1098:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
 1099:     }
 1100:     #
 1101:     my $result;
 1102:     $result = &OR_build_problem_data_worksheet($problem_data_sheet,$format,
 1103:                                             $Concepts,$ORdata);
 1104:     if ($result ne 'okay') {
 1105:         # Do something useful
 1106:     }
 1107:     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
 1108:                                              $PerformanceData,$Foils,
 1109:                                              $ORdata);
 1110:     if ($result ne 'okay') {
 1111:         # Do something useful
 1112:     }
 1113:     $response_data_sheet->activate();
 1114:     #
 1115:     # Close the excel file
 1116:     $workbook->close();
 1117:     #
 1118:     # Write a link to allow them to download it
 1119:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
 1120:               '<p><a href="'.$filename.'">'.
 1121:               &mt('Your Excel spreadsheet.').
 1122:               '</a></p>'."\n";
 1123:     return $result;
 1124: }
 1125: 
 1126: sub OR_build_problem_data_worksheet {
 1127:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
 1128:     my $rows_output = 3;
 1129:     my $cols_output = 0;
 1130:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
 1131:     ##
 1132:     ##
 1133:     my @Headers;
 1134:     if (@$Concepts > 1) {
 1135:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
 1136:                     'Foil Name','Foil Text','Correct value');
 1137:     } else {
 1138:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
 1139:     }
 1140:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1141:     my %Foildata = %{$ORdata->{'_Foils'}};
 1142:     my $conceptindex = 1;
 1143:     my $foilindex = 1;
 1144:     foreach my $concept (@$Concepts) {
 1145:         my @FoilsInConcept = @{$concept->{'foils'}};
 1146:         my $firstfoil = shift(@FoilsInConcept);
 1147:         if (@$Concepts > 1) {
 1148:             $worksheet->write_row($rows_output++,0,
 1149:                                   [$conceptindex,
 1150:                                    $concept->{'name'},
 1151:                                    $foilindex++,
 1152:                                    $Foildata{$firstfoil}->{'name'},
 1153:                                    $Foildata{$firstfoil}->{'text'},
 1154:                                    $Foildata{$firstfoil}->{'value'},]);
 1155:         } else {
 1156:             $worksheet->write_row($rows_output++,0,
 1157:                                   [ $foilindex++,
 1158:                                     $Foildata{$firstfoil}->{'name'},
 1159:                                     $Foildata{$firstfoil}->{'text'},
 1160:                                     $Foildata{$firstfoil}->{'value'},]);
 1161:         }
 1162:         foreach my $foilid (@FoilsInConcept) {
 1163:             if (@$Concepts > 1) {
 1164:                 $worksheet->write_row($rows_output++,0,
 1165:                                       ['',
 1166:                                        '',
 1167:                                        $foilindex,
 1168:                                        $Foildata{$foilid}->{'name'},
 1169:                                        $Foildata{$foilid}->{'text'},
 1170:                                        $Foildata{$foilid}->{'value'},]);
 1171:             } else {
 1172:                 $worksheet->write_row($rows_output++,0,                
 1173:                                       [$foilindex,
 1174:                                        $Foildata{$foilid}->{'name'},
 1175:                                        $Foildata{$foilid}->{'text'},
 1176:                                        $Foildata{$foilid}->{'value'},]);
 1177:             }                
 1178:         } continue {
 1179:             $foilindex++;
 1180:         }
 1181:     } continue {
 1182:         $conceptindex++;
 1183:     }
 1184:     $rows_output++;
 1185:     $rows_output++;
 1186:     ##
 1187:     ## Option data output
 1188:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
 1189:     foreach my $string (@{$ORdata->{'_Options'}}) {
 1190:         $worksheet->write($rows_output++,0,$string);
 1191:     }
 1192:     return 'okay';
 1193: }
 1194: 
 1195: sub OR_build_response_data_worksheet {
 1196:     my ($worksheet,$format,$PerformanceData,$Foils,$ORdata)=@_;
 1197:     my $rows_output = 3;
 1198:     my $cols_output = 0;
 1199:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
 1200:     $worksheet->set_column(1,1,20);
 1201:     $worksheet->set_column(2,2,13);
 1202:     my @Headers = ('identifier','time','award detail','attempt');
 1203:     foreach my $foil (@$Foils) {
 1204:         push (@Headers,$foil.' submission');
 1205:         push (@Headers,$foil.' grading');
 1206:     }
 1207:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1208:     #
 1209:     foreach my $row (@$PerformanceData) {
 1210:         next if (! defined($row));
 1211:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
 1212:         my @Foilgrades = split('&',$grading);
 1213:         my @Foilsubs   = split('&',$submission);
 1214:         my %ResponseData;
 1215:         for (my $j=0;$j<=$#Foilgrades;$j++) {
 1216:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1217:             my (undef,$submission) = split('=',$Foilsubs[$j]);
 1218:             $submission = &Apache::lonnet::unescape($submission);
 1219:             $ResponseData{$foilid.' submission'}=$submission;
 1220:             $ResponseData{$foilid.' award'}=$correct;
 1221:         }
 1222:         $worksheet->write($rows_output,$cols_output++,$student);
 1223:         $worksheet->write($rows_output,$cols_output++,
 1224:              &Apache::lonstathelpers::calc_serial($time),$format->{'date'});
 1225:         $worksheet->write($rows_output,$cols_output++,$award);
 1226:         $worksheet->write($rows_output,$cols_output++,$tries);
 1227:         foreach my $foilid (@$Foils) {
 1228:             $worksheet->write($rows_output,$cols_output++,
 1229:                               $ResponseData{$foilid.' submission'});
 1230:             $worksheet->write($rows_output,$cols_output++,
 1231:                               $ResponseData{$foilid.' award'});
 1232:         }
 1233:         $rows_output++;
 1234:         $cols_output = 0;
 1235:     }
 1236:     return;
 1237: }
 1238: 
 1239: sub build_foil_index {
 1240:     my ($ORdata) = @_;
 1241:     return if (! exists($ORdata->{'_Foils'}));
 1242:     my %Foildata = %{$ORdata->{'_Foils'}};
 1243:     my @Foils = sort(keys(%Foildata));
 1244:     my %Concepts;
 1245:     foreach my $foilid (@Foils) {
 1246:         push(@{$Concepts{$Foildata{$foilid}->{'_Concept'}}},
 1247:              $foilid);
 1248:     }
 1249:     undef(@Foils);
 1250:     # Having gathered the concept information in a hash, we now translate it
 1251:     # into an array because we need to be consistent about order.
 1252:     # Also put the foils in order, too.
 1253:     my $sortfunction = sub {
 1254:         my %Numbers = (one   => 1,
 1255:                        two   => 2,
 1256:                        three => 3,
 1257:                        four  => 4,
 1258:                        five  => 5,
 1259:                        six   => 6,
 1260:                        seven => 7,
 1261:                        eight => 8,
 1262:                        nine  => 9,
 1263:                        ten   => 10,);
 1264:         my $a1 = lc($a); 
 1265:         my $b1 = lc($b);
 1266:         if (exists($Numbers{$a1})) {
 1267:             $a1 = $Numbers{$a1};
 1268:         }
 1269:         if (exists($Numbers{$b1})) {
 1270:             $b1 = $Numbers{$b1};
 1271:         }
 1272:         if (($a1 =~/^\d+$/) && ($b1 =~/^\d+$/)) {
 1273:             return $a1 <=> $b1;
 1274:         } else {
 1275:             return $a1 cmp $b1;
 1276:         }
 1277:     };
 1278:     my @Concepts;
 1279:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
 1280:         if (! defined($Concepts{$concept})) {
 1281:             $Concepts{$concept}=[];
 1282: #            next;
 1283:         }
 1284:         push(@Concepts,{ name => $concept,
 1285:                         foils => [@{$Concepts{$concept}}]});
 1286:         push(@Foils,(@{$Concepts{$concept}}));
 1287:     }
 1288:     #
 1289:     # Build up the table of row labels.
 1290:     my $table = '<table border="1" >'."\n";
 1291:     if (@Concepts > 1) {
 1292:         $table .= '<tr>'.
 1293:             '<th>'.&mt('Concept Number').'</th>'.
 1294:             '<th>'.&mt('Concept').'</th>'.
 1295:             '<th>'.&mt('Foil Number').'</th>'.
 1296:             '<th>'.&mt('Foil Name').'</th>'.
 1297:             '<th>'.&mt('Foil Text').'</th>'.
 1298:             '<th>'.&mt('Correct Value').'</th>'.
 1299:             "</tr>\n";
 1300:     } else {
 1301:         $table .= '<tr>'.
 1302:             '<th>'.&mt('Foil Number').'</th>'.
 1303:             '<th>'.&mt('Foil Name').'</th>'.
 1304:             '<th>'.&mt('Foil Text').'</th>'.
 1305:             '<th>'.&mt('Correct Value').'</th>'.
 1306:             "</tr>\n";
 1307:     }        
 1308:     my $conceptindex = 1;
 1309:     my $foilindex = 1;
 1310:     foreach my $concept (@Concepts) {
 1311:         my @FoilsInConcept = @{$concept->{'foils'}};
 1312:         my $firstfoil = shift(@FoilsInConcept);
 1313:         if (@Concepts > 1) {
 1314:             $table .= '<tr>'.
 1315:                 '<td>'.$conceptindex.'</td>'.
 1316:                 '<td>'.&HTML::Entities::encode($concept->{'name'}).'</td>'.
 1317:                 '<td>'.$foilindex++.'</td>'.
 1318:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'}).'</td>'.
 1319:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1320:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'}).'</td>'.
 1321:                 "</tr>\n";
 1322:         } else {
 1323:             $table .= '<tr>'.
 1324:                 '<td>'.$foilindex++.'</td>'.
 1325:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'}).'</td>'.
 1326:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1327:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'}).'</td>'.
 1328:                 "</tr>\n";
 1329:         }
 1330:         foreach my $foilid (@FoilsInConcept) {
 1331:             if (@Concepts > 1) {
 1332:                 $table .= '<tr>'.
 1333:                     '<td></td>'.
 1334:                     '<td></td>'.
 1335:                     '<td>'.$foilindex.'</td>'.
 1336:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'}).'</td>'.
 1337:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1338:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'}).'</td>'.
 1339:                     "</tr>\n";
 1340:             } else {
 1341:                 $table .= '<tr>'.
 1342:                     '<td>'.$foilindex.'</td>'.
 1343:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'}).'</td>'.
 1344:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1345:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'}).'</td>'.
 1346:                     "</tr>\n";
 1347:             }                
 1348:         } continue {
 1349:             $foilindex++;
 1350:         }
 1351:     } continue {
 1352:         $conceptindex++;
 1353:     }
 1354:     $table .= "</table>\n";
 1355:     #
 1356:     # Build option index with color stuff
 1357:     return ($table,\@Foils,\@Concepts);
 1358: }
 1359: 
 1360: sub build_option_index {
 1361:     my ($ORdata)= @_;
 1362:     my $table = "<table>\n";
 1363:     my $optionindex = 0;
 1364:     my @Rows;
 1365:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'_Options'}}) {
 1366:         push (@Rows,
 1367:               '<tr>'.
 1368:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
 1369:               ('&nbsp;'x4).'</td>'.
 1370:               '<td>'.&HTML::Entities::encode($option).'</td>'.
 1371:               "</tr>\n");
 1372:     }
 1373:     shift(@Rows); # Throw away 'correct option chosen' color
 1374:     $table .= join('',reverse(@Rows));
 1375:     $table .= "</table>\n";
 1376: }
 1377: 
 1378: #########################################################
 1379: #########################################################
 1380: ##
 1381: ##   Generic Interface Routines
 1382: ##
 1383: #########################################################
 1384: #########################################################
 1385: sub CreateInterface {
 1386:     ##
 1387:     ## Environment variable initialization
 1388:     if (! exists$ENV{'form.AnalyzeOver'}) {
 1389:         $ENV{'form.AnalyzeOver'} = 'Tries';
 1390:     }
 1391:     ##
 1392:     ## Build the menu
 1393:     my $Str = '';
 1394:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
 1395:         (undef,'Detailed Problem Analysis');
 1396:     $Str .= '<table cellspacing="5">'."\n";
 1397:     $Str .= '<tr>';
 1398:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
 1399:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
 1400: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
 1401:     $Str .= '<td align="center">&nbsp;</td>';
 1402:     $Str .= '</tr>'."\n";
 1403:     ##
 1404:     ## 
 1405:     $Str .= '<tr><td align="center">'."\n";
 1406:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1407:     $Str .= '</td>';
 1408:     #
 1409:     $Str .= '<td align="center">';
 1410:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1411:     $Str .= '</td>';
 1412:     #
 1413: #    $Str .= '<td align="center">';
 1414:     my $only_seq_with_assessments = sub { 
 1415:         my $s=shift;
 1416:         if ($s->{'num_assess'} < 1) { 
 1417:             return 0;
 1418:         } else { 
 1419:             return 1;
 1420:         }
 1421:     };
 1422:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
 1423:                                               $only_seq_with_assessments);
 1424:     ##
 1425:     ##
 1426:     $Str .= '<td>';
 1427:     { # These braces are here to organize the code, not scope it.
 1428:         {
 1429:             $Str .= '<nobr>'.&mt('Analyze Over ');
 1430:             $Str .= &Apache::loncommon::help_open_topic
 1431:                                                   ('Analysis_Analyze_Over');
 1432:             $Str .='<select name="AnalyzeOver" >';
 1433:             $Str .= '<option value="Tries" ';
 1434:             if (! exists($ENV{'form.AnalyzeOver'}) || 
 1435:                 $ENV{'form.AnalyzeOver'} eq 'Tries'){
 1436:                 # Default to Tries
 1437:                 $Str .= ' selected ';
 1438:             }
 1439:             $Str .= '>'.&mt('Tries').'</option>';
 1440:             $Str .= '<option value="Time" ';
 1441:             $Str .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'Time');
 1442:             $Str .= '>'.&mt('Time').'</option>';
 1443:             $Str .= '</select>';
 1444:             $Str .= '</nobr><br />';
 1445:         }
 1446:         {
 1447:             $Str .= '<nobr>'.&mt('Analyze as ');
 1448:             $Str .= &Apache::loncommon::help_open_topic
 1449:                                                   ('Analysis_Analyze_as');
 1450:             $Str .='<select name="AnalyzeAs" >';
 1451:             $Str .= '<option value="Concepts" ';
 1452:             if (! exists($ENV{'form.AnalyzeAs'}) || 
 1453:                 $ENV{'form.AnalyzeAs'} eq 'Concepts'){
 1454:                 # Default to Concepts
 1455:                 $Str .= ' selected ';
 1456:             }
 1457:             $Str .= '>'.&mt('Concepts').'</option>';
 1458:             $Str .= '<option value="Foils" ';
 1459:             $Str .= ' selected ' if ($ENV{'form.AnalyzeAs'} eq 'Foils');
 1460:             $Str .= '>'.&mt('Foils').'</option>';
 1461:             $Str .= '</select></nobr><br />';
 1462:         }
 1463:         {
 1464:             $Str .= '<br /><nobr>'.&mt('Number of Plots:');
 1465:             $Str .= &Apache::loncommon::help_open_topic
 1466:                                                   ('Analysis_num_plots');
 1467:             $Str .= '<select name="NumPlots">';
 1468:             if (! exists($ENV{'form.NumPlots'}) 
 1469:                 || $ENV{'form.NumPlots'} < 1 
 1470:                 || $ENV{'form.NumPlots'} > 20) {
 1471:                 $ENV{'form.NumPlots'} = 5;
 1472:             }
 1473:             foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
 1474:                 $Str .= '<option value="'.$i.'" ';
 1475:                 if ($ENV{'form.NumPlots'} == $i) { $Str.=' selected '; }
 1476:                 $Str .= '>'.$i.'</option>';
 1477:             }
 1478:             $Str .= '</select></nobr>';
 1479:         }
 1480:     }
 1481:     $Str .= '</td>';
 1482:     ##
 1483:     ##
 1484:     $Str .= '</tr>'."\n";
 1485:     $Str .= '</table>'."\n";
 1486:     return $Str;
 1487: }
 1488: 
 1489: #########################################################
 1490: #########################################################
 1491: ##
 1492: ##              Misc Option Response functions
 1493: ##
 1494: #########################################################
 1495: #########################################################
 1496: sub get_time_from_row {
 1497:     my ($row) = @_;
 1498:     if (ref($row)) {
 1499:         return $row->[&Apache::loncoursedata::RD_timestamp()];
 1500:     } 
 1501:     return undef;
 1502: }
 1503: 
 1504: sub get_tries_from_row {
 1505:     my ($row) = @_;
 1506:     if (ref($row)) {
 1507:         return $row->[&Apache::loncoursedata::RD_tries()];
 1508:     }
 1509:     return undef;
 1510: }
 1511: 
 1512: sub hashify_attempt {
 1513:     my ($row) = @_;
 1514:     my %attempt;
 1515:     $attempt{'tries'}      = $row->[&Apache::loncoursedata::RD_tries()];
 1516:     $attempt{'submission'} = $row->[&Apache::loncoursedata::RD_submission()];
 1517:     $attempt{'award'}      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1518:     $attempt{'timestamp'}  = $row->[&Apache::loncoursedata::RD_timestamp()];
 1519:     return %attempt;
 1520: }
 1521: 
 1522: sub Process_OR_Row {
 1523:     my ($row) = @_;
 1524:     my %RowData;
 1525:     my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 1526:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1527:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
 1528:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
 1529:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
 1530:     my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
 1531:     return undef if ($award eq 'MISSING_ANSWER');
 1532:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
 1533:         $RowData{'_correct'} = 1;
 1534:     }
 1535:     $RowData{'_total'} = 1;
 1536:     my @Foilgrades = split('&',$grading);
 1537:     my @Foilsubs   = split('&',$submission);
 1538:     for (my $j=0;$j<=$#Foilgrades;$j++) {
 1539:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1540:         $foilid = &Apache::lonnet::unescape($foilid);
 1541:         my (undef,$submission) = split('=',$Foilsubs[$j]);
 1542:         if ($correct) {
 1543:             $RowData{$foilid}->{'_correct'}++;
 1544:         } else {
 1545:             $submission = &Apache::lonnet::unescape($submission);
 1546:             $RowData{$foilid}->{$submission}++;
 1547:         }
 1548:         $RowData{$foilid}->{'_total'}++;
 1549:     }
 1550:     return %RowData;
 1551: }
 1552: 
 1553: 1;
 1554: 
 1555: __END__

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