File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.68: download - view: text, annotated - select for diffs
Mon Feb 16 20:50:03 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Some progress on numerical problem analysis.  Makes a plot that is nearly
useless.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemanalysis.pm,v 1.68 2004/02/16 20:50:03 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 HTML::Entities();
   38: use Time::Local();
   39: use Spreadsheet::WriteExcel();
   40: 
   41: my $plotcolors = ['#33ff00', 
   42:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
   43:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
   44:                   ]; 
   45: 
   46: my @SubmitButtons = ({ name => 'PrevProblemAnalysis',
   47:                        text => 'Previous Problem' },
   48:                      { name => 'ProblemAnalysis',
   49:                        text => 'Analyze Problem Again' },
   50:                      { name => 'NextProblemAnalysis',
   51:                        text => 'Next Problem' },
   52:                      { name => 'break'},
   53:                      { name => 'ClearCache',
   54:                        text => 'Clear Caches' },
   55:                      { name => 'updatecaches',
   56:                        text => 'Update Student Data' },
   57:                      { name => 'SelectAnother',
   58:                        text => 'Choose a different Problem' },
   59:                      { name => 'ExcelOutput',
   60:                        text => 'Produce Excel Output' });
   61: 
   62: 
   63: sub BuildProblemAnalysisPage {
   64:     my ($r,$c)=@_;
   65:     #
   66:     my %Saveable_Parameters = ('Status' => 'scalar',
   67:                                'Section' => 'array',
   68:                                'NumPlots' => 'scalar',
   69:                                'AnalyzeAs' => 'scalar',
   70:                                'AnalyzeOver' => 'scalar',
   71:                                );
   72:     &Apache::loncommon::store_course_settings('problem_analysis',
   73:                                               \%Saveable_Parameters);
   74:     &Apache::loncommon::restore_course_settings('problem_analysis',
   75:                                                 \%Saveable_Parameters);
   76:     #
   77:     &Apache::lonstatistics::PrepareClasslist();
   78:     #
   79:     $r->print('<h2>'.&mt('Detailed Problem Analysis').'</h2>');
   80:     $r->print(&CreateInterface());
   81:     #
   82:     my @Students = @Apache::lonstatistics::Students;
   83:     #
   84:     if (@Students < 1) {
   85:         $r->print('<h2>There are no students in the sections selected</h2>');
   86:     }
   87:     #
   88:     &Apache::loncoursedata::clear_internal_caches();
   89:     if (exists($ENV{'form.ClearCache'}) || 
   90:         exists($ENV{'form.updatecaches'}) ||
   91:         (exists($ENV{'form.firstanalysis'}) &&
   92:          $ENV{'form.firstanalysis'} ne 'no')) {
   93:         &Apache::lonstatistics::Gather_Full_Student_Data($r);
   94:     }
   95:     if (! exists($ENV{'form.firstanalysis'})) {
   96:         $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
   97:     } else {
   98:         $r->print('<input type="hidden" name="firstanalysis" value="no" />');
   99:     }
  100:     $r->rflush();
  101:     #
  102:     my $problem_types = '(option|radiobutton|numerical)';
  103:     if (exists($ENV{'form.problemchoice'}) && 
  104:         ! exists($ENV{'form.SelectAnother'})) {
  105:         foreach my $button (@SubmitButtons) {
  106:             if ($button->{'name'} eq 'break') {
  107:                 $r->print("<br />\n");
  108:             } else {
  109:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
  110:                           'value="'.&mt($button->{'text'}).'" />');
  111:                 $r->print('&nbsp;'x5);
  112:             }
  113:         }
  114:         #
  115:         $r->print('<hr />');
  116:         $r->rflush();
  117:         #
  118:         # Determine which problem we are to analyze
  119:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
  120:             ($ENV{'form.problemchoice'});
  121:         #
  122:         my ($prev,$curr,$next) = 
  123:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  124:                                                         $problem_types,
  125:                                                         'response',
  126:                                                         );
  127:         if (exists($ENV{'form.PrevProblemAnalysis'}) && defined($prev)) {
  128:             $current_problem = $prev;
  129:         } elsif (exists($ENV{'form.NextProblemAnalysis'}) && defined($next)) {
  130:             $current_problem = $next;
  131:         } else {
  132:             $current_problem = $curr;
  133:         }
  134:         #
  135:         # Store the current problem choice and send it out in the form
  136:         $ENV{'form.problemchoice'} = 
  137:             &Apache::lonstathelpers::make_target_id($current_problem);
  138:         $r->print('<input type="hidden" name="problemchoice" value="'.
  139:                   $ENV{'form.problemchoice'}.'" />');
  140:         #
  141:         if (! defined($current_problem->{'resource'})) {
  142:             $r->print('resource is undefined');
  143:         } else {
  144:             my $resource = $current_problem->{'resource'};
  145:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  146:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  147:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  148:             $r->rflush();
  149:             my %Data = &get_problem_data($resource->{'src'});
  150:             my $ProblemData = $Data{$current_problem->{'part'}.
  151:                                     '.'.
  152:                                     $current_problem->{'respid'}};
  153:             if ($current_problem->{'resptype'} eq 'option') {
  154:                 &OptionResponseAnalysis($r,$current_problem,
  155:                                         $ProblemData,
  156:                                         \@Students);
  157:             } elsif ($current_problem->{'resptype'} eq 'radiobutton') {
  158:                 &RadioResponseAnalysis($r,$current_problem,
  159:                                        $ProblemData,
  160:                                        \@Students);
  161:             } elsif ($current_problem->{'resptype'} eq 'numerical') {
  162:                 &NumericalResponseAnalysis($r,$current_problem,
  163:                                            $ProblemData,\@Students);
  164:             } else {
  165:                 $r->print('<h2>This analysis is not supported</h2>');
  166:             }
  167:         }
  168:         $r->print('<hr />');
  169:     } else {
  170:         $r->print('<input type="submit" name="ProblemAnalysis" value="'.
  171:                   &mt('Analyze Problem').'" />');
  172:         $r->print('&nbsp;'x5);
  173:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
  174:         $r->print(&Apache::lonstathelpers::ProblemSelector
  175:                   ($problem_types));
  176:     }
  177: }
  178: 
  179: #########################################################
  180: #########################################################
  181: ##
  182: ##      Numerical Response Routines
  183: ##
  184: #########################################################
  185: #########################################################
  186: sub NumericalResponseAnalysis {
  187:     my ($r,$problem,$ProblemData,$Students) = @_;
  188:     $r->print('<h2>This analysis is not yet supported</h2>');
  189:     my ($resource,$respid) = ($problem->{'resource'},
  190:                               $problem->{'respid'});
  191:     my $analysis_html;
  192:     my $PerformanceData = 
  193:         &Apache::loncoursedata::get_response_data
  194:         ($Students,$resource->{'symb'},$respid);
  195:     if (! defined($PerformanceData) || 
  196:         ref($PerformanceData) ne 'ARRAY' ) {
  197:         $analysis_html = '<h2>'.
  198:             &mt('There is no submission data for this resource').
  199:             '</h2>';
  200:         $r->print($analysis_html);
  201:         return;
  202:     }
  203:     my ($max,$min) = &GetStudentAnswers($r,$problem,$Students);
  204:     $r->print('Maximum = '.$max.' Minimum = '.$min);
  205:     my $max_students = 0;
  206:     my %Data;
  207:     foreach my $student (@$Students) {
  208:         my $answer = $student->{'answer'};
  209:         $Data{$answer}++;
  210:         if ($max_students < $Data{$answer}) {
  211:             $max_students = $Data{$answer};
  212:         }
  213:     }
  214:     foreach (5,10,20,25,50,75,100,150,200,250,500,1000,1500,2000,2500,5000) {
  215:         if ($max_students < $_) {
  216:             $max_students = $_;
  217:             last;
  218:         }
  219:     }
  220:     my @Labels = sort {$a <=> $b } keys(%Data);
  221:     $r->print('number of labels = '.scalar(@Labels));
  222:     my @PlotData = @Data{@Labels};
  223:     $r->print('number of PlotData = '.scalar(@PlotData));
  224:     my $graph = 
  225:         &Apache::loncommon::DrawXYGraph('Correct Answer Distribution',
  226:                                         'Correct Answer',
  227:                                         'Number of students',
  228:                                         $max_students,
  229:                                         undef,
  230:                                         \@Labels,
  231:                                         [\@PlotData],
  232:                                         (xskip=>10));
  233:     $r->print($graph);
  234:     return;
  235: }
  236: 
  237: sub GetStudentAnswers {
  238:     my ($r,$problem,$Students) = @_;
  239:     my %Answers;
  240:     my ($resource,$partid,$respid) = ($problem->{'resource'},
  241:                                       $problem->{'part'},
  242:                                       $problem->{'respid'});
  243:     # Open progress window
  244:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  245:         ($r,'Student Answer Compilation Status',
  246:          'Student Answer Compilation Progress', scalar(@$Students));
  247:     $r->print("<table>\n");
  248:     $r->rflush();
  249:     my ($min,$max);
  250:     foreach my $student (@$Students) {
  251:         my $sname = $student->{'username'};
  252:         my $sdom = $student->{'domain'};
  253:         my $answer = analyze_problem_as_student($resource,
  254:                                                 $sname,$sdom,
  255:                                                 $partid,$respid);
  256:         if (! defined($min) || $min > $answer) {
  257:             $min = $answer;
  258:         }
  259:         if (! defined($max) || $max < $answer) {
  260:             $max = $answer;
  261:         }
  262: #        $r->print('<tr>'.
  263: #                  '<td>'.$sname.'</td>'.
  264: #                  '<td>'.$sdom.'</td>'.
  265: #                  '<td>'.$answer.'</td>'.
  266: #                  '</tr>'."\n");
  267:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  268:                                                  &mt('last student'));
  269:         $student->{'answer'} = $answer;
  270:     }
  271:     $r->print("</table>\n");
  272:     $r->rflush();
  273:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  274:     # close progress window
  275:     return ($max,$min);
  276: }
  277: 
  278: sub build_student_data_worksheet {
  279:     my ($workbook,$format) = @_;
  280:     my $rows_output = 3;
  281:     my $cols_output = 0;
  282:     my $worksheet  = $workbook->addworksheet('Student Data');
  283:     $worksheet->write($rows_output++,0,'Student Data',$format->{'h3'});
  284:     my @Headers = ('full name','username','domain','section',
  285:                    "student\nnumber",'identifier');
  286:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
  287:     my @Students = @Apache::lonstatistics::Students;
  288:     my $studentrows = &Apache::loncoursedata::get_student_data(\@Students);
  289:     my %ids;
  290:     foreach my $row (@$studentrows) {
  291:         my ($mysqlid,$student) = @$row;
  292:         $ids{$student}=$mysqlid;
  293:     }
  294:     foreach my $student (@Students) {
  295:         my $name_domain = $student->{'username'}.':'.$student->{'domain'};
  296:         $worksheet->write_row($rows_output++,0,
  297:                           [$student->{'fullname'},
  298:                            $student->{'username'},$student->{'domain'},
  299:                            $student->{'section'},$student->{'id'},
  300:                            $ids{$name_domain}]);
  301:     }
  302:     return $worksheet;
  303: }
  304: 
  305: #########################################################
  306: #########################################################
  307: ##
  308: ##      Radio Response Routines
  309: ##
  310: #########################################################
  311: #########################################################
  312: sub RadioResponseAnalysis {
  313:     my ($r,$problem,$ProblemData,$Students) = @_;
  314:     my ($resource,$respid) = ($problem->{'resource'},
  315:                               $problem->{'respid'});
  316:     my $analysis_html;
  317:     my $PerformanceData = 
  318:         &Apache::loncoursedata::get_response_data
  319:         ($Students,$resource->{'symb'},$respid);
  320:     if (! defined($PerformanceData) || 
  321:         ref($PerformanceData) ne 'ARRAY' ) {
  322:         $analysis_html = '<h2>'.
  323:             &mt('There is no submission data for this resource').
  324:             '</h2>';
  325:         $r->print($analysis_html);
  326:         return;
  327:     }
  328:     if (exists($ENV{'form.ExcelOutput'})) {
  329:         $analysis_html .= &RR_Excel_output($r,$problem->{'resource'},
  330:                                            $PerformanceData,$ProblemData);
  331:     } elsif ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  332:         $analysis_html .= &RR_Tries_Analysis($r,$problem->{'resource'},
  333:                                              $PerformanceData,$ProblemData);
  334:     } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  335:         $analysis_html .= &RR_Time_Analysis($r,$problem->{'resource'},
  336:                                             $PerformanceData,$ProblemData);
  337:     } else {
  338:         $analysis_html .= '<h2>'.
  339:            &mt('The analysis you have selected is not supported at this time').
  340:            '</h2>';
  341:     }
  342:     $r->print($analysis_html);
  343: }
  344: 
  345: sub RR_Excel_output   { 
  346:     my ($r,$PerformanceData,$ProblemData) = @_;
  347:     return '<h1>No!</h1>';
  348: }
  349: 
  350: sub RR_Tries_Analysis { 
  351:     my ($r,$resource,$PerformanceData,$ProblemData) = @_;
  352:     my $analysis_html;
  353:     my $mintries = 1;
  354:     my $maxtries = $ENV{'form.NumPlots'};
  355:     my ($table,$Foils,$Concepts) = &build_foil_index($ProblemData);
  356:     if ((! defined($Concepts)) || ((@$Concepts < 2) && 
  357:                                    ($ENV{'form.AnalyzeAs'} ne 'Foils'))) {
  358:         $table = '<h3>'.
  359:             &mt('Not enough data for concept analysis.  '.
  360:                 'Performing Foil Analysis').
  361:             '</h3>'.$table;
  362:         $ENV{'form.AnalyzeAs'} = 'Foils';
  363:     }
  364:     $analysis_html .= $table;
  365:     my @TryData = &RR_tries_data_analysis($r,$PerformanceData);
  366: #    if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  367:         $analysis_html .= &RR_Tries_Foil_Analysis($mintries,$maxtries,$Foils,
  368:                                                  \@TryData,$ProblemData);
  369: #    } else {
  370: #        $analysis_html = &RR_Tries_Concept_Analysis($mintries,$maxtries,
  371: #                                                    $Concepts,
  372: #                                                    \@TryData,
  373: #                                                    $ProblemData);
  374: #    }
  375:     return $analysis_html;
  376: }
  377: 
  378: sub RR_tries_data_analysis {
  379:     my ($r,$Attempt_data) = @_;
  380:     my @TryData;
  381:     foreach my $attempt (@$Attempt_data) {
  382:         my %Attempt = &hashify_attempt($attempt);
  383:         my ($answer,undef) = split('=',$Attempt{'submission'});
  384:         $TryData[$Attempt{'tries'}]->{$answer}++;
  385:     }
  386:     return @TryData;
  387: }
  388: 
  389: sub RR_Time_Analysis  { 
  390:     my ($r,$PerformanceData,$ProblemData) = @_;
  391:     my $html;
  392:     return $html;
  393: }
  394: 
  395: sub RR_Tries_Foil_Analysis {
  396:     my ($min,$max,$Foils,$TryData,$ProblemData) = @_;
  397:     my $html;
  398:     #
  399:     # Compute the data neccessary to make the plots
  400:     for (my $try=$min;$try<=$max;$try++) {
  401:         my @PlotData_Correct; 
  402:         my @PlotData_Incorrect;
  403:         next if ($try > scalar(@{$TryData}));
  404:         next if (! defined($TryData->[$try]));
  405:         my %DataSet = %{$TryData->[$try]};
  406:         my $total = 0;
  407:         foreach my $foilid (@$Foils) {
  408:             $total += $DataSet{$foilid};
  409:         }
  410:         foreach my $foilid (@$Foils) {
  411:             if ($total == 0) {
  412:                 push (@PlotData_Correct,0);
  413:                 push (@PlotData_Incorrect,0);
  414:             } else {
  415:                 if ($ProblemData->{'_Foils'}->{$foilid}->{'value'} eq 'true') {
  416:                     push (@PlotData_Correct,
  417:                           int(100*$DataSet{$foilid}/$total));
  418:                     push (@PlotData_Incorrect,0);
  419:                 } else {
  420:                     push (@PlotData_Correct,0);
  421:                     push (@PlotData_Incorrect,
  422:                           int(100*$DataSet{$foilid}/$total));
  423:                 }
  424:             }
  425:         }
  426:         my $title='Attempt '.$try;
  427:         my $xlabel = $total.' Submissions';
  428:         $html.=  &Apache::loncommon::DrawBarGraph($title,
  429:                                                   $xlabel,
  430:                                                   'Percent Choosing',
  431:                                                   100,
  432:                                                   ['#33ff00','#ff3300'],
  433:                                                   undef,
  434:                                                   \@PlotData_Correct,
  435:                                                   \@PlotData_Incorrect);
  436:     }
  437:     return $html;
  438: }
  439: 
  440: sub RR_Tries_Concept_Analysis {
  441:     my ($min,$max,$Concepts,$ResponseData,$ProblemData) = @_;
  442:     my $html;
  443:     return $html;
  444: }
  445: 
  446: sub RR_Time_Foil_Analysis {
  447:     my ($min,$max,$Foils,$ResponseData,$ProblemData) = @_;
  448:     my $html;
  449:     return $html;
  450: }
  451: 
  452: sub RR_Time_Concept_Analysis {
  453:     my ($min,$max,$Concepts,$ResponseData,$ProblemData) = @_;
  454:     my $html;
  455:     return $html;
  456: }
  457: 
  458: 
  459: sub get_Radio_problem_data {
  460:     my ($url) = @_;
  461:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
  462:     (my $garbage,$Answ)=split('_HASH_REF__',$Answ,2);
  463:     my %Answer = &Apache::lonnet::str2hash($Answ);
  464:     my %Partdata;
  465:     foreach my $part (@{$Answer{'parts'}}) {
  466:         while (my($key,$value) = each(%Answer)) {
  467: #            if (ref($value) eq 'ARRAY') {
  468: #                &Apache::lonnet::logthis('is ref part:'.$part.' '.$key.'='.join(',',@$value));
  469: #            } else {
  470: #                &Apache::lonnet::logthis('notref part:'.$part.' '.$key.'='.$value);
  471: #            }                
  472:             next if ($key !~ /^$part/);
  473:             $key =~ s/^$part\.//;
  474:             if ($key eq 'foils') {
  475:                 $Partdata{$part}->{'_Foils'}=$value;
  476:             } elsif ($key eq 'options') {
  477:                 $Partdata{$part}->{'_Options'}=$value;
  478:             } elsif ($key eq 'shown') {
  479:                 $Partdata{$part}->{'_Shown'}=$value;
  480:             } elsif ($key =~ /^foil.value.(.*)$/) {
  481:                 $Partdata{$part}->{$1}->{'value'}=$value;
  482:             } elsif ($key =~ /^foil.text.(.*)$/) {
  483:                 $Partdata{$part}->{$1}->{'text'}=$value;
  484:             }
  485:         }
  486:     }
  487:     return %Partdata;
  488: }
  489: 
  490: #########################################################
  491: #########################################################
  492: ##
  493: ##      Option Response Routines
  494: ##
  495: #########################################################
  496: #########################################################
  497: sub OptionResponseAnalysis {
  498:     my ($r,$problem,$ProblemData,$Students) = @_;
  499:     my ($resource,$respid) = ($problem->{'resource'},
  500:                               $problem->{'respid'});
  501:     # Note: part data is not needed.
  502:     my $PerformanceData = 
  503:         &Apache::loncoursedata::get_response_data
  504:         ($Students,$resource->{'symb'},$respid);
  505:     if (! defined($PerformanceData) || 
  506:         ref($PerformanceData) ne 'ARRAY' ) {
  507:         $r->print('<h2>'.
  508:                   &mt('There is no student data for this problem.').
  509:                   '</h2>');
  510:     }  else {
  511:         $r->rflush();
  512:         if (exists($ENV{'form.ExcelOutput'})) {
  513:             my $result = &OR_excel_sheet($r,$resource,
  514:                                          $PerformanceData,
  515:                                          $ProblemData);
  516:             $r->print($result);
  517:             $r->rflush();
  518:         } else {
  519:             if ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  520:                 my $analysis_html = &OR_tries_analysis($r,
  521:                                                     $PerformanceData,
  522:                                                     $ProblemData);
  523:                 $r->print($analysis_html);
  524:                 $r->rflush();
  525:             } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  526:                 my $analysis_html = &OR_time_analysis($PerformanceData,
  527:                                                    $ProblemData);
  528:                 $r->print($analysis_html);
  529:                 $r->rflush();
  530:             } else {
  531:                 $r->print('<h2>'.
  532:                           &mt('The analysis you have selected is '.
  533:                               'not supported at this time').
  534:                           '</h2>');
  535:             }
  536:         }
  537:     }
  538: }
  539: 
  540: #########################################################
  541: #
  542: #       Option Response:  Tries Analysis
  543: #
  544: #########################################################
  545: sub OR_tries_analysis {
  546:     my ($r,$PerformanceData,$ORdata) = @_;
  547:     my $mintries = 1;
  548:     my $maxtries = $ENV{'form.NumPlots'};
  549:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  550:     if ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils')) {
  551:         $table = '<h3>'.
  552:             &mt('Not enough data for concept analysis.  '.
  553:                 'Performing Foil Analysis').
  554:             '</h3>'.$table;
  555:         $ENV{'form.AnalyzeAs'} = 'Foils';
  556:     }
  557:     my %ResponseData = &OR_analyze_by_tries($r,$PerformanceData,
  558:                                                      $mintries,$maxtries);
  559:     my $analysis = '';
  560:     if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  561:         $analysis = &OR_Tries_Foil_Analysis($mintries,$maxtries,$Foils,
  562:                                          \%ResponseData,$ORdata);
  563:     } else {
  564:         $analysis = &OR_Tries_Concept_Analysis($mintries,$maxtries,
  565:                                             $Concepts,\%ResponseData,$ORdata);
  566:     }
  567:     $table .= $analysis;
  568:     return $table;
  569: }
  570: 
  571: sub OR_Tries_Foil_Analysis {
  572:     my ($mintries,$maxtries,$Foils,$respdat,$ORdata) = @_;
  573:     my %ResponseData = %$respdat;
  574:     #
  575:     # Compute the data neccessary to make the plots
  576:     my @PlotData; 
  577:     foreach my $foilid (@$Foils) {
  578:         for (my $i=$mintries;$i<=$maxtries;$i++) {
  579:             if ($ResponseData{$foilid}->[$i]->{'_total'} == 0) {
  580:                 push(@{$PlotData[$i]->{'_correct'}},0);
  581:             } else {
  582:                 push(@{$PlotData[$i]->{'_correct'}},
  583:                      100*$ResponseData{$foilid}->[$i]->{'_correct'}/
  584:                      $ResponseData{$foilid}->[$i]->{'_total'});
  585:             }
  586:             foreach my $option (@{$ORdata->{'_Options'}}) {
  587:                 push(@{$PlotData[$i]->{'_total'}},
  588:                      $ResponseData{$foilid}->[$i]->{'_total'});
  589:                 if ($ResponseData{$foilid}->[$i]->{'_total'} == 0) {
  590:                     push (@{$PlotData[$i]->{$option}},0);
  591:                 } else {
  592:                     if ($ResponseData{$foilid}->[$i]->{'_total'} ==
  593:                         $ResponseData{$foilid}->[$i]->{'_correct'}) {
  594:                         push(@{$PlotData[$i]->{$option}},0);
  595:                     } else {
  596:                         push (@{$PlotData[$i]->{$option}},
  597:                               100 * $ResponseData{$foilid}->[$i]->{$option} / 
  598:                               ($ResponseData{$foilid}->[$i]->{'_total'} - 
  599:                                $ResponseData{$foilid}->[$i]->{'_correct'}));
  600:                     }
  601:                 }
  602:             }
  603:         }
  604:     }
  605:     # 
  606:     # Build a table for the plots
  607:     my $analysis_html = "<table>\n";
  608:     my $foilkey = &build_option_index($ORdata);
  609:     for (my $i=$mintries;$i<=$maxtries;$i++) {
  610:         my $count = $ResponseData{'_total'}->[$i];
  611:         if ($count == 0) {
  612:             $count = 'no submissions';
  613:         } elsif ($count == 1) {
  614:             $count = '1 submission';
  615:         } else {
  616:             $count = $count.' submissions';
  617:         }
  618:         my $title = 'Attempt '.$i.', '.$count;
  619:         my @Datasets;
  620:         foreach my $option ('_correct',@{$ORdata->{'_Options'}}) {
  621:             next if (! exists($PlotData[$i]->{$option}));
  622:             push(@Datasets,$PlotData[$i]->{$option});
  623:         }
  624:         my $correctgraph = &Apache::loncommon::DrawBarGraph
  625:             ($title,'Foil Number','Percent Correct',
  626:              100,$plotcolors,undef,$Datasets[0]);
  627:         $analysis_html.= '<tr><td>'.$correctgraph.'</td>';
  628:         ##
  629:         ##
  630:         next if (! defined($Datasets[0]));
  631:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
  632:             $Datasets[0]->[$i]=0;
  633:         }
  634:         $count = $ResponseData{'_total'}->[$i]-$ResponseData{'_correct'}->[$i];
  635:         if ($count == 0) {
  636:             $count = 'no submissions';
  637:         } elsif ($count == 1) {
  638:             $count = '1 submission';
  639:         } else {
  640:             $count = $count.' submissions';
  641:         }
  642:         $title = 'Attempt '.$i.', '.$count;
  643:         my $incorrectgraph = &Apache::loncommon::DrawBarGraph
  644:             ($title,'Foil Number','% Option Chosen Incorrectly',
  645:              100,$plotcolors,undef,@Datasets);
  646:         $analysis_html.= '<td>'.$incorrectgraph.'</td>';
  647:         $analysis_html.= '<td>'.$foilkey."<td></tr>\n";
  648:     }
  649:     $analysis_html .= "</table>\n";
  650:     return $analysis_html;
  651: }
  652: 
  653: sub OR_Tries_Concept_Analysis {
  654:     my ($mintries,$maxtries,$Concepts,$respdat,$ORdata) = @_;
  655:     my %ResponseData = %$respdat;
  656:     my $analysis_html = "<table>\n";
  657:     #
  658:     # Compute the data neccessary to make the plots
  659:     my @PlotData;
  660:     # Concept analysis
  661:     #
  662:     # Note: we do not bother with characterizing the students incorrect
  663:     # answers at the concept level because an incorrect answer for one foil
  664:     # may be a correct answer for another foil.
  665:     my %ConceptData;
  666:     foreach my $concept (@{$Concepts}) {
  667:         for (my $i=$mintries;$i<=$maxtries;$i++) {
  668:             #
  669:             # Gather the per-attempt data
  670:             my $cdata = $ConceptData{$concept}->[$i];
  671:             foreach my $foilid (@{$concept->{'foils'}}) {
  672:                 $cdata->{'_correct'} += 
  673:                     $ResponseData{$foilid}->[$i]->{'_correct'};
  674:                 $cdata->{'_total'}   += 
  675:                     $ResponseData{$foilid}->[$i]->{'_total'};
  676:             }
  677:             push (@{$PlotData[$i]->{'_total'}},$cdata->{'_total'});
  678:             if ($cdata->{'_total'} == 0) {
  679:                 push (@{$PlotData[$i]->{'_correct'}},0);
  680:             } else {
  681:                 push (@{$PlotData[$i]->{'_correct'}},
  682:                       100*$cdata->{'_correct'}/$cdata->{'_total'});
  683:                 }
  684:         }
  685:     }
  686:     # Build a table for the plots
  687:     for (my $i=$mintries;$i<=$maxtries;$i++) {
  688:         my $minstu = $PlotData[$i]->{'_total'}->[0];
  689:         my $maxstu = $PlotData[$i]->{'_total'}->[0];
  690:         foreach my $count (@{$PlotData[$i]->{'_total'}}) {
  691:             if ($minstu > $count) {
  692:                 $minstu = $count;
  693:             }
  694:             if ($maxstu < $count) {
  695:                 $maxstu = $count;
  696:             }
  697:         }
  698:         $maxstu = 0 if (! defined($maxstu));
  699:         $minstu = 0 if (! defined($minstu));
  700:         my $title;
  701:         my $count = $ResponseData{'_total'}->[$i];
  702:         if ($count == 0) {
  703:             $count = 'no submissions';
  704:         } elsif ($count == 1) {
  705:             $count = '1 submission';
  706:         } else {
  707:             $count = $count.' submissions';
  708:         }
  709:         $title = 'Attempt '.$i.', '.$count;
  710:         my $graphlink = &Apache::loncommon::DrawBarGraph
  711:             ($title,'Concept Number','Percent Correct',
  712:              100,$plotcolors,undef,$PlotData[$i]->{'_correct'});
  713:         $analysis_html.= '<tr><td>'.$graphlink."</td></tr>\n";
  714:     }
  715:     $analysis_html .= "</table>\n";
  716:     return $analysis_html;
  717: }
  718: 
  719: sub OR_analyze_by_tries {
  720:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
  721:     my %Trydata;
  722:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
  723:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
  724:     foreach my $row (@$PerformanceData) {
  725:         next if (! defined($row));
  726:         my $tries = &get_tries_from_row($row);
  727:         my %Row   = &Process_OR_Row($row);
  728:         next if (! %Row);
  729:         while (my ($foilid,$href) = each(%Row)) {
  730:             if (! ref($href)) { 
  731:                 $Trydata{$foilid}->[$tries] += $href;
  732:                 next;
  733:             }
  734:             while (my ($option,$value) = each(%$href)) {
  735:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
  736:             }
  737:         }
  738:     }
  739:     return %Trydata;
  740: }
  741: 
  742: #########################################################
  743: #
  744: #     Option Response: Time Analysis
  745: #
  746: #########################################################
  747: sub OR_time_analysis {
  748:     my ($PerformanceData,$ORdata) = @_;
  749:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  750:     if ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils')) {
  751:         $table = '<h3>'.
  752:             &mt('Not enough data for concept analysis.  '.
  753:                 'Performing Foil Analysis').
  754:             '</h3>'.$table;
  755:         $ENV{'form.AnalyzeAs'} = 'Foils';
  756:     }
  757:     my $num_plots = $ENV{'form.NumPlots'};
  758:     my $num_data = scalar(@$PerformanceData)-1;
  759:     my $percent = sprintf('%2f',100/$num_plots);
  760:     #
  761:     $table .= "<table>\n";
  762:     for (my $i=0;$i<$num_plots;$i++) {
  763:         ##
  764:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  765:             ('startdate_'.$i);
  766:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  767:             ('enddate_'.$i);
  768:         if (! defined($starttime) || ! defined($endtime)) {
  769:             my $sec_in_day = 86400;
  770:             my $last_sub_time = &get_time_from_row($PerformanceData->[-1]);
  771:             my ($sday,$smon,$syear);
  772:             (undef,undef,undef,$sday,$smon,$syear) = 
  773:                 localtime($last_sub_time - $sec_in_day*$i);
  774:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
  775:             $endtime = $starttime + $sec_in_day;
  776:             if ($i == ($num_plots -1 )) {
  777:                 $starttime = &get_time_from_row($PerformanceData->[0]);
  778:             }
  779:         }
  780:         my $startdateform = &Apache::lonhtmlcommon::date_setter
  781:             ('Statistics','startdate_'.$i,$starttime);
  782:         my $enddateform = &Apache::lonhtmlcommon::date_setter
  783:             ('Statistics','enddate_'.$i,$endtime);
  784:         #
  785:         my $begin_index;
  786:         my $end_index;
  787:         my $j;
  788:         while (++$j < scalar(@$PerformanceData)) {
  789:             last if (&get_time_from_row($PerformanceData->[$j]) 
  790:                                                               > $starttime);
  791:         }
  792:         $begin_index = $j;
  793:         while (++$j < scalar(@$PerformanceData)) {
  794:             last if (&get_time_from_row($PerformanceData->[$j]) > $endtime);
  795:         }
  796:         $end_index = $j;
  797:         ##
  798:         my $interval = {
  799:             begin_index => $begin_index,
  800:             end_index   => $end_index,
  801:             startdateform => $startdateform,
  802:             enddateform   => $enddateform,
  803:         };
  804:         if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  805:             $table .= &OR_Foil_Time_Analysis($PerformanceData,$ORdata,$Foils,
  806:                                           $interval);
  807:         } else {
  808:             $table .= &OR_Concept_Time_Analysis($PerformanceData,$ORdata,
  809:                                              $Concepts,$interval);
  810:         }
  811:     }
  812:     #
  813:     return $table;
  814: }
  815: 
  816: sub OR_Foil_Time_Analysis {
  817:     my ($PerformanceData,$ORdata,$Foils,$interval) = @_;
  818:     my $analysis_html;
  819:     my $foilkey = &build_option_index($ORdata);
  820:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  821:                                     $interval->{'end_index'});
  822:     my %TimeData;
  823:     #
  824:     # Compute the number getting the foils correct or incorrects
  825:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  826:         my $row = $PerformanceData->[$j];
  827:         next if (! defined($row));
  828:         my %Row = &Process_OR_Row($row);
  829:         while (my ($foilid,$href) = each(%Row)) {
  830:             if (! ref($href)) {
  831:                 $TimeData{$foilid} += $href;
  832:                 next;
  833:             }
  834:             while (my ($option,$value) = each(%$href)) {
  835:                 $TimeData{$foilid}->{$option}+=$value;
  836:             }
  837:         }
  838:     }
  839:     my @Plotdata;
  840:     foreach my $foil (@$Foils) {
  841:         my $total = $TimeData{$foil}->{'_total'};
  842:         if ($total == 0) {
  843:             push(@{$Plotdata[0]},0);
  844:         } else {
  845:             push(@{$Plotdata[0]},
  846:                  100 * $TimeData{$foil}->{'_correct'} / $total);
  847:         }
  848:         my $total_incorrect = $total - $TimeData{$foil}->{'_correct'};
  849:         my $optionidx = 1;
  850:         foreach my $option (@{$ORdata->{'_Options'}}) {
  851:             if ($total_incorrect == 0) {
  852:                 push(@{$Plotdata[$optionidx]},0);
  853:             } else {
  854:                 push(@{$Plotdata[$optionidx]},
  855:                      100 * $TimeData{$foil}->{$option} / $total_incorrect);
  856:             }
  857:         } continue {
  858:             $optionidx++;
  859:         }
  860:     }
  861:     #
  862:     # Create the plot
  863:     my $count = $end_index-$begin_index;
  864:     my $title;
  865:     if ($count == 0) {
  866:         $title = 'no submissions';
  867:     } elsif ($count == 1) {
  868:         $title = 'one submission';
  869:     } else {
  870:         $title = $count.' submissions';
  871:     }
  872:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
  873:                                                        'Foil Number',
  874:                                                        'Percent Correct',
  875:                                                        100,
  876:                                                        $plotcolors,
  877:                                                        undef,
  878:                                                        $Plotdata[0]);
  879:     for (my $j=0; $j< scalar(@{$Plotdata[0]});$j++) {
  880:         $Plotdata[0]->[$j]=0;
  881:     }
  882:     $count = $end_index-$begin_index-$TimeData{'_correct'};
  883:     if ($count == 0) {
  884:         $title = 'no submissions';
  885:     } elsif ($count == 1) {
  886:         $title = 'one submission';
  887:     } else {
  888:         $title = $count.' submissions';
  889:     }
  890:     my $incorrectplot = &Apache::loncommon::DrawBarGraph($title,
  891:                                                  'Foil Number',
  892:                                                  'Incorrect Option Choice',
  893:                                                  100,
  894:                                                  $plotcolors,
  895:                                                  undef,
  896:                                                  @Plotdata);        
  897:     $analysis_html.='<tr>'.
  898:         '<td>'.$correctplot.'</td>'.
  899:         '<td>'.$incorrectplot.'</td>'.
  900:         '<td align="left" valign="top">'.$foilkey.'</td>'."</tr>\n";
  901:     $analysis_html.= '<tr>'.'<td colspan="3">'.
  902:         '<b>Start Time</b>:'.
  903:         ' &nbsp;'.$interval->{'startdateform'}.'<br />'.
  904:         '<b>End Time</b>&nbsp;&nbsp;: '.
  905:         '&nbsp;'.$interval->{'enddateform'}.'<br />'.
  906: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.
  907: #        ("&nbsp;"x3).$interval->{'titleform'}.
  908:         '</td>'.
  909:         "</tr>\n";
  910:     return $analysis_html;
  911: }
  912: 
  913: sub OR_Concept_Time_Analysis {
  914:     my ($PerformanceData,$ORdata,$Concepts,$interval) = @_;
  915:     my $analysis_html;
  916:     ##
  917:     ## Determine starttime, endtime, startindex, endindex
  918:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  919:                                     $interval->{'end_index'});
  920:     my %TimeData;
  921:     #
  922:     # Compute the number getting the foils correct or incorrects
  923:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  924:         my $row = $PerformanceData->[$j];
  925:         next if (! defined($row));
  926:         my %Row = &Process_OR_Row($row);
  927:         while (my ($foilid,$href) = each(%Row)) {
  928:             if (! ref($href)) {
  929:                 $TimeData{$foilid} += $href;
  930:                 next;
  931:             }
  932:             while (my ($option,$value) = each(%$href)) {
  933:                 $TimeData{$foilid}->{$option}+=$value;
  934:             }
  935:         }
  936:     }
  937:     #
  938:     # Put the data in plottable form
  939:     my @Plotdata;
  940:     foreach my $concept (@$Concepts) {
  941:         my ($total,$correct);
  942:         foreach my $foil (@{$concept->{'foils'}}) {
  943:             $total += $TimeData{$foil}->{'_total'};
  944:             $correct += $TimeData{$foil}->{'_correct'};
  945:         }
  946:         if ($total == 0) {
  947:             push(@Plotdata,0);
  948:         } else {
  949:             push(@Plotdata,100 * $correct / $total);
  950:         }
  951:     }
  952:     #
  953:     # Create the plot
  954:     my $title = ($end_index - $begin_index).' submissions';
  955:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
  956:                                                     'Concept Number',
  957:                                                     'Percent Correct',
  958:                                                     100,
  959:                                                     $plotcolors,
  960:                                                     undef,
  961:                                                     \@Plotdata);
  962:     $analysis_html.='<tr>'.
  963:         '<td>'.$correctplot.'</td>'.
  964:         '<td align="left" valign="top">'.
  965:         '<b>Start Time</b>: &nbsp;'.$interval->{'startdateform'}.'<br />'.
  966:         '<b>End Time</b>&nbsp;&nbsp;: '.
  967:            '&nbsp;'.$interval->{'enddateform'}.'<br />'.
  968: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.("&nbsp;"x3).
  969: #            $interval->{'titleform'}.
  970:         '</td>'.
  971:         "</tr>\n";
  972:     return $analysis_html;
  973: }
  974: 
  975: #########################################################
  976: #########################################################
  977: ##
  978: ##             Excel output 
  979: ##
  980: #########################################################
  981: #########################################################
  982: sub OR_excel_sheet {
  983:     my ($r,$resource,$PerformanceData,$ORdata) = @_;
  984:     my $response = '';
  985:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
  986:     #
  987:     # Create excel worksheet
  988:     my $filename = '/prtspool/'.
  989:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  990:         time.'_'.rand(1000000000).'.xls';
  991:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  992:     if (! defined($workbook)) {
  993:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  994:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
  995:                             "This error has been logged.  ".
  996:                             "Please alert your LON-CAPA administrator").
  997:                   '</p>');
  998:         return undef;
  999:     }
 1000:     #
 1001:     $workbook->set_tempdir('/home/httpd/perl/tmp');
 1002:     #
 1003:     # Define some potentially useful formats
 1004:     my $format;
 1005:     $format->{'header'} = $workbook->add_format(bold      => 1, 
 1006:                                                 bottom    => 1,
 1007:                                                 align     => 'center');
 1008:     $format->{'bold'} = $workbook->add_format(bold=>1);
 1009:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
 1010:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
 1011:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
 1012:     $format->{'date'} = $workbook->add_format(num_format=>
 1013:                                               'mmm d yyyy hh:mm AM/PM');
 1014:     #
 1015:     # Create and populate main worksheets
 1016:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
 1017:     my $student_data_sheet  = $workbook->addworksheet('Student Data');
 1018:     my $response_data_sheet = $workbook->addworksheet('Response Data');
 1019:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
 1020:                        $response_data_sheet) {
 1021:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
 1022:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
 1023:     }
 1024:     #
 1025:     my $result;
 1026:     $result = &OR_build_problem_data_worksheet($problem_data_sheet,$format,
 1027:                                             $Concepts,$ORdata);
 1028:     if ($result ne 'okay') {
 1029:         # Do something useful
 1030:     }
 1031:     $result = &OR_build_student_data_worksheet($student_data_sheet,$format);
 1032:     if ($result ne 'okay') {
 1033:         # Do something useful
 1034:     }
 1035:     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
 1036:                                              $PerformanceData,$Foils,
 1037:                                              $ORdata);
 1038:     if ($result ne 'okay') {
 1039:         # Do something useful
 1040:     }
 1041:     $response_data_sheet->activate();
 1042:     #
 1043:     # Close the excel file
 1044:     $workbook->close();
 1045:     #
 1046:     # Write a link to allow them to download it
 1047:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
 1048:               '<p><a href="'.$filename.'">'.
 1049:               &mt('Your Excel spreadsheet.').
 1050:               '</a></p>'."\n";
 1051:     return $result;
 1052: }
 1053: 
 1054: sub OR_build_problem_data_worksheet {
 1055:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
 1056:     my $rows_output = 3;
 1057:     my $cols_output = 0;
 1058:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
 1059:     ##
 1060:     ##
 1061:     my @Headers;
 1062:     if (@$Concepts > 1) {
 1063:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
 1064:                     'Foil Name','Foil Text','Correct value');
 1065:     } else {
 1066:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
 1067:     }
 1068:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1069:     my %Foildata = %{$ORdata->{'_Foils'}};
 1070:     my $conceptindex = 1;
 1071:     my $foilindex = 1;
 1072:     foreach my $concept (@$Concepts) {
 1073:         my @FoilsInConcept = @{$concept->{'foils'}};
 1074:         my $firstfoil = shift(@FoilsInConcept);
 1075:         if (@$Concepts > 1) {
 1076:             $worksheet->write_row($rows_output++,0,
 1077:                                   [$conceptindex,
 1078:                                    $concept->{'name'},
 1079:                                    $foilindex++,
 1080:                                    $Foildata{$firstfoil}->{'name'},
 1081:                                    $Foildata{$firstfoil}->{'text'},
 1082:                                    $Foildata{$firstfoil}->{'value'},]);
 1083:         } else {
 1084:             $worksheet->write_row($rows_output++,0,
 1085:                                   [ $foilindex++,
 1086:                                     $Foildata{$firstfoil}->{'name'},
 1087:                                     $Foildata{$firstfoil}->{'text'},
 1088:                                     $Foildata{$firstfoil}->{'value'},]);
 1089:         }
 1090:         foreach my $foilid (@FoilsInConcept) {
 1091:             if (@$Concepts > 1) {
 1092:                 $worksheet->write_row($rows_output++,0,
 1093:                                       ['',
 1094:                                        '',
 1095:                                        $foilindex,
 1096:                                        $Foildata{$foilid}->{'name'},
 1097:                                        $Foildata{$foilid}->{'text'},
 1098:                                        $Foildata{$foilid}->{'value'},]);
 1099:             } else {
 1100:                 $worksheet->write_row($rows_output++,0,                
 1101:                                       [$foilindex,
 1102:                                        $Foildata{$foilid}->{'name'},
 1103:                                        $Foildata{$foilid}->{'text'},
 1104:                                        $Foildata{$foilid}->{'value'},]);
 1105:             }                
 1106:         } continue {
 1107:             $foilindex++;
 1108:         }
 1109:     } continue {
 1110:         $conceptindex++;
 1111:     }
 1112:     $rows_output++;
 1113:     $rows_output++;
 1114:     ##
 1115:     ## Option data output
 1116:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
 1117:     foreach my $string (@{$ORdata->{'_Options'}}) {
 1118:         $worksheet->write($rows_output++,0,$string);
 1119:     }
 1120:     return 'okay';
 1121: }
 1122: 
 1123: sub OR_build_student_data_worksheet {
 1124:     my ($worksheet,$format) = @_;
 1125:     my $rows_output = 3;
 1126:     my $cols_output = 0;
 1127:     $worksheet->write($rows_output++,0,'Student Data',$format->{'h3'});
 1128:     my @Headers = ('full name','username','domain','section',
 1129:                    "student\nnumber",'identifier');
 1130:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1131:     my @Students = @Apache::lonstatistics::Students;
 1132:     my $studentrows = &Apache::loncoursedata::get_student_data(\@Students);
 1133:     my %ids;
 1134:     foreach my $row (@$studentrows) {
 1135:         my ($mysqlid,$student) = @$row;
 1136:         $ids{$student}=$mysqlid;
 1137:     }
 1138:     foreach my $student (@Students) {
 1139:         my $name_domain = $student->{'username'}.':'.$student->{'domain'};
 1140:         $worksheet->write_row($rows_output++,0,
 1141:                           [$student->{'fullname'},
 1142:                            $student->{'username'},$student->{'domain'},
 1143:                            $student->{'section'},$student->{'id'},
 1144:                            $ids{$name_domain}]);
 1145:     }
 1146:     return;
 1147: }
 1148: 
 1149: sub OR_build_response_data_worksheet {
 1150:     my ($worksheet,$format,$PerformanceData,$Foils,$ORdata)=@_;
 1151:     my $rows_output = 3;
 1152:     my $cols_output = 0;
 1153:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
 1154:     $worksheet->set_column(1,1,20);
 1155:     $worksheet->set_column(2,2,13);
 1156:     my @Headers = ('identifier','time','award detail','attempt');
 1157:     foreach my $foil (@$Foils) {
 1158:         push (@Headers,$foil.' submission');
 1159:         push (@Headers,$foil.' grading');
 1160:     }
 1161:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1162:     #
 1163:     foreach my $row (@$PerformanceData) {
 1164:         next if (! defined($row));
 1165:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
 1166:         my @Foilgrades = split('&',$grading);
 1167:         my @Foilsubs   = split('&',$submission);
 1168:         my %ResponseData;
 1169:         for (my $j=0;$j<=$#Foilgrades;$j++) {
 1170:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1171:             my (undef,$submission) = split('=',$Foilsubs[$j]);
 1172:             $submission = &Apache::lonnet::unescape($submission);
 1173:             $ResponseData{$foilid.' submission'}=$submission;
 1174:             $ResponseData{$foilid.' award'}=$correct;
 1175:         }
 1176:         $worksheet->write($rows_output,$cols_output++,$student);
 1177:         $worksheet->write($rows_output,$cols_output++,
 1178:                           &calc_serial($time),$format->{'date'});
 1179:         $worksheet->write($rows_output,$cols_output++,$award);
 1180:         $worksheet->write($rows_output,$cols_output++,$tries);
 1181:         foreach my $foilid (@$Foils) {
 1182:             $worksheet->write($rows_output,$cols_output++,
 1183:                               $ResponseData{$foilid.' submission'});
 1184:             $worksheet->write($rows_output,$cols_output++,
 1185:                               $ResponseData{$foilid.' award'});
 1186:         }
 1187:         $rows_output++;
 1188:         $cols_output = 0;
 1189:     }
 1190:     return;
 1191: }
 1192: 
 1193: 
 1194: ##
 1195: ## The following is copied from datecalc1.pl, part of the 
 1196: ## Spreadsheet::WriteExcel CPAN module.
 1197: ##
 1198: ##
 1199: ######################################################################
 1200: #
 1201: # Demonstration of writing date/time cells to Excel spreadsheets,
 1202: # using UNIX/Perl time as source of date/time.
 1203: #
 1204: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
 1205: #
 1206: ######################################################################
 1207: #
 1208: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
 1209: # measured in seconds.
 1210: #
 1211: # An Excel file can use exactly one of two different date/time systems.
 1212: # In these systems, a floating point number represents the number of days
 1213: # (and fractional parts of the day) since a start point. The floating point
 1214: # number is referred to as a 'serial'.
 1215: # The two systems ('1900' and '1904') use different starting points:
 1216: #  '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
 1217: #          a leap year - see:
 1218: #            http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
 1219: #          for the excuse^H^H^H^H^H^Hreason.
 1220: #  '1904'; '1.00' is 2 Jan 1904.
 1221: #
 1222: # The '1904' system is the default for Apple Macs. Windows versions of
 1223: # Excel have the option to use the '1904' system.
 1224: #
 1225: # Note that Visual Basic's "DateSerial" function does NOT erroneously
 1226: # regard 1900 as a leap year, and thus its serials do not agree with
 1227: # the 1900 serials of Excel for dates before 1 Mar 1900.
 1228: #
 1229: # Note that StarOffice (at least at version 5.2) does NOT erroneously
 1230: # regard 1900 as a leap year, and thus its serials do not agree with
 1231: # the 1900 serials of Excel for dates before 1 Mar 1900.
 1232: #
 1233: ######################################################################
 1234: #
 1235: # Calculation description
 1236: # =======================
 1237: #
 1238: # 1900 system
 1239: # -----------
 1240: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
 1241: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
 1242: # were leap years with an extra day.
 1243: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
 1244: # 1 Jan 1970.
 1245: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
 1246: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
 1247: #
 1248: # 1904 system
 1249: # -----------
 1250: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
 1251: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
 1252: # were leap years with an extra day.
 1253: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
 1254: # 1 Jan 1970.
 1255: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
 1256: #
 1257: ######################################################################
 1258: #
 1259: # Copyright (c) 2000, Andrew Benham.
 1260: # This program is free software. It may be used, redistributed and/or
 1261: # modified under the same terms as Perl itself.
 1262: #
 1263: # Andrew Benham, adsb@bigfoot.com
 1264: # London, United Kingdom
 1265: # 11 Nov 2000
 1266: #
 1267: ######################################################################
 1268: 
 1269: # Use 1900 date system on all platforms other than Apple Mac (for which
 1270: # use 1904 date system).
 1271: my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
 1272: 
 1273: #-----------------------------------------------------------
 1274: # calc_serial()
 1275: #
 1276: # Called with (up to) 2 parameters.
 1277: #   1.  Unix timestamp.  If omitted, uses current time.
 1278: #   2.  GMT flag. Set to '1' to return serial in GMT.
 1279: #       If omitted, returns serial in appropriate timezone.
 1280: #
 1281: # Returns date/time serial according to $DATE_SYSTEM selected
 1282: #-----------------------------------------------------------
 1283: sub calc_serial {
 1284:         my $time = (defined $_[0]) ? $_[0] : time();
 1285:         my $gmtflag = (defined $_[1]) ? $_[1] : 0;
 1286: 
 1287:         # Divide timestamp by number of seconds in a day.
 1288:         # This gives a date serial with '0' on 1 Jan 1970.
 1289:         my $serial = $time / 86400;
 1290: 
 1291:         # Adjust the date serial by the offset appropriate to the
 1292:         # currently selected system (1900/1904).
 1293:         if ($DATE_SYSTEM == 0) {        # use 1900 system
 1294:                 $serial += 25569;
 1295:         } else {                        # use 1904 system
 1296:                 $serial += 24107;
 1297:         }
 1298: 
 1299:         unless ($gmtflag) {
 1300:                 # Now have a 'raw' serial with the right offset. But this
 1301:                 # gives a serial in GMT, which is false unless the timezone
 1302:                 # is GMT. We need to adjust the serial by the appropriate
 1303:                 # timezone offset.
 1304:                 # Calculate the appropriate timezone offset by seeing what
 1305:                 # the differences between localtime and gmtime for the given
 1306:                 # time are.
 1307: 
 1308:                 my @gmtime = gmtime($time);
 1309:                 my @ltime  = localtime($time);
 1310: 
 1311:                 # For the first 7 elements of the two arrays, adjust the
 1312:                 # date serial where the elements differ.
 1313:                 for (0 .. 6) {
 1314:                         my $diff = $ltime[$_] - $gmtime[$_];
 1315:                         if ($diff) {
 1316:                                 $serial += _adjustment($diff,$_);
 1317:                         }
 1318:                 }
 1319:         }
 1320: 
 1321:         # Perpetuate the error that 1900 was a leap year by decrementing
 1322:         # the serial if we're using the 1900 system and the date is prior to
 1323:         # 1 Mar 1900. This has the effect of making serial value '60'
 1324:         # 29 Feb 1900.
 1325: 
 1326:         # This fix only has any effect if UNIX/Perl time on the platform
 1327:         # can represent 1900. Many can't.
 1328: 
 1329:         unless ($DATE_SYSTEM) {
 1330:                 $serial-- if ($serial < 61);    # '61' is 1 Mar 1900
 1331:         }
 1332:         return $serial;
 1333: }
 1334: 
 1335: sub _adjustment {
 1336:         # Based on the difference in the localtime/gmtime array elements
 1337:         # number, return the adjustment required to the serial.
 1338: 
 1339:         # We only look at some elements of the localtime/gmtime arrays:
 1340:         #    seconds    unlikely to be different as all known timezones
 1341:         #               have an offset of integral multiples of 15 minutes,
 1342:         #               but it's easy to do.
 1343:         #    minutes    will be different for timezone offsets which are
 1344:         #               not an exact number of hours.
 1345:         #    hours      very likely to be different.
 1346:         #    weekday    will differ when localtime/gmtime difference
 1347:         #               straddles midnight.
 1348:         #
 1349:         # Assume that difference between localtime and gmtime is less than
 1350:         # 5 days, then don't have to do maths for day of month, month number,
 1351:         # year number, etc...
 1352: 
 1353:         my ($delta,$element) = @_;
 1354:         my $adjust = 0;
 1355: 
 1356:         if ($element == 0) {            # Seconds
 1357:                 $adjust = $delta/86400;         # 60 * 60 * 24
 1358:         } elsif ($element == 1) {       # Minutes
 1359:                 $adjust = $delta/1440;          # 60 * 24
 1360:         } elsif ($element == 2) {       # Hours
 1361:                 $adjust = $delta/24;            # 24
 1362:         } elsif ($element == 6) {       # Day of week number
 1363:                 # Catch difference straddling Sat/Sun in either direction
 1364:                 $delta += 7 if ($delta < -4);
 1365:                 $delta -= 7 if ($delta > 4);
 1366: 
 1367:                 $adjust = $delta;
 1368:         }
 1369:         return $adjust;
 1370: }
 1371: 
 1372: sub build_foil_index {
 1373:     my ($ORdata) = @_;
 1374:     return if (! exists($ORdata->{'_Foils'}));
 1375:     my %Foildata = %{$ORdata->{'_Foils'}};
 1376:     my @Foils = sort(keys(%Foildata));
 1377:     my %Concepts;
 1378:     foreach my $foilid (@Foils) {
 1379:         push(@{$Concepts{$Foildata{$foilid}->{'_Concept'}}},
 1380:              $foilid);
 1381:     }
 1382:     undef(@Foils);
 1383:     # Having gathered the concept information in a hash, we now translate it
 1384:     # into an array because we need to be consistent about order.
 1385:     # Also put the foils in order, too.
 1386:     my $sortfunction = sub {
 1387:         my %Numbers = (one   => 1,
 1388:                        two   => 2,
 1389:                        three => 3,
 1390:                        four  => 4,
 1391:                        five  => 5,
 1392:                        six   => 6,
 1393:                        seven => 7,
 1394:                        eight => 8,
 1395:                        nine  => 9,
 1396:                        ten   => 10,);
 1397:         my $a1 = lc($a); 
 1398:         my $b1 = lc($b);
 1399:         if (exists($Numbers{$a1})) {
 1400:             $a1 = $Numbers{$a1};
 1401:         }
 1402:         if (exists($Numbers{$b1})) {
 1403:             $b1 = $Numbers{$b1};
 1404:         }
 1405:         if (($a1 =~/^\d+$/) && ($b1 =~/^\d+$/)) {
 1406:             return $a1 <=> $b1;
 1407:         } else {
 1408:             return $a1 cmp $b1;
 1409:         }
 1410:     };
 1411:     my @Concepts;
 1412:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
 1413:         if (! defined($Concepts{$concept})) {
 1414:             $Concepts{$concept}=[];
 1415: #            next;
 1416:         }
 1417:         push(@Concepts,{ name => $concept,
 1418:                         foils => [@{$Concepts{$concept}}]});
 1419:         push(@Foils,(@{$Concepts{$concept}}));
 1420:     }
 1421:     #
 1422:     # Build up the table of row labels.
 1423:     my $table = '<table border="1" >'."\n";
 1424:     if (@Concepts > 1) {
 1425:         $table .= '<tr>'.
 1426:             '<th>'.&mt('Concept Number').'</th>'.
 1427:             '<th>'.&mt('Concept').'</th>'.
 1428:             '<th>'.&mt('Foil Number').'</th>'.
 1429:             '<th>'.&mt('Foil Name').'</th>'.
 1430:             '<th>'.&mt('Foil Text').'</th>'.
 1431:             '<th>'.&mt('Correct Value').'</th>'.
 1432:             "</tr>\n";
 1433:     } else {
 1434:         $table .= '<tr>'.
 1435:             '<th>'.&mt('Foil Number').'</th>'.
 1436:             '<th>'.&mt('Foil Name').'</th>'.
 1437:             '<th>'.&mt('Foil Text').'</th>'.
 1438:             '<th>'.&mt('Correct Value').'</th>'.
 1439:             "</tr>\n";
 1440:     }        
 1441:     my $conceptindex = 1;
 1442:     my $foilindex = 1;
 1443:     foreach my $concept (@Concepts) {
 1444:         my @FoilsInConcept = @{$concept->{'foils'}};
 1445:         my $firstfoil = shift(@FoilsInConcept);
 1446:         if (@Concepts > 1) {
 1447:             $table .= '<tr>'.
 1448:                 '<td>'.$conceptindex.'</td>'.
 1449:                 '<td>'.&HTML::Entities::encode($concept->{'name'}).'</td>'.
 1450:                 '<td>'.$foilindex++.'</td>'.
 1451:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'}).'</td>'.
 1452:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1453:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'}).'</td>'.
 1454:                 "</tr>\n";
 1455:         } else {
 1456:             $table .= '<tr>'.
 1457:                 '<td>'.$foilindex++.'</td>'.
 1458:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'}).'</td>'.
 1459:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1460:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'}).'</td>'.
 1461:                 "</tr>\n";
 1462:         }
 1463:         foreach my $foilid (@FoilsInConcept) {
 1464:             if (@Concepts > 1) {
 1465:                 $table .= '<tr>'.
 1466:                     '<td></td>'.
 1467:                     '<td></td>'.
 1468:                     '<td>'.$foilindex.'</td>'.
 1469:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'}).'</td>'.
 1470:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1471:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'}).'</td>'.
 1472:                     "</tr>\n";
 1473:             } else {
 1474:                 $table .= '<tr>'.
 1475:                     '<td>'.$foilindex.'</td>'.
 1476:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'}).'</td>'.
 1477:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1478:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'}).'</td>'.
 1479:                     "</tr>\n";
 1480:             }                
 1481:         } continue {
 1482:             $foilindex++;
 1483:         }
 1484:     } continue {
 1485:         $conceptindex++;
 1486:     }
 1487:     $table .= "</table>\n";
 1488:     #
 1489:     # Build option index with color stuff
 1490:     return ($table,\@Foils,\@Concepts);
 1491: }
 1492: 
 1493: sub build_option_index {
 1494:     my ($ORdata)= @_;
 1495:     my $table = "<table>\n";
 1496:     my $optionindex = 0;
 1497:     my @Rows;
 1498:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'_Options'}}) {
 1499:         push (@Rows,
 1500:               '<tr>'.
 1501:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
 1502:               ('&nbsp;'x4).'</td>'.
 1503:               '<td>'.&HTML::Entities::encode($option).'</td>'.
 1504:               "</tr>\n");
 1505:     }
 1506:     shift(@Rows); # Throw away 'correct option chosen' color
 1507:     $table .= join('',reverse(@Rows));
 1508:     $table .= "</table>\n";
 1509: }
 1510: 
 1511: #########################################################
 1512: #########################################################
 1513: ##
 1514: ##   Generic Interface Routines
 1515: ##
 1516: #########################################################
 1517: #########################################################
 1518: sub CreateInterface {
 1519:     ##
 1520:     ## Environment variable initialization
 1521:     if (! exists$ENV{'form.AnalyzeOver'}) {
 1522:         $ENV{'form.AnalyzeOver'} = 'Tries';
 1523:     }
 1524:     ##
 1525:     ## Build the menu
 1526:     my $Str = '';
 1527:     $Str .= '<table cellspacing="5">'."\n";
 1528:     $Str .= '<tr>';
 1529:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
 1530:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
 1531: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
 1532:     $Str .= '<td align="center">&nbsp;</td>';
 1533:     $Str .= '</tr>'."\n";
 1534:     ##
 1535:     ## 
 1536:     $Str .= '<tr><td align="center">'."\n";
 1537:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1538:     $Str .= '</td>';
 1539:     #
 1540:     $Str .= '<td align="center">';
 1541:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1542:     $Str .= '</td>';
 1543:     #
 1544: #    $Str .= '<td align="center">';
 1545:     my $only_seq_with_assessments = sub { 
 1546:         my $s=shift;
 1547:         if ($s->{'num_assess'} < 1) { 
 1548:             return 0;
 1549:         } else { 
 1550:             return 1;
 1551:         }
 1552:     };
 1553:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
 1554:                                               $only_seq_with_assessments);
 1555:     ##
 1556:     ##
 1557:     $Str .= '<td>';
 1558:     { # These braces are here to organize the code, not scope it.
 1559:         {
 1560:             $Str .= '<nobr>'.&mt('Analyze Over ');
 1561:             $Str .= &Apache::loncommon::help_open_topic
 1562:                                                   ('Analysis_Analyze_Over');
 1563:             $Str .='<select name="AnalyzeOver" >';
 1564:             $Str .= '<option value="Tries" ';
 1565:             if (! exists($ENV{'form.AnalyzeOver'}) || 
 1566:                 $ENV{'form.AnalyzeOver'} eq 'Tries'){
 1567:                 # Default to Tries
 1568:                 $Str .= ' selected ';
 1569:             }
 1570:             $Str .= '>'.&mt('Tries').'</option>';
 1571:             $Str .= '<option value="Time" ';
 1572:             $Str .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'Time');
 1573:             $Str .= '>'.&mt('Time').'</option>';
 1574:             $Str .= '</select>';
 1575:             $Str .= '</nobr><br />';
 1576:         }
 1577:         {
 1578:             $Str .= '<nobr>'.&mt('Analyze as ');
 1579:             $Str .= &Apache::loncommon::help_open_topic
 1580:                                                   ('Analysis_Analyze_as');
 1581:             $Str .='<select name="AnalyzeAs" >';
 1582:             $Str .= '<option value="Concepts" ';
 1583:             if (! exists($ENV{'form.AnalyzeAs'}) || 
 1584:                 $ENV{'form.AnalyzeAs'} eq 'Concepts'){
 1585:                 # Default to Concepts
 1586:                 $Str .= ' selected ';
 1587:             }
 1588:             $Str .= '>'.&mt('Concepts').'</option>';
 1589:             $Str .= '<option value="Foils" ';
 1590:             $Str .= ' selected ' if ($ENV{'form.AnalyzeAs'} eq 'Foils');
 1591:             $Str .= '>'.&mt('Foils').'</option>';
 1592:             $Str .= '</select></nobr><br />';
 1593:         }
 1594:         {
 1595:             $Str .= '<br /><nobr>'.&mt('Number of Plots:');
 1596:             $Str .= &Apache::loncommon::help_open_topic
 1597:                                                   ('Analysis_num_plots');
 1598:             $Str .= '<select name="NumPlots">';
 1599:             if (! exists($ENV{'form.NumPlots'}) 
 1600:                 || $ENV{'form.NumPlots'} < 1 
 1601:                 || $ENV{'form.NumPlots'} > 20) {
 1602:                 $ENV{'form.NumPlots'} = 5;
 1603:             }
 1604:             foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
 1605:                 $Str .= '<option value="'.$i.'" ';
 1606:                 if ($ENV{'form.NumPlots'} == $i) { $Str.=' selected '; }
 1607:                 $Str .= '>'.$i.'</option>';
 1608:             }
 1609:             $Str .= '</select></nobr>';
 1610:         }
 1611:     }
 1612:     $Str .= '</td>';
 1613:     ##
 1614:     ##
 1615:     $Str .= '</tr>'."\n";
 1616:     $Str .= '</table>'."\n";
 1617:     return $Str;
 1618: }
 1619: 
 1620: #########################################################
 1621: #########################################################
 1622: ##
 1623: ##              Misc Option Response functions
 1624: ##
 1625: #########################################################
 1626: #########################################################
 1627: sub get_time_from_row {
 1628:     my ($row) = @_;
 1629:     if (ref($row)) {
 1630:         return $row->[&Apache::loncoursedata::RD_timestamp()];
 1631:     } 
 1632:     return undef;
 1633: }
 1634: 
 1635: sub get_tries_from_row {
 1636:     my ($row) = @_;
 1637:     if (ref($row)) {
 1638:         return $row->[&Apache::loncoursedata::RD_tries()];
 1639:     }
 1640:     return undef;
 1641: }
 1642: 
 1643: sub hashify_attempt {
 1644:     my ($row) = @_;
 1645:     my %attempt;
 1646:     $attempt{'tries'}      = $row->[&Apache::loncoursedata::RD_tries()];
 1647:     $attempt{'submission'} = $row->[&Apache::loncoursedata::RD_submission()];
 1648:     $attempt{'award'}      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1649:     $attempt{'timestamp'}  = $row->[&Apache::loncoursedata::RD_timestamp()];
 1650:     return %attempt;
 1651: }
 1652: 
 1653: sub Process_OR_Row {
 1654:     my ($row) = @_;
 1655:     my %RowData;
 1656:     my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 1657:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1658:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
 1659:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
 1660:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
 1661:     my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
 1662:     return undef if ($award eq 'MISSING_ANSWER');
 1663:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
 1664:         $RowData{'_correct'} = 1;
 1665:     }
 1666:     $RowData{'_total'} = 1;
 1667:     my @Foilgrades = split('&',$grading);
 1668:     my @Foilsubs   = split('&',$submission);
 1669:     for (my $j=0;$j<=$#Foilgrades;$j++) {
 1670:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1671:         $foilid = &Apache::lonnet::unescape($foilid);
 1672:         my (undef,$submission) = split('=',$Foilsubs[$j]);
 1673:         if ($correct) {
 1674:             $RowData{$foilid}->{'_correct'}++;
 1675:         } else {
 1676:             $submission = &Apache::lonnet::unescape($submission);
 1677:             $RowData{$foilid}->{$submission}++;
 1678:         }
 1679:         $RowData{$foilid}->{'_total'}++;
 1680:     }
 1681:     return %RowData;
 1682: }
 1683: 
 1684: 
 1685: sub analyze_problem_as_student {
 1686:     my ($resource,$sname,$sdom,$partid,$respid) = @_;
 1687:     my $url = $resource->{'src'};
 1688:     my $symb = $resource->{'symb'};
 1689:     my $courseid = $ENV{'request.course.id'};
 1690:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze',
 1691:                                         'grade_domain' => $sdom,
 1692:                                         'grade_username' => $sname,
 1693:                                         'grade_symb' => $symb,
 1694:                                         'grade_courseid' => $courseid));
 1695:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
 1696:     my %Answer=&Apache::lonnet::str2hash($Answ);
 1697:     my $key = $partid.'.'.$respid.'.answer';
 1698:     my $student_answer = $Answer{$key}->[0];
 1699:     if (! defined($student_answer)) {
 1700:         $student_answer = $Answer{$key}->[1];
 1701:     }
 1702:     return ($student_answer);
 1703: }
 1704: 
 1705: ##
 1706: ## get problem data and put it into a useful data structure.
 1707: ## note: we must force each foil and option to not begin or end with
 1708: ##       spaces as they are stored without such data.
 1709: ##
 1710: sub get_problem_data {
 1711:     my ($url) = @_;
 1712:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
 1713:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
 1714:     my %Answer;
 1715:     %Answer=&Apache::lonnet::str2hash($Answ);
 1716:     my %Partdata;
 1717:     foreach my $part (@{$Answer{'parts'}}) {
 1718:         while (my($key,$value) = each(%Answer)) {
 1719:             #
 1720:             # Logging code:
 1721:             if (0) {
 1722:                 &Apache::lonnet::logthis($part.' got key "'.$key.'"');
 1723:                 if (ref($value) eq 'ARRAY') {
 1724:                     &Apache::lonnet::logthis('    '.join(',',@$value));
 1725:                 } else {
 1726:                     &Apache::lonnet::logthis('    '.$value);
 1727:                 }
 1728:             }
 1729:             # End of logging code
 1730:             next if ($key !~ /^$part/);
 1731:             $key =~ s/^$part\.//;
 1732:             if (ref($value) eq 'ARRAY') {
 1733:                 if ($key eq 'options') {
 1734:                     $Partdata{$part}->{'_Options'}=$value;
 1735:                 } elsif ($key eq 'concepts') {
 1736:                     $Partdata{$part}->{'_Concepts'}=$value;
 1737:                 } elsif ($key =~ /^concept\.(.*)$/) {
 1738:                     my $concept = $1;
 1739:                     foreach my $foil (@$value) {
 1740:                         $Partdata{$part}->{'_Foils'}->{$foil}->{'_Concept'}=
 1741:                                                                       $concept;
 1742:                     }
 1743:                 } elsif ($key =~ /^(incorrect|answer|ans_low|ans_high)$/) {
 1744:                     $Partdata{$part}->{$key}=$value;
 1745:                 }
 1746:             } else {
 1747:                 if ($key=~ /^foil\.text\.(.*)$/) {
 1748:                     my $foil = $1;
 1749:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'name'}=$foil;
 1750:                     $value =~ s/(\s*$|^\s*)//g;
 1751:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'text'}=$value;
 1752:                 } elsif ($key =~ /^foil\.value\.(.*)$/) {
 1753:                     my $foil = $1;
 1754:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'value'}=$value;
 1755:                 }
 1756:             }
 1757:         }
 1758:     }
 1759:     return %Partdata;
 1760: }
 1761: 
 1762: 1;
 1763: 
 1764: __END__

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