File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.76: download - view: text, annotated - select for diffs
Thu Mar 11 20:06:50 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
OR_Tries_Foil_Analysis: Added a blank column between concepts on the plots.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemanalysis.pm,v 1.76 2004/03/11 20:06:50 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)';
  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,$Concepts,
  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,$Concepts,$respdat,$ORdata) = @_;
  567:     my %ResponseData = %$respdat;
  568:     #
  569:     # Compute the data neccessary to make the plots
  570:     my @PlotData; 
  571:     foreach my $concept (@$Concepts) {
  572:         foreach my $foilid (@{$concept->{'foils'}}) {
  573:             for (my $try=$mintries;$try<=$maxtries;$try++) {
  574:                 if ($ResponseData{$foilid}->[$try]->{'_total'} == 0) {
  575:                     push(@{$PlotData[$try]->{'_correct'}},0);
  576:                 } else {
  577:                     push(@{$PlotData[$try]->{'_correct'}},
  578:                          100*$ResponseData{$foilid}->[$try]->{'_correct'}/
  579:                          $ResponseData{$foilid}->[$try]->{'_total'});
  580:                 }
  581:                 foreach my $option (@{$ORdata->{'_Options'}}) {
  582:                     push(@{$PlotData[$try]->{'_total'}},
  583:                          $ResponseData{$foilid}->[$try]->{'_total'});
  584:                     if ($ResponseData{$foilid}->[$try]->{'_total'} == 0) {
  585:                         push (@{$PlotData[$try]->{$option}},0);
  586:                     } else {
  587:                         if ($ResponseData{$foilid}->[$try]->{'_total'} ==
  588:                             $ResponseData{$foilid}->[$try]->{'_correct'}) {
  589:                             push(@{$PlotData[$try]->{$option}},0);
  590:                         } else {
  591:                             push (@{$PlotData[$try]->{$option}},
  592:                                   100 * 
  593:                                   $ResponseData{$foilid}->[$try]->{$option} / 
  594:                                   ($ResponseData{$foilid}->[$try]->{'_total'} 
  595:                                    - 
  596:                                    $ResponseData{$foilid}->[$try]->{'_correct'}
  597:                                    ));
  598:                         }
  599:                     }
  600:                 } # End of foreach my $option
  601:             }
  602:         } # End of foreach my $foilid
  603:     } # End of foreach my $concept
  604:     # 
  605:     # Build a table for the plots
  606:     my $analysis_html = "<table>\n";
  607:     my $optionkey = &build_option_index($ORdata);
  608:     for (my $try=$mintries;$try<=$maxtries;$try++) {
  609:         my $count = $ResponseData{'_total'}->[$try];
  610:         if ($count == 0) {
  611:             $count = 'no submissions';
  612:         } elsif ($count == 1) {
  613:             $count = '1 submission';
  614:         } else {
  615:             $count = $count.' submissions';
  616:         }
  617:         my $title = 'Attempt '.$try.', '.$count;
  618:         my @Datasets;
  619:         foreach my $option ('_correct',@{$ORdata->{'_Options'}}) {
  620:             next if (! exists($PlotData[$try]->{$option}));
  621:             push(@Datasets,$PlotData[$try]->{$option});
  622:         }
  623:         #
  624:         # Put a blank in the data set between concepts
  625:         for (my $set =0;$set<=$#Datasets;$set++) {
  626:             my @Data = @{$Datasets[$set]};
  627:             my $idx = 0;
  628:             foreach my $concept (@{$Concepts}) {
  629:                 foreach my $foilid (@{$concept->{'foils'}}) {
  630:                     $Datasets[$set]->[$idx++]=shift(@Data);
  631:                 }
  632:                 if ($concept->{'name'} ne $Concepts->[-1]->{'name'}) {
  633:                     $Datasets[$set]->[$idx++] = 0;
  634:                 }
  635:             }
  636:         }
  637:         #
  638:         # Set up the labels needed for the bar graph
  639:         my @Labels;
  640:         my $idx = 1;
  641:         foreach my $concept (@{$Concepts}) {
  642:             foreach my $foilid (@{$concept->{'foils'}}) {
  643:                 push(@Labels,$idx++);
  644:             }
  645:             push(@Labels,'');
  646:         }
  647:         #
  648:         my $correctgraph = &Apache::loncommon::DrawBarGraph
  649:             ($title,'Foil Number','Percent Correct',
  650:              100,$plotcolors,\@Labels,$Datasets[0]);
  651:         $analysis_html.= '<tr><td>'.$correctgraph.'</td>';
  652:         
  653:         #
  654:         #
  655:         next if (! defined($Datasets[0]));
  656:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
  657:             $Datasets[0]->[$i]=0;
  658:         }
  659:         $count = $ResponseData{'_total'}->[$try] - 
  660:                                            $ResponseData{'_correct'}->[$try];
  661:         if ($count == 0) {
  662:             $count = 'no submissions';
  663:         } elsif ($count == 1) {
  664:             $count = '1 submission';
  665:         } else {
  666:             $count = $count.' submissions';
  667:         }
  668:         $title = 'Attempt '.$try.', '.$count;
  669:         my $incorrectgraph = &Apache::loncommon::DrawBarGraph
  670:             ($title,'Foil Number','% Option Chosen Incorrectly',
  671:              100,$plotcolors,\@Labels,@Datasets);
  672:         $analysis_html.= '<td>'.$incorrectgraph.'</td>';
  673:         $analysis_html.= '<td>'.$optionkey."<td></tr>\n";
  674:     }
  675:     $analysis_html .= "</table>\n";
  676:     return $analysis_html;
  677: }
  678: 
  679: sub OR_Tries_Concept_Analysis {
  680:     my ($mintries,$maxtries,$Concepts,$respdat,$ORdata) = @_;
  681:     my %ResponseData = %$respdat;
  682:     my $analysis_html = "<table>\n";
  683:     #
  684:     # Compute the data neccessary to make the plots
  685:     my @PlotData;
  686:     # Concept analysis
  687:     #
  688:     # Note: we do not bother with characterizing the students incorrect
  689:     # answers at the concept level because an incorrect answer for one foil
  690:     # may be a correct answer for another foil.
  691:     my %ConceptData;
  692:     foreach my $concept (@{$Concepts}) {
  693:         for (my $i=$mintries;$i<=$maxtries;$i++) {
  694:             #
  695:             # Gather the per-attempt data
  696:             my $cdata = $ConceptData{$concept}->[$i];
  697:             foreach my $foilid (@{$concept->{'foils'}}) {
  698:                 $cdata->{'_correct'} += 
  699:                     $ResponseData{$foilid}->[$i]->{'_correct'};
  700:                 $cdata->{'_total'}   += 
  701:                     $ResponseData{$foilid}->[$i]->{'_total'};
  702:             }
  703:             push (@{$PlotData[$i]->{'_total'}},$cdata->{'_total'});
  704:             if ($cdata->{'_total'} == 0) {
  705:                 push (@{$PlotData[$i]->{'_correct'}},0);
  706:             } else {
  707:                 push (@{$PlotData[$i]->{'_correct'}},
  708:                       100*$cdata->{'_correct'}/$cdata->{'_total'});
  709:                 }
  710:         }
  711:     }
  712:     # Build a table for the plots
  713:     for (my $i=$mintries;$i<=$maxtries;$i++) {
  714:         my $minstu = $PlotData[$i]->{'_total'}->[0];
  715:         my $maxstu = $PlotData[$i]->{'_total'}->[0];
  716:         foreach my $count (@{$PlotData[$i]->{'_total'}}) {
  717:             if ($minstu > $count) {
  718:                 $minstu = $count;
  719:             }
  720:             if ($maxstu < $count) {
  721:                 $maxstu = $count;
  722:             }
  723:         }
  724:         $maxstu = 0 if (! defined($maxstu));
  725:         $minstu = 0 if (! defined($minstu));
  726:         my $title;
  727:         my $count = $ResponseData{'_total'}->[$i];
  728:         if ($count == 0) {
  729:             $count = 'no submissions';
  730:         } elsif ($count == 1) {
  731:             $count = '1 submission';
  732:         } else {
  733:             $count = $count.' submissions';
  734:         }
  735:         $title = 'Attempt '.$i.', '.$count;
  736:         my $graphlink = &Apache::loncommon::DrawBarGraph
  737:             ($title,'Concept Number','Percent Correct',
  738:              100,$plotcolors,undef,$PlotData[$i]->{'_correct'});
  739:         $analysis_html.= '<tr><td>'.$graphlink."</td></tr>\n";
  740:     }
  741:     $analysis_html .= "</table>\n";
  742:     return $analysis_html;
  743: }
  744: 
  745: sub OR_analyze_by_tries {
  746:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
  747:     my %Trydata;
  748:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
  749:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
  750:     foreach my $row (@$PerformanceData) {
  751:         next if (! defined($row));
  752:         my $tries = &get_tries_from_row($row);
  753:         my %Row   = &Process_OR_Row($row);
  754:         next if (! %Row);
  755:         while (my ($foilid,$href) = each(%Row)) {
  756:             if (! ref($href)) { 
  757:                 $Trydata{$foilid}->[$tries] += $href;
  758:                 next;
  759:             }
  760:             while (my ($option,$value) = each(%$href)) {
  761:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
  762:             }
  763:         }
  764:     }
  765:     return %Trydata;
  766: }
  767: 
  768: #########################################################
  769: #
  770: #     Option Response: Time Analysis
  771: #
  772: #########################################################
  773: sub OR_time_analysis {
  774:     my ($PerformanceData,$ORdata) = @_;
  775:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  776:     if ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils')) {
  777:         $table = '<h3>'.
  778:             &mt('Not enough data for concept analysis.  '.
  779:                 'Performing Foil Analysis').
  780:             '</h3>'.$table;
  781:         $ENV{'form.AnalyzeAs'} = 'Foils';
  782:     }
  783:     my $num_plots = $ENV{'form.NumPlots'};
  784:     my $num_data = scalar(@$PerformanceData)-1;
  785:     my $percent = sprintf('%2f',100/$num_plots);
  786:     #
  787:     $table .= "<table>\n";
  788:     for (my $i=0;$i<$num_plots;$i++) {
  789:         ##
  790:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  791:             ('startdate_'.$i);
  792:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  793:             ('enddate_'.$i);
  794:         if (! defined($starttime) || ! defined($endtime)) {
  795:             my $sec_in_day = 86400;
  796:             my $last_sub_time = &get_time_from_row($PerformanceData->[-1]);
  797:             my ($sday,$smon,$syear);
  798:             (undef,undef,undef,$sday,$smon,$syear) = 
  799:                 localtime($last_sub_time - $sec_in_day*$i);
  800:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
  801:             $endtime = $starttime + $sec_in_day;
  802:             if ($i == ($num_plots -1 )) {
  803:                 $starttime = &get_time_from_row($PerformanceData->[0]);
  804:             }
  805:         }
  806:         my $startdateform = &Apache::lonhtmlcommon::date_setter
  807:             ('Statistics','startdate_'.$i,$starttime);
  808:         my $enddateform = &Apache::lonhtmlcommon::date_setter
  809:             ('Statistics','enddate_'.$i,$endtime);
  810:         #
  811:         my $begin_index;
  812:         my $end_index;
  813:         my $j;
  814:         while (++$j < scalar(@$PerformanceData)) {
  815:             last if (&get_time_from_row($PerformanceData->[$j]) 
  816:                                                               > $starttime);
  817:         }
  818:         $begin_index = $j;
  819:         while (++$j < scalar(@$PerformanceData)) {
  820:             last if (&get_time_from_row($PerformanceData->[$j]) > $endtime);
  821:         }
  822:         $end_index = $j;
  823:         ##
  824:         my $interval = {
  825:             begin_index => $begin_index,
  826:             end_index   => $end_index,
  827:             startdateform => $startdateform,
  828:             enddateform   => $enddateform,
  829:         };
  830:         if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  831:             $table .= &OR_Foil_Time_Analysis($PerformanceData,$ORdata,$Foils,
  832:                                           $interval);
  833:         } else {
  834:             $table .= &OR_Concept_Time_Analysis($PerformanceData,$ORdata,
  835:                                              $Concepts,$interval);
  836:         }
  837:     }
  838:     #
  839:     return $table;
  840: }
  841: 
  842: sub OR_Foil_Time_Analysis {
  843:     my ($PerformanceData,$ORdata,$Foils,$interval) = @_;
  844:     my $analysis_html;
  845:     my $foilkey = &build_option_index($ORdata);
  846:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  847:                                     $interval->{'end_index'});
  848:     my %TimeData;
  849:     #
  850:     # Compute the number getting the foils correct or incorrects
  851:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  852:         my $row = $PerformanceData->[$j];
  853:         next if (! defined($row));
  854:         my %Row = &Process_OR_Row($row);
  855:         while (my ($foilid,$href) = each(%Row)) {
  856:             if (! ref($href)) {
  857:                 $TimeData{$foilid} += $href;
  858:                 next;
  859:             }
  860:             while (my ($option,$value) = each(%$href)) {
  861:                 $TimeData{$foilid}->{$option}+=$value;
  862:             }
  863:         }
  864:     }
  865:     my @Plotdata;
  866:     foreach my $foil (@$Foils) {
  867:         my $total = $TimeData{$foil}->{'_total'};
  868:         if ($total == 0) {
  869:             push(@{$Plotdata[0]},0);
  870:         } else {
  871:             push(@{$Plotdata[0]},
  872:                  100 * $TimeData{$foil}->{'_correct'} / $total);
  873:         }
  874:         my $total_incorrect = $total - $TimeData{$foil}->{'_correct'};
  875:         my $optionidx = 1;
  876:         foreach my $option (@{$ORdata->{'_Options'}}) {
  877:             if ($total_incorrect == 0) {
  878:                 push(@{$Plotdata[$optionidx]},0);
  879:             } else {
  880:                 push(@{$Plotdata[$optionidx]},
  881:                      100 * $TimeData{$foil}->{$option} / $total_incorrect);
  882:             }
  883:         } continue {
  884:             $optionidx++;
  885:         }
  886:     }
  887:     #
  888:     # Create the plot
  889:     my $count = $end_index-$begin_index;
  890:     my $title;
  891:     if ($count == 0) {
  892:         $title = 'no submissions';
  893:     } elsif ($count == 1) {
  894:         $title = 'one submission';
  895:     } else {
  896:         $title = $count.' submissions';
  897:     }
  898:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
  899:                                                        'Foil Number',
  900:                                                        'Percent Correct',
  901:                                                        100,
  902:                                                        $plotcolors,
  903:                                                        undef,
  904:                                                        $Plotdata[0]);
  905:     for (my $j=0; $j< scalar(@{$Plotdata[0]});$j++) {
  906:         $Plotdata[0]->[$j]=0;
  907:     }
  908:     $count = $end_index-$begin_index-$TimeData{'_correct'};
  909:     if ($count == 0) {
  910:         $title = 'no submissions';
  911:     } elsif ($count == 1) {
  912:         $title = 'one submission';
  913:     } else {
  914:         $title = $count.' submissions';
  915:     }
  916:     my $incorrectplot = &Apache::loncommon::DrawBarGraph($title,
  917:                                                  'Foil Number',
  918:                                                  'Incorrect Option Choice',
  919:                                                  100,
  920:                                                  $plotcolors,
  921:                                                  undef,
  922:                                                  @Plotdata);        
  923:     $analysis_html.='<tr>'.
  924:         '<td>'.$correctplot.'</td>'.
  925:         '<td>'.$incorrectplot.'</td>'.
  926:         '<td align="left" valign="top">'.$foilkey.'</td>'."</tr>\n";
  927:     $analysis_html.= '<tr>'.'<td colspan="3">'.
  928:         '<b>Start Time</b>:'.
  929:         ' &nbsp;'.$interval->{'startdateform'}.'<br />'.
  930:         '<b>End Time</b>&nbsp;&nbsp;: '.
  931:         '&nbsp;'.$interval->{'enddateform'}.'<br />'.
  932: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.
  933: #        ("&nbsp;"x3).$interval->{'titleform'}.
  934:         '</td>'.
  935:         "</tr>\n";
  936:     return $analysis_html;
  937: }
  938: 
  939: sub OR_Concept_Time_Analysis {
  940:     my ($PerformanceData,$ORdata,$Concepts,$interval) = @_;
  941:     my $analysis_html;
  942:     ##
  943:     ## Determine starttime, endtime, startindex, endindex
  944:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  945:                                     $interval->{'end_index'});
  946:     my %TimeData;
  947:     #
  948:     # Compute the number getting the foils correct or incorrects
  949:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  950:         my $row = $PerformanceData->[$j];
  951:         next if (! defined($row));
  952:         my %Row = &Process_OR_Row($row);
  953:         while (my ($foilid,$href) = each(%Row)) {
  954:             if (! ref($href)) {
  955:                 $TimeData{$foilid} += $href;
  956:                 next;
  957:             }
  958:             while (my ($option,$value) = each(%$href)) {
  959:                 $TimeData{$foilid}->{$option}+=$value;
  960:             }
  961:         }
  962:     }
  963:     #
  964:     # Put the data in plottable form
  965:     my @Plotdata;
  966:     foreach my $concept (@$Concepts) {
  967:         my ($total,$correct);
  968:         foreach my $foil (@{$concept->{'foils'}}) {
  969:             $total += $TimeData{$foil}->{'_total'};
  970:             $correct += $TimeData{$foil}->{'_correct'};
  971:         }
  972:         if ($total == 0) {
  973:             push(@Plotdata,0);
  974:         } else {
  975:             push(@Plotdata,100 * $correct / $total);
  976:         }
  977:     }
  978:     #
  979:     # Create the plot
  980:     my $title = ($end_index - $begin_index).' submissions';
  981:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
  982:                                                     'Concept Number',
  983:                                                     'Percent Correct',
  984:                                                     100,
  985:                                                     $plotcolors,
  986:                                                     undef,
  987:                                                     \@Plotdata);
  988:     $analysis_html.='<tr>'.
  989:         '<td>'.$correctplot.'</td>'.
  990:         '<td align="left" valign="top">'.
  991:         '<b>Start Time</b>: &nbsp;'.$interval->{'startdateform'}.'<br />'.
  992:         '<b>End Time</b>&nbsp;&nbsp;: '.
  993:            '&nbsp;'.$interval->{'enddateform'}.'<br />'.
  994: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.("&nbsp;"x3).
  995: #            $interval->{'titleform'}.
  996:         '</td>'.
  997:         "</tr>\n";
  998:     return $analysis_html;
  999: }
 1000: 
 1001: #########################################################
 1002: #########################################################
 1003: ##
 1004: ##             Excel output 
 1005: ##
 1006: #########################################################
 1007: #########################################################
 1008: sub OR_excel_sheet {
 1009:     my ($r,$resource,$PerformanceData,$ORdata) = @_;
 1010:     my $response = '';
 1011:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
 1012:     #
 1013:     # Create excel worksheet
 1014:     my $filename = '/prtspool/'.
 1015:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1016:         time.'_'.rand(1000000000).'.xls';
 1017:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1018:     if (! defined($workbook)) {
 1019:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1020:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
 1021:                             "This error has been logged.  ".
 1022:                             "Please alert your LON-CAPA administrator").
 1023:                   '</p>');
 1024:         return undef;
 1025:     }
 1026:     #
 1027:     $workbook->set_tempdir('/home/httpd/perl/tmp');
 1028:     my $format = &Apache::loncommon::define_excel_formats($workbook);
 1029:     #
 1030:     # Create and populate main worksheets
 1031:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
 1032:     my $student_data_sheet = &build_student_data_worksheet($workbook,$format);
 1033:     my $response_data_sheet = $workbook->addworksheet('Response Data');
 1034:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
 1035:                        $response_data_sheet) {
 1036:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
 1037:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
 1038:     }
 1039:     #
 1040:     my $result;
 1041:     $result = &OR_build_problem_data_worksheet($problem_data_sheet,$format,
 1042:                                             $Concepts,$ORdata);
 1043:     if ($result ne 'okay') {
 1044:         # Do something useful
 1045:     }
 1046:     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
 1047:                                              $PerformanceData,$Foils,
 1048:                                              $ORdata);
 1049:     if ($result ne 'okay') {
 1050:         # Do something useful
 1051:     }
 1052:     $response_data_sheet->activate();
 1053:     #
 1054:     # Close the excel file
 1055:     $workbook->close();
 1056:     #
 1057:     # Write a link to allow them to download it
 1058:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
 1059:               '<p><a href="'.$filename.'">'.
 1060:               &mt('Your Excel spreadsheet.').
 1061:               '</a></p>'."\n";
 1062:     return $result;
 1063: }
 1064: 
 1065: sub OR_build_problem_data_worksheet {
 1066:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
 1067:     my $rows_output = 3;
 1068:     my $cols_output = 0;
 1069:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
 1070:     ##
 1071:     ##
 1072:     my @Headers;
 1073:     if (@$Concepts > 1) {
 1074:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
 1075:                     'Foil Name','Foil Text','Correct value');
 1076:     } else {
 1077:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
 1078:     }
 1079:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1080:     my %Foildata = %{$ORdata->{'_Foils'}};
 1081:     my $conceptindex = 1;
 1082:     my $foilindex = 1;
 1083:     foreach my $concept (@$Concepts) {
 1084:         my @FoilsInConcept = @{$concept->{'foils'}};
 1085:         my $firstfoil = shift(@FoilsInConcept);
 1086:         if (@$Concepts > 1) {
 1087:             $worksheet->write_row($rows_output++,0,
 1088:                                   [$conceptindex,
 1089:                                    $concept->{'name'},
 1090:                                    $foilindex++,
 1091:                                    $Foildata{$firstfoil}->{'name'},
 1092:                                    $Foildata{$firstfoil}->{'text'},
 1093:                                    $Foildata{$firstfoil}->{'value'},]);
 1094:         } else {
 1095:             $worksheet->write_row($rows_output++,0,
 1096:                                   [ $foilindex++,
 1097:                                     $Foildata{$firstfoil}->{'name'},
 1098:                                     $Foildata{$firstfoil}->{'text'},
 1099:                                     $Foildata{$firstfoil}->{'value'},]);
 1100:         }
 1101:         foreach my $foilid (@FoilsInConcept) {
 1102:             if (@$Concepts > 1) {
 1103:                 $worksheet->write_row($rows_output++,0,
 1104:                                       ['',
 1105:                                        '',
 1106:                                        $foilindex,
 1107:                                        $Foildata{$foilid}->{'name'},
 1108:                                        $Foildata{$foilid}->{'text'},
 1109:                                        $Foildata{$foilid}->{'value'},]);
 1110:             } else {
 1111:                 $worksheet->write_row($rows_output++,0,                
 1112:                                       [$foilindex,
 1113:                                        $Foildata{$foilid}->{'name'},
 1114:                                        $Foildata{$foilid}->{'text'},
 1115:                                        $Foildata{$foilid}->{'value'},]);
 1116:             }                
 1117:         } continue {
 1118:             $foilindex++;
 1119:         }
 1120:     } continue {
 1121:         $conceptindex++;
 1122:     }
 1123:     $rows_output++;
 1124:     $rows_output++;
 1125:     ##
 1126:     ## Option data output
 1127:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
 1128:     foreach my $string (@{$ORdata->{'_Options'}}) {
 1129:         $worksheet->write($rows_output++,0,$string);
 1130:     }
 1131:     return 'okay';
 1132: }
 1133: 
 1134: sub OR_build_response_data_worksheet {
 1135:     my ($worksheet,$format,$PerformanceData,$Foils,$ORdata)=@_;
 1136:     my $rows_output = 3;
 1137:     my $cols_output = 0;
 1138:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
 1139:     $worksheet->set_column(1,1,20);
 1140:     $worksheet->set_column(2,2,13);
 1141:     my @Headers = ('identifier','time','award detail','attempt');
 1142:     foreach my $foil (@$Foils) {
 1143:         push (@Headers,$foil.' submission');
 1144:         push (@Headers,$foil.' grading');
 1145:     }
 1146:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1147:     #
 1148:     foreach my $row (@$PerformanceData) {
 1149:         next if (! defined($row));
 1150:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
 1151:         my @Foilgrades = split('&',$grading);
 1152:         my @Foilsubs   = split('&',$submission);
 1153:         my %ResponseData;
 1154:         for (my $j=0;$j<=$#Foilgrades;$j++) {
 1155:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1156:             my (undef,$submission) = split('=',$Foilsubs[$j]);
 1157:             $submission = &Apache::lonnet::unescape($submission);
 1158:             $ResponseData{$foilid.' submission'}=$submission;
 1159:             $ResponseData{$foilid.' award'}=$correct;
 1160:         }
 1161:         $worksheet->write($rows_output,$cols_output++,$student);
 1162:         $worksheet->write($rows_output,$cols_output++,
 1163:              &Apache::lonstathelpers::calc_serial($time),$format->{'date'});
 1164:         $worksheet->write($rows_output,$cols_output++,$award);
 1165:         $worksheet->write($rows_output,$cols_output++,$tries);
 1166:         foreach my $foilid (@$Foils) {
 1167:             $worksheet->write($rows_output,$cols_output++,
 1168:                               $ResponseData{$foilid.' submission'});
 1169:             $worksheet->write($rows_output,$cols_output++,
 1170:                               $ResponseData{$foilid.' award'});
 1171:         }
 1172:         $rows_output++;
 1173:         $cols_output = 0;
 1174:     }
 1175:     return;
 1176: }
 1177: 
 1178: sub build_foil_index {
 1179:     my ($ORdata) = @_;
 1180:     return if (! exists($ORdata->{'_Foils'}));
 1181:     my %Foildata = %{$ORdata->{'_Foils'}};
 1182:     my @Foils = sort(keys(%Foildata));
 1183:     my %Concepts;
 1184:     foreach my $foilid (@Foils) {
 1185:         push(@{$Concepts{$Foildata{$foilid}->{'_Concept'}}},
 1186:              $foilid);
 1187:     }
 1188:     undef(@Foils);
 1189:     # Having gathered the concept information in a hash, we now translate it
 1190:     # into an array because we need to be consistent about order.
 1191:     # Also put the foils in order, too.
 1192:     my $sortfunction = sub {
 1193:         my %Numbers = (one   => 1,
 1194:                        two   => 2,
 1195:                        three => 3,
 1196:                        four  => 4,
 1197:                        five  => 5,
 1198:                        six   => 6,
 1199:                        seven => 7,
 1200:                        eight => 8,
 1201:                        nine  => 9,
 1202:                        ten   => 10,);
 1203:         my $a1 = lc($a); 
 1204:         my $b1 = lc($b);
 1205:         if (exists($Numbers{$a1})) {
 1206:             $a1 = $Numbers{$a1};
 1207:         }
 1208:         if (exists($Numbers{$b1})) {
 1209:             $b1 = $Numbers{$b1};
 1210:         }
 1211:         if (($a1 =~/^\d+$/) && ($b1 =~/^\d+$/)) {
 1212:             return $a1 <=> $b1;
 1213:         } else {
 1214:             return $a1 cmp $b1;
 1215:         }
 1216:     };
 1217:     my @Concepts;
 1218:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
 1219:         if (! defined($Concepts{$concept})) {
 1220:             $Concepts{$concept}=[];
 1221: #            next;
 1222:         }
 1223:         push(@Concepts,{ name => $concept,
 1224:                         foils => [@{$Concepts{$concept}}]});
 1225:         push(@Foils,(@{$Concepts{$concept}}));
 1226:     }
 1227:     #
 1228:     # Build up the table of row labels.
 1229:     my $table = '<table border="1" >'."\n";
 1230:     if (@Concepts > 1) {
 1231:         $table .= '<tr>'.
 1232:             '<th>'.&mt('Concept Number').'</th>'.
 1233:             '<th>'.&mt('Concept').'</th>'.
 1234:             '<th>'.&mt('Foil Number').'</th>'.
 1235:             '<th>'.&mt('Foil Name').'</th>'.
 1236:             '<th>'.&mt('Foil Text').'</th>'.
 1237:             '<th>'.&mt('Correct Value').'</th>'.
 1238:             "</tr>\n";
 1239:     } else {
 1240:         $table .= '<tr>'.
 1241:             '<th>'.&mt('Foil Number').'</th>'.
 1242:             '<th>'.&mt('Foil Name').'</th>'.
 1243:             '<th>'.&mt('Foil Text').'</th>'.
 1244:             '<th>'.&mt('Correct Value').'</th>'.
 1245:             "</tr>\n";
 1246:     }        
 1247:     my $conceptindex = 1;
 1248:     my $foilindex = 1;
 1249:     foreach my $concept (@Concepts) {
 1250:         my @FoilsInConcept = @{$concept->{'foils'}};
 1251:         my $firstfoil = shift(@FoilsInConcept);
 1252:         if (@Concepts > 1) {
 1253:             $table .= '<tr>'.
 1254:                 '<td>'.$conceptindex.'</td>'.
 1255:                 '<td>'.&HTML::Entities::encode($concept->{'name'}).'</td>'.
 1256:                 '<td>'.$foilindex++.'</td>'.
 1257:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'}).'</td>'.
 1258:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1259:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'}).'</td>'.
 1260:                 "</tr>\n";
 1261:         } else {
 1262:             $table .= '<tr>'.
 1263:                 '<td>'.$foilindex++.'</td>'.
 1264:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'}).'</td>'.
 1265:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1266:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'}).'</td>'.
 1267:                 "</tr>\n";
 1268:         }
 1269:         foreach my $foilid (@FoilsInConcept) {
 1270:             if (@Concepts > 1) {
 1271:                 $table .= '<tr>'.
 1272:                     '<td></td>'.
 1273:                     '<td></td>'.
 1274:                     '<td>'.$foilindex.'</td>'.
 1275:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'}).'</td>'.
 1276:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1277:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'}).'</td>'.
 1278:                     "</tr>\n";
 1279:             } else {
 1280:                 $table .= '<tr>'.
 1281:                     '<td>'.$foilindex.'</td>'.
 1282:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'}).'</td>'.
 1283:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1284:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'}).'</td>'.
 1285:                     "</tr>\n";
 1286:             }                
 1287:         } continue {
 1288:             $foilindex++;
 1289:         }
 1290:     } continue {
 1291:         $conceptindex++;
 1292:     }
 1293:     $table .= "</table>\n";
 1294:     #
 1295:     # Build option index with color stuff
 1296:     return ($table,\@Foils,\@Concepts);
 1297: }
 1298: 
 1299: sub build_option_index {
 1300:     my ($ORdata)= @_;
 1301:     my $table = "<table>\n";
 1302:     my $optionindex = 0;
 1303:     my @Rows;
 1304:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'_Options'}}) {
 1305:         push (@Rows,
 1306:               '<tr>'.
 1307:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
 1308:               ('&nbsp;'x4).'</td>'.
 1309:               '<td>'.&HTML::Entities::encode($option).'</td>'.
 1310:               "</tr>\n");
 1311:     }
 1312:     shift(@Rows); # Throw away 'correct option chosen' color
 1313:     $table .= join('',reverse(@Rows));
 1314:     $table .= "</table>\n";
 1315: }
 1316: 
 1317: #########################################################
 1318: #########################################################
 1319: ##
 1320: ##   Generic Interface Routines
 1321: ##
 1322: #########################################################
 1323: #########################################################
 1324: sub CreateInterface {
 1325:     ##
 1326:     ## Environment variable initialization
 1327:     if (! exists$ENV{'form.AnalyzeOver'}) {
 1328:         $ENV{'form.AnalyzeOver'} = 'Tries';
 1329:     }
 1330:     ##
 1331:     ## Build the menu
 1332:     my $Str = '';
 1333:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
 1334:         (undef,'Detailed Problem Analysis');
 1335:     $Str .= '<table cellspacing="5">'."\n";
 1336:     $Str .= '<tr>';
 1337:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
 1338:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
 1339: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
 1340:     $Str .= '<td align="center">&nbsp;</td>';
 1341:     $Str .= '</tr>'."\n";
 1342:     ##
 1343:     ## 
 1344:     $Str .= '<tr><td align="center">'."\n";
 1345:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1346:     $Str .= '</td>';
 1347:     #
 1348:     $Str .= '<td align="center">';
 1349:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1350:     $Str .= '</td>';
 1351:     #
 1352: #    $Str .= '<td align="center">';
 1353:     my $only_seq_with_assessments = sub { 
 1354:         my $s=shift;
 1355:         if ($s->{'num_assess'} < 1) { 
 1356:             return 0;
 1357:         } else { 
 1358:             return 1;
 1359:         }
 1360:     };
 1361:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
 1362:                                               $only_seq_with_assessments);
 1363:     ##
 1364:     ##
 1365:     $Str .= '<td>';
 1366:     { # These braces are here to organize the code, not scope it.
 1367:         {
 1368:             $Str .= '<nobr>'.&mt('Analyze Over ');
 1369:             $Str .= &Apache::loncommon::help_open_topic
 1370:                                                   ('Analysis_Analyze_Over');
 1371:             $Str .='<select name="AnalyzeOver" >';
 1372:             $Str .= '<option value="Tries" ';
 1373:             if (! exists($ENV{'form.AnalyzeOver'}) || 
 1374:                 $ENV{'form.AnalyzeOver'} eq 'Tries'){
 1375:                 # Default to Tries
 1376:                 $Str .= ' selected ';
 1377:             }
 1378:             $Str .= '>'.&mt('Tries').'</option>';
 1379:             $Str .= '<option value="Time" ';
 1380:             $Str .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'Time');
 1381:             $Str .= '>'.&mt('Time').'</option>';
 1382:             $Str .= '</select>';
 1383:             $Str .= '</nobr><br />';
 1384:         }
 1385:         {
 1386:             $Str .= '<nobr>'.&mt('Analyze as ');
 1387:             $Str .= &Apache::loncommon::help_open_topic
 1388:                                                   ('Analysis_Analyze_as');
 1389:             $Str .='<select name="AnalyzeAs" >';
 1390:             $Str .= '<option value="Concepts" ';
 1391:             if (! exists($ENV{'form.AnalyzeAs'}) || 
 1392:                 $ENV{'form.AnalyzeAs'} eq 'Concepts'){
 1393:                 # Default to Concepts
 1394:                 $Str .= ' selected ';
 1395:             }
 1396:             $Str .= '>'.&mt('Concepts').'</option>';
 1397:             $Str .= '<option value="Foils" ';
 1398:             $Str .= ' selected ' if ($ENV{'form.AnalyzeAs'} eq 'Foils');
 1399:             $Str .= '>'.&mt('Foils').'</option>';
 1400:             $Str .= '</select></nobr><br />';
 1401:         }
 1402:         {
 1403:             $Str .= '<br /><nobr>'.&mt('Number of Plots:');
 1404:             $Str .= &Apache::loncommon::help_open_topic
 1405:                                                   ('Analysis_num_plots');
 1406:             $Str .= '<select name="NumPlots">';
 1407:             if (! exists($ENV{'form.NumPlots'}) 
 1408:                 || $ENV{'form.NumPlots'} < 1 
 1409:                 || $ENV{'form.NumPlots'} > 20) {
 1410:                 $ENV{'form.NumPlots'} = 5;
 1411:             }
 1412:             foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
 1413:                 $Str .= '<option value="'.$i.'" ';
 1414:                 if ($ENV{'form.NumPlots'} == $i) { $Str.=' selected '; }
 1415:                 $Str .= '>'.$i.'</option>';
 1416:             }
 1417:             $Str .= '</select></nobr>';
 1418:         }
 1419:     }
 1420:     $Str .= '</td>';
 1421:     ##
 1422:     ##
 1423:     $Str .= '</tr>'."\n";
 1424:     $Str .= '</table>'."\n";
 1425:     return $Str;
 1426: }
 1427: 
 1428: #########################################################
 1429: #########################################################
 1430: ##
 1431: ##              Misc Option Response functions
 1432: ##
 1433: #########################################################
 1434: #########################################################
 1435: sub get_time_from_row {
 1436:     my ($row) = @_;
 1437:     if (ref($row)) {
 1438:         return $row->[&Apache::loncoursedata::RD_timestamp()];
 1439:     } 
 1440:     return undef;
 1441: }
 1442: 
 1443: sub get_tries_from_row {
 1444:     my ($row) = @_;
 1445:     if (ref($row)) {
 1446:         return $row->[&Apache::loncoursedata::RD_tries()];
 1447:     }
 1448:     return undef;
 1449: }
 1450: 
 1451: sub hashify_attempt {
 1452:     my ($row) = @_;
 1453:     my %attempt;
 1454:     $attempt{'tries'}      = $row->[&Apache::loncoursedata::RD_tries()];
 1455:     $attempt{'submission'} = $row->[&Apache::loncoursedata::RD_submission()];
 1456:     $attempt{'award'}      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1457:     $attempt{'timestamp'}  = $row->[&Apache::loncoursedata::RD_timestamp()];
 1458:     return %attempt;
 1459: }
 1460: 
 1461: sub Process_OR_Row {
 1462:     my ($row) = @_;
 1463:     my %RowData;
 1464:     my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 1465:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1466:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
 1467:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
 1468:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
 1469:     my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
 1470:     return undef if ($award eq 'MISSING_ANSWER');
 1471:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
 1472:         $RowData{'_correct'} = 1;
 1473:     }
 1474:     $RowData{'_total'} = 1;
 1475:     my @Foilgrades = split('&',$grading);
 1476:     my @Foilsubs   = split('&',$submission);
 1477:     for (my $j=0;$j<=$#Foilgrades;$j++) {
 1478:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1479:         $foilid = &Apache::lonnet::unescape($foilid);
 1480:         my (undef,$submission) = split('=',$Foilsubs[$j]);
 1481:         if ($correct) {
 1482:             $RowData{$foilid}->{'_correct'}++;
 1483:         } else {
 1484:             $submission = &Apache::lonnet::unescape($submission);
 1485:             $RowData{$foilid}->{$submission}++;
 1486:         }
 1487:         $RowData{$foilid}->{'_total'}++;
 1488:     }
 1489:     return %RowData;
 1490: }
 1491: 
 1492: 1;
 1493: 
 1494: __END__

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