File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.75: download - view: text, annotated - select for diffs
Thu Mar 11 19:34:10 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
&OR_Tries_Foil_Analysis: Variable name cleanup,  Use $Concepts instead
of $Foils for looping.

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

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