File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.97: download - view: text, annotated - select for diffs
Fri Oct 29 15:27:39 2004 UTC (19 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Radiobutton response works for concepts, computed answers, and the much
more common incarnations.
Removed some dead code.
More to be done.

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

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