File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.128: download - view: text, annotated - select for diffs
Fri Aug 18 15:15:38 2006 UTC (17 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_2_X, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, HEAD
Bug 4954.  Filter title changed from "Enrollment Status" to "Access Status".  Access Status selections in lonhtmlcommon set to:
Currently Has Access
Will Have Future Access
Previously Had Access
Any Access Status
See comment appended to bug 4954 for more information.
Documentation updated to include ability to selectively display students with future access.

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

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