File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.63: download - view: text, annotated - select for diffs
Fri Jan 30 16:31:36 2004 UTC (20 years, 5 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Bug 2675: html entities not handled correctly in option response analysis
display of concepts and options.  Can go in 1.1.2 without issues.

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

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