File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.52: download - view: text, annotated - select for diffs
Mon Dec 15 16:23:00 2003 UTC (20 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added help file and help link for problem analysis.

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

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