File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.92: download - view: text, annotated - select for diffs
Mon Oct 4 19:49:41 2004 UTC (19 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
OR tries analysis now checks for missing data and gives a friendly
message instead of showing a bunch of empty graphs.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemanalysis.pm,v 1.92 2004/10/04 19:49:41 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:                 &RadioResponseAnalysis($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:     my ($max,$min) = 
  226:         &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$Students,
  227:                                                    'Statistics',
  228:                                                    'stats_status');
  229:     return if ($c->aborted());
  230:     #
  231:     # Collate the data
  232:     my %Data;
  233:     foreach my $student (@$Students) {
  234:         my $answer = $student->{'answer'};
  235:         $Data{$answer}++;
  236:     }
  237:     my @Labels = sort {$a <=> $b } keys(%Data);
  238:     my @PlotData = @Data{@Labels};
  239:     #
  240:     my $width  = 500; 
  241:     my $height = 100;
  242:     my $plot = &one_dimensional_plot($r,500,100,scalar(@$Students),
  243:                                      \@Labels,\@PlotData);
  244:     $r->print($plot);
  245:     return;
  246: }
  247: 
  248: sub one_dimensional_plot {
  249:     my ($r,$width,$height,$N,$Labels,$Data)=@_;
  250:     #
  251:     # Compute data -> image scaling factors
  252:     my $min = $Labels->[0];
  253:     my $max = $Labels->[-1];
  254:     if ($max == $min) {
  255:         $max =$min+1;
  256:     }
  257:     my $h_scale = ($width-10)/($max-$min);
  258:     #
  259:     my $max_y = 0;
  260:     foreach (@$Data) {
  261:         $max_y = $_ if ($max_y < $_);
  262:     }
  263:     my $ticscale = 5;
  264:     if ($max_y * $ticscale > $height/2) {
  265:         $ticscale = int($height/2/$max_y);
  266:         $ticscale = 1 if ($ticscale < 1);
  267:     }
  268:     #
  269:     # Create the plot
  270:     my $plot = 
  271:         qq{<drawimage width="$width" height="$height" bgcolor="transparent" >};
  272:     for (my $idx=0;$idx<scalar(@$Labels);$idx++) {
  273:         my $xloc = 5+$h_scale*($Labels->[$idx] - $min);
  274:         my $top    = $height/2-$Data->[$idx]*$ticscale;
  275:         my $bottom = $height/2+$Data->[$idx]*$ticscale;
  276:         $plot .= 
  277:             &line($xloc,$top,$xloc,$bottom,'888888',1);
  278:     }
  279:     #
  280:     # Put the scale on last to ensure it is on top of the data.
  281:     if ($min < 0 && $max > 0) {
  282:         my $circle_x = 5+$h_scale*abs($min);  # '0' in data coordinates
  283:         my $r = 4;
  284:         $plot .= &line(5,$height/2,$circle_x-$r,$height/2,'000000',1);
  285:         $plot .= &circle($circle_x,$height/2,$r+1,'000000');
  286:         $plot .= &line($circle_x+$r,$height/2,$width-5,$height/2,'000000',1);
  287:     } else {
  288:         $plot .= &line(5,$height/2,$width-5,$height/2,'000000',1);
  289:     }
  290:     $plot .= '</drawimage>';
  291:     my $plotresult =  &Apache::lonxml::xmlparse($r,'web',$plot);
  292:     
  293:     my $title = 'Distribution of correct answers';
  294:     my $result = '<table>'.
  295:         '<tr><td colspan="3" align="center">'.
  296:         '<font size="+2">'.$title.' (N='.$N.')'.
  297:         '</font>'.
  298:         '</td></tr>'.
  299:         '<tr>'.
  300:         '<td valign="center">'.$min.'</td>'.
  301:         '<td>'.$plotresult.'</td>'.
  302:         '<td valign="center">'.$max.'</td>'.
  303:         '</tr>'.
  304:         '<tr><td colspan="3" align="center">'.
  305:         'Maximum Number of Coinciding Values: '.$max_y.
  306:         '</td></tr>'.
  307:         '</table>';
  308:     return $result;
  309: }
  310: 
  311: ##
  312: ## Helper subroutines for <drawimage>.  
  313: ## These should probably go somewhere more suitable soon.
  314: sub line {
  315:     my ($x1,$y1,$x2,$y2,$color,$thickness) = @_;
  316:     return qq{<line x1="$x1" y1="$y1" x2="$x2" y2="$y2" color="$color" thickness="$thickness" />$/};
  317: }
  318: 
  319: sub text {
  320:     my ($x,$y,$color,$text,$font,$direction) = @_;
  321:     if (! defined($font) || $font !~ /^(tiny|small|medium|large|giant)$/) {
  322:         $font = 'medium';
  323:     }
  324:     if (! defined($direction) || $direction ne 'vertical') {
  325:         $direction = '';
  326:     }
  327:     return qq{<text x="$x" y="$y" color="$color" font="$font" direction="$direction" >$text</text>};
  328: }
  329: 
  330: sub rectangle {
  331:     my ($x1,$y1,$x2,$y2,$color,$thickness,$filled) = @_;
  332:     return qq{<rectangle x1="$x1" y1="$y1" x2="$x2" y2="$y2" color="$color" thickness="$thickness" filled="$filled" />};
  333: }
  334: 
  335: sub arc {
  336:     my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=@_;
  337:     return qq{<arc x="$x" y="$y" width="$width" height="$height" start="$start" end="$end" color="$color" thickness="$thickness" filled="$filled" />};
  338: }
  339: 
  340: sub circle {
  341:     my ($x,$y,$radius,$color,$thickness,$filled)=@_;
  342:     return &arc($x,$y,$radius,$radius,0,360,$color,$thickness,$filled);
  343: }
  344: 
  345: sub build_student_data_worksheet {
  346:     my ($workbook,$format) = @_;
  347:     my $rows_output = 3;
  348:     my $cols_output = 0;
  349:     my $worksheet  = $workbook->addworksheet('Student Data');
  350:     $worksheet->write($rows_output++,0,'Student Data',$format->{'h3'});
  351:     my @Headers = ('full name','username','domain','section',
  352:                    "student\nnumber",'identifier');
  353:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
  354:     my @Students = @Apache::lonstatistics::Students;
  355:     my $studentrows = &Apache::loncoursedata::get_student_data(\@Students);
  356:     my %ids;
  357:     foreach my $row (@$studentrows) {
  358:         my ($mysqlid,$student) = @$row;
  359:         $ids{$student}=$mysqlid;
  360:     }
  361:     foreach my $student (@Students) {
  362:         my $name_domain = $student->{'username'}.':'.$student->{'domain'};
  363:         $worksheet->write_row($rows_output++,0,
  364:                           [$student->{'fullname'},
  365:                            $student->{'username'},$student->{'domain'},
  366:                            $student->{'section'},$student->{'id'},
  367:                            $ids{$name_domain}]);
  368:     }
  369:     return $worksheet;
  370: }
  371: 
  372: #########################################################
  373: #########################################################
  374: ##
  375: ##      Radio Response Routines
  376: ##
  377: #########################################################
  378: #########################################################
  379: sub RadioResponseAnalysis {
  380:     my ($r,$problem,$problem_data,$Students) = @_;
  381:     my ($resource,$respid) = ($problem->{'resource'},
  382:                               $problem->{'respid'});
  383:     my $analysis_html;
  384:     my $PerformanceData = &Apache::loncoursedata::get_response_data
  385:         (\@Apache::lonstatistics::SelectedSections,
  386:          $Apache::lonstatistics::enrollment_status,
  387:          $resource->{'symb'},$respid);
  388:     if (! defined($PerformanceData) || 
  389:         ref($PerformanceData) ne 'ARRAY' ) {
  390:         $analysis_html = '<h2>'.
  391:             &mt('There is no submission data for this resource').
  392:             '</h2>';
  393:         $r->print($analysis_html);
  394:         return;
  395:     }
  396:     if (exists($ENV{'form.ExcelOutput'})) {
  397:         $analysis_html .= &RR_Excel_output($r,$problem->{'resource'},
  398:                                            $PerformanceData,$problem_data);
  399:     } elsif ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  400:         $analysis_html .= &RR_Tries_Analysis($r,$problem->{'resource'},
  401:                                              $PerformanceData,$problem_data);
  402:     } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  403:         $analysis_html .= &RR_Time_Analysis($r,$problem->{'resource'},
  404:                                             $PerformanceData,$problem_data);
  405:     } else {
  406:         $analysis_html .= '<h2>'.
  407:            &mt('The analysis you have selected is not supported at this time').
  408:             '</h2>';
  409:     }
  410:     $r->print($analysis_html);
  411: }
  412: 
  413: sub RR_Excel_output   { 
  414:     my ($r,$PerformanceData,$problem_data) = @_;
  415:     return '<h1>No!</h1>';
  416: }
  417: 
  418: sub RR_Tries_Analysis { 
  419:     my ($r,$resource,$PerformanceData,$problem_data) = @_;
  420:     my $analysis_html;
  421:     my $mintries = 1;
  422:     my $maxtries = $ENV{'form.NumPlots'};
  423:     my ($table,$Foils,$Concepts) = &build_foil_index($problem_data);
  424:     if ((! defined($Concepts)) || (@$Concepts < 2)) {
  425:         $table = '<h3>'.
  426:             &mt('Not enough data for concept analysis.  '.
  427:                 'Performing Foil Analysis').
  428:             '</h3>'.$table;
  429:     }
  430:     $analysis_html .= $table;
  431:     my @TryData = &RR_tries_data_analysis($r,$PerformanceData);
  432:         $analysis_html .= &RR_Tries_Foil_Analysis($mintries,$maxtries,$Foils,
  433:                                                  \@TryData,$problem_data);
  434:     return $analysis_html;
  435: }
  436: 
  437: sub RR_tries_data_analysis {
  438:     my ($r,$Attempt_data) = @_;
  439:     my @TryData;
  440:     foreach my $attempt (@$Attempt_data) {
  441:         my %Attempt = &hashify_attempt($attempt);
  442:         my ($answer,undef) = split('=',$Attempt{'submission'});
  443:         $TryData[$Attempt{'tries'}]->{$answer}++;
  444:     }
  445:     return @TryData;
  446: }
  447: 
  448: sub RR_Time_Analysis  { 
  449:     my ($r,$PerformanceData,$problem_data) = @_;
  450:     my $html;
  451:     return $html;
  452: }
  453: 
  454: sub RR_Tries_Foil_Analysis {
  455:     my ($min,$max,$Foils,$TryData,$problem_data) = @_;
  456:     my $html;
  457:     #
  458:     # Compute the data neccessary to make the plots
  459:     for (my $try=$min;$try<=$max;$try++) {
  460:         my @PlotData_Correct; 
  461:         my @PlotData_Incorrect;
  462:         next if ($try > scalar(@{$TryData}));
  463:         next if (! defined($TryData->[$try]));
  464:         my %DataSet = %{$TryData->[$try]};
  465:         my $total = 0;
  466:         foreach my $foilid (@$Foils) {
  467:             $total += $DataSet{$foilid};
  468:         }
  469:         foreach my $foilid (@$Foils) {
  470:             if ($total == 0) {
  471:                 push (@PlotData_Correct,0);
  472:                 push (@PlotData_Incorrect,0);
  473:             } else {
  474:                 if ($problem_data->{'_Foils'}->{$foilid}->{'value'} eq 'true') {
  475:                     push (@PlotData_Correct,
  476:                           int(100*$DataSet{$foilid}/$total));
  477:                     push (@PlotData_Incorrect,0);
  478:                 } else {
  479:                     push (@PlotData_Correct,0);
  480:                     push (@PlotData_Incorrect,
  481:                           int(100*$DataSet{$foilid}/$total));
  482:                 }
  483:             }
  484:         }
  485:         my $title='Attempt '.$try.' (N='.$total.')';
  486:         my $xlabel = 'Foil Chosen';
  487:         $html.=  &Apache::loncommon::DrawBarGraph($title,
  488:                                                   $xlabel,
  489:                                                   'Percent Choosing',
  490:                                                   100,
  491:                                                   ['#33ff00','#ff3300'],
  492:                                                   undef,
  493:                                                   \@PlotData_Correct,
  494:                                                   \@PlotData_Incorrect);
  495:     }
  496:     return $html;
  497: }
  498: 
  499: sub RR_Tries_Concept_Analysis {
  500:     my ($min,$max,$Concepts,$ResponseData,$problem_data) = @_;
  501:     my $html;
  502:     return $html;
  503: }
  504: 
  505: sub RR_Time_Foil_Analysis {
  506:     my ($min,$max,$Foils,$ResponseData,$problem_data) = @_;
  507:     my $html;
  508:     return $html;
  509: }
  510: 
  511: sub RR_Time_Concept_Analysis {
  512:     my ($min,$max,$Concepts,$ResponseData,$problem_data) = @_;
  513:     my $html;
  514:     return $html;
  515: }
  516: 
  517: 
  518: sub get_Radio_problem_data {
  519:     my ($url) = @_;
  520:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
  521:     (my $garbage,$Answ)=split('_HASH_REF__',$Answ,2);
  522:     my %Answer = &Apache::lonnet::str2hash($Answ);
  523:     my %Partdata;
  524:     foreach my $part (@{$Answer{'parts'}}) {
  525:         while (my($key,$value) = each(%Answer)) {
  526: #            if (ref($value) eq 'ARRAY') {
  527: #                &Apache::lonnet::logthis('is ref part:'.$part.' '.$key.'='.join(',',@$value));
  528: #            } else {
  529: #                &Apache::lonnet::logthis('notref part:'.$part.' '.$key.'='.$value);
  530: #            }                
  531:             next if ($key !~ /^$part/);
  532:             $key =~ s/^$part\.//;
  533:             if ($key eq 'foils') {
  534:                 $Partdata{$part}->{'_Foils'}=$value;
  535:             } elsif ($key eq 'options') {
  536:                 $Partdata{$part}->{'_Options'}=$value;
  537:             } elsif ($key eq 'shown') {
  538:                 $Partdata{$part}->{'_Shown'}=$value;
  539:             } elsif ($key =~ /^foil.value.(.*)$/) {
  540:                 $Partdata{$part}->{$1}->{'value'}=$value;
  541:             } elsif ($key =~ /^foil.text.(.*)$/) {
  542:                 $Partdata{$part}->{$1}->{'text'}=$value;
  543:             }
  544:         }
  545:     }
  546:     return %Partdata;
  547: }
  548: 
  549: #########################################################
  550: #########################################################
  551: ##
  552: ##      Option Response Routines
  553: ##
  554: #########################################################
  555: #########################################################
  556: sub OptionResponseAnalysis {
  557:     my ($r,$problem,$problem_data,$Students) = @_;
  558:     my ($resource,$respid) = ($problem->{'resource'},
  559:                               $problem->{'respid'});
  560:     # Note: part data is not needed.
  561:     my $PerformanceData = &Apache::loncoursedata::get_response_data
  562:         (\@Apache::lonstatistics::SelectedSections,
  563:          $Apache::lonstatistics::enrollment_status,
  564:          $resource->{'symb'},$respid);
  565:     if (! defined($PerformanceData) || 
  566:         ref($PerformanceData) ne 'ARRAY' ) {
  567:         $r->print('<h2>'.
  568:                   &mt('There is no student data for this problem.').
  569:                   '</h2>');
  570:     }  else {
  571:         $r->rflush();
  572:         if (exists($ENV{'form.ExcelOutput'})) {
  573:             my $result = &OR_excel_sheet($r,$resource,
  574:                                          $PerformanceData,
  575:                                          $problem_data);
  576:             $r->print($result);
  577:             $r->rflush();
  578:         } else {
  579:             if ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  580:                 my $analysis_html = &OR_tries_analysis($r,
  581:                                                     $PerformanceData,
  582:                                                     $problem_data);
  583:                 $r->print($analysis_html);
  584:                 $r->rflush();
  585:             } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  586:                 my $analysis_html = &OR_time_analysis($PerformanceData,
  587:                                                    $problem_data);
  588:                 $r->print($analysis_html);
  589:                 $r->rflush();
  590:             } else {
  591:                 $r->print('<h2>'.
  592:                           &mt('The analysis you have selected is '.
  593:                               'not supported at this time').
  594:                           '</h2>');
  595:             }
  596:         }
  597:     }
  598: }
  599: 
  600: #########################################################
  601: #
  602: #       Option Response:  Tries Analysis
  603: #
  604: #########################################################
  605: sub OR_tries_analysis {
  606:     my ($r,$PerformanceData,$ORdata) = @_;
  607:     my $mintries = 1;
  608:     my $maxtries = $ENV{'form.NumPlots'};
  609:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  610:     my %response_data = &OR_analyze_by_tries($r,$PerformanceData,
  611:                                                      $mintries,$maxtries);
  612:     my $analysis = '';
  613:     #
  614:     # Compute the data necessary to make the plots
  615:     my @foil_plot; 
  616:     my @concept_data;
  617:     for (my $j=0;$j<=scalar(@$Concepts);$j++) {
  618:         my $concept = $Concepts->[$j];
  619:         foreach my $foilid (@{$concept->{'foils'}}) {
  620:             for (my $try=$mintries;$try<=$maxtries;$try++) {
  621:                 # concept analysis data
  622:                 $concept_data[$j]->{'_correct'} +=
  623:                     $response_data{$foilid}->[$try]->{'_correct'};
  624:                 $concept_data[$j]->{'_total'} +=
  625:                     $response_data{$foilid}->[$try]->{'_total'};
  626:                 #
  627:                 # foil analysis data
  628:                 if ($response_data{$foilid}->[$try]->{'_total'} == 0) {
  629:                     push(@{$foil_plot[$try]->{'_correct'}},0);
  630:                 } else {
  631:                     push(@{$foil_plot[$try]->{'_correct'}},
  632:                          100*$response_data{$foilid}->[$try]->{'_correct'}/
  633:                          $response_data{$foilid}->[$try]->{'_total'});
  634:                 }
  635:                 foreach my $option (@{$ORdata->{'_Options'}}) {
  636:                     push(@{$foil_plot[$try]->{'_total'}},
  637:                          $response_data{$foilid}->[$try]->{'_total'});
  638:                     if ($response_data{$foilid}->[$try]->{'_total'} == 0) {
  639:                         push (@{$foil_plot[$try]->{$option}},0);
  640:                     } else {
  641:                         if ($response_data{$foilid}->[$try]->{'_total'} ==
  642:                             $response_data{$foilid}->[$try]->{'_correct'}) {
  643:                             push(@{$foil_plot[$try]->{$option}},0);
  644:                         } else {
  645:                             push (@{$foil_plot[$try]->{$option}},
  646:                                   100 * 
  647:                                   $response_data{$foilid}->[$try]->{$option} / 
  648:                                   ($response_data{$foilid}->[$try]->{'_total'} 
  649:                                    - 
  650:                                    $response_data{$foilid}->[$try]->{'_correct'}
  651:                                    ));
  652:                         }
  653:                     }
  654:                 } # End of foreach my $option
  655:             }
  656:         } # End of foreach my $foilid
  657:     } # End of concept loops
  658:     # 
  659:     # Build a table for the plots
  660:     my $analysis_html = "<table>\n";
  661:     my $optionkey = &build_option_index($ORdata);
  662:     my $num_concepts = 1;
  663:     if (defined($Concepts)) { $num_concepts = scalar(@$Concepts); }
  664:     #
  665:     for (my $try=$mintries;$try<=$maxtries;$try++) {
  666:         if (! defined($response_data{'_total'}->[$try]) ||
  667:             $response_data{'_total'}->[$try] == 0) { 
  668:             if ($try > 1) {
  669:                 $analysis_html.= '<tr><td align="center" colspan="4"><b>'.
  670:                     &mt('None of the selected students attempted the problem more than [_1] times.',$try-1).
  671:                     '</b></td></tr>';
  672:             } else {
  673:                 $analysis_html.= '<tr><td colspan="4" align="center"><b>'.
  674:                     &mt('None of the selected students have attempted the problem').'</b></td></tr>';
  675:             }
  676:             last;
  677:         }
  678:         my $concept_graph='';
  679:         if ($num_concepts > 1) {
  680:             #
  681:             # Create concept plot
  682:             my @concept_plot_data;
  683:             for (my $j=0;$j<=$#concept_data;$j++) {
  684:                 my $total = $concept_data[$j]->{'_total'};
  685:                 if ($total == 0) {
  686:                     $concept_plot_data[$j] = 0;
  687:                 } else {
  688:                     $concept_plot_data[$j] = 100 * 
  689:                         sprintf('%0.3f',
  690:                                 $concept_data[$j]->{'_correct'} / $total);
  691:                 }
  692:             }
  693:             #
  694:             my $title;
  695:             my $count = $response_data{'_total'}->[$try];
  696:             $title = 'Attempt '.$try.' (N='.$count.')';
  697:             $concept_graph = &Apache::loncommon::DrawBarGraph
  698:                 ($title,'Concept Number','Percent Correct',
  699:                  100,$plotcolors,undef,\@concept_plot_data);
  700:         }
  701:         #
  702:         # Create Foil Plots
  703:         my $count = $response_data{'_total'}->[$try];
  704:         my $title = 'Attempt '.$try.' (N='.$count.')';
  705:         my @Datasets;
  706:         foreach my $option ('_correct',@{$ORdata->{'_Options'}}) {
  707:             next if (! exists($foil_plot[$try]->{$option}));
  708:             push(@Datasets,$foil_plot[$try]->{$option});
  709:         }
  710:         #
  711:         # Put a blank in the data set between concepts
  712:         for (my $set =0;$set<=$#Datasets;$set++) {
  713:             my @Data = @{$Datasets[$set]};
  714:             my $idx = 0;
  715:             foreach my $concept (@{$Concepts}) {
  716:                 foreach my $foilid (@{$concept->{'foils'}}) {
  717:                     $Datasets[$set]->[$idx++]=shift(@Data);
  718:                 }
  719:                 if ($concept->{'name'} ne $Concepts->[-1]->{'name'}) {
  720:                     $Datasets[$set]->[$idx++] = 0;
  721:                 }
  722:             }
  723:         }
  724:         #
  725:         # Set up the labels needed for the bar graph
  726:         my @Labels;
  727:         my $idx = 1;
  728:         foreach my $concept (@{$Concepts}) {
  729:             foreach my $foilid (@{$concept->{'foils'}}) {
  730:                 push(@Labels,$idx++);
  731:             }
  732:             push(@Labels,'');
  733:         }
  734:         #
  735:         my $correct_graph = &Apache::loncommon::DrawBarGraph
  736:             ($title,'Foil Number','Percent Correct',
  737:              100,$plotcolors,\@Labels,$Datasets[0]);
  738:         
  739:         #
  740:         #
  741:         next if (! defined($Datasets[0]));
  742:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
  743:             $Datasets[0]->[$i]=0;
  744:         }
  745:         $count = $response_data{'_total'}->[$try] - 
  746:                                            $response_data{'_correct'}->[$try];
  747:         $title = 'Attempt '.$try.' (N='.$count.')';
  748:         my $incorrect_graph = &Apache::loncommon::DrawBarGraph
  749:             ($title,'Foil Number','% Option Chosen Incorrectly',
  750:              100,$plotcolors,\@Labels,@Datasets);
  751:         $analysis_html.= 
  752:             '<tr>'.
  753:             '<td>'.$concept_graph.'</td>'.
  754:             '<td>'.$correct_graph.'</td>'.
  755:             '<td>'.$incorrect_graph.'</td>'.
  756:             '<td>'.$optionkey.'<td>'.
  757:             '</tr>'.$/;
  758:     }
  759:     $analysis_html .= "</table>\n";
  760:     $table .= $analysis_html;
  761:     return $table;
  762: }
  763: 
  764: sub OR_analyze_by_tries {
  765:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
  766:     my %Trydata;
  767:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
  768:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
  769:     foreach my $row (@$PerformanceData) {
  770:         next if (! defined($row));
  771:         my $tries = &get_tries_from_row($row);
  772:         my %Row   = &Process_OR_Row($row);
  773:         next if (! %Row);
  774:         while (my ($foilid,$href) = each(%Row)) {
  775:             if (! ref($href)) { 
  776:                 $Trydata{$foilid}->[$tries] += $href;
  777:                 next;
  778:             }
  779:             while (my ($option,$value) = each(%$href)) {
  780:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
  781:             }
  782:         }
  783:     }
  784:     return %Trydata;
  785: }
  786: 
  787: #########################################################
  788: #
  789: #     Option Response: Time Analysis
  790: #
  791: #########################################################
  792: sub OR_time_analysis {
  793:     my ($performance_data,$ORdata) = @_;
  794:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  795:     my $foilkey = &build_option_index($ORdata);
  796:     my $num_concepts = 1;
  797:     if (defined($Concepts)) { $num_concepts = scalar(@$Concepts); }
  798:     #
  799:     if ($num_concepts < 2) {
  800:         $table = '<h3>'.
  801:             &mt('Not enough data for concept analysis.  '.
  802:                 'Performing Foil Analysis').
  803:                 '</h3>'.$table;
  804:     }
  805:     #
  806:     my $num_plots = $ENV{'form.NumPlots'};
  807:     my $num_data = scalar(@$performance_data)-1;
  808:     my $percent = sprintf('%2f',100/$num_plots);
  809:     #
  810:     $table .= "<table>\n";
  811:     for (my $i=0;$i<$num_plots;$i++) {
  812:         ##
  813:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  814:             ('startdate_'.$i);
  815:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  816:             ('enddate_'.$i);
  817:         if (! defined($starttime) || ! defined($endtime)) {
  818:             my $sec_in_day = 86400;
  819:             my $last_sub_time = &get_time_from_row($performance_data->[-1]);
  820:             my ($sday,$smon,$syear);
  821:             (undef,undef,undef,$sday,$smon,$syear) = 
  822:                 localtime($last_sub_time - $sec_in_day*$i);
  823:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
  824:             $endtime = $starttime + $sec_in_day;
  825:             if ($i == ($num_plots -1 )) {
  826:                 $starttime = &get_time_from_row($performance_data->[0]);
  827:             }
  828:         }
  829:         $table .= '<tr><td colspan="4" align="center">'.
  830:             &mt('Data from [_1] to [_2]',
  831:                 &Apache::lonlocal::locallocaltime($starttime),
  832:                 &Apache::lonlocal::locallocaltime($endtime)).'</td></tr>'.$/;
  833:         my $startdateform = &Apache::lonhtmlcommon::date_setter
  834:             ('Statistics','startdate_'.$i,$starttime);
  835:         my $enddateform = &Apache::lonhtmlcommon::date_setter
  836:             ('Statistics','enddate_'.$i,$endtime);
  837:         #
  838:         my $begin_index;
  839:         my $end_index;
  840:         my $j;
  841:         while (++$j < scalar(@$performance_data)) {
  842:             last if (&get_time_from_row($performance_data->[$j]) 
  843:                                                               > $starttime);
  844:         }
  845:         $begin_index = $j;
  846:         while ($j < scalar(@$performance_data)) {
  847:             if (&get_time_from_row($performance_data->[$j]) > $endtime) {
  848:                 last;
  849:             } else {
  850:                 $j++;
  851:             }
  852:         }
  853:         $end_index = $j;
  854:         ##
  855:         my $interval = {
  856:             begin_index => $begin_index,
  857:             end_index   => $end_index,
  858:             startdateform => $startdateform,
  859:             enddateform   => $enddateform,
  860:         };
  861:         my $concept_correct_plot = '';
  862:         if ($num_concepts > 1) {
  863:             $concept_correct_plot = 
  864:                 &OR_Concept_Time_Analysis($performance_data,
  865:                                           $ORdata,$Concepts,
  866:                                           $interval);
  867:         }
  868:         my ($foil_correct_plot,$foil_incorrect_plot) = 
  869:             &OR_Foil_Time_Analysis($performance_data,$ORdata,
  870:                                    $Foils,$Concepts,$interval);
  871:         $table .= '<tr>'.
  872:             '<td>'.$concept_correct_plot.'</td>'.
  873:             '<td>'.$foil_correct_plot.'</td>'.
  874:             '<td>'.$foil_incorrect_plot.'</td>'.
  875:             '<td align="left" valign="top">'.$foilkey.'</td></tr>'.$/;
  876:         $table .= '<tr><td colspan="4" align="center">'.
  877:             &mt('Start time: [_1]',
  878:                 $interval->{'startdateform'}).'<br />'.
  879:             &mt('End time: [_1]',
  880:                 $interval->{'enddateform'}).
  881:                 '</td></tr>'.$/;
  882:         $table.= '<tr><td colspan="4">&nbsp</td></tr>'.$/;
  883:     }
  884:     $table .= '</table>';
  885:     #
  886:     return $table;
  887: }
  888: 
  889: sub OR_Foil_Time_Analysis {
  890:     my ($performance_data,$ORdata,$Foils,$Concepts,$interval) = @_;
  891:     my $analysis_html;
  892:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  893:                                     $interval->{'end_index'});
  894:     my %TimeData;
  895:     #
  896:     # Compute the number getting the foils correct or incorrects
  897:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  898:         my $row = $performance_data->[$j];
  899:         next if (! defined($row));
  900:         my %Row = &Process_OR_Row($row);
  901:         while (my ($foilid,$href) = each(%Row)) {
  902:             if (! ref($href)) {
  903:                 $TimeData{$foilid} += $href;
  904:                 next;
  905:             }
  906:             while (my ($option,$value) = each(%$href)) {
  907:                 $TimeData{$foilid}->{$option}+=$value;
  908:             }
  909:         }
  910:     }
  911:     my @plotdata;
  912:     my @labels;
  913:     foreach my $concept (@{$Concepts}) {
  914:         foreach my $foil (@{$concept->{'foils'}}) {
  915:             push(@labels,scalar(@labels)+1);
  916:             my $total = $TimeData{$foil}->{'_total'};
  917:             if ($total == 0) {
  918:                 push(@{$plotdata[0]},0);
  919:             } else {
  920:                 push(@{$plotdata[0]},
  921:                      100 * $TimeData{$foil}->{'_correct'} / $total);
  922:             }
  923:             my $total_incorrect = $total - $TimeData{$foil}->{'_correct'};
  924:             for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
  925:                 my $option = $ORdata->{'_Options'}->[$i];
  926:                 if ($total_incorrect == 0) {
  927:                     push(@{$plotdata[$i+1]},0);
  928:                 } else {
  929:                     push(@{$plotdata[$i+1]},
  930:                          100 * $TimeData{$foil}->{$option} / $total_incorrect);
  931:                 }
  932:             }
  933:         }
  934:         # Put in a blank one
  935:         push(@labels,'');
  936:         push(@{$plotdata[0]},0);
  937:         for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
  938:             push(@{$plotdata[$i+1]},0);
  939:         }
  940:     }
  941:     #
  942:     # Create the plot
  943:     my $count = $end_index-$begin_index;
  944:     return ('No data','') if ($count <= 0);
  945:     my $title;
  946:     if ($count == 0) {
  947:         $title = 'no submissions';
  948:     } elsif ($count == 1) {
  949:         $title = 'one submission';
  950:     } elsif ($count > 1) {
  951:         $title = $count.' submissions';
  952:     }
  953:     my $correct_plot = &Apache::loncommon::DrawBarGraph($title,
  954:                                                        'Foil Number',
  955:                                                        'Percent Correct',
  956:                                                        100,
  957:                                                        $plotcolors,
  958:                                                        undef,
  959:                                                        $plotdata[0]);
  960:     for (my $j=0; $j< scalar(@{$plotdata[0]});$j++) {
  961:         $plotdata[0]->[$j]=0;
  962:     }
  963:     $count = $end_index-$begin_index-$TimeData{'_correct'};
  964:     if ($count == 0) {
  965:         $title = 'no submissions';
  966:     } elsif ($count < 0) {
  967:         return ($correct_plot,'');
  968:     } elsif ($count == 1) {
  969:         $title = 'one submission';
  970:     } else {
  971:         $title = $count.' submissions';
  972:     }
  973:     my $incorrect_plot = &Apache::loncommon::DrawBarGraph($title,
  974:                                                  'Foil Number',
  975:                                                  'Incorrect Option Choice',
  976:                                                  100,
  977:                                                  $plotcolors,
  978:                                                  undef,
  979:                                                  @plotdata);
  980:     return ($correct_plot,$incorrect_plot);
  981: }
  982: 
  983: sub OR_Concept_Time_Analysis {
  984:     my ($performance_data,$ORdata,$Concepts,$interval) = @_;
  985:     ##
  986:     ## Determine starttime, endtime, startindex, endindex
  987:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  988:                                     $interval->{'end_index'});
  989:     my %TimeData;
  990:     #
  991:     # Compute the number getting the foils correct or incorrects
  992:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  993:         my $row = $performance_data->[$j];
  994:         next if (! defined($row));
  995:         my %Row = &Process_OR_Row($row);
  996:         while (my ($foilid,$href) = each(%Row)) {
  997:             if (! ref($href)) {
  998:                 $TimeData{$foilid} += $href;
  999:                 next;
 1000:             }
 1001:             while (my ($option,$value) = each(%$href)) {
 1002:                 $TimeData{$foilid}->{$option}+=$value;
 1003:             }
 1004:         }
 1005:     }
 1006:     #
 1007:     # Put the data in plottable form
 1008:     my @plotdata;
 1009:     foreach my $concept (@$Concepts) {
 1010:         my ($total,$correct);
 1011:         foreach my $foil (@{$concept->{'foils'}}) {
 1012:             $total += $TimeData{$foil}->{'_total'};
 1013:             $correct += $TimeData{$foil}->{'_correct'};
 1014:         }
 1015:         if ($total == 0) {
 1016:             push(@plotdata,0);
 1017:         } else {
 1018:             push(@plotdata,100 * $correct / $total);
 1019:         }
 1020:     }
 1021:     #
 1022:     # Create the plot
 1023:     my $title = ($end_index - $begin_index).' submissions';
 1024:     return &Apache::loncommon::DrawBarGraph($title,
 1025:                                             'Concept Number',
 1026:                                             'Percent Correct',
 1027:                                             100,
 1028:                                             $plotcolors,
 1029:                                             undef,
 1030:                                             \@plotdata);
 1031: }
 1032: 
 1033: #########################################################
 1034: #########################################################
 1035: ##
 1036: ##             Excel output 
 1037: ##
 1038: #########################################################
 1039: #########################################################
 1040: sub OR_excel_sheet {
 1041:     my ($r,$resource,$performance_data,$ORdata) = @_;
 1042:     my $response = '';
 1043:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
 1044:     #
 1045:     # Create excel worksheet
 1046:     my $filename = '/prtspool/'.
 1047:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1048:         time.'_'.rand(1000000000).'.xls';
 1049:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1050:     if (! defined($workbook)) {
 1051:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1052:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
 1053:                             "This error has been logged.  ".
 1054:                             "Please alert your LON-CAPA administrator").
 1055:                   '</p>');
 1056:         return undef;
 1057:     }
 1058:     #
 1059:     $workbook->set_tempdir('/home/httpd/perl/tmp');
 1060:     my $format = &Apache::loncommon::define_excel_formats($workbook);
 1061:     #
 1062:     # Create and populate main worksheets
 1063:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
 1064:     my $student_data_sheet = &build_student_data_worksheet($workbook,$format);
 1065:     my $response_data_sheet = $workbook->addworksheet('Response Data');
 1066:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
 1067:                        $response_data_sheet) {
 1068:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
 1069:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
 1070:     }
 1071:     #
 1072:     my $result;
 1073:     $result = &OR_build_problem_data_worksheet($problem_data_sheet,$format,
 1074:                                             $Concepts,$ORdata);
 1075:     if ($result ne 'okay') {
 1076:         # Do something useful
 1077:     }
 1078:     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
 1079:                                              $performance_data,$Foils,
 1080:                                              $ORdata);
 1081:     if ($result ne 'okay') {
 1082:         # Do something useful
 1083:     }
 1084:     $response_data_sheet->activate();
 1085:     #
 1086:     # Close the excel file
 1087:     $workbook->close();
 1088:     #
 1089:     # Write a link to allow them to download it
 1090:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
 1091:               '<p><a href="'.$filename.'">'.
 1092:               &mt('Your Excel spreadsheet.').
 1093:               '</a></p>'."\n";
 1094:     return $result;
 1095: }
 1096: 
 1097: sub OR_build_problem_data_worksheet {
 1098:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
 1099:     my $rows_output = 3;
 1100:     my $cols_output = 0;
 1101:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
 1102:     ##
 1103:     ##
 1104:     my @Headers;
 1105:     if (@$Concepts > 1) {
 1106:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
 1107:                     'Foil Name','Foil Text','Correct value');
 1108:     } else {
 1109:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
 1110:     }
 1111:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1112:     my %Foildata = %{$ORdata->{'_Foils'}};
 1113:     my $conceptindex = 1;
 1114:     my $foilindex = 1;
 1115:     foreach my $concept (@$Concepts) {
 1116:         my @FoilsInConcept = @{$concept->{'foils'}};
 1117:         my $firstfoil = shift(@FoilsInConcept);
 1118:         if (@$Concepts > 1) {
 1119:             $worksheet->write_row($rows_output++,0,
 1120:                                   [$conceptindex,
 1121:                                    $concept->{'name'},
 1122:                                    $foilindex++,
 1123:                                    $Foildata{$firstfoil}->{'name'},
 1124:                                    $Foildata{$firstfoil}->{'text'},
 1125:                                    $Foildata{$firstfoil}->{'value'},]);
 1126:         } else {
 1127:             $worksheet->write_row($rows_output++,0,
 1128:                                   [ $foilindex++,
 1129:                                     $Foildata{$firstfoil}->{'name'},
 1130:                                     $Foildata{$firstfoil}->{'text'},
 1131:                                     $Foildata{$firstfoil}->{'value'},]);
 1132:         }
 1133:         foreach my $foilid (@FoilsInConcept) {
 1134:             if (@$Concepts > 1) {
 1135:                 $worksheet->write_row($rows_output++,0,
 1136:                                       ['',
 1137:                                        '',
 1138:                                        $foilindex,
 1139:                                        $Foildata{$foilid}->{'name'},
 1140:                                        $Foildata{$foilid}->{'text'},
 1141:                                        $Foildata{$foilid}->{'value'},]);
 1142:             } else {
 1143:                 $worksheet->write_row($rows_output++,0,                
 1144:                                       [$foilindex,
 1145:                                        $Foildata{$foilid}->{'name'},
 1146:                                        $Foildata{$foilid}->{'text'},
 1147:                                        $Foildata{$foilid}->{'value'},]);
 1148:             }                
 1149:         } continue {
 1150:             $foilindex++;
 1151:         }
 1152:     } continue {
 1153:         $conceptindex++;
 1154:     }
 1155:     $rows_output++;
 1156:     $rows_output++;
 1157:     ##
 1158:     ## Option data output
 1159:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
 1160:     foreach my $string (@{$ORdata->{'_Options'}}) {
 1161:         $worksheet->write($rows_output++,0,$string);
 1162:     }
 1163:     return 'okay';
 1164: }
 1165: 
 1166: sub OR_build_response_data_worksheet {
 1167:     my ($worksheet,$format,$performance_data,$Foils,$ORdata)=@_;
 1168:     my $rows_output = 3;
 1169:     my $cols_output = 0;
 1170:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
 1171:     $worksheet->set_column(1,1,20);
 1172:     $worksheet->set_column(2,2,13);
 1173:     my @Headers = ('identifier','time','award detail','attempt');
 1174:     foreach my $foil (@$Foils) {
 1175:         push (@Headers,$foil.' submission');
 1176:         push (@Headers,$foil.' grading');
 1177:     }
 1178:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1179:     #
 1180:     foreach my $row (@$performance_data) {
 1181:         next if (! defined($row));
 1182:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
 1183:         my @Foilgrades = split('&',$grading);
 1184:         my @Foilsubs   = split('&',$submission);
 1185:         my %response_data;
 1186:         for (my $j=0;$j<=$#Foilgrades;$j++) {
 1187:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1188:             my (undef,$submission) = split('=',$Foilsubs[$j]);
 1189:             $submission = &Apache::lonnet::unescape($submission);
 1190:             $response_data{$foilid.' submission'}=$submission;
 1191:             $response_data{$foilid.' award'}=$correct;
 1192:         }
 1193:         $worksheet->write($rows_output,$cols_output++,$student);
 1194:         $worksheet->write($rows_output,$cols_output++,
 1195:              &Apache::lonstathelpers::calc_serial($time),$format->{'date'});
 1196:         $worksheet->write($rows_output,$cols_output++,$award);
 1197:         $worksheet->write($rows_output,$cols_output++,$tries);
 1198:         foreach my $foilid (@$Foils) {
 1199:             $worksheet->write($rows_output,$cols_output++,
 1200:                               $response_data{$foilid.' submission'});
 1201:             $worksheet->write($rows_output,$cols_output++,
 1202:                               $response_data{$foilid.' award'});
 1203:         }
 1204:         $rows_output++;
 1205:         $cols_output = 0;
 1206:     }
 1207:     return;
 1208: }
 1209: 
 1210: sub build_foil_index {
 1211:     my ($ORdata) = @_;
 1212:     return if (! exists($ORdata->{'_Foils'}));
 1213:     my %Foildata = %{$ORdata->{'_Foils'}};
 1214:     my @Foils = sort(keys(%Foildata));
 1215:     my %Concepts;
 1216:     foreach my $foilid (@Foils) {
 1217:         push(@{$Concepts{$Foildata{$foilid}->{'_Concept'}}},
 1218:              $foilid);
 1219:     }
 1220:     undef(@Foils);
 1221:     # Having gathered the concept information in a hash, we now translate it
 1222:     # into an array because we need to be consistent about order.
 1223:     # Also put the foils in order, too.
 1224:     my $sortfunction = sub {
 1225:         my %Numbers = (one   => 1,
 1226:                        two   => 2,
 1227:                        three => 3,
 1228:                        four  => 4,
 1229:                        five  => 5,
 1230:                        six   => 6,
 1231:                        seven => 7,
 1232:                        eight => 8,
 1233:                        nine  => 9,
 1234:                        ten   => 10,);
 1235:         my $a1 = lc($a); 
 1236:         my $b1 = lc($b);
 1237:         if (exists($Numbers{$a1})) {
 1238:             $a1 = $Numbers{$a1};
 1239:         }
 1240:         if (exists($Numbers{$b1})) {
 1241:             $b1 = $Numbers{$b1};
 1242:         }
 1243:         if (($a1 =~/^\d+$/) && ($b1 =~/^\d+$/)) {
 1244:             return $a1 <=> $b1;
 1245:         } else {
 1246:             return $a1 cmp $b1;
 1247:         }
 1248:     };
 1249:     my @Concepts;
 1250:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
 1251:         if (! defined($Concepts{$concept})) {
 1252:             $Concepts{$concept}=[];
 1253: #            next;
 1254:         }
 1255:         push(@Concepts,{ name => $concept,
 1256:                         foils => [@{$Concepts{$concept}}]});
 1257:         push(@Foils,(@{$Concepts{$concept}}));
 1258:     }
 1259:     #
 1260:     # Build up the table of row labels.
 1261:     my $table = '<table border="1" >'."\n";
 1262:     if (@Concepts > 1) {
 1263:         $table .= '<tr>'.
 1264:             '<th>'.&mt('Concept Number').'</th>'.
 1265:             '<th>'.&mt('Concept').'</th>'.
 1266:             '<th>'.&mt('Foil Number').'</th>'.
 1267:             '<th>'.&mt('Foil Name').'</th>'.
 1268:             '<th>'.&mt('Foil Text').'</th>'.
 1269:             '<th>'.&mt('Correct Value').'</th>'.
 1270:             "</tr>\n";
 1271:     } else {
 1272:         $table .= '<tr>'.
 1273:             '<th>'.&mt('Foil Number').'</th>'.
 1274:             '<th>'.&mt('Foil Name').'</th>'.
 1275:             '<th>'.&mt('Foil Text').'</th>'.
 1276:             '<th>'.&mt('Correct Value').'</th>'.
 1277:             "</tr>\n";
 1278:     }        
 1279:     my $conceptindex = 1;
 1280:     my $foilindex = 1;
 1281:     foreach my $concept (@Concepts) {
 1282:         my @FoilsInConcept = @{$concept->{'foils'}};
 1283:         my $firstfoil = shift(@FoilsInConcept);
 1284:         if (@Concepts > 1) {
 1285:             $table .= '<tr>'.
 1286:                 '<td>'.$conceptindex.'</td>'.
 1287:                 '<td>'.&HTML::Entities::encode($concept->{'name'},'<>&"').'</td>'.
 1288:                 '<td>'.$foilindex++.'</td>'.
 1289:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
 1290:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1291:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
 1292:                 "</tr>\n";
 1293:         } else {
 1294:             $table .= '<tr>'.
 1295:                 '<td>'.$foilindex++.'</td>'.
 1296:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
 1297:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1298:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
 1299:                 "</tr>\n";
 1300:         }
 1301:         foreach my $foilid (@FoilsInConcept) {
 1302:             if (@Concepts > 1) {
 1303:                 $table .= '<tr>'.
 1304:                     '<td></td>'.
 1305:                     '<td></td>'.
 1306:                     '<td>'.$foilindex.'</td>'.
 1307:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
 1308:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1309:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
 1310:                     "</tr>\n";
 1311:             } else {
 1312:                 $table .= '<tr>'.
 1313:                     '<td>'.$foilindex.'</td>'.
 1314:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
 1315:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1316:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
 1317:                     "</tr>\n";
 1318:             }                
 1319:         } continue {
 1320:             $foilindex++;
 1321:         }
 1322:     } continue {
 1323:         $conceptindex++;
 1324:     }
 1325:     $table .= "</table>\n";
 1326:     #
 1327:     # Build option index with color stuff
 1328:     return ($table,\@Foils,\@Concepts);
 1329: }
 1330: 
 1331: sub build_option_index {
 1332:     my ($ORdata)= @_;
 1333:     my $table = "<table>\n";
 1334:     my $optionindex = 0;
 1335:     my @Rows;
 1336:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'_Options'}}) {
 1337:         push (@Rows,
 1338:               '<tr>'.
 1339:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
 1340:               ('&nbsp;'x4).'</td>'.
 1341:               '<td>'.&HTML::Entities::encode($option,'<>&"').'</td>'.
 1342:               "</tr>\n");
 1343:     }
 1344:     shift(@Rows); # Throw away 'correct option chosen' color
 1345:     $table .= join('',reverse(@Rows));
 1346:     $table .= "</table>\n";
 1347: }
 1348: 
 1349: #########################################################
 1350: #########################################################
 1351: ##
 1352: ##   Generic Interface Routines
 1353: ##
 1354: #########################################################
 1355: #########################################################
 1356: sub CreateInterface {
 1357:     ##
 1358:     ## Environment variable initialization
 1359:     if (! exists$ENV{'form.AnalyzeOver'}) {
 1360:         $ENV{'form.AnalyzeOver'} = 'Tries';
 1361:     }
 1362:     ##
 1363:     ## Build the menu
 1364:     my $Str = '';
 1365:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
 1366:         (undef,'Detailed Problem Analysis');
 1367:     $Str .= '<table cellspacing="5">'."\n";
 1368:     $Str .= '<tr>';
 1369:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
 1370:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
 1371: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
 1372:     $Str .= '<td align="center">&nbsp;</td>';
 1373:     $Str .= '</tr>'."\n";
 1374:     ##
 1375:     ## 
 1376:     $Str .= '<tr><td align="center">'."\n";
 1377:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1378:     $Str .= '</td>';
 1379:     #
 1380:     $Str .= '<td align="center">';
 1381:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1382:     $Str .= '</td>';
 1383:     #
 1384: #    $Str .= '<td align="center">';
 1385:     my $only_seq_with_assessments = sub { 
 1386:         my $s=shift;
 1387:         if ($s->{'num_assess'} < 1) { 
 1388:             return 0;
 1389:         } else { 
 1390:             return 1;
 1391:         }
 1392:     };
 1393:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
 1394:                                               $only_seq_with_assessments);
 1395:     ##
 1396:     ##
 1397:     $Str .= '<td>';
 1398:     ##
 1399:     my $showprob_checkbox = 
 1400:         '<input type="checkbox" name="show_prob" value="true" ';
 1401:     if ($ENV{'form.show_prob'} eq 'true') {
 1402:         $showprob_checkbox .= 'checked ';
 1403:     }
 1404:     $showprob_checkbox.= ' />';
 1405:     $Str.= '<nobr><label>'.
 1406:         &mt('Show problem [_1]',$showprob_checkbox).
 1407:         '</label></nobr><br />';
 1408:     ##
 1409:     my $analyze_selector = '<select name="AnalyzeOver" >';
 1410:     $analyze_selector .= '<option value="Tries" ';
 1411:     if (! exists($ENV{'form.AnalyzeOver'}) || 
 1412:         $ENV{'form.AnalyzeOver'} eq 'Tries'){
 1413:         # Default to Tries
 1414:         $analyze_selector .= ' selected ';
 1415:     }
 1416:     $analyze_selector .= '>'.&mt('Tries').'</option>';
 1417:     $analyze_selector .= '<option value="Time" ';
 1418:     $analyze_selector .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'Time');
 1419:     $analyze_selector .= '>'.&mt('Time').'</option>';
 1420:     $analyze_selector .= '</select>';
 1421:     $Str .= '<nobr><label>'.
 1422:         &mt('Analyze Over [_1] [_2]',
 1423:             $analyze_selector,
 1424:             &Apache::loncommon::help_open_topic('Analysis_Analyze_Over')).
 1425:             '</label></nobr><br />'.$/;
 1426:     ##
 1427:     my $numplots_selector = '<select name="NumPlots">';
 1428:     if (! exists($ENV{'form.NumPlots'}) 
 1429:         || $ENV{'form.NumPlots'} < 1 
 1430:         || $ENV{'form.NumPlots'} > 20) {
 1431:         $ENV{'form.NumPlots'} = 5;
 1432:     }
 1433:     foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
 1434:         $numplots_selector .= '<option value="'.$i.'" ';
 1435:         if ($ENV{'form.NumPlots'} == $i) { $numplots_selector.=' selected '; }
 1436:         $numplots_selector .= '>'.$i.'</option>';
 1437:     }
 1438:     $numplots_selector .= '</select></nobr><br />';
 1439:     $Str .= '<nobr><label>'.&mt('Number of Plots [_1]',$numplots_selector).
 1440:         '</label></nobr>';
 1441:     ##
 1442:     $Str .= '<nobr><label>'.&mt('Status: [_1]',
 1443:                                  '<input type="text" '.
 1444:                                  'name="stats_status" size="60" value="" />'
 1445:                                  ).
 1446:                     '</label></nobr>';
 1447:     $Str .= '</td>';
 1448:     ##
 1449:     ##
 1450:     $Str .= '</tr>'."\n";
 1451:     $Str .= '</table>'."\n";
 1452:     return $Str;
 1453: }
 1454: 
 1455: #########################################################
 1456: #########################################################
 1457: ##
 1458: ##              Misc Option Response functions
 1459: ##
 1460: #########################################################
 1461: #########################################################
 1462: sub get_time_from_row {
 1463:     my ($row) = @_;
 1464:     if (ref($row)) {
 1465:         return $row->[&Apache::loncoursedata::RD_timestamp()];
 1466:     } 
 1467:     return undef;
 1468: }
 1469: 
 1470: sub get_tries_from_row {
 1471:     my ($row) = @_;
 1472:     if (ref($row)) {
 1473:         return $row->[&Apache::loncoursedata::RD_tries()];
 1474:     }
 1475:     return undef;
 1476: }
 1477: 
 1478: sub hashify_attempt {
 1479:     my ($row) = @_;
 1480:     my %attempt;
 1481:     $attempt{'tries'}      = $row->[&Apache::loncoursedata::RD_tries()];
 1482:     $attempt{'submission'} = $row->[&Apache::loncoursedata::RD_submission()];
 1483:     $attempt{'award'}      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1484:     $attempt{'timestamp'}  = $row->[&Apache::loncoursedata::RD_timestamp()];
 1485:     return %attempt;
 1486: }
 1487: 
 1488: sub Process_OR_Row {
 1489:     my ($row) = @_;
 1490:     my %RowData;
 1491:     my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 1492:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1493:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
 1494:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
 1495:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
 1496:     my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
 1497:     return undef if ($award eq 'MISSING_ANSWER');
 1498:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
 1499:         $RowData{'_correct'} = 1;
 1500:     }
 1501:     $RowData{'_total'} = 1;
 1502:     my @Foilgrades = split('&',$grading);
 1503:     my @Foilsubs   = split('&',$submission);
 1504:     for (my $j=0;$j<=$#Foilgrades;$j++) {
 1505:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1506:         $foilid = &Apache::lonnet::unescape($foilid);
 1507:         my (undef,$submission) = split('=',$Foilsubs[$j]);
 1508:         if ($correct) {
 1509:             $RowData{$foilid}->{'_correct'}++;
 1510:         } else {
 1511:             $submission = &Apache::lonnet::unescape($submission);
 1512:             $RowData{$foilid}->{$submission}++;
 1513:         }
 1514:         $RowData{$foilid}->{'_total'}++;
 1515:     }
 1516:     return %RowData;
 1517: }
 1518: 
 1519: 1;
 1520: 
 1521: __END__

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