File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.71: download - view: text, annotated - select for diffs
Thu Feb 19 20:17:01 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added lonstudentsubmissions.pm
Moved code from lonproblemanalysis to lonstathelpers and lonstudentsubmissions.
lonstudentsubmissions can produce excel spreadsheets of student submissions
for numerical, radio, option, click on image, string, and formula responses.

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

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