File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.60: download - view: text, annotated - select for diffs
Mon Jan 19 21:31:08 2004 UTC (20 years, 5 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added entry for lonstathelpers.pm in loncapafiles.
Modified lonsubmissiontimeanalysis and lonproblemanalysis to use
lonstathelpers.

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

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