File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.80: download - view: text, annotated - select for diffs
Tue Mar 16 16:30:31 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
lonstathelpers: added local caching of computed answers.
lonproblemanalysis and lonstudentsubmissions: modified to begin using the
caching of computed answers.

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

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