File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.81: download - view: text, annotated - select for diffs
Tue Mar 16 16:41:26 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Moved &GetStudentAnswers from lonstudentsubmissions and lonproblemanalysis
to lonstathelpers.

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

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