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

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

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