File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.93: download - view: text, annotated - select for diffs
Tue Oct 5 14:03:45 2004 UTC (19 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fix bug wherein option response tries analysis always showed the same
concept graph.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemanalysis.pm,v 1.93 2004/10/05 14:03:45 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]->[$try]->{'_correct'} +=
  623:                     $response_data{$foilid}->[$try]->{'_correct'};
  624:                 $concept_data[$j]->[$try]->{'_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]->[$try]->{'_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]->[$try]->{'_correct'} / 
  691:                                 $total);
  692:                 }
  693:             }
  694:             #
  695:             my $title;
  696:             my $count = $response_data{'_total'}->[$try];
  697:             $title = 'Attempt '.$try.' (N='.$count.')';
  698:             $concept_graph = &Apache::loncommon::DrawBarGraph
  699:                 ($title,'Concept Number','Percent Correct',
  700:                  100,$plotcolors,undef,\@concept_plot_data);
  701:         }
  702:         #
  703:         # Create Foil Plots
  704:         my $count = $response_data{'_total'}->[$try];
  705:         my $title = 'Attempt '.$try.' (N='.$count.')';
  706:         my @Datasets;
  707:         foreach my $option ('_correct',@{$ORdata->{'_Options'}}) {
  708:             next if (! exists($foil_plot[$try]->{$option}));
  709:             push(@Datasets,$foil_plot[$try]->{$option});
  710:         }
  711:         #
  712:         # Put a blank in the data set between concepts
  713:         for (my $set =0;$set<=$#Datasets;$set++) {
  714:             my @Data = @{$Datasets[$set]};
  715:             my $idx = 0;
  716:             foreach my $concept (@{$Concepts}) {
  717:                 foreach my $foilid (@{$concept->{'foils'}}) {
  718:                     $Datasets[$set]->[$idx++]=shift(@Data);
  719:                 }
  720:                 if ($concept->{'name'} ne $Concepts->[-1]->{'name'}) {
  721:                     $Datasets[$set]->[$idx++] = 0;
  722:                 }
  723:             }
  724:         }
  725:         #
  726:         # Set up the labels needed for the bar graph
  727:         my @Labels;
  728:         my $idx = 1;
  729:         foreach my $concept (@{$Concepts}) {
  730:             foreach my $foilid (@{$concept->{'foils'}}) {
  731:                 push(@Labels,$idx++);
  732:             }
  733:             push(@Labels,'');
  734:         }
  735:         #
  736:         my $correct_graph = &Apache::loncommon::DrawBarGraph
  737:             ($title,'Foil Number','Percent Correct',
  738:              100,$plotcolors,\@Labels,$Datasets[0]);
  739:         
  740:         #
  741:         #
  742:         next if (! defined($Datasets[0]));
  743:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
  744:             $Datasets[0]->[$i]=0;
  745:         }
  746:         $count = $response_data{'_total'}->[$try] - 
  747:                                            $response_data{'_correct'}->[$try];
  748:         $title = 'Attempt '.$try.' (N='.$count.')';
  749:         my $incorrect_graph = &Apache::loncommon::DrawBarGraph
  750:             ($title,'Foil Number','% Option Chosen Incorrectly',
  751:              100,$plotcolors,\@Labels,@Datasets);
  752:         $analysis_html.= 
  753:             '<tr>'.
  754:             '<td>'.$concept_graph.'</td>'.
  755:             '<td>'.$correct_graph.'</td>'.
  756:             '<td>'.$incorrect_graph.'</td>'.
  757:             '<td>'.$optionkey.'<td>'.
  758:             '</tr>'.$/;
  759:     }
  760:     $analysis_html .= "</table>\n";
  761:     $table .= $analysis_html;
  762:     return $table;
  763: }
  764: 
  765: sub OR_analyze_by_tries {
  766:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
  767:     my %Trydata;
  768:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
  769:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
  770:     foreach my $row (@$PerformanceData) {
  771:         next if (! defined($row));
  772:         my $tries = &get_tries_from_row($row);
  773:         my %Row   = &Process_OR_Row($row);
  774:         next if (! %Row);
  775:         while (my ($foilid,$href) = each(%Row)) {
  776:             if (! ref($href)) { 
  777:                 $Trydata{$foilid}->[$tries] += $href;
  778:                 next;
  779:             }
  780:             while (my ($option,$value) = each(%$href)) {
  781:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
  782:             }
  783:         }
  784:     }
  785:     return %Trydata;
  786: }
  787: 
  788: #########################################################
  789: #
  790: #     Option Response: Time Analysis
  791: #
  792: #########################################################
  793: sub OR_time_analysis {
  794:     my ($performance_data,$ORdata) = @_;
  795:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  796:     my $foilkey = &build_option_index($ORdata);
  797:     my $num_concepts = 1;
  798:     if (defined($Concepts)) { $num_concepts = scalar(@$Concepts); }
  799:     #
  800:     if ($num_concepts < 2) {
  801:         $table = '<h3>'.
  802:             &mt('Not enough data for concept analysis.  '.
  803:                 'Performing Foil Analysis').
  804:                 '</h3>'.$table;
  805:     }
  806:     #
  807:     my $num_plots = $ENV{'form.NumPlots'};
  808:     my $num_data = scalar(@$performance_data)-1;
  809:     my $percent = sprintf('%2f',100/$num_plots);
  810:     #
  811:     $table .= "<table>\n";
  812:     for (my $i=0;$i<$num_plots;$i++) {
  813:         ##
  814:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  815:             ('startdate_'.$i);
  816:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  817:             ('enddate_'.$i);
  818:         if (! defined($starttime) || ! defined($endtime)) {
  819:             my $sec_in_day = 86400;
  820:             my $last_sub_time = &get_time_from_row($performance_data->[-1]);
  821:             my ($sday,$smon,$syear);
  822:             (undef,undef,undef,$sday,$smon,$syear) = 
  823:                 localtime($last_sub_time - $sec_in_day*$i);
  824:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
  825:             $endtime = $starttime + $sec_in_day;
  826:             if ($i == ($num_plots -1 )) {
  827:                 $starttime = &get_time_from_row($performance_data->[0]);
  828:             }
  829:         }
  830:         $table .= '<tr><td colspan="4" align="center">'.
  831:             &mt('Data from [_1] to [_2]',
  832:                 &Apache::lonlocal::locallocaltime($starttime),
  833:                 &Apache::lonlocal::locallocaltime($endtime)).'</td></tr>'.$/;
  834:         my $startdateform = &Apache::lonhtmlcommon::date_setter
  835:             ('Statistics','startdate_'.$i,$starttime);
  836:         my $enddateform = &Apache::lonhtmlcommon::date_setter
  837:             ('Statistics','enddate_'.$i,$endtime);
  838:         #
  839:         my $begin_index;
  840:         my $end_index;
  841:         my $j;
  842:         while (++$j < scalar(@$performance_data)) {
  843:             last if (&get_time_from_row($performance_data->[$j]) 
  844:                                                               > $starttime);
  845:         }
  846:         $begin_index = $j;
  847:         while ($j < scalar(@$performance_data)) {
  848:             if (&get_time_from_row($performance_data->[$j]) > $endtime) {
  849:                 last;
  850:             } else {
  851:                 $j++;
  852:             }
  853:         }
  854:         $end_index = $j;
  855:         ##
  856:         my $interval = {
  857:             begin_index => $begin_index,
  858:             end_index   => $end_index,
  859:             startdateform => $startdateform,
  860:             enddateform   => $enddateform,
  861:         };
  862:         my $concept_correct_plot = '';
  863:         if ($num_concepts > 1) {
  864:             $concept_correct_plot = 
  865:                 &OR_Concept_Time_Analysis($performance_data,
  866:                                           $ORdata,$Concepts,
  867:                                           $interval);
  868:         }
  869:         my ($foil_correct_plot,$foil_incorrect_plot) = 
  870:             &OR_Foil_Time_Analysis($performance_data,$ORdata,
  871:                                    $Foils,$Concepts,$interval);
  872:         $table .= '<tr>'.
  873:             '<td>'.$concept_correct_plot.'</td>'.
  874:             '<td>'.$foil_correct_plot.'</td>'.
  875:             '<td>'.$foil_incorrect_plot.'</td>'.
  876:             '<td align="left" valign="top">'.$foilkey.'</td></tr>'.$/;
  877:         $table .= '<tr><td colspan="4" align="center">'.
  878:             &mt('Start time: [_1]',
  879:                 $interval->{'startdateform'}).'<br />'.
  880:             &mt('End time: [_1]',
  881:                 $interval->{'enddateform'}).
  882:                 '</td></tr>'.$/;
  883:         $table.= '<tr><td colspan="4">&nbsp</td></tr>'.$/;
  884:     }
  885:     $table .= '</table>';
  886:     #
  887:     return $table;
  888: }
  889: 
  890: sub OR_Foil_Time_Analysis {
  891:     my ($performance_data,$ORdata,$Foils,$Concepts,$interval) = @_;
  892:     my $analysis_html;
  893:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  894:                                     $interval->{'end_index'});
  895:     my %TimeData;
  896:     #
  897:     # Compute the number getting the foils correct or incorrects
  898:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  899:         my $row = $performance_data->[$j];
  900:         next if (! defined($row));
  901:         my %Row = &Process_OR_Row($row);
  902:         while (my ($foilid,$href) = each(%Row)) {
  903:             if (! ref($href)) {
  904:                 $TimeData{$foilid} += $href;
  905:                 next;
  906:             }
  907:             while (my ($option,$value) = each(%$href)) {
  908:                 $TimeData{$foilid}->{$option}+=$value;
  909:             }
  910:         }
  911:     }
  912:     my @plotdata;
  913:     my @labels;
  914:     foreach my $concept (@{$Concepts}) {
  915:         foreach my $foil (@{$concept->{'foils'}}) {
  916:             push(@labels,scalar(@labels)+1);
  917:             my $total = $TimeData{$foil}->{'_total'};
  918:             if ($total == 0) {
  919:                 push(@{$plotdata[0]},0);
  920:             } else {
  921:                 push(@{$plotdata[0]},
  922:                      100 * $TimeData{$foil}->{'_correct'} / $total);
  923:             }
  924:             my $total_incorrect = $total - $TimeData{$foil}->{'_correct'};
  925:             for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
  926:                 my $option = $ORdata->{'_Options'}->[$i];
  927:                 if ($total_incorrect == 0) {
  928:                     push(@{$plotdata[$i+1]},0);
  929:                 } else {
  930:                     push(@{$plotdata[$i+1]},
  931:                          100 * $TimeData{$foil}->{$option} / $total_incorrect);
  932:                 }
  933:             }
  934:         }
  935:         # Put in a blank one
  936:         push(@labels,'');
  937:         push(@{$plotdata[0]},0);
  938:         for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
  939:             push(@{$plotdata[$i+1]},0);
  940:         }
  941:     }
  942:     #
  943:     # Create the plot
  944:     my $count = $end_index-$begin_index;
  945:     return ('No data','') if ($count <= 0);
  946:     my $title;
  947:     if ($count == 0) {
  948:         $title = 'no submissions';
  949:     } elsif ($count == 1) {
  950:         $title = 'one submission';
  951:     } elsif ($count > 1) {
  952:         $title = $count.' submissions';
  953:     }
  954:     my $correct_plot = &Apache::loncommon::DrawBarGraph($title,
  955:                                                        'Foil Number',
  956:                                                        'Percent Correct',
  957:                                                        100,
  958:                                                        $plotcolors,
  959:                                                        undef,
  960:                                                        $plotdata[0]);
  961:     for (my $j=0; $j< scalar(@{$plotdata[0]});$j++) {
  962:         $plotdata[0]->[$j]=0;
  963:     }
  964:     $count = $end_index-$begin_index-$TimeData{'_correct'};
  965:     if ($count == 0) {
  966:         $title = 'no submissions';
  967:     } elsif ($count < 0) {
  968:         return ($correct_plot,'');
  969:     } elsif ($count == 1) {
  970:         $title = 'one submission';
  971:     } else {
  972:         $title = $count.' submissions';
  973:     }
  974:     my $incorrect_plot = &Apache::loncommon::DrawBarGraph($title,
  975:                                                  'Foil Number',
  976:                                                  'Incorrect Option Choice',
  977:                                                  100,
  978:                                                  $plotcolors,
  979:                                                  undef,
  980:                                                  @plotdata);
  981:     return ($correct_plot,$incorrect_plot);
  982: }
  983: 
  984: sub OR_Concept_Time_Analysis {
  985:     my ($performance_data,$ORdata,$Concepts,$interval) = @_;
  986:     ##
  987:     ## Determine starttime, endtime, startindex, endindex
  988:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  989:                                     $interval->{'end_index'});
  990:     my %TimeData;
  991:     #
  992:     # Compute the number getting the foils correct or incorrects
  993:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  994:         my $row = $performance_data->[$j];
  995:         next if (! defined($row));
  996:         my %Row = &Process_OR_Row($row);
  997:         while (my ($foilid,$href) = each(%Row)) {
  998:             if (! ref($href)) {
  999:                 $TimeData{$foilid} += $href;
 1000:                 next;
 1001:             }
 1002:             while (my ($option,$value) = each(%$href)) {
 1003:                 $TimeData{$foilid}->{$option}+=$value;
 1004:             }
 1005:         }
 1006:     }
 1007:     #
 1008:     # Put the data in plottable form
 1009:     my @plotdata;
 1010:     foreach my $concept (@$Concepts) {
 1011:         my ($total,$correct);
 1012:         foreach my $foil (@{$concept->{'foils'}}) {
 1013:             $total += $TimeData{$foil}->{'_total'};
 1014:             $correct += $TimeData{$foil}->{'_correct'};
 1015:         }
 1016:         if ($total == 0) {
 1017:             push(@plotdata,0);
 1018:         } else {
 1019:             push(@plotdata,100 * $correct / $total);
 1020:         }
 1021:     }
 1022:     #
 1023:     # Create the plot
 1024:     my $title = ($end_index - $begin_index).' submissions';
 1025:     return &Apache::loncommon::DrawBarGraph($title,
 1026:                                             'Concept Number',
 1027:                                             'Percent Correct',
 1028:                                             100,
 1029:                                             $plotcolors,
 1030:                                             undef,
 1031:                                             \@plotdata);
 1032: }
 1033: 
 1034: #########################################################
 1035: #########################################################
 1036: ##
 1037: ##             Excel output 
 1038: ##
 1039: #########################################################
 1040: #########################################################
 1041: sub OR_excel_sheet {
 1042:     my ($r,$resource,$performance_data,$ORdata) = @_;
 1043:     my $response = '';
 1044:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
 1045:     #
 1046:     # Create excel worksheet
 1047:     my $filename = '/prtspool/'.
 1048:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1049:         time.'_'.rand(1000000000).'.xls';
 1050:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1051:     if (! defined($workbook)) {
 1052:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1053:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
 1054:                             "This error has been logged.  ".
 1055:                             "Please alert your LON-CAPA administrator").
 1056:                   '</p>');
 1057:         return undef;
 1058:     }
 1059:     #
 1060:     $workbook->set_tempdir('/home/httpd/perl/tmp');
 1061:     my $format = &Apache::loncommon::define_excel_formats($workbook);
 1062:     #
 1063:     # Create and populate main worksheets
 1064:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
 1065:     my $student_data_sheet = &build_student_data_worksheet($workbook,$format);
 1066:     my $response_data_sheet = $workbook->addworksheet('Response Data');
 1067:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
 1068:                        $response_data_sheet) {
 1069:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
 1070:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
 1071:     }
 1072:     #
 1073:     my $result;
 1074:     $result = &OR_build_problem_data_worksheet($problem_data_sheet,$format,
 1075:                                             $Concepts,$ORdata);
 1076:     if ($result ne 'okay') {
 1077:         # Do something useful
 1078:     }
 1079:     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
 1080:                                              $performance_data,$Foils,
 1081:                                              $ORdata);
 1082:     if ($result ne 'okay') {
 1083:         # Do something useful
 1084:     }
 1085:     $response_data_sheet->activate();
 1086:     #
 1087:     # Close the excel file
 1088:     $workbook->close();
 1089:     #
 1090:     # Write a link to allow them to download it
 1091:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
 1092:               '<p><a href="'.$filename.'">'.
 1093:               &mt('Your Excel spreadsheet.').
 1094:               '</a></p>'."\n";
 1095:     return $result;
 1096: }
 1097: 
 1098: sub OR_build_problem_data_worksheet {
 1099:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
 1100:     my $rows_output = 3;
 1101:     my $cols_output = 0;
 1102:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
 1103:     ##
 1104:     ##
 1105:     my @Headers;
 1106:     if (@$Concepts > 1) {
 1107:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
 1108:                     'Foil Name','Foil Text','Correct value');
 1109:     } else {
 1110:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
 1111:     }
 1112:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1113:     my %Foildata = %{$ORdata->{'_Foils'}};
 1114:     my $conceptindex = 1;
 1115:     my $foilindex = 1;
 1116:     foreach my $concept (@$Concepts) {
 1117:         my @FoilsInConcept = @{$concept->{'foils'}};
 1118:         my $firstfoil = shift(@FoilsInConcept);
 1119:         if (@$Concepts > 1) {
 1120:             $worksheet->write_row($rows_output++,0,
 1121:                                   [$conceptindex,
 1122:                                    $concept->{'name'},
 1123:                                    $foilindex++,
 1124:                                    $Foildata{$firstfoil}->{'name'},
 1125:                                    $Foildata{$firstfoil}->{'text'},
 1126:                                    $Foildata{$firstfoil}->{'value'},]);
 1127:         } else {
 1128:             $worksheet->write_row($rows_output++,0,
 1129:                                   [ $foilindex++,
 1130:                                     $Foildata{$firstfoil}->{'name'},
 1131:                                     $Foildata{$firstfoil}->{'text'},
 1132:                                     $Foildata{$firstfoil}->{'value'},]);
 1133:         }
 1134:         foreach my $foilid (@FoilsInConcept) {
 1135:             if (@$Concepts > 1) {
 1136:                 $worksheet->write_row($rows_output++,0,
 1137:                                       ['',
 1138:                                        '',
 1139:                                        $foilindex,
 1140:                                        $Foildata{$foilid}->{'name'},
 1141:                                        $Foildata{$foilid}->{'text'},
 1142:                                        $Foildata{$foilid}->{'value'},]);
 1143:             } else {
 1144:                 $worksheet->write_row($rows_output++,0,                
 1145:                                       [$foilindex,
 1146:                                        $Foildata{$foilid}->{'name'},
 1147:                                        $Foildata{$foilid}->{'text'},
 1148:                                        $Foildata{$foilid}->{'value'},]);
 1149:             }                
 1150:         } continue {
 1151:             $foilindex++;
 1152:         }
 1153:     } continue {
 1154:         $conceptindex++;
 1155:     }
 1156:     $rows_output++;
 1157:     $rows_output++;
 1158:     ##
 1159:     ## Option data output
 1160:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
 1161:     foreach my $string (@{$ORdata->{'_Options'}}) {
 1162:         $worksheet->write($rows_output++,0,$string);
 1163:     }
 1164:     return 'okay';
 1165: }
 1166: 
 1167: sub OR_build_response_data_worksheet {
 1168:     my ($worksheet,$format,$performance_data,$Foils,$ORdata)=@_;
 1169:     my $rows_output = 3;
 1170:     my $cols_output = 0;
 1171:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
 1172:     $worksheet->set_column(1,1,20);
 1173:     $worksheet->set_column(2,2,13);
 1174:     my @Headers = ('identifier','time','award detail','attempt');
 1175:     foreach my $foil (@$Foils) {
 1176:         push (@Headers,$foil.' submission');
 1177:         push (@Headers,$foil.' grading');
 1178:     }
 1179:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
 1180:     #
 1181:     foreach my $row (@$performance_data) {
 1182:         next if (! defined($row));
 1183:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
 1184:         my @Foilgrades = split('&',$grading);
 1185:         my @Foilsubs   = split('&',$submission);
 1186:         my %response_data;
 1187:         for (my $j=0;$j<=$#Foilgrades;$j++) {
 1188:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1189:             my (undef,$submission) = split('=',$Foilsubs[$j]);
 1190:             $submission = &Apache::lonnet::unescape($submission);
 1191:             $response_data{$foilid.' submission'}=$submission;
 1192:             $response_data{$foilid.' award'}=$correct;
 1193:         }
 1194:         $worksheet->write($rows_output,$cols_output++,$student);
 1195:         $worksheet->write($rows_output,$cols_output++,
 1196:              &Apache::lonstathelpers::calc_serial($time),$format->{'date'});
 1197:         $worksheet->write($rows_output,$cols_output++,$award);
 1198:         $worksheet->write($rows_output,$cols_output++,$tries);
 1199:         foreach my $foilid (@$Foils) {
 1200:             $worksheet->write($rows_output,$cols_output++,
 1201:                               $response_data{$foilid.' submission'});
 1202:             $worksheet->write($rows_output,$cols_output++,
 1203:                               $response_data{$foilid.' award'});
 1204:         }
 1205:         $rows_output++;
 1206:         $cols_output = 0;
 1207:     }
 1208:     return;
 1209: }
 1210: 
 1211: sub build_foil_index {
 1212:     my ($ORdata) = @_;
 1213:     return if (! exists($ORdata->{'_Foils'}));
 1214:     my %Foildata = %{$ORdata->{'_Foils'}};
 1215:     my @Foils = sort(keys(%Foildata));
 1216:     my %Concepts;
 1217:     foreach my $foilid (@Foils) {
 1218:         push(@{$Concepts{$Foildata{$foilid}->{'_Concept'}}},
 1219:              $foilid);
 1220:     }
 1221:     undef(@Foils);
 1222:     # Having gathered the concept information in a hash, we now translate it
 1223:     # into an array because we need to be consistent about order.
 1224:     # Also put the foils in order, too.
 1225:     my $sortfunction = sub {
 1226:         my %Numbers = (one   => 1,
 1227:                        two   => 2,
 1228:                        three => 3,
 1229:                        four  => 4,
 1230:                        five  => 5,
 1231:                        six   => 6,
 1232:                        seven => 7,
 1233:                        eight => 8,
 1234:                        nine  => 9,
 1235:                        ten   => 10,);
 1236:         my $a1 = lc($a); 
 1237:         my $b1 = lc($b);
 1238:         if (exists($Numbers{$a1})) {
 1239:             $a1 = $Numbers{$a1};
 1240:         }
 1241:         if (exists($Numbers{$b1})) {
 1242:             $b1 = $Numbers{$b1};
 1243:         }
 1244:         if (($a1 =~/^\d+$/) && ($b1 =~/^\d+$/)) {
 1245:             return $a1 <=> $b1;
 1246:         } else {
 1247:             return $a1 cmp $b1;
 1248:         }
 1249:     };
 1250:     my @Concepts;
 1251:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
 1252:         if (! defined($Concepts{$concept})) {
 1253:             $Concepts{$concept}=[];
 1254: #            next;
 1255:         }
 1256:         push(@Concepts,{ name => $concept,
 1257:                         foils => [@{$Concepts{$concept}}]});
 1258:         push(@Foils,(@{$Concepts{$concept}}));
 1259:     }
 1260:     #
 1261:     # Build up the table of row labels.
 1262:     my $table = '<table border="1" >'."\n";
 1263:     if (@Concepts > 1) {
 1264:         $table .= '<tr>'.
 1265:             '<th>'.&mt('Concept Number').'</th>'.
 1266:             '<th>'.&mt('Concept').'</th>'.
 1267:             '<th>'.&mt('Foil Number').'</th>'.
 1268:             '<th>'.&mt('Foil Name').'</th>'.
 1269:             '<th>'.&mt('Foil Text').'</th>'.
 1270:             '<th>'.&mt('Correct Value').'</th>'.
 1271:             "</tr>\n";
 1272:     } else {
 1273:         $table .= '<tr>'.
 1274:             '<th>'.&mt('Foil Number').'</th>'.
 1275:             '<th>'.&mt('Foil Name').'</th>'.
 1276:             '<th>'.&mt('Foil Text').'</th>'.
 1277:             '<th>'.&mt('Correct Value').'</th>'.
 1278:             "</tr>\n";
 1279:     }        
 1280:     my $conceptindex = 1;
 1281:     my $foilindex = 1;
 1282:     foreach my $concept (@Concepts) {
 1283:         my @FoilsInConcept = @{$concept->{'foils'}};
 1284:         my $firstfoil = shift(@FoilsInConcept);
 1285:         if (@Concepts > 1) {
 1286:             $table .= '<tr>'.
 1287:                 '<td>'.$conceptindex.'</td>'.
 1288:                 '<td>'.&HTML::Entities::encode($concept->{'name'},'<>&"').'</td>'.
 1289:                 '<td>'.$foilindex++.'</td>'.
 1290:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
 1291:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1292:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
 1293:                 "</tr>\n";
 1294:         } else {
 1295:             $table .= '<tr>'.
 1296:                 '<td>'.$foilindex++.'</td>'.
 1297:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
 1298:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1299:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
 1300:                 "</tr>\n";
 1301:         }
 1302:         foreach my $foilid (@FoilsInConcept) {
 1303:             if (@Concepts > 1) {
 1304:                 $table .= '<tr>'.
 1305:                     '<td></td>'.
 1306:                     '<td></td>'.
 1307:                     '<td>'.$foilindex.'</td>'.
 1308:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
 1309:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1310:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
 1311:                     "</tr>\n";
 1312:             } else {
 1313:                 $table .= '<tr>'.
 1314:                     '<td>'.$foilindex.'</td>'.
 1315:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
 1316:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1317:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
 1318:                     "</tr>\n";
 1319:             }                
 1320:         } continue {
 1321:             $foilindex++;
 1322:         }
 1323:     } continue {
 1324:         $conceptindex++;
 1325:     }
 1326:     $table .= "</table>\n";
 1327:     #
 1328:     # Build option index with color stuff
 1329:     return ($table,\@Foils,\@Concepts);
 1330: }
 1331: 
 1332: sub build_option_index {
 1333:     my ($ORdata)= @_;
 1334:     my $table = "<table>\n";
 1335:     my $optionindex = 0;
 1336:     my @Rows;
 1337:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'_Options'}}) {
 1338:         push (@Rows,
 1339:               '<tr>'.
 1340:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
 1341:               ('&nbsp;'x4).'</td>'.
 1342:               '<td>'.&HTML::Entities::encode($option,'<>&"').'</td>'.
 1343:               "</tr>\n");
 1344:     }
 1345:     shift(@Rows); # Throw away 'correct option chosen' color
 1346:     $table .= join('',reverse(@Rows));
 1347:     $table .= "</table>\n";
 1348: }
 1349: 
 1350: #########################################################
 1351: #########################################################
 1352: ##
 1353: ##   Generic Interface Routines
 1354: ##
 1355: #########################################################
 1356: #########################################################
 1357: sub CreateInterface {
 1358:     ##
 1359:     ## Environment variable initialization
 1360:     if (! exists$ENV{'form.AnalyzeOver'}) {
 1361:         $ENV{'form.AnalyzeOver'} = 'Tries';
 1362:     }
 1363:     ##
 1364:     ## Build the menu
 1365:     my $Str = '';
 1366:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
 1367:         (undef,'Detailed Problem Analysis');
 1368:     $Str .= '<table cellspacing="5">'."\n";
 1369:     $Str .= '<tr>';
 1370:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
 1371:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
 1372: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
 1373:     $Str .= '<td align="center">&nbsp;</td>';
 1374:     $Str .= '</tr>'."\n";
 1375:     ##
 1376:     ## 
 1377:     $Str .= '<tr><td align="center">'."\n";
 1378:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1379:     $Str .= '</td>';
 1380:     #
 1381:     $Str .= '<td align="center">';
 1382:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1383:     $Str .= '</td>';
 1384:     #
 1385: #    $Str .= '<td align="center">';
 1386:     my $only_seq_with_assessments = sub { 
 1387:         my $s=shift;
 1388:         if ($s->{'num_assess'} < 1) { 
 1389:             return 0;
 1390:         } else { 
 1391:             return 1;
 1392:         }
 1393:     };
 1394:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
 1395:                                               $only_seq_with_assessments);
 1396:     ##
 1397:     ##
 1398:     $Str .= '<td>';
 1399:     ##
 1400:     my $showprob_checkbox = 
 1401:         '<input type="checkbox" name="show_prob" value="true" ';
 1402:     if ($ENV{'form.show_prob'} eq 'true') {
 1403:         $showprob_checkbox .= 'checked ';
 1404:     }
 1405:     $showprob_checkbox.= ' />';
 1406:     $Str.= '<nobr><label>'.
 1407:         &mt('Show problem [_1]',$showprob_checkbox).
 1408:         '</label></nobr><br />';
 1409:     ##
 1410:     my $analyze_selector = '<select name="AnalyzeOver" >';
 1411:     $analyze_selector .= '<option value="Tries" ';
 1412:     if (! exists($ENV{'form.AnalyzeOver'}) || 
 1413:         $ENV{'form.AnalyzeOver'} eq 'Tries'){
 1414:         # Default to Tries
 1415:         $analyze_selector .= ' selected ';
 1416:     }
 1417:     $analyze_selector .= '>'.&mt('Tries').'</option>';
 1418:     $analyze_selector .= '<option value="Time" ';
 1419:     $analyze_selector .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'Time');
 1420:     $analyze_selector .= '>'.&mt('Time').'</option>';
 1421:     $analyze_selector .= '</select>';
 1422:     $Str .= '<nobr><label>'.
 1423:         &mt('Analyze Over [_1] [_2]',
 1424:             $analyze_selector,
 1425:             &Apache::loncommon::help_open_topic('Analysis_Analyze_Over')).
 1426:             '</label></nobr><br />'.$/;
 1427:     ##
 1428:     my $numplots_selector = '<select name="NumPlots">';
 1429:     if (! exists($ENV{'form.NumPlots'}) 
 1430:         || $ENV{'form.NumPlots'} < 1 
 1431:         || $ENV{'form.NumPlots'} > 20) {
 1432:         $ENV{'form.NumPlots'} = 5;
 1433:     }
 1434:     foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
 1435:         $numplots_selector .= '<option value="'.$i.'" ';
 1436:         if ($ENV{'form.NumPlots'} == $i) { $numplots_selector.=' selected '; }
 1437:         $numplots_selector .= '>'.$i.'</option>';
 1438:     }
 1439:     $numplots_selector .= '</select></nobr><br />';
 1440:     $Str .= '<nobr><label>'.&mt('Number of Plots [_1]',$numplots_selector).
 1441:         '</label></nobr>';
 1442:     ##
 1443:     $Str .= '<nobr><label>'.&mt('Status: [_1]',
 1444:                                  '<input type="text" '.
 1445:                                  'name="stats_status" size="60" value="" />'
 1446:                                  ).
 1447:                     '</label></nobr>';
 1448:     $Str .= '</td>';
 1449:     ##
 1450:     ##
 1451:     $Str .= '</tr>'."\n";
 1452:     $Str .= '</table>'."\n";
 1453:     return $Str;
 1454: }
 1455: 
 1456: #########################################################
 1457: #########################################################
 1458: ##
 1459: ##              Misc Option Response functions
 1460: ##
 1461: #########################################################
 1462: #########################################################
 1463: sub get_time_from_row {
 1464:     my ($row) = @_;
 1465:     if (ref($row)) {
 1466:         return $row->[&Apache::loncoursedata::RD_timestamp()];
 1467:     } 
 1468:     return undef;
 1469: }
 1470: 
 1471: sub get_tries_from_row {
 1472:     my ($row) = @_;
 1473:     if (ref($row)) {
 1474:         return $row->[&Apache::loncoursedata::RD_tries()];
 1475:     }
 1476:     return undef;
 1477: }
 1478: 
 1479: sub hashify_attempt {
 1480:     my ($row) = @_;
 1481:     my %attempt;
 1482:     $attempt{'tries'}      = $row->[&Apache::loncoursedata::RD_tries()];
 1483:     $attempt{'submission'} = $row->[&Apache::loncoursedata::RD_submission()];
 1484:     $attempt{'award'}      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1485:     $attempt{'timestamp'}  = $row->[&Apache::loncoursedata::RD_timestamp()];
 1486:     return %attempt;
 1487: }
 1488: 
 1489: sub Process_OR_Row {
 1490:     my ($row) = @_;
 1491:     my %RowData;
 1492:     my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 1493:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1494:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
 1495:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
 1496:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
 1497:     my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
 1498:     return undef if ($award eq 'MISSING_ANSWER');
 1499:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
 1500:         $RowData{'_correct'} = 1;
 1501:     }
 1502:     $RowData{'_total'} = 1;
 1503:     my @Foilgrades = split('&',$grading);
 1504:     my @Foilsubs   = split('&',$submission);
 1505:     for (my $j=0;$j<=$#Foilgrades;$j++) {
 1506:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1507:         $foilid = &Apache::lonnet::unescape($foilid);
 1508:         my (undef,$submission) = split('=',$Foilsubs[$j]);
 1509:         if ($correct) {
 1510:             $RowData{$foilid}->{'_correct'}++;
 1511:         } else {
 1512:             $submission = &Apache::lonnet::unescape($submission);
 1513:             $RowData{$foilid}->{$submission}++;
 1514:         }
 1515:         $RowData{$foilid}->{'_total'}++;
 1516:     }
 1517:     return %RowData;
 1518: }
 1519: 
 1520: 1;
 1521: 
 1522: __END__

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