File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.109: download - view: text, annotated - select for diffs
Thu Dec 23 19:57:35 2004 UTC (19 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
re-enable numerical response analysis with units.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemanalysis.pm,v 1.109 2004/12/23 19:57:35 matthew Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: package Apache::lonproblemanalysis;
   28: 
   29: use strict;
   30: use Apache::lonnet();
   31: use Apache::loncommon();
   32: use Apache::lonhtmlcommon();
   33: use Apache::loncoursedata();
   34: use Apache::lonstatistics;
   35: use Apache::lonlocal;
   36: use Apache::lonstathelpers();
   37: use Apache::lonstudentsubmissions();
   38: use HTML::Entities();
   39: use Time::Local();
   40: use Spreadsheet::WriteExcel();
   41: use capa;
   42: 
   43: my $plotcolors = ['#33ff00', 
   44:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
   45:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
   46:                   ]; 
   47: 
   48: my @SubmitButtons = ({ name => 'PrevProblemAnalysis',
   49:                        text => 'Previous Problem' },
   50:                      { name => 'ProblemAnalysis',
   51:                        text => 'Analyze Problem Again' },
   52:                      { name => 'NextProblemAnalysis',
   53:                        text => 'Next Problem' },
   54:                      { name => 'break'},
   55:                      { name => 'SelectAnother',
   56:                        text => 'Choose a different Problem' },
   57:                      { name => 'ExcelOutput',
   58:                        text => 'Produce Excel Output' });
   59: 
   60: sub BuildProblemAnalysisPage {
   61:     my ($r,$c)=@_;
   62:     #
   63:     my %Saveable_Parameters = ('Status' => 'scalar',
   64:                                'Section' => 'array',
   65:                                'NumPlots' => 'scalar',
   66:                                'AnalyzeOver' => 'scalar',
   67:                                );
   68:     &Apache::loncommon::store_course_settings('problem_analysis',
   69:                                               \%Saveable_Parameters);
   70:     &Apache::loncommon::restore_course_settings('problem_analysis',
   71:                                                 \%Saveable_Parameters);
   72:     #
   73:     &Apache::lonstatistics::PrepareClasslist();
   74:     #
   75:     $r->print(&CreateInterface());
   76:     #
   77:     my @Students = @Apache::lonstatistics::Students;
   78:     #
   79:     if (@Students < 1 && exists($ENV{'form.firstrun'})) {
   80:         $r->print('<h2>There are no students in the sections selected</h2>');
   81:     }
   82:     #
   83:     my @CacheButtonHTML = 
   84:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
   85:     $r->rflush();
   86:     #
   87:     # Support for numerical and radio response isn't complete enough to
   88:     # include in 1.2 release.
   89:     my $problem_types = '(option|radiobutton|numerical)';
   90:     # my $problem_types = '.';#(option)';
   91:     if (exists($ENV{'form.problemchoice'}) && 
   92:         ! exists($ENV{'form.SelectAnother'})) {
   93:         foreach my $button (@SubmitButtons) {
   94:             if ($button->{'name'} eq 'break') {
   95:                 $r->print("<br />\n");
   96:             } else {
   97:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
   98:                           'value="'.&mt($button->{'text'}).'" />');
   99:                 $r->print('&nbsp;'x5);
  100:             }
  101:         }
  102:         foreach my $html (@CacheButtonHTML) {
  103:             $r->print($html.('&nbsp;'x5));
  104:         }
  105:         #
  106:         $r->print('<hr />');
  107:         $r->rflush();
  108:         #
  109:         # Determine which problem we are to analyze
  110:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
  111:             ($ENV{'form.problemchoice'});
  112:         #
  113:         my ($prev,$curr,$next) = 
  114:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  115:                                                         $problem_types,
  116:                                                         'response',
  117:                                                         );
  118:         if (exists($ENV{'form.PrevProblemAnalysis'}) && defined($prev)) {
  119:             $current_problem = $prev;
  120:         } elsif (exists($ENV{'form.NextProblemAnalysis'}) && defined($next)) {
  121:             $current_problem = $next;
  122:         } else {
  123:             $current_problem = $curr;
  124:         }
  125:         #
  126:         # Store the current problem choice and send it out in the form
  127:         $ENV{'form.problemchoice'} = 
  128:             &Apache::lonstathelpers::make_target_id($current_problem);
  129:         $r->print('<input type="hidden" name="problemchoice" value="'.
  130:                   $ENV{'form.problemchoice'}.'" />');
  131:         #
  132:         if (! defined($current_problem->{'resource'})) {
  133:             $r->print('resource is undefined');
  134:         } else {
  135:             my $resource = $current_problem->{'resource'};
  136:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  137:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  138:             if ($ENV{'form.show_prob'} eq 'true') {
  139:                 $r->print(&Apache::lonstathelpers::render_resource($resource));
  140:             }
  141:             $r->rflush();
  142:             my %Data = &Apache::lonstathelpers::get_problem_data
  143:                 ($resource->{'src'});
  144:             my $problem_data = $Data{$current_problem->{'part'}.
  145:                                     '.'.
  146:                                     $current_problem->{'respid'}};
  147:             if ($current_problem->{'resptype'} eq 'option') {
  148:                 &OptionResponseAnalysis($r,$current_problem,
  149:                                         $problem_data,
  150:                                         \@Students);
  151:             } elsif ($current_problem->{'resptype'} eq 'radiobutton') {
  152:                 &radio_response_analysis($r,$current_problem,
  153:                                          $problem_data,
  154:                                          \@Students);
  155:             } elsif ($current_problem->{'resptype'} eq 'numerical') {
  156:                 &numerical_response_analysis($r,$current_problem,
  157:                                              $problem_data,\@Students);
  158:             } else {
  159:                 $r->print('<h2>Analysis of '.$current_problem->{'resptype'}.' is not supported</h2>');
  160:             }
  161:         }
  162:         $r->print('<hr />');
  163:     } else {
  164:         $r->print('<input type="submit" name="ProblemAnalysis" value="'.
  165:                   &mt('Analyze Problem').'" />');
  166:         $r->print('&nbsp;'x5);
  167:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
  168:         $r->print(&Apache::lonstathelpers::ProblemSelector
  169:                   ($problem_types));
  170:     }
  171: }
  172: 
  173: #########################################################
  174: #########################################################
  175: ##
  176: ##      Numerical Response Routines
  177: ##
  178: #########################################################
  179: #########################################################
  180: sub numerical_response_analysis {
  181:     my ($r,$problem,$problem_analysis,$students) = @_;
  182:     my $c = $r->connection();
  183:     #
  184:     if ($ENV{'form.AnalyzeOver'} !~ /^(tries|time)$/) {
  185:         $r->print('Bad request');
  186:     }
  187:     #
  188:     my ($resource,$partid,$respid) = ($problem->{'resource'},
  189:                                       $problem->{'part'},
  190:                                       $problem->{'respid'});
  191:     # Gather student data
  192:     my $response_data = &Apache::loncoursedata::get_response_data
  193:         (\@Apache::lonstatistics::SelectedSections,
  194:          $Apache::lonstatistics::enrollment_status,
  195:          $resource->{'symb'},$respid);
  196:     #
  197:     $problem_analysis->{'answercomputed'} = 1;
  198:     if ($problem_analysis->{'answercomputed'}) {
  199:         my $answers = 
  200:             &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$students,
  201:                                                        'Statistics',
  202:                                                        'stats_status');
  203:         $r->print(&numerical_one_dimensional_plot($r,600,150,$answers));
  204:     }
  205:     #
  206:     if (ref($response_data) ne 'ARRAY') {
  207:         $r->print('<h2>'.
  208:                   &mt('There is no submission data for this resource').
  209:                   '</h2>');
  210:         return;
  211:     }
  212:     my $analysis_html = '<table>';
  213:     for (my $plot_num = 1;$plot_num<=$ENV{'form.NumPlots'};$plot_num++) {
  214:         my $restriction_function;
  215:         my $header_message;
  216:         my $stats_message;
  217:         my $post_message; # passed through &mt sooner rather than later
  218:         my $no_data_message;
  219:         my @extra_data;
  220:         if ($ENV{'form.AnalyzeOver'} eq 'tries') {
  221:             $restriction_function = sub {($_[0]->{'tries'} == $plot_num?1:0)};
  222:             $header_message = 'Attempt [_1]';
  223:             $stats_message = 
  224:                 '[_1] submissions, [_2] correct, [_3] incorrect';
  225:             $post_message = '';
  226:             $no_data_message = 'No data exists for attempt [_1]';
  227:         } else {
  228:             my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  229:                 ('startdate_'.$plot_num);
  230:             my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  231:                 ('enddate_'.$plot_num);
  232:             ($starttime,$endtime) = &ensure_start_end_times
  233:                 ($starttime,$endtime,
  234:                  &get_time_from_row($response_data->[0]),
  235:                  &get_time_from_row($response_data->[-1]),
  236:                  $plot_num);
  237:             $header_message = 'Data from [_2] to [_3]';
  238:             $extra_data[0] = &Apache::lonlocal::locallocaltime($starttime);
  239:             $extra_data[1] = &Apache::lonlocal::locallocaltime($endtime);
  240:             #
  241:             $stats_message = 
  242:                 '[_1] submissions from [_4] students, [_2] correct, [_3] incorrect';
  243:             #
  244:             $post_message = 
  245:                 &mt('Start time: [_1]',
  246:                     &Apache::lonhtmlcommon::date_setter
  247:                     ('Statistics','startdate_'.$plot_num,$starttime)).
  248:                 '<br />'.
  249:                 &mt('End time: [_1]',
  250:                     &Apache::lonhtmlcommon::date_setter
  251:                     ('Statistics','enddate_'.$plot_num,$endtime));
  252:             $restriction_function = 
  253:                 sub { 
  254:                     my $t = $_[0]->{'timestamp'};
  255:                     if ($t >= $starttime && $t < $endtime) {
  256:                         return 1;
  257:                     } else { 
  258:                         return 0;
  259:                     }
  260:                 };
  261:             $no_data_message = 'No data for [_2] to [_3]';
  262:         }
  263:         #
  264:         my ($correct,$answers) = 
  265:             &numerical_determine_answers($r,$resource,$partid,
  266:                                          $respid,$students);
  267:         if ($c->aborted()) { return; };
  268:         #
  269:         my $responses = &numerical_classify_responses($response_data,$correct,
  270:                                                       $restriction_function);
  271:         if ($responses->{'_count'} == 0) {
  272:             $analysis_html.= 
  273:                 '<tr><td colspan="2"><font size="+1"><b>'.
  274:                 &mt($no_data_message,$plot_num,@extra_data).
  275:                 '</b></font></td></tr>';
  276:         } else {
  277:             $analysis_html.= 
  278:                 '<tr><td colspan="2" align="center"><font size="+1"><b>'.
  279:                 &mt($header_message,$plot_num,@extra_data).
  280:                 '</b></font></td></tr>'.
  281:                 '<tr><td colspan="2" align="center">'.
  282:                 &mt($stats_message,
  283:                     $responses->{'_count'},
  284:                     $responses->{'_correct'},
  285:                     $responses->{'_count'}-$responses->{'_correct'},
  286:                     $responses->{'_students'},
  287:                     @extra_data).
  288:                     '</td></tr>'.
  289:                     '<tr>'.'<td valign="top" align="center">'.
  290:                     &numerical_plot_percent($r,$responses).'</td>'.
  291:                     '<td align="center" valign="top">'.
  292:                     &numerical_plot_differences($r,$responses).'</td>'.
  293:                     '</tr>';
  294:         }
  295:         if ($post_message ne '') {
  296:             $analysis_html .=
  297:                 '<tr><td colspan="2">'.$post_message.'</td></tr>';
  298:         }
  299:     }
  300:     $analysis_html.='</table>';
  301:     $r->print($analysis_html);
  302:     #
  303:     return;
  304: }
  305: 
  306: sub numerical_plot_percent {
  307:     my ($r,$responses) = @_;
  308:     #
  309:     my $total = $responses->{'_count'};
  310:     return '' if ($total == 0);
  311:     my $minbin = 5;
  312:     while (my ($interval,$submissions) = each(%$responses)) {
  313:         next if ($interval =~ /^_/);
  314:         my ($ans,$ans_low,$ans_high) = split(" ",$interval);
  315:         my $low_percent  = abs(100*($ans-$ans_low)/$ans);
  316:         my $high_percent = abs(100*($ans_high-$ans)/$ans);
  317:         if ($minbin > $high_percent) { $minbin = $high_percent; }
  318:         if ($minbin > $low_percent) { $minbin = $low_percent; }
  319:     }
  320:     #
  321:     my @bins;
  322:     if ($minbin < 1) {
  323:         @bins = ('0.1','0.5','1.0','1.5','2.0','2.5','3.0','4.0','5.0',10,20,50,100);        
  324:     } elsif ($minbin < 2) {
  325:         @bins = ('0.5','1.0','1.5','2.0','2.5','3.0','4.0','5.0',10,20,50,100);
  326:     } elsif ($minbin < 5) {
  327:         @bins = (1,2,3,4,5,10,25,50,75,100,200);
  328:     } elsif ($minbin < 10) {
  329:         @bins = (2,4,6,8,10,12,15,20,25,30,50,75,100,200);
  330:     } else {
  331:         @bins = (5,10,15,20,25,30,50,75,100,200);
  332:     }
  333:     my @labels = (1..scalar(@bins));
  334:     #
  335:     my @correct;
  336:     my @incorrect;
  337:     my @count;
  338:     while (my ($interval,$submissions) = each(%$responses)) {
  339:         next if ($interval =~ /^_/);
  340:         my ($ans,$ans_low,$ans_high) = split(" ",$interval);
  341:         while (my ($submission,$counts) = each(%$submissions)) {
  342:             my ($correct_count,$incorrect_count) = @$counts;
  343:             my $scaled_value = abs(($submission-$ans)/$ans);
  344:             my $bin=0;
  345:             for ($bin=0;$bin<$#bins;$bin++) { # not <= for a reason
  346:                 last if ($bins[$bin]>$scaled_value);
  347:             }
  348:             $correct[$bin]+=$correct_count;
  349:             $incorrect[$bin]+=$incorrect_count;
  350:             $count[$bin]+=$correct_count+$incorrect_count;
  351:         }
  352:     }
  353:     #
  354:     my @plot_correct;
  355:     my @plot_incorrect;
  356:     for (my $i=0;$i<=$#bins;$i++) {
  357:         $plot_correct[$i] = $correct[$i]*100/$total;
  358:         $plot_incorrect[$i] = $incorrect[$i]*100/$total;
  359:     }
  360:     my $title = &mt('Distribution by Percent');
  361:     my $graph = &Apache::loncommon::DrawBarGraph
  362:         ($title,'Percent difference from correct','Number of answers',
  363:          100,['#33FF00','#FF3300'],\@labels,\@plot_correct,\@plot_incorrect,
  364:          {xskip=>1});
  365:     #
  366:     my $table = $graph.$/.
  367:         &numerical_bin_table(\@bins,\@labels,\@incorrect,\@correct,\@count).$/;
  368:     return $table;
  369: }
  370: 
  371: sub numerical_plot_differences {
  372:     my ($r,$responses) = @_;
  373:     #
  374:     my $total = $responses->{'_count'};
  375:     return '' if ($total == 0);
  376:     my $minbin = undef;
  377:     my $maxbin = undef;
  378:     while (my ($interval,$submissions) = each(%$responses)) {
  379:         next if ($interval =~ /^_/);
  380:         my ($ans,$ans_low,$ans_high) = split(" ",$interval);
  381:         my $low_diff  = abs($ans-$ans_low);
  382:         my $high_diff = abs($ans_high-$ans);
  383:         if (! defined($maxbin)) { $maxbin = $low_diff;}
  384:         if (! defined($minbin)) { $minbin = $low_diff;}
  385:         #
  386:         if ($minbin > $high_diff) { $minbin = $high_diff; }
  387:         if ($minbin > $low_diff ) { $minbin = $low_diff; }
  388:         #
  389:         if ($maxbin < $high_diff) { $maxbin = $high_diff; }
  390:         if ($maxbin < $low_diff ) { $maxbin = $low_diff; }
  391:     }    
  392:     #
  393:     my @bins;
  394:     my @labels;
  395:     # Hmmmm, should switch to absolute difference
  396:     for (my $i=1;$i<=20;$i++) {
  397:         push(@bins,$i*$minbin/2);
  398:         push(@labels,$i);
  399:     }
  400:     #
  401:     my @correct;
  402:     my @incorrect;
  403:     my @count;
  404:     while (my ($interval,$submissions) = each(%$responses)) {
  405:         next if ($interval =~ /^_/);
  406:         my ($ans,$ans_low,$ans_high) = split(" ",$interval);
  407:         while (my ($submission,$counts) = each(%$submissions)) {
  408:             my ($correct_count,$incorrect_count) = @$counts;
  409:             my $value = abs($submission-$ans);
  410:             my $bin=0;
  411:             for ($bin=0;$bin<$#bins;$bin++) { # not <= for a reason
  412:                 last if ($bins[$bin]>$value);
  413:             }
  414:             $correct[$bin]+=$correct_count;
  415:             $incorrect[$bin]+=$incorrect_count;
  416:             $count[$bin]+=$correct_count+$incorrect_count;
  417:         }
  418:     }
  419:     #
  420:     my @plot_correct;
  421:     my @plot_incorrect;
  422:     for (my $i=0;$i<=$#bins;$i++) {
  423:         $plot_correct[$i]   =   $correct[$i]*100/$total;
  424:         $plot_incorrect[$i] = $incorrect[$i]*100/$total;
  425:     }
  426:     my $title = &mt('Distribution by Magnitude');
  427:     my $graph = &Apache::loncommon::DrawBarGraph
  428:         ($title,'magnitude difference from correct','Number of answers',
  429:          100,['#33FF00','#FF3300'],\@labels,\@plot_correct,\@plot_incorrect,
  430:          {xskip=>1});
  431:     #
  432:     my $table = $graph.$/.
  433:         &numerical_bin_table(\@bins,\@labels,\@incorrect,\@correct,\@count).$/;
  434:     return $table;
  435: }
  436: 
  437: sub numerical_classify_responses {
  438:     &Apache::lonnet::logthis('--------------');
  439:     my ($full_row_data,$correct,$function) = @_;
  440:     my %submission_data;
  441:     my %students;
  442:     my $max=0;
  443:     foreach my $row (@$full_row_data) {
  444: #        &Apache::lonnet::logthis(' row = '.join(',',@$row));
  445:         my %subm = &hashify_attempt($row);
  446:         if (ref($correct) eq 'HASH') {
  447:             $subm{'correct'} = $correct->{$subm{'student'}}->{'answer'};
  448:             $subm{'unit'} = $correct->{$subm{'student'}}->{'unit'};
  449:         } else { # This probably never happens....
  450:             $subm{'correct'} = $correct->{'answer'};
  451:             $subm{'unit'} = $correct->{'unit'};
  452:         }
  453:         $subm{'submission'} =~ s/=\d+\s*$//;
  454:         if (&$function(\%subm)) {
  455:             my $scaled = '1';
  456:             my ($sname,$sdom) = split(':',$subm{'student'});
  457:             # Note that $subm{'unit'} is modified by the following call
  458:             # We do not use it again but you should be aware just in case.
  459:             my ($myunit,$mysub) = ($subm{'unit'},$subm{'submission'});
  460:             my $result = 
  461:                 &capa::caparesponse_get_real_response($myunit,
  462:                                                       $mysub,
  463:                                                       \$scaled);
  464:             next if (! defined($scaled));
  465:             next if ($result ne '6');
  466:             my $submission = $scaled;
  467:             $students{$subm{'student'}}++;
  468:             if (&numerical_submission_is_correct($subm{'award'})) { 
  469:                 &Apache::lonnet::logthis('correct:'.$submission.':'.$subm{'correct'});
  470:                 $submission_data{'_correct'}++;
  471:                 $submission_data{'_count'}++;
  472:                 $submission_data{$subm{'correct'}}->{$submission}->[0]++;
  473:             } elsif (&numerical_submission_is_incorrect($subm{'award'})) { 
  474:                 &Apache::lonnet::logthis('incorrect:'.$submission.':'.$subm{'correct'});
  475:                 $submission_data{'_count'}++;
  476:                 $submission_data{$subm{'correct'}}->{$submission}->[1]++;
  477:             }
  478:             my $value = 
  479:                 $submission_data{$subm{'correct'}}->{$submission}->[0]+ 
  480:                 $submission_data{$subm{'correct'}}->{$submission}->[1];
  481:             if ($max < $value) { $max = $value; }
  482:         }
  483:     }
  484:     $submission_data{'_max'} = $max;
  485:     $submission_data{'_students'}=scalar(keys(%students));
  486:     return \%submission_data;
  487: }
  488: 
  489: sub numerical_submission_is_correct {
  490:     my ($award) = @_;
  491:     &Apache::lonnet::logthis('award = "'.$award.'"');
  492:     if ($award =~ /^(APPROX_ANS|EXACT_ANS)$/) {
  493:         return 1;
  494:     } else {
  495:         return 0;
  496:     }
  497: }
  498: 
  499: sub numerical_submission_is_incorrect {
  500:     my ($award) = @_;
  501:     if ($award =~ /^(INCORRECT)$/) {
  502:         return 1;
  503:     } else {
  504:         return 0;
  505:     }
  506: }
  507: 
  508: sub numerical_bin_table {
  509:     my ($bins,$labels,$incorrect,$correct,$count)=@_;
  510:     my $table = 
  511:         '<table><tr><th>'.&mt('Bar').'</th>'.
  512:         '<th colspan="3">'.&mt('Range').'</th>'.
  513:         '<th>'.&mt('Incorrect').'</th>'.
  514:         '<th>'.&mt('Correct').'</th>'.
  515:         '<th>'.&mt('Count').'</th>'.
  516:         '</tr>'.$/;
  517:     for (my $i=0;$i<scalar(@{$bins});$i++) {
  518:         my $lownum;
  519:         if ($i == 0) {
  520:             $lownum = 0;
  521:         } else {
  522:             $lownum = $bins->[$i-1];
  523:         }
  524:         my $highnum = $bins->[$i];
  525:         $table .= 
  526:             '<tr>'.
  527:             '<td>'.$labels->[$i].'</td>'.
  528:             '<td align="right">'.$lownum.'</td>'.
  529:             '<td>&nbsp;-&nbsp;</td>'.
  530:             '<td align="right">'.$highnum.'</td>'.
  531:             '<td align="right">'.$incorrect->[$i].'</td>'.
  532:             '<td align="right">'.$correct->[$i].'</td>'.
  533:             '<td align="right">'.$count->[$i].'</td>'.
  534:             '</tr>'.$/;
  535:     }
  536:     $table.= '</table>';
  537:     return $table;
  538: }
  539: 
  540: sub numerical_determine_answers {
  541:     my ($r,$resource,$partid,$respid,$students)=@_;
  542:     my $c = $r->connection();
  543:     #
  544:     # FIX ME: May need progress dialog updates
  545:     #
  546:     # Read in the cache (if it exists) before we start timing things.
  547:     &Apache::lonstathelpers::ensure_proper_cache($resource->{'symb'});
  548:     #
  549:     my $correct;
  550:     my %answers;
  551:     foreach my $student (@$students) {
  552:         last if ($c->aborted());
  553:         my $sname = $student->{'username'};
  554:         my $sdom = $student->{'domain'};
  555:         # analyze problem
  556:         my $analysis = 
  557:             &Apache::lonstathelpers::analyze_problem_as_student($resource,
  558:                                                                 $sname,
  559:                                                                 $sdom);
  560:         # make the key
  561:         my $key = $partid.'.'.$respid;
  562:         $correct->{$sname.':'.$sdom}->{'answer'} = 
  563:             $analysis->{$key.'.answer'}->[0];
  564:         $correct->{$sname.':'.$sdom}->{'unit'} = 
  565:             $analysis->{$key.'.unit'}->[0];
  566:         $answers{$analysis->{$key.'.answer'}->[0]}++;
  567:     }
  568:     &Apache::lonstathelpers::write_analysis_cache();
  569:     return ($correct,\%answers);
  570: }
  571: 
  572: #
  573: # Inputs: $r, $width, $height, $data
  574: #         $n = number of students
  575: #         $data = hashref of $answer => $frequency pairs
  576: sub numerical_one_dimensional_plot {
  577:     my ($r,$width,$height,$data)=@_;
  578:     #
  579:     # Compute data -> image scaling factors
  580:     my $max_y = 0;
  581:     my $min_x = undef;
  582:     my $max_x = undef;
  583:     my $n = 0;
  584:     while (my ($answer,$count) = each(%$data)) {
  585:         $n+=$count;
  586:         $max_y = $count if ($max_y < $count);
  587:         if (! defined($min_x) || $answer < $min_x) {
  588:             $min_x = $answer;
  589:         }
  590:         if (! defined($max_x) || $answer > $max_x) {
  591:             $max_x = $answer;
  592:         }
  593:     }
  594:     #
  595:     my $min_max_difference = $max_x - $min_x;
  596:     if (! defined($min_max_difference) || $min_max_difference == 0) {
  597:         $min_max_difference = 1;
  598:     }
  599:     my $h_scale = ($width-10)/$min_max_difference;
  600:     #
  601:     my $ticscale = 5;
  602:     if ($max_y * $ticscale > $height/2) {
  603:         $ticscale = int($height/2/$max_y);
  604:         $ticscale = 1 if ($ticscale < 1);
  605:     }
  606:     #
  607:     # Create the plot
  608:     my $plot = 
  609:         qq{<drawimage width="$width" height="$height" bgcolor="transparent" >};
  610:     while (my ($answer,$count) = each(%$data)) {
  611:         my $xloc = 5+$h_scale*($answer - $min_x);
  612:         my $top    = $height/2-$count*$ticscale;
  613:         my $bottom = $height/2+$count*$ticscale;
  614:         $plot .= &line($xloc,$top,$xloc,$bottom,'888888',1);
  615:     }
  616:     #
  617:     # Put the scale on last to ensure it is on top of the data.
  618:     if ($min_x < 0 && $max_x > 0) {
  619:         my $circle_x = 5+$h_scale*abs($min_x);  # '0' in data coordinates
  620:         my $r = 4;
  621:         $plot .= &line(5,$height/2,$circle_x-$r,$height/2,'000000',1);
  622:         $plot .= &circle($circle_x,$height/2,$r+1,'000000');
  623:         $plot .= &line($circle_x+$r,$height/2,$width-5,$height/2,'000000',1);
  624:     } else {
  625:         $plot .= &line(5,$height/2,$width-5,$height/2,'000000',1);
  626:     }
  627:     $plot .= '</drawimage>';
  628:     my $plotresult =  &Apache::lonxml::xmlparse($r,'web',$plot);
  629:     my $result = '<table>'.
  630:         '<tr><td colspan="3" align="center">'.
  631:         '<font size="+2">'.&mt('Distribution of correct answers').'</font>'.
  632:         '<br />'.&mt('[_1] students, [_2] distinct correct answers',
  633:                      $n,scalar(keys(%$data))).
  634:         '<br />'.&mt('Maximum number of coinciding values: [_1]',$max_y).
  635:         '</td></tr>'.
  636:         '<tr>'.
  637:         '<td valign="center">'.$min_x.'</td>'.
  638:         '<td>'.$plotresult.'</td>'.
  639:         '<td valign="center">'.$max_x.'</td>'.
  640:         '</tr>'.
  641:         '</table>';
  642:     return $result;
  643: }
  644: 
  645: ##
  646: ## Helper subroutines for <drawimage>.  
  647: ## These should probably go somewhere more suitable soon.
  648: sub line {
  649:     my ($x1,$y1,$x2,$y2,$color,$thickness) = @_;
  650:     return qq{<line x1="$x1" y1="$y1" x2="$x2" y2="$y2" color="$color" thickness="$thickness" />};
  651: }
  652: 
  653: sub text {
  654:     my ($x,$y,$color,$text,$font,$direction) = @_;
  655:     if (! defined($font) || $font !~ /^(tiny|small|medium|large|giant)$/) {
  656:         $font = 'medium';
  657:     }
  658:     if (! defined($direction) || $direction ne 'vertical') {
  659:         $direction = '';
  660:     }
  661:     return qq{<text x="$x" y="$y" color="$color" font="$font" direction="$direction" >$text</text>};
  662: }
  663: 
  664: sub rectangle {
  665:     my ($x1,$y1,$x2,$y2,$color,$thickness,$filled) = @_;
  666:     return qq{<rectangle x1="$x1" y1="$y1" x2="$x2" y2="$y2" color="$color" thickness="$thickness" filled="$filled" />};
  667: }
  668: 
  669: sub arc {
  670:     my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=@_;
  671:     return qq{<arc x="$x" y="$y" width="$width" height="$height" start="$start" end="$end" color="$color" thickness="$thickness" filled="$filled" />};
  672: }
  673: 
  674: sub circle {
  675:     my ($x,$y,$radius,$color,$thickness,$filled)=@_;
  676:     return &arc($x,$y,$radius,$radius,0,360,$color,$thickness,$filled);
  677: }
  678: 
  679: #########################################################
  680: #########################################################
  681: ##
  682: ##      Radio Response Routines
  683: ##
  684: #########################################################
  685: #########################################################
  686: sub radio_response_analysis {
  687:     my ($r,$problem,$problem_analysis,$students) = @_;
  688:     #
  689:     if ($ENV{'form.AnalyzeOver'} !~ /^(tries|time)$/) {
  690:         $r->print('Bad request');
  691:     }
  692:     #
  693:     my ($resource,$partid,$respid) = ($problem->{'resource'},
  694:                                       $problem->{'part'},
  695:                                       $problem->{'respid'});
  696:     #
  697:     my $analysis_html;
  698:     my $foildata = $problem_analysis->{'_Foils'};
  699:     my ($table,$foils,$concepts) = &build_foil_index($problem_analysis);
  700:     if (! defined($concepts)) {
  701:         $concepts = [];
  702:     }
  703:     #
  704:     my %true_foils;
  705:     my $num_true = 0;
  706:     if (! $problem_analysis->{'answercomputed'}) {
  707:         foreach my $foil (@$foils) {
  708:             if ($foildata->{$foil}->{'value'} eq 'true') {
  709:                 $true_foils{$foil}++; 
  710:             }
  711:         }
  712:         $num_true = scalar(keys(%true_foils));
  713:     }
  714:     #
  715:     $analysis_html .= $table;
  716:     # Gather student data
  717:     my $response_data = &Apache::loncoursedata::get_response_data
  718:         (\@Apache::lonstatistics::SelectedSections,
  719:          $Apache::lonstatistics::enrollment_status,
  720:          $resource->{'symb'},$respid);
  721:     my $correct;   # either a hash reference or a scalar
  722:     if ($problem_analysis->{'answercomputed'} || scalar(@$concepts) > 1) {
  723:         # This takes a while for large classes...
  724:         &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$students,
  725:                                                    'Statistics',
  726:                                                    'stats_status');
  727:         foreach my $student (@$students) {
  728:             my ($idx,@remainder) = split('&',$student->{'answer'});
  729:             my ($answer) = ($remainder[$idx]=~/^(.*)=([^=]*)$/);
  730:             $correct->{$student->{'username'}.':'.$student->{'domain'}}=
  731:                 &Apache::lonnet::unescape($answer);
  732:         }
  733:     } else {
  734:         foreach my $foil (keys(%$foildata)) {
  735:             if ($foildata->{$foil}->{'value'} eq 'true') {
  736:                 $correct = $foildata->{$foil}->{'name'};
  737:             }
  738:         }
  739:     }
  740:     #
  741:     if (! defined($response_data) || ref($response_data) ne 'ARRAY' ) {
  742:         $analysis_html = '<h2>'.
  743:             &mt('There is no submission data for this resource').
  744:             '</h2>';
  745:         $r->print($analysis_html);
  746:         return;
  747:     }
  748:     #
  749:     $analysis_html.='<table>';
  750:     for (my $plot_num = 1;$plot_num<=$ENV{'form.NumPlots'};$plot_num++) {
  751:         # classify data ->correct foil -> selected foil
  752:         my ($restriction_function,
  753:             $correct_foil_title,$incorrect_foil_title,
  754:             $pre_graph_text,$post_graph_text,
  755:             $no_data_text,@extra_data);
  756:         if ($ENV{'form.AnalyzeOver'} eq 'tries') {
  757:             $restriction_function = sub {($_[0]->{'tries'} == $plot_num?1:0)};
  758:             $correct_foil_title = 'Attempt '.$plot_num;
  759:             $incorrect_foil_title = 'Attempt '.$plot_num;
  760:             $pre_graph_text = 
  761:                 'Attempt [_1], [_2] submissions, [_3] correct, [_4] incorrect';
  762:             $post_graph_text = '';
  763:             $no_data_text = 'No data exists for attempt [_1]';
  764:         } elsif ($ENV{'form.AnalyzeOver'} eq 'time') {
  765:             my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  766:                 ('startdate_'.$plot_num);
  767:             my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  768:                 ('enddate_'.$plot_num);
  769:             ($starttime,$endtime) = &ensure_start_end_times
  770:                 ($starttime,$endtime,
  771:                  &get_time_from_row($response_data->[0]),
  772:                  &get_time_from_row($response_data->[-1]),
  773:                  $plot_num);
  774:             $pre_graph_text = 
  775:                 'Data from [_6] to [_7]<br /> [_2] submissions from [_5] students, [_3] correct, [_4] incorrect';
  776:             $extra_data[0] = &Apache::lonlocal::locallocaltime($starttime);
  777:             $extra_data[1] = &Apache::lonlocal::locallocaltime($endtime);
  778:             #
  779:             $post_graph_text = 
  780:                 &mt('Start time: [_1]',
  781:                     &Apache::lonhtmlcommon::date_setter
  782:                     ('Statistics','startdate_'.$plot_num,$starttime)).
  783:                 '<br />'.
  784:                 &mt('End time: [_1]',
  785:                     &Apache::lonhtmlcommon::date_setter
  786:                     ('Statistics','enddate_'.$plot_num,$endtime));
  787:             $restriction_function = 
  788:                 sub { 
  789:                     my $t = $_[0]->{'timestamp'};
  790:                     if ($t >= $starttime && $t < $endtime) {
  791:                         return 1;
  792:                     } else { 
  793:                         return 0;
  794:                     }
  795:                 };
  796:             $no_data_text = 'No data for [_5] to [_6]';
  797:         }
  798:         my $foil_choice_data =
  799:             &classify_response_data($response_data,$correct,
  800:                                     $restriction_function);
  801:         # &Apache::lonstathelpers::log_hash_ref($foil_choice_data);
  802:         my $answers;
  803:         if (ref($correct)) {
  804:             my %tmp;
  805:             foreach my $foil (values(%$correct)) {
  806:                 $tmp{$foil}++;
  807:             }
  808:             $answers = [keys(%tmp)];
  809:         } else {
  810:             $answers = [$correct];
  811:         }
  812:         # Concept Plot
  813:         my $concept_plot = '';
  814:         if (scalar(@$concepts) > 1) {
  815:             $concept_plot = &RR_concept_plot($concepts,$foil_choice_data,
  816:                                              'Correct Concepts');
  817:         }
  818:         # % Choosing plot
  819:         my $choice_plot = &RR_create_percent_selected_plot
  820:             ($concepts,$foils,$foil_choice_data,$correct_foil_title);
  821:         # for each correct foil, how did they mark it? (stacked bar graph)
  822:         my ($stacked_plot,$count_by_foil);
  823:         if ($problem_analysis->{'answercomputed'} || $num_true > 1) {
  824:             ($stacked_plot,$count_by_foil) =
  825:                 &RR_create_stacked_selection_plot($foils,$foil_choice_data,
  826:                                                   $incorrect_foil_title,
  827:                                                   \%true_foils);
  828:         }
  829:         #
  830:         if ($concept_plot ne '' ||
  831:             $choice_plot  ne '' ||
  832:             $stacked_plot ne '') {
  833:             my $correct = $foil_choice_data->{'_correct'};
  834:             if (! defined($correct) || $correct eq '') {
  835:                 $correct = 0;
  836:             }
  837:             my $incorrect = 
  838:             $analysis_html.= '<tr><td colspan="4" align="center">'.
  839:                 '<font size="+1">'.
  840:                 &mt($pre_graph_text,
  841:                     $plot_num,$foil_choice_data->{'_count'},
  842:                     $correct,
  843:                     $foil_choice_data->{'_count'}-$correct,
  844:                     $foil_choice_data->{'_students'},
  845:                     @extra_data).
  846:                     '</td></tr>'.$/;
  847:             $analysis_html.=
  848:                 '<tr>'.
  849:                 '<td>'.$concept_plot.'</td>'.
  850:                 '<td>'.$choice_plot.'</td>';
  851:             if ($stacked_plot ne '') {
  852:                 $analysis_html .= 
  853:                     '<td>'.$stacked_plot.'</td>'.
  854:                     '<td>'.&build_foil_key($foils,$count_by_foil).'</td>';
  855:             } else {
  856:                 $analysis_html .= ('<td></td>'x2);
  857:             }
  858:             $analysis_html.='</tr>'.$/;
  859:             if (defined($post_graph_text)) {
  860:                 $analysis_html.= '<tr><td colspan="4" align="center">'.
  861:                     $post_graph_text.'</td></tr>'.$/;
  862:             }
  863:         } elsif ($no_data_text ne '') {
  864:             $analysis_html.='<tr><td colspan="4" align="center">'.
  865:                 &mt($no_data_text,
  866:                     $plot_num,$foil_choice_data->{'_count'},
  867:                     $correct,                    
  868:                     $foil_choice_data->{'_count'}-$correct,
  869:                     @extra_data);
  870:             if (defined($post_graph_text)) {
  871:                 $analysis_html.='<br />'.$post_graph_text;
  872:             }
  873:             $analysis_html.='</td></tr>'.$/;
  874:         }
  875:     } # end of loop for plots
  876:     $analysis_html.='</table>';
  877:     $r->print($analysis_html);
  878: }
  879: 
  880: sub ensure_start_end_times {
  881:     my ($start,$end,$first,$last,$plot_num) = @_;
  882:     if (! defined($start) || ! defined($end)) {
  883:         my $sec_in_day = 86400;
  884:         my ($sday,$smon,$syear) = 
  885:             (localtime($last - $sec_in_day*($plot_num-1)))[3..5];
  886:         $start = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
  887:         $end   = $start + $sec_in_day;
  888:         if ($plot_num == $ENV{'form.NumPlots'}) {
  889:             $start = $first;
  890:         }
  891:     }
  892:     return ($start,$end);
  893: }
  894: 
  895: sub RR_concept_plot {
  896:     my ($concepts,$foil_data,$title) = @_;
  897:     #
  898:     my %correct_by_concept;
  899:     my %incorrect_by_concept;
  900:     my %true;
  901:     foreach my $concept (@$concepts) {
  902:         foreach my $foil (@{$concept->{'foils'}}) {
  903:             next if (! exists($foil_data->{$foil}));
  904:             foreach my $choice (keys(%{$foil_data->{$foil}})) {
  905:                 if ($choice eq $foil) {
  906:                     $correct_by_concept{$concept->{'name'}} +=
  907:                         $foil_data->{$foil}->{$choice};
  908:                 } else {
  909:                     $incorrect_by_concept{$concept->{'name'}} +=
  910:                         $foil_data->{$foil}->{$choice};
  911:                     
  912:                 }
  913:             }
  914:         }
  915:     }
  916:     # 
  917:     # need arrays for incorrect and correct because we want to use different
  918:     # colors for them
  919:     my @correct;
  920:     #
  921:     my $total =0;
  922:     for (my $i=0;$i<scalar(@$concepts);$i++) {
  923:         my $concept = $concepts->[$i];
  924:         $correct[$i]   =   $correct_by_concept{$concept->{'name'}};
  925:         $total += $correct_by_concept{$concept->{'name'}}+
  926:             $incorrect_by_concept{$concept->{'name'}};
  927:     }
  928:     if ($total == 0) { return ''; };
  929:     for (my $i=0;$i<=$#correct;$i++) { 
  930:         $correct[$i] = sprintf('%0f',$correct[$i]/$total*100);
  931:     }
  932:     my $xlabel = 'concept';
  933:     my $plot=  &Apache::loncommon::DrawBarGraph($title,
  934:                                                 $xlabel,
  935:                                                 'Percent Choosing',
  936:                                                 100,
  937:                                                 ['#33ff00','#ff3300'],
  938:                                                 undef,
  939:                                                 \@correct);
  940:     return $plot;
  941: }
  942: 
  943: sub RR_create_percent_selected_plot {
  944:     my ($concepts,$foils,$foil_data,$title) = @_;
  945:     #
  946:     if ($foil_data->{'_count'} == 0) { return ''; };
  947:     my %correct_selections;
  948:     my %incorrect_selections;
  949:     foreach my $foil (@$foils) {
  950:         # foil_data has format $foil_data->{true_foil}->{selected foil}=count
  951:         next if (! exists($foil_data->{$foil}));
  952:         while (my ($f,$count)= each(%{$foil_data->{$foil}})) {
  953:             if ($f eq $foil) {
  954:                 $correct_selections{$foil} += $count;
  955:             } else {
  956:                 $incorrect_selections{$f} += $count;
  957:             }
  958:         }
  959:     }
  960:     # 
  961:     # need arrays for incorrect and correct because we want to use different
  962:     # colors for them
  963:     my @correct;
  964:     my @incorrect;
  965:     #
  966:     my $total = $foil_data->{'_count'};
  967:     for (my $i=0;$i<scalar(@$foils);$i++) {
  968:         my $foil = $foils->[$i];
  969:         $correct[$i]   = $correct_selections{$foil};
  970:         $incorrect[$i] = $incorrect_selections{$foil};
  971:     }
  972:     for (my $i=0;$i<=$#correct;$i++) { 
  973:         $correct[$i] = sprintf('%2f',$correct[$i]/$total*100);
  974:     }
  975:     for (my $i=0;$i<=$#incorrect;$i++) {
  976:         $incorrect[$i] = sprintf('%2f',$incorrect[$i]/$total*100);
  977:     }
  978:     #
  979:     # Put a blank in the data sets between concepts, if there are concepts
  980:     my @labels;
  981:     if (defined($concepts) && scalar(@$concepts) > 1) {
  982:         my @new_correct;
  983:         my @new_incorrect;
  984:         my $foil_count = 0;
  985:         foreach my $concept (@$concepts) {
  986:             foreach (@{$concept->{'foils'}}) {
  987:                 push(@new_correct,  $correct[$foil_count]);
  988:                 push(@new_incorrect,$incorrect[$foil_count]);
  989:                 push(@labels,++$foil_count);
  990:             }
  991:             push(@new_correct,'');
  992:             push(@new_incorrect,'');
  993:             push(@labels,'');
  994:         }
  995:         @correct = @new_correct;
  996:         @incorrect = @new_incorrect;
  997:     } else {
  998:         @labels = (1 .. scalar(@correct));
  999:     }
 1000:     #
 1001:     my $xlabel = 'foil chosen';
 1002:     my $plot=  &Apache::loncommon::DrawBarGraph($title,
 1003:                                                 $xlabel,
 1004:                                                 'Percent Choosing',
 1005:                                                 100,
 1006:                                                 ['#33ff00','#ff3300'],
 1007:                                                 \@labels,
 1008:                                                 \@correct,
 1009:                                                 \@incorrect);
 1010:     return $plot;
 1011: }
 1012: 
 1013: sub RR_create_stacked_selection_plot {
 1014:     my ($foils,$foil_data,$title,$true_foils)=@_;
 1015:     #
 1016:     my @dataset; # array of array refs - multicolor rows $datasets[row]->[col]
 1017:     my @labels;
 1018:     my $count;
 1019:     my %column; # maps foil name to column in @datasets
 1020:     for (my $i=0;$i<scalar(@$foils);$i++) {
 1021:         my $foil = $foils->[$i];
 1022:         if (defined($true_foils) && scalar(keys(%$true_foils)) > 0 ) {
 1023:             next if (! $true_foils->{$foil} );
 1024:             push(@labels,$i+1);
 1025:         } else {
 1026:             next if (! exists($foil_data->{$foil}));
 1027:             push(@labels,$i+1);
 1028:         }
 1029:         next if (! exists($foil_data->{$foils->[$i]}));
 1030:         $column{$foil}= $count++;
 1031:         for (my $j=0;$j<scalar(@$foils);$j++) {
 1032:             my $value = 0;
 1033:             if ($i != $j ) {
 1034:                 $value += $foil_data->{$foil}->{$foils->[$j]};
 1035:             }
 1036:             $dataset[$j]->[$column{$foil}]=$value;
 1037:         }
 1038:     }
 1039:     #
 1040:     return '' if (! scalar(keys(%column)));
 1041:     #
 1042:     my $grand_total = 0;
 1043:     my %count_per_foil;
 1044:     while (my ($foil,$bar) = each (%column)) {
 1045:         my $bar_total = 0;
 1046:         for (my $j=0;$j<scalar(@dataset);$j++) {
 1047:             $bar_total += $dataset[$j]->[$bar];
 1048:         }
 1049:         next if ($bar_total == 0);
 1050:         for (my $j=0;$j<scalar(@dataset);$j++) {
 1051:             $dataset[$j]->[$bar] = 
 1052:                 sprintf('%2f',$dataset[$j]->[$bar]/$bar_total * 100);
 1053:         }
 1054:         $count_per_foil{$foil}=' ( '.$bar_total.' )';
 1055:         $grand_total += $bar_total;
 1056:     }
 1057:     if ($grand_total == 0) {
 1058:         return ('',undef);
 1059:     }
 1060:     my @empty_row = ();
 1061:     foreach (@{$dataset[0]}) {
 1062:         push(@empty_row,0);
 1063:     }
 1064:     #
 1065:     my $graph = &Apache::loncommon::DrawBarGraph
 1066:         ($title,'Correct Foil','foils chosen Incorrectly',
 1067:          100,$plotcolors,\@labels,\@empty_row,@dataset);
 1068:     return ($graph,\%count_per_foil);
 1069: }
 1070: 
 1071: 
 1072: #########################################################
 1073: #########################################################
 1074: ##
 1075: ##       Misc routines
 1076: ##
 1077: #########################################################
 1078: #########################################################
 1079: 
 1080: # if $correct is a hash ref, it is assumed to be indexed by student names.
 1081: #    the values are assumed to be hash refs with a key of 'answer'.
 1082: sub classify_response_data {
 1083:     my ($full_row_data,$correct,$function) = @_;
 1084:     my %submission_data;
 1085:     my %students;
 1086:     my $max=0;
 1087:     foreach my $row (@$full_row_data) {
 1088:         my %subm = &hashify_attempt($row);
 1089:         if (ref($correct) eq 'HASH') {
 1090:             $subm{'correct'} = $correct->{$subm{'student'}};
 1091:         } else {
 1092:             $subm{'correct'} = $correct;
 1093:         }
 1094:         $subm{'submission'} =~ s/=\d+\s*$//;
 1095:         if (&$function(\%subm)) {
 1096:             $students{$subm{'student'}}++;
 1097:             $submission_data{'_count'}++;
 1098:             if (&submission_is_correct($subm{'award'})) { 
 1099:                 $submission_data{'_correct'}++;
 1100:             }
 1101:             
 1102:             if($max<++$submission_data{$subm{'correct'}}->{$subm{'submission'}}) {
 1103:                 $max=$submission_data{$subm{'correct'}}->{$subm{'submission'}};
 1104:             }
 1105:         }
 1106:     }
 1107:     $submission_data{'_max'} = $max;
 1108:     $submission_data{'_students'}=scalar(keys(%students));
 1109:     return \%submission_data;
 1110: }
 1111: 
 1112: 
 1113: #########################################################
 1114: #########################################################
 1115: ##
 1116: ##      Option Response Routines
 1117: ##
 1118: #########################################################
 1119: #########################################################
 1120: sub OptionResponseAnalysis {
 1121:     my ($r,$problem,$problem_data,$Students) = @_;
 1122:     my ($resource,$respid) = ($problem->{'resource'},
 1123:                               $problem->{'respid'});
 1124:     # Note: part data is not needed.
 1125:     my $PerformanceData = &Apache::loncoursedata::get_response_data
 1126:         (\@Apache::lonstatistics::SelectedSections,
 1127:          $Apache::lonstatistics::enrollment_status,
 1128:          $resource->{'symb'},$respid);
 1129:     if (! defined($PerformanceData) || 
 1130:         ref($PerformanceData) ne 'ARRAY' ) {
 1131:         $r->print('<h2>'.
 1132:                   &mt('There is no student data for this problem.').
 1133:                   '</h2>');
 1134:     }  else {
 1135:         $r->rflush();
 1136:         if (exists($ENV{'form.ExcelOutput'})) {
 1137:             my $result = &OR_excel_sheet($r,$resource,
 1138:                                          $PerformanceData,
 1139:                                          $problem_data);
 1140:             $r->print($result);
 1141:             $r->rflush();
 1142:         } else {
 1143:             if ($ENV{'form.AnalyzeOver'} eq 'tries') {
 1144:                 my $analysis_html = &OR_tries_analysis($r,
 1145:                                                     $PerformanceData,
 1146:                                                     $problem_data);
 1147:                 $r->print($analysis_html);
 1148:                 $r->rflush();
 1149:             } elsif ($ENV{'form.AnalyzeOver'} eq 'time') {
 1150:                 my $analysis_html = &OR_time_analysis($PerformanceData,
 1151:                                                    $problem_data);
 1152:                 $r->print($analysis_html);
 1153:                 $r->rflush();
 1154:             } else {
 1155:                 $r->print('<h2>'.
 1156:                           &mt('The analysis you have selected is '.
 1157:                               'not supported at this time').
 1158:                           '</h2>');
 1159:             }
 1160:         }
 1161:     }
 1162: }
 1163: 
 1164: #########################################################
 1165: #
 1166: #       Option Response:  tries Analysis
 1167: #
 1168: #########################################################
 1169: sub OR_tries_analysis {
 1170:     my ($r,$PerformanceData,$ORdata) = @_;
 1171:     my $mintries = 1;
 1172:     my $maxtries = $ENV{'form.NumPlots'};
 1173:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
 1174:     if (! defined($Concepts)) {
 1175:         $Concepts = [];
 1176:     }
 1177:     my %response_data = &OR_analyze_by_tries($r,$PerformanceData,
 1178:                                                      $mintries,$maxtries);
 1179:     my $analysis = '';
 1180:     #
 1181:     # Compute the data necessary to make the plots
 1182:     my @foil_plot; 
 1183:     my @concept_data;
 1184:     for (my $j=0;$j<=scalar(@$Concepts);$j++) {
 1185:         my $concept = $Concepts->[$j];
 1186:         foreach my $foilid (@{$concept->{'foils'}}) {
 1187:             for (my $try=$mintries;$try<=$maxtries;$try++) {
 1188:                 # concept analysis data
 1189:                 $concept_data[$j]->[$try]->{'_correct'} +=
 1190:                     $response_data{$foilid}->[$try]->{'_correct'};
 1191:                 $concept_data[$j]->[$try]->{'_total'} +=
 1192:                     $response_data{$foilid}->[$try]->{'_total'};
 1193:                 #
 1194:                 # foil analysis data
 1195:                 if ($response_data{$foilid}->[$try]->{'_total'} == 0) {
 1196:                     push(@{$foil_plot[$try]->{'_correct'}},0);
 1197:                 } else {
 1198:                     push(@{$foil_plot[$try]->{'_correct'}},
 1199:                          100*$response_data{$foilid}->[$try]->{'_correct'}/
 1200:                          $response_data{$foilid}->[$try]->{'_total'});
 1201:                 }
 1202:                 foreach my $option (@{$ORdata->{'_Options'}}) {
 1203:                     push(@{$foil_plot[$try]->{'_total'}},
 1204:                          $response_data{$foilid}->[$try]->{'_total'});
 1205:                     if ($response_data{$foilid}->[$try]->{'_total'} == 0) {
 1206:                         push (@{$foil_plot[$try]->{$option}},0);
 1207:                     } else {
 1208:                         if ($response_data{$foilid}->[$try]->{'_total'} ==
 1209:                             $response_data{$foilid}->[$try]->{'_correct'}) {
 1210:                             push(@{$foil_plot[$try]->{$option}},0);
 1211:                         } else {
 1212:                             push (@{$foil_plot[$try]->{$option}},
 1213:                                   100 * 
 1214:                                   $response_data{$foilid}->[$try]->{$option} / 
 1215:                                   ($response_data{$foilid}->[$try]->{'_total'} 
 1216:                                    - 
 1217:                                    $response_data{$foilid}->[$try]->{'_correct'}
 1218:                                    ));
 1219:                         }
 1220:                     }
 1221:                 } # End of foreach my $option
 1222:             }
 1223:         } # End of foreach my $foilid
 1224:     } # End of concept loops
 1225:     # 
 1226:     # Build a table for the plots
 1227:     my $analysis_html = "<br /><table>\n";
 1228:     my $optionkey = &build_option_index($ORdata);
 1229:     my $num_concepts = 1;
 1230:     if (defined($Concepts)) { $num_concepts = scalar(@$Concepts); }
 1231:     #
 1232:     for (my $try=$mintries;$try<=$maxtries;$try++) {
 1233:         if (! defined($response_data{'_total'}->[$try]) ||
 1234:             $response_data{'_total'}->[$try] == 0) { 
 1235:             if ($try > 1) {
 1236:                 $analysis_html.= '<tr><td align="center" colspan="4"><b>'.
 1237:                     &mt('None of the selected students attempted the problem more than [_1] times.',$try-1).
 1238:                     '</b></td></tr>';
 1239:             } else {
 1240:                 $analysis_html.= '<tr><td colspan="4" align="center"><b>'.
 1241:                     &mt('None of the selected students have attempted the problem').'</b></td></tr>';
 1242:             }
 1243:             last;
 1244:         }
 1245:         my $concept_graph='';
 1246:         if ($num_concepts > 1) {
 1247:             #
 1248:             # Create concept plot
 1249:             my @concept_plot_data;
 1250:             for (my $j=0;$j<=$#concept_data;$j++) {
 1251:                 my $total = $concept_data[$j]->[$try]->{'_total'};
 1252:                 if ($total == 0) {
 1253:                     $concept_plot_data[$j] = 0;
 1254:                 } else {
 1255:                     $concept_plot_data[$j] = 100 * 
 1256:                         sprintf('%0.3f',
 1257:                                 $concept_data[$j]->[$try]->{'_correct'} / 
 1258:                                 $total);
 1259:                 }
 1260:             }
 1261:             #
 1262:             $concept_graph = &Apache::loncommon::DrawBarGraph
 1263:                 ('Correct Concepts','Concept Number','Percent Correct',
 1264:                  100,$plotcolors,undef,\@concept_plot_data,{xskip=>1});
 1265:         }
 1266:         #
 1267:         # Create Foil Plots
 1268:         my $data_count = $response_data{'_total'}->[$try];
 1269:         my $correct = $response_data{'_correct'}->[$try];
 1270:         my @Datasets;
 1271:         foreach my $option ('_correct',@{$ORdata->{'_Options'}}) {
 1272:             next if (! exists($foil_plot[$try]->{$option}));
 1273:             push(@Datasets,$foil_plot[$try]->{$option});
 1274:         }
 1275:         #
 1276:         # Put a blank in the data set between concepts
 1277:         for (my $set =0;$set<=$#Datasets;$set++) {
 1278:             my @Data = @{$Datasets[$set]};
 1279:             my $idx = 0;
 1280:             foreach my $concept (@{$Concepts}) {
 1281:                 foreach my $foilid (@{$concept->{'foils'}}) {
 1282:                     $Datasets[$set]->[$idx++]=shift(@Data);
 1283:                 }
 1284:                 if ($concept->{'name'} ne $Concepts->[-1]->{'name'}) {
 1285:                     $Datasets[$set]->[$idx++] = 0;
 1286:                 }
 1287:             }
 1288:         }
 1289:         #
 1290:         # Set up the labels needed for the bar graph
 1291:         my @Labels;
 1292:         my $idx = 1;
 1293:         foreach my $concept (@{$Concepts}) {
 1294:             foreach my $foilid (@{$concept->{'foils'}}) {
 1295:                 push(@Labels,$idx++);
 1296:             }
 1297:             push(@Labels,'');
 1298:         }
 1299:         #
 1300:         my $correct_graph = &Apache::loncommon::DrawBarGraph
 1301:             ('Correct Statements','Statement','% Answered Correct',
 1302:              100,$plotcolors,\@Labels,$Datasets[0],{xskip=>1});
 1303:         
 1304:         #
 1305:         #
 1306:         next if (! defined($Datasets[0]));
 1307:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
 1308:             $Datasets[0]->[$i]=0;
 1309:         }
 1310:         my $count = $response_data{'_total'}->[$try] - 
 1311:                                            $response_data{'_correct'}->[$try];
 1312:         my $incorrect_graph = &Apache::loncommon::DrawBarGraph
 1313:             ('Incorrect Statements','Statement','% Chosen Incorrectly',
 1314:              100,$plotcolors,\@Labels,@Datasets,{xskip=>1});
 1315:         $analysis_html.= 
 1316:             '<tr><td colspan="4" align="center">'.
 1317:             '<font size="+1">'.
 1318:             &mt('Attempt [_1], [_2] submissions, [_3] correct, [_4] incorrect',
 1319:                 $try,$data_count,$correct,$data_count-$correct).
 1320:             '</font>'.'</td></tr>'.$/.                
 1321:             '<tr>'.
 1322:             '<td>'.$concept_graph.'</td>'.
 1323:             '<td>'.$correct_graph.'</td>'.
 1324:             '<td>'.$incorrect_graph.'</td>'.
 1325:             '<td>'.$optionkey.'<td>'.
 1326:             '</tr>'.$/;
 1327:     }
 1328:     $analysis_html .= "</table>\n";
 1329:     $table .= $analysis_html;
 1330:     return $table;
 1331: }
 1332: 
 1333: sub OR_analyze_by_tries {
 1334:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
 1335:     my %Trydata;
 1336:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
 1337:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
 1338:     my @students;
 1339:     foreach my $row (@$PerformanceData) {
 1340:         next if (! defined($row));
 1341:         my $tries = &get_tries_from_row($row);
 1342:         my %Row   = &Process_OR_Row($row);
 1343:         next if (! %Row);
 1344:         my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 1345:         $students[$tries]->{$student_id}++;
 1346:         while (my ($foilid,$href) = each(%Row)) {
 1347:             if (! ref($href)) { 
 1348:                 $Trydata{$foilid}->[$tries] += $href;
 1349:                 next;
 1350:             }
 1351:             while (my ($option,$value) = each(%$href)) {
 1352:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
 1353:             }
 1354:         }
 1355:     }
 1356:     for (my $try=$mintries;$try<=$maxtries;$try++) {
 1357:         $Trydata{'_studentcount'}->[$try] = scalar(keys(%{$students[$try]}));
 1358:     }
 1359:     return %Trydata;
 1360: }
 1361: 
 1362: #########################################################
 1363: #
 1364: #     Option Response: Time Analysis
 1365: #
 1366: #########################################################
 1367: sub OR_time_analysis {
 1368:     my ($performance_data,$ORdata) = @_;
 1369:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
 1370:     my $foilkey = &build_option_index($ORdata);
 1371:     my $num_concepts = 1;
 1372:     if (defined($Concepts)) { $num_concepts = scalar(@$Concepts); }
 1373:     #
 1374:     if ($num_concepts < 2) {
 1375:         $table = '<h3>'.
 1376:             &mt('Not enough data for concept analysis.  '.
 1377:                 'Performing Foil Analysis').
 1378:                 '</h3>'.$table;
 1379:     }
 1380:     #
 1381:     my $num_plots = $ENV{'form.NumPlots'};
 1382:     my $num_data = scalar(@$performance_data)-1;
 1383:     #
 1384:     my $current_index;
 1385:     $table .= "<table>\n";
 1386:     for (my $i=0;$i<$num_plots;$i++) {
 1387:         ##
 1388:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
 1389:             ('startdate_'.$i);
 1390:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
 1391:             ('enddate_'.$i);
 1392:         if (! defined($starttime) || ! defined($endtime)) {
 1393:             my $sec_in_day = 86400;
 1394:             my $last_sub_time = &get_time_from_row($performance_data->[-1]);
 1395:             my ($sday,$smon,$syear) = 
 1396:                 (localtime($last_sub_time - $sec_in_day*$i))[3..5];
 1397:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
 1398:             $endtime = $starttime + $sec_in_day;
 1399:             if ($i == ($num_plots -1 )) {
 1400:                 $starttime = &get_time_from_row($performance_data->[0]);
 1401:             }
 1402:         }
 1403:         $table .= '<tr><td colspan="4" align="center"><font size="+1">'.
 1404:             &mt('Data from [_1] to [_2]',
 1405:                 &Apache::lonlocal::locallocaltime($starttime),
 1406:                 &Apache::lonlocal::locallocaltime($endtime)).
 1407:                 '</font></td></tr>'.$/;
 1408:         my $startdateform = &Apache::lonhtmlcommon::date_setter
 1409:             ('Statistics','startdate_'.$i,$starttime);
 1410:         my $enddateform = &Apache::lonhtmlcommon::date_setter
 1411:             ('Statistics','enddate_'.$i,$endtime);
 1412:         #
 1413:         my $begin_index;
 1414:         my $end_index;
 1415:         my $j;
 1416:         while (++$j < scalar(@$performance_data)) {
 1417:             last if (&get_time_from_row($performance_data->[$j]) 
 1418:                                                               > $starttime);
 1419:         }
 1420:         $begin_index = $j;
 1421:         while ($j < scalar(@$performance_data)) {
 1422:             if (&get_time_from_row($performance_data->[$j]) > $endtime) {
 1423:                 last;
 1424:             } else {
 1425:                 $j++;
 1426:             }
 1427:         }
 1428:         $end_index = $j;
 1429:         ##
 1430:         my ($processed_time_data,$correct,$data_count,$student_count) =
 1431:             &OR_time_process_data($performance_data,$begin_index,$end_index);
 1432:         ##
 1433:         $table .= '<tr><td colspan="4" align="center"><font size="+1">'.
 1434:             &mt('[_1] submissions from [_2] students, [_3] correct, [_4] incorrect',
 1435:                 $data_count,$student_count,$correct,$data_count-$correct).
 1436:                 '</font></td></tr>'.$/;
 1437:         my $concept_correct_plot = '';
 1438:         if ($num_concepts > 1) {
 1439:             $concept_correct_plot = 
 1440:                 &OR_Concept_Time_Analysis($processed_time_data,
 1441:                                           $correct,$data_count,$student_count,
 1442:                                           $ORdata,$Concepts);
 1443:         }
 1444:         my ($foil_correct_plot,$foil_incorrect_plot) = 
 1445:             &OR_Foil_Time_Analysis($processed_time_data,
 1446:                                    $correct,$data_count,$student_count,
 1447:                                    $ORdata,$Foils,$Concepts);
 1448:         $table .= '<tr>'.
 1449:             '<td>'.$concept_correct_plot.'</td>'.
 1450:             '<td>'.$foil_correct_plot.'</td>'.
 1451:             '<td>'.$foil_incorrect_plot.'</td>'.
 1452:             '<td align="left" valign="top">'.$foilkey.'</td></tr>'.$/;
 1453:         $table .= '<tr><td colspan="4" align="center">'.
 1454:             &mt('Start time: [_1]',$startdateform).'<br />'.
 1455:             &mt('End time: [_1]',$enddateform).'</td></tr>'.$/;
 1456:         $table.= '<tr><td colspan="4">&nbsp</td></tr>'.$/;
 1457:     }
 1458:     $table .= '</table>';
 1459:     #
 1460:     return $table;
 1461: }
 1462: 
 1463: sub OR_Foil_Time_Analysis {
 1464:     my ($processed_time_data,$correct,$data_count,$student_count,
 1465:         $ORdata,$Foils,$Concepts) = @_;
 1466:     if ($data_count <= 0) {
 1467:         return ('<h2>'.&mt('There is no data to plot').'</h2>','');
 1468:     }
 1469:     my $analysis_html;
 1470:     my @plotdata;
 1471:     my @labels;
 1472:     foreach my $concept (@{$Concepts}) {
 1473:         foreach my $foil (@{$concept->{'foils'}}) {
 1474:             push(@labels,scalar(@labels)+1);
 1475:             my $total = $processed_time_data->{$foil}->{'_total'};
 1476:             if ($total == 0) {
 1477:                 push(@{$plotdata[0]},0);
 1478:             } else {
 1479:                 push(@{$plotdata[0]},
 1480:                      100 * $processed_time_data->{$foil}->{'_correct'} / $total);
 1481:             }
 1482:             my $total_incorrect = $total - $processed_time_data->{$foil}->{'_correct'};
 1483:             for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
 1484:                 my $option = $ORdata->{'_Options'}->[$i];
 1485:                 if ($total_incorrect == 0) {
 1486:                     push(@{$plotdata[$i+1]},0);
 1487:                 } else {
 1488:                     push(@{$plotdata[$i+1]},
 1489:                          100 * $processed_time_data->{$foil}->{$option} / $total_incorrect);
 1490:                 }
 1491:             }
 1492:         }
 1493:         # Put in a blank one
 1494:         push(@labels,'');
 1495:         push(@{$plotdata[0]},0);
 1496:         for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
 1497:             push(@{$plotdata[$i+1]},0);
 1498:         }
 1499:     }
 1500:     #
 1501:     # Create the plot
 1502:     my $correct_plot = &Apache::loncommon::DrawBarGraph('Correct Statements',
 1503:                                                         'Statement Number',
 1504:                                                         'Percent Correct',
 1505:                                                         100,
 1506:                                                         $plotcolors,
 1507:                                                         undef,
 1508:                                                         $plotdata[0],
 1509:                                                         {xskip=>1});
 1510:     for (my $j=0; $j< scalar(@{$plotdata[0]});$j++) {
 1511:         $plotdata[0]->[$j]=0;
 1512:     }
 1513:     my $incorrect_plot = 
 1514:         &Apache::loncommon::DrawBarGraph('Incorrect Statements',
 1515:                                          'Statement Number',
 1516:                                          'Incorrect Option Choice',
 1517:                                          100,
 1518:                                          $plotcolors,
 1519:                                          undef,
 1520:                                          @plotdata,{xskip=>1});
 1521:     return ($correct_plot,$incorrect_plot);
 1522: }
 1523: 
 1524: sub OR_Concept_Time_Analysis {
 1525:     my ($processed_time_data,$correct,$data_count,$student_count,
 1526:         $ORdata,$Concepts) = @_;
 1527:     return '' if ($data_count == 0);
 1528:     #
 1529:     # Put the data in plottable form
 1530:     my @plotdata;
 1531:     foreach my $concept (@$Concepts) {
 1532:         my ($total,$correct);
 1533:         foreach my $foil (@{$concept->{'foils'}}) {
 1534:             $total += $processed_time_data->{$foil}->{'_total'};
 1535:             $correct += $processed_time_data->{$foil}->{'_correct'};
 1536:         }
 1537:         if ($total == 0) {
 1538:             push(@plotdata,0);
 1539:         } else {
 1540:             push(@plotdata,100 * $correct / $total);
 1541:         }
 1542:     }
 1543:     #
 1544:     # Create the plot
 1545:     return &Apache::loncommon::DrawBarGraph('Correct Concepts',
 1546:                                             'Concept Number',
 1547:                                             'Percent Correct',
 1548:                                             100,
 1549:                                             $plotcolors,
 1550:                                             undef,
 1551:                                             \@plotdata,{xskip=>1});
 1552: }
 1553: 
 1554: sub OR_time_process_data {
 1555:     my ($performance_data,$begin_index,$end_index)=@_;
 1556:     my %processed_time_data;
 1557:     my %distinct_students;
 1558:     my ($correct,$data_count);
 1559:     if (($begin_index == $end_index) && 
 1560:         ($end_index != scalar(@$performance_data)-1)) { 
 1561:         return undef;
 1562:     }
 1563:     # Be sure we include the last one if we are asked for it.
 1564:     # That we have to correct here (and not when $end_index is 
 1565:     # given a value) should probably be considered a bug.
 1566:     if ($end_index == scalar(@$performance_data)-1) {
 1567:         $end_index++;
 1568:     }
 1569:     my $count;
 1570:     for (my $i=$begin_index;$i<$end_index;$i++) {
 1571:         my $attempt = $performance_data->[$i];
 1572:         $count++;
 1573:         next if (! defined($attempt));
 1574:         my %attempt = &Process_OR_Row($attempt);
 1575:         $data_count++;
 1576:         $correct += $attempt{'_correct'};
 1577:         $distinct_students{$attempt->[&Apache::loncoursedata::RD_student_id()]}++;
 1578:         while (my ($foilid,$href) = each(%attempt)) {
 1579:             if (! ref($href)) {
 1580:                 $processed_time_data{$foilid} += $href;
 1581:                 next;
 1582:             }
 1583:             while (my ($option,$value) = each(%$href)) {
 1584:                 $processed_time_data{$foilid}->{$option}+=$value;
 1585:             }
 1586:         }
 1587:     }
 1588:     return (\%processed_time_data,$correct,$data_count,
 1589:             scalar(keys(%distinct_students)));
 1590: }
 1591: 
 1592: #########################################################
 1593: #########################################################
 1594: ##
 1595: ##             Excel output 
 1596: ##
 1597: #########################################################
 1598: #########################################################
 1599: sub build_student_data_worksheet {
 1600:     my ($workbook,$format) = @_;
 1601:     my $rows_output = 3;
 1602:     my $cols_output = 0;
 1603:     my $worksheet  = $workbook->addworksheet('Student Data');
 1604:     $worksheet->write($rows_output++,0,'Student Data',$format->{'h3'});
 1605:     my @Headers = ('full name','username','domain','section',
 1606:                    "student\nnumber",'identifier');
 1607:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1608:     my @Students = @Apache::lonstatistics::Students;
 1609:     my $studentrows = &Apache::loncoursedata::get_student_data(\@Students);
 1610:     my %ids;
 1611:     foreach my $row (@$studentrows) {
 1612:         my ($mysqlid,$student) = @$row;
 1613:         $ids{$student}=$mysqlid;
 1614:     }
 1615:     foreach my $student (@Students) {
 1616:         my $name_domain = $student->{'username'}.':'.$student->{'domain'};
 1617:         $worksheet->write_row($rows_output++,0,
 1618:                           [$student->{'fullname'},
 1619:                            $student->{'username'},$student->{'domain'},
 1620:                            $student->{'section'},$student->{'id'},
 1621:                            $ids{$name_domain}]);
 1622:     }
 1623:     return $worksheet;
 1624: }
 1625: 
 1626: sub OR_excel_sheet {
 1627:     my ($r,$resource,$performance_data,$ORdata) = @_;
 1628:     my $response = '';
 1629:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
 1630:     #
 1631:     # Create excel worksheet
 1632:     my $filename = '/prtspool/'.
 1633:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1634:         time.'_'.rand(1000000000).'.xls';
 1635:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1636:     if (! defined($workbook)) {
 1637:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1638:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
 1639:                             "This error has been logged.  ".
 1640:                             "Please alert your LON-CAPA administrator").
 1641:                   '</p>');
 1642:         return undef;
 1643:     }
 1644:     #
 1645:     $workbook->set_tempdir('/home/httpd/perl/tmp');
 1646:     my $format = &Apache::loncommon::define_excel_formats($workbook);
 1647:     #
 1648:     # Create and populate main worksheets
 1649:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
 1650:     my $student_data_sheet = &build_student_data_worksheet($workbook,$format);
 1651:     my $response_data_sheet = $workbook->addworksheet('Response Data');
 1652:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
 1653:                        $response_data_sheet) {
 1654:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
 1655:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
 1656:     }
 1657:     #
 1658:     my $result;
 1659:     $result = &OR_build_problem_data_worksheet($problem_data_sheet,$format,
 1660:                                             $Concepts,$ORdata);
 1661:     if ($result ne 'okay') {
 1662:         # Do something useful
 1663:     }
 1664:     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
 1665:                                              $performance_data,$Foils,
 1666:                                              $ORdata);
 1667:     if ($result ne 'okay') {
 1668:         # Do something useful
 1669:     }
 1670:     $response_data_sheet->activate();
 1671:     #
 1672:     # Close the excel file
 1673:     $workbook->close();
 1674:     #
 1675:     # Write a link to allow them to download it
 1676:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
 1677:               '<p><a href="'.$filename.'">'.
 1678:               &mt('Your Excel spreadsheet.').
 1679:               '</a></p>'."\n";
 1680:     return $result;
 1681: }
 1682: 
 1683: sub OR_build_problem_data_worksheet {
 1684:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
 1685:     my $rows_output = 3;
 1686:     my $cols_output = 0;
 1687:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
 1688:     ##
 1689:     ##
 1690:     my @Headers;
 1691:     if (@$Concepts > 1) {
 1692:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
 1693:                     'Foil Name','Foil Text','Correct value');
 1694:     } else {
 1695:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
 1696:     }
 1697:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1698:     my %Foildata = %{$ORdata->{'_Foils'}};
 1699:     my $conceptindex = 1;
 1700:     my $foilindex = 1;
 1701:     foreach my $concept (@$Concepts) {
 1702:         my @FoilsInConcept = @{$concept->{'foils'}};
 1703:         my $firstfoil = shift(@FoilsInConcept);
 1704:         if (@$Concepts > 1) {
 1705:             $worksheet->write_row($rows_output++,0,
 1706:                                   [$conceptindex,
 1707:                                    $concept->{'name'},
 1708:                                    $foilindex++,
 1709:                                    $Foildata{$firstfoil}->{'name'},
 1710:                                    $Foildata{$firstfoil}->{'text'},
 1711:                                    $Foildata{$firstfoil}->{'value'},]);
 1712:         } else {
 1713:             $worksheet->write_row($rows_output++,0,
 1714:                                   [ $foilindex++,
 1715:                                     $Foildata{$firstfoil}->{'name'},
 1716:                                     $Foildata{$firstfoil}->{'text'},
 1717:                                     $Foildata{$firstfoil}->{'value'},]);
 1718:         }
 1719:         foreach my $foilid (@FoilsInConcept) {
 1720:             if (@$Concepts > 1) {
 1721:                 $worksheet->write_row($rows_output++,0,
 1722:                                       ['',
 1723:                                        '',
 1724:                                        $foilindex,
 1725:                                        $Foildata{$foilid}->{'name'},
 1726:                                        $Foildata{$foilid}->{'text'},
 1727:                                        $Foildata{$foilid}->{'value'},]);
 1728:             } else {
 1729:                 $worksheet->write_row($rows_output++,0,                
 1730:                                       [$foilindex,
 1731:                                        $Foildata{$foilid}->{'name'},
 1732:                                        $Foildata{$foilid}->{'text'},
 1733:                                        $Foildata{$foilid}->{'value'},]);
 1734:             }                
 1735:         } continue {
 1736:             $foilindex++;
 1737:         }
 1738:     } continue {
 1739:         $conceptindex++;
 1740:     }
 1741:     $rows_output++;
 1742:     $rows_output++;
 1743:     ##
 1744:     ## Option data output
 1745:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
 1746:     foreach my $string (@{$ORdata->{'_Options'}}) {
 1747:         $worksheet->write($rows_output++,0,$string);
 1748:     }
 1749:     return 'okay';
 1750: }
 1751: 
 1752: sub OR_build_response_data_worksheet {
 1753:     my ($worksheet,$format,$performance_data,$Foils,$ORdata)=@_;
 1754:     my $rows_output = 3;
 1755:     my $cols_output = 0;
 1756:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
 1757:     $worksheet->set_column(1,1,20);
 1758:     $worksheet->set_column(2,2,13);
 1759:     my @Headers = ('identifier','time','award detail','attempt');
 1760:     foreach my $foil (@$Foils) {
 1761:         push (@Headers,$foil.' submission');
 1762:         push (@Headers,$foil.' grading');
 1763:     }
 1764:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1765:     #
 1766:     foreach my $row (@$performance_data) {
 1767:         next if (! defined($row));
 1768:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
 1769:         my @Foilgrades = split('&',$grading);
 1770:         my @Foilsubs   = split('&',$submission);
 1771:         my %response_data;
 1772:         for (my $j=0;$j<=$#Foilgrades;$j++) {
 1773:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1774:             my (undef,$submission) = split('=',$Foilsubs[$j]);
 1775:             $submission = &Apache::lonnet::unescape($submission);
 1776:             $foilid = &Apache::lonnet::unescape($foilid);
 1777:             $response_data{$foilid.' submission'}=$submission;
 1778:             $response_data{$foilid.' award'}=$correct;
 1779:         }
 1780:         $worksheet->write($rows_output,$cols_output++,$student);
 1781:         $worksheet->write($rows_output,$cols_output++,
 1782:              &Apache::lonstathelpers::calc_serial($time),$format->{'date'});
 1783:         $worksheet->write($rows_output,$cols_output++,$award);
 1784:         $worksheet->write($rows_output,$cols_output++,$tries);
 1785:         foreach my $foilid (@$Foils) {
 1786:             $worksheet->write($rows_output,$cols_output++,
 1787:                               $response_data{$foilid.' submission'});
 1788:             $worksheet->write($rows_output,$cols_output++,
 1789:                               $response_data{$foilid.' award'});
 1790:         }
 1791:         $rows_output++;
 1792:         $cols_output = 0;
 1793:     }
 1794:     return;
 1795: }
 1796: 
 1797: sub build_foil_index {
 1798:     my ($ORdata) = @_;
 1799:     return if (! exists($ORdata->{'_Foils'}));
 1800:     my %Foildata = %{$ORdata->{'_Foils'}};
 1801:     my @Foils = sort(keys(%Foildata));
 1802:     my %Concepts;
 1803:     foreach my $foilid (@Foils) {
 1804:         push(@{$Concepts{$Foildata{$foilid}->{'_Concept'}}},
 1805:              $foilid);
 1806:     }
 1807:     undef(@Foils);
 1808:     # Having gathered the concept information in a hash, we now translate it
 1809:     # into an array because we need to be consistent about order.
 1810:     # Also put the foils in order, too.
 1811:     my $sortfunction = sub {
 1812:         my %Numbers = (one   => 1,
 1813:                        two   => 2,
 1814:                        three => 3,
 1815:                        four  => 4,
 1816:                        five  => 5,
 1817:                        six   => 6,
 1818:                        seven => 7,
 1819:                        eight => 8,
 1820:                        nine  => 9,
 1821:                        ten   => 10,);
 1822:         my $a1 = lc($a); 
 1823:         my $b1 = lc($b);
 1824:         if (exists($Numbers{$a1})) {
 1825:             $a1 = $Numbers{$a1};
 1826:         }
 1827:         if (exists($Numbers{$b1})) {
 1828:             $b1 = $Numbers{$b1};
 1829:         }
 1830:         if (($a1 =~/^\d+$/) && ($b1 =~/^\d+$/)) {
 1831:             return $a1 <=> $b1;
 1832:         } else {
 1833:             return $a1 cmp $b1;
 1834:         }
 1835:     };
 1836:     my @Concepts;
 1837:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
 1838:         if (! defined($Concepts{$concept})) {
 1839:             $Concepts{$concept}=[];
 1840: #            next;
 1841:         }
 1842:         push(@Concepts,{ name => $concept,
 1843:                         foils => [@{$Concepts{$concept}}]});
 1844:         push(@Foils,(@{$Concepts{$concept}}));
 1845:     }
 1846:     #
 1847:     # Build up the table of row labels.
 1848:     my $table = '<table border="1" >'."\n";
 1849:     if (@Concepts > 1) {
 1850:         $table .= '<tr>'.
 1851:             '<th>'.&mt('Concept Number').'</th>'.
 1852:             '<th>'.&mt('Concept').'</th>'.
 1853:             '<th>'.&mt('Foil Number').'</th>'.
 1854:             '<th>'.&mt('Foil Name').'</th>'.
 1855:             '<th>'.&mt('Foil Text').'</th>'.
 1856:             '<th>'.&mt('Correct Value').'</th>'.
 1857:             "</tr>\n";
 1858:     } else {
 1859:         $table .= '<tr>'.
 1860:             '<th>'.&mt('Foil Number').'</th>'.
 1861:             '<th>'.&mt('Foil Name').'</th>'.
 1862:             '<th>'.&mt('Foil Text').'</th>'.
 1863:             '<th>'.&mt('Correct Value').'</th>'.
 1864:             "</tr>\n";
 1865:     }        
 1866:     my $conceptindex = 1;
 1867:     my $foilindex = 1;
 1868:     foreach my $concept (@Concepts) {
 1869:         my @FoilsInConcept = @{$concept->{'foils'}};
 1870:         my $firstfoil = shift(@FoilsInConcept);
 1871:         if (@Concepts > 1) {
 1872:             $table .= '<tr>'.
 1873:                 '<td>'.$conceptindex.'</td>'.
 1874:                 '<td>'.&HTML::Entities::encode($concept->{'name'},'<>&"').'</td>'.
 1875:                 '<td>'.$foilindex++.'</td>'.
 1876:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
 1877:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1878:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
 1879:                 "</tr>\n";
 1880:         } else {
 1881:             $table .= '<tr>'.
 1882:                 '<td>'.$foilindex++.'</td>'.
 1883:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
 1884:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1885:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
 1886:                 "</tr>\n";
 1887:         }
 1888:         foreach my $foilid (@FoilsInConcept) {
 1889:             if (@Concepts > 1) {
 1890:                 $table .= '<tr>'.
 1891:                     '<td></td>'.
 1892:                     '<td></td>'.
 1893:                     '<td>'.$foilindex.'</td>'.
 1894:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
 1895:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1896:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
 1897:                     "</tr>\n";
 1898:             } else {
 1899:                 $table .= '<tr>'.
 1900:                     '<td>'.$foilindex.'</td>'.
 1901:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
 1902:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1903:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
 1904:                     "</tr>\n";
 1905:             }                
 1906:         } continue {
 1907:             $foilindex++;
 1908:         }
 1909:     } continue {
 1910:         $conceptindex++;
 1911:     }
 1912:     $table .= "</table>\n";
 1913:     #
 1914:     # Build option index with color stuff
 1915:     return ($table,\@Foils,\@Concepts);
 1916: }
 1917: 
 1918: sub build_option_index {
 1919:     my ($ORdata)= @_;
 1920:     my $table = "<table>\n";
 1921:     my $optionindex = 0;
 1922:     my @Rows;
 1923:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'_Options'}}) {
 1924:         my $color = $plotcolors->[$optionindex++];
 1925:         push (@Rows,
 1926:               '<tr>'.
 1927:               '<td bgcolor="'.$color.'">'.
 1928:               '<font color="'.$color.'">'.('*'x3).'</font>'.'</td>'.
 1929:               '<td>'.&HTML::Entities::encode($option,'<>&"').'</td>'.
 1930:               "</tr>\n");
 1931:     }
 1932:     shift(@Rows); # Throw away 'correct option chosen' color
 1933:     $table .= join('',reverse(@Rows));
 1934:     $table .= "</table>\n";
 1935: }
 1936: 
 1937: sub build_foil_key {
 1938:     my ($foils,$extra_data)= @_;
 1939:     if (! defined($extra_data)) { $extra_data = {}; }
 1940:     my $table = "<table>\n";
 1941:     my $foil_index = 0;
 1942:     my @rows;
 1943:     foreach my $foil (&mt('correct foil chosen'),@{$foils}) {
 1944:         my $color = $plotcolors->[$foil_index++];
 1945:         push (@rows,
 1946:               '<tr>'.
 1947:               '<td bgcolor="'.$color.'" class="key">'.
 1948:               '<font color="'.$color.'">'.('*'x4).'</font></td>'.
 1949:               '<td>'.&HTML::Entities::encode($foil,'<>&"').
 1950:               ('&nbsp;'x2).$extra_data->{$foil}.'</td>'.
 1951:               "</tr>\n");
 1952:     }
 1953:     shift(@rows); # Throw away 'correct foil chosen' color
 1954:     $table .= join('',reverse(@rows));
 1955:     $table .= "</table>\n";
 1956: }
 1957: 
 1958: #########################################################
 1959: #########################################################
 1960: ##
 1961: ##   Generic Interface Routines
 1962: ##
 1963: #########################################################
 1964: #########################################################
 1965: sub CreateInterface {
 1966:     ##
 1967:     ## Environment variable initialization
 1968:     if (! exists$ENV{'form.AnalyzeOver'}) {
 1969:         $ENV{'form.AnalyzeOver'} = 'tries';
 1970:     }
 1971:     ##
 1972:     ## Build the menu
 1973:     my $Str = '';
 1974:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
 1975:         (undef,'Detailed Problem Analysis');
 1976:     $Str .= '<table cellspacing="5">'."\n";
 1977:     $Str .= '<tr>';
 1978:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
 1979:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
 1980: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
 1981:     $Str .= '<td align="center">&nbsp;</td>';
 1982:     $Str .= '</tr>'."\n";
 1983:     ##
 1984:     ## 
 1985:     $Str .= '<tr><td align="center">'."\n";
 1986:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1987:     $Str .= '</td>';
 1988:     #
 1989:     $Str .= '<td align="center">';
 1990:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1991:     $Str .= '</td>';
 1992:     #
 1993: #    $Str .= '<td align="center">';
 1994:     my $only_seq_with_assessments = sub { 
 1995:         my $s=shift;
 1996:         if ($s->{'num_assess'} < 1) { 
 1997:             return 0;
 1998:         } else { 
 1999:             return 1;
 2000:         }
 2001:     };
 2002:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
 2003:                                               $only_seq_with_assessments);
 2004:     ##
 2005:     ##
 2006:     $Str .= '<td>';
 2007:     ##
 2008:     my $showprob_checkbox = 
 2009:         '<input type="checkbox" name="show_prob" value="true" ';
 2010:     if ($ENV{'form.show_prob'} eq 'true') {
 2011:         $showprob_checkbox .= 'checked ';
 2012:     }
 2013:     $showprob_checkbox.= ' />';
 2014:     $Str.= '<nobr><label>'.
 2015:         &mt('Show problem [_1]',$showprob_checkbox).
 2016:         '</label></nobr><br />';
 2017:     ##
 2018:     my $analyze_selector = '<select name="AnalyzeOver" >';
 2019:     $analyze_selector .= '<option value="tries" ';
 2020:     if (! exists($ENV{'form.AnalyzeOver'}) || 
 2021:         $ENV{'form.AnalyzeOver'} eq 'tries'){
 2022:         # Default to tries
 2023:         $analyze_selector .= ' selected ';
 2024:     }
 2025:     $analyze_selector .= '>'.&mt('Tries').'</option>';
 2026:     $analyze_selector .= '<option value="time" ';
 2027:     $analyze_selector .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'time');
 2028:     $analyze_selector .= '>'.&mt('Time').'</option>';
 2029:     $analyze_selector .= '</select>';
 2030:     $Str .= '<nobr><label>'.
 2031:         &mt('Analyze Over [_1] [_2]',
 2032:             $analyze_selector,
 2033:             &Apache::loncommon::help_open_topic('Analysis_Analyze_Over')).
 2034:             '</label></nobr><br />'.$/;
 2035:     ##
 2036:     my $numplots_selector = '<select name="NumPlots">';
 2037:     if (! exists($ENV{'form.NumPlots'}) 
 2038:         || $ENV{'form.NumPlots'} < 1 
 2039:         || $ENV{'form.NumPlots'} > 20) {
 2040:         $ENV{'form.NumPlots'} = 5;
 2041:     }
 2042:     foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
 2043:         $numplots_selector .= '<option value="'.$i.'" ';
 2044:         if ($ENV{'form.NumPlots'} == $i) { $numplots_selector.=' selected '; }
 2045:         $numplots_selector .= '>'.$i.'</option>';
 2046:     }
 2047:     $numplots_selector .= '</select></nobr><br />';
 2048:     $Str .= '<nobr><label>'.&mt('Number of Plots [_1]',$numplots_selector).
 2049:         '</label></nobr>';
 2050:     ##
 2051:     $Str .= '<nobr><label>'.&mt('Status: [_1]',
 2052:                                  '<input type="text" '.
 2053:                                  'name="stats_status" size="60" value="" />'
 2054:                                  ).
 2055:                     '</label></nobr>';
 2056:     $Str .= '</td>';
 2057:     ##
 2058:     ##
 2059:     $Str .= '</tr>'."\n";
 2060:     $Str .= '</table>'."\n";
 2061:     return $Str;
 2062: }
 2063: 
 2064: #########################################################
 2065: #########################################################
 2066: ##
 2067: ##              Misc Option Response functions
 2068: ##
 2069: #########################################################
 2070: #########################################################
 2071: sub get_time_from_row {
 2072:     my ($row) = @_;
 2073:     if (ref($row)) {
 2074:         return $row->[&Apache::loncoursedata::RD_timestamp()];
 2075:     } 
 2076:     return undef;
 2077: }
 2078: 
 2079: sub get_tries_from_row {
 2080:     my ($row) = @_;
 2081:     if (ref($row)) {
 2082:         return $row->[&Apache::loncoursedata::RD_tries()];
 2083:     }
 2084:     return undef;
 2085: }
 2086: 
 2087: sub hashify_attempt {
 2088:     my ($row) = @_;
 2089:     my %attempt;
 2090:     $attempt{'student'}    = $row->[&Apache::loncoursedata::RD_sname()];
 2091:     $attempt{'tries'}      = $row->[&Apache::loncoursedata::RD_tries()];
 2092:     $attempt{'submission'} = &Apache::lonnet::unescape($row->[&Apache::loncoursedata::RD_submission()]);
 2093:     $attempt{'award'}      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 2094:     $attempt{'timestamp'}  = $row->[&Apache::loncoursedata::RD_timestamp()];
 2095:     return %attempt;
 2096: }
 2097: 
 2098: sub Process_OR_Row {
 2099:     my ($row) = @_;
 2100:     my %RowData;
 2101: #    my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 2102:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 2103:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
 2104:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
 2105:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
 2106: #    my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
 2107:     return undef if ($award eq 'MISSING_ANSWER');
 2108:     if (&submission_is_correct($award)) {
 2109:         $RowData{'_correct'} = 1;
 2110:     }
 2111:     $RowData{'_total'} = 1;
 2112:     my @Foilgrades = split('&',$grading);
 2113:     my @Foilsubs   = split('&',$submission);
 2114:     for (my $j=0;$j<=$#Foilgrades;$j++) {
 2115:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 2116:         $foilid = &Apache::lonnet::unescape($foilid);
 2117:         my (undef,$submission) = split('=',$Foilsubs[$j]);
 2118:         if ($correct) {
 2119:             $RowData{$foilid}->{'_correct'}++;
 2120:         } else {
 2121:             $submission = &Apache::lonnet::unescape($submission);
 2122:             $RowData{$foilid}->{$submission}++;
 2123:         }
 2124:         $RowData{$foilid}->{'_total'}++;
 2125:     }
 2126:     return %RowData;
 2127: }
 2128: 
 2129: sub submission_is_correct {
 2130:     my ($award) = @_;
 2131:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
 2132:         return 1;
 2133:     } else {
 2134:         return 0;
 2135:     }
 2136: }
 2137: 
 2138: 1;
 2139: 
 2140: __END__

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