Annotation of loncom/interface/statistics/lonproblemanalysis.pm, revision 1.44

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

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