File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.72: download - view: text, annotated - select for diffs
Fri Feb 20 16:24:20 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Modified to use &Apache::htmlcommon breadcrumb routines.

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

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