Annotation of loncom/interface/statistics/lonproblemanalysis.pm, revision 1.105

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

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