File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.111: download - view: text, annotated - select for diffs
Wed Feb 16 17:43:20 2005 UTC (19 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Remove excel output of option response data.

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

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