File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.58: download - view: text, annotated - select for diffs
Mon Jan 19 18:55:10 2004 UTC (20 years, 5 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
1. Removed debugging code.
2. Unescape the foilid.  This fixes a bug with spaces in foilids causing
data to be missing in the plots.

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

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