File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.119: download - view: text, annotated - select for diffs
Tue Mar 15 00:51:43 2005 UTC (19 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Make sure we ahve a value (not undef) for the number of correct answers.
Fixed unfinished code in numerical response analysis.

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

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