File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.56: download - view: text, annotated - select for diffs
Fri Jan 16 20:19:33 2004 UTC (20 years, 5 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Implement basic radio response analysis.

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

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