File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.64: download - view: text, annotated - select for diffs
Mon Feb 2 19:32:11 2004 UTC (20 years, 5 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added \@Labels to DrawBarGraph inputs (labels on the x-axis).

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

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