File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.87: download - view: text, annotated - select for diffs
Thu Jun 24 18:47:36 2004 UTC (19 years, 11 months ago) by matthew
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
Removing access to numerical and radio response analysis for 1.2 release.

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

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