File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.78: download - view: text, annotated - select for diffs
Fri Mar 12 20:29:48 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Reworked analysis of numerical responses.  Uses <drawimage> and &xmlparse
to create a chart of answers.

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

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