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

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.94    ! matthew     3: # $Id: lonproblemanalysis.pm,v 1.93 2004/10/05 14:03: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.56      matthew   151:                 &RadioResponseAnalysis($r,$current_problem,
1.88      matthew   152:                                        $problem_data,
1.47      matthew   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: #########################################################
                    378: sub RadioResponseAnalysis {
1.94    ! matthew   379:     my ($r,$problem,$problem_analysis,$students) = @_;
        !           380:     if ($ENV{'form.AnalyzeOver'} eq 'tries') {
        !           381:         &RR_tries_analysis($r,$problem,$problem_analysis,$students);
        !           382:     } elsif ($ENV{'form.AnalyzeOver'} eq 'time') {
        !           383:         &RR_static_time_analysis($r,$problem,$problem_analysis,$students);
        !           384:     } else {
        !           385:         $r->print('Bad request');
        !           386:     }
        !           387:     return;
        !           388: }
        !           389: 
        !           390: sub RR_computed_tries_analysis {
        !           391:     my ($r,$problem,$problem_analysis) = @_;
        !           392:     my ($resource,$partid,$respid) = ($problem->{'resource'},
        !           393:                                       $problem->{'part'},
        !           394:                                       $problem->{'respid'});
        !           395:     $r->print('The tries answer you seek must be computed');
        !           396:     # Gather student data
        !           397:     # for each try
        !           398:     #    loop through data, classifying it by
        !           399:     #         correct foil -> selected foil
        !           400:     #    if there is concept data
        !           401:     #         make a concept correct plot
        !           402:     #    for each correct foil
        !           403:     #         make a plot of the data
        !           404: }
        !           405: 
        !           406: sub RR_computed_time_analysis {
        !           407:     my ($r,$problem,$problem_analysis) = @_;
        !           408:     my ($resource,$partid,$respid) = ($problem->{'resource'},
        !           409:                                       $problem->{'part'},
        !           410:                                       $problem->{'respid'});
        !           411:     $r->print('The time answer you seek must be computed');
        !           412:     # Gather student data
        !           413:     # for time division
        !           414:     #    limit to between start time & end time
        !           415:     #    loop through data, classifying it by
        !           416:     #         correct foil -> selected foil
        !           417:     #    if there is concept data
        !           418:     #         make a concept correct plot
        !           419:     #    for each correct foil
        !           420:     #         make a plot of the data
        !           421: }
        !           422: 
        !           423: sub RR_tries_analysis {
        !           424:     my ($r,$problem,$problem_analysis,$students) = @_;
        !           425:     my ($resource,$partid,$respid) = ($problem->{'resource'},
        !           426:                                       $problem->{'part'},
        !           427:                                       $problem->{'respid'});
        !           428:     #
        !           429:     my $analysis_html;
        !           430:     my $foildata = $problem_analysis->{'_Foils'};
        !           431:     my ($table,$foils,$concepts) = &build_foil_index($problem_analysis);
        !           432:     $analysis_html .= $table;
        !           433:     # Gather student data
        !           434:     my $response_data = &Apache::loncoursedata::get_response_data
        !           435:         (\@Apache::lonstatistics::SelectedSections,
        !           436:          $Apache::lonstatistics::enrollment_status,
        !           437:          $resource->{'symb'},$respid);
        !           438:     my $correct;   # either a hash reference or a scalar
        !           439:     if ($problem_analysis->{'answercomputed'} || scalar(@$concepts) > 1) {
        !           440:         # This takes a while for large classes...
        !           441:         &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$students,
        !           442:                                                    'Statistics',
        !           443:                                                    'stats_status');
        !           444:         foreach my $student (@$students) {
        !           445:             my ($idx,@remainder) = split('&',$student->{'answer'});
        !           446:             my ($answer) = ($remainder[$idx]=~/^(.*)=([^=]*)$/);
        !           447:             $correct->{$student->{'username'}.':'.$student->{'domain'}}=
        !           448:                 &Apache::lonnet::unescape($answer);
        !           449:         }
        !           450:     } else {
        !           451:         foreach my $foil (keys(%$foildata)) {
        !           452:             if ($foildata->{$foil}->{'value'} eq 'true') {
        !           453:                 $correct = $foildata->{$foil}->{'name'};
        !           454:                 last;
        !           455:             }
        !           456:         }
        !           457:     }
        !           458:     if (! defined($response_data) || 
        !           459:         ref($response_data) ne 'ARRAY' ) {
        !           460:         $analysis_html = '<h2>'.
        !           461:             &mt('There is no submission data for this resource').
        !           462:             '</h2>';
        !           463:         $r->print($analysis_html);
        !           464:         return;
        !           465:     }
        !           466:     #
        !           467:     $analysis_html.='<table>';
        !           468:     for (my $try = 1;$try<=$ENV{'form.NumPlots'};$try++) {
        !           469:         # classify data ->correct foil -> selected foil
        !           470:         my $attempt_restriction_function = 
        !           471:         my $foil_choice_data = 
        !           472:             &RR_classify_response_data($response_data,$correct,
        !           473:                                        sub {($_[0]->{'tries'} == $try?1:0)});
        !           474:         my $answers;
        !           475:         if (ref($correct)) {
        !           476:             my %tmp;
        !           477:             foreach my $foil (values(%$correct)) {
        !           478:                 $tmp{$foil}++;
        !           479:             }
        !           480:             $answers = [keys(%tmp)];
        !           481:         } else {
        !           482:             $answers = [$correct];
        !           483:         }
        !           484:         # Concept Plot
        !           485:         my $concept_plot = '';
        !           486:         if (scalar(@$concepts) > 1) {
        !           487:             $concept_plot = &RR_concept_plot($concepts,$foil_choice_data,
        !           488:                                              '% choosing');
        !           489:         }
        !           490:         # % Choosing plot
        !           491:         my $choice_plot = &RR_create_percent_selected_plot
        !           492:             ($foils,$foil_choice_data,'Attempt '.$try);
        !           493:         # for each correct foil, how did they mark it? (stacked bar graph)
        !           494:         my ($stacked_plot,$count_by_foil) = 
        !           495:             &RR_create_stacked_selection_plot($foils,$foil_choice_data,'');
        !           496:         #
        !           497:         if ($concept_plot ne '' ||
        !           498:             $choice_plot ne '' ||
        !           499:             $stacked_plot ne '') {
        !           500:             $analysis_html.=
        !           501:                 '<tr>'.
        !           502:                 '<td>'.$concept_plot.'</td>'.
        !           503:                 '<td>'.$choice_plot.'</td>';
        !           504:             if ($stacked_plot ne '') {
        !           505:                 $analysis_html .= 
        !           506:                 '<td>'.$stacked_plot.'</td>'.
        !           507:                 '<td>'.&build_foil_key($foils,$count_by_foil).'</td>';
        !           508:             } else {
        !           509:                 $analysis_html .= ('<td></td>'x2);
        !           510:             }
        !           511:             $analysis_html.='</tr>'.$/;
        !           512:         }
        !           513:     }
        !           514:     $analysis_html.='</table>';
        !           515:     $r->print($analysis_html);
        !           516: }
        !           517: 
        !           518: 
        !           519: sub RR_concept_plot {
        !           520:     my ($concepts,$foil_data,$title) = @_;
        !           521:     #
        !           522:     my %correct_by_concept;
        !           523:     my %incorrect_by_concept;
        !           524:     my %true;
        !           525:     foreach my $concept (@$concepts) {
        !           526:         foreach my $foil (@{$concept->{'foils'}}) {
        !           527:             next if (! exists($foil_data->{$foil}));
        !           528:             foreach my $choice (keys(%{$foil_data->{$foil}})) {
        !           529:                 if ($choice eq $foil) {
        !           530:                     $correct_by_concept{$concept->{'name'}} +=
        !           531:                         $foil_data->{$foil}->{$choice};
        !           532:                 } else {
        !           533:                     $incorrect_by_concept{$concept->{'name'}} +=
        !           534:                         $foil_data->{$foil}->{$choice};
        !           535:                     
        !           536:                 }
        !           537:             }
        !           538:         }
        !           539:     }
        !           540:     # 
        !           541:     # need arrays for incorrect and correct because we want to use different
        !           542:     # colors for them
        !           543:     my @correct;
        !           544:     #
        !           545:     my $total =0;
        !           546:     for (my $i=0;$i<scalar(@$concepts);$i++) {
        !           547:         my $concept = $concepts->[$i];
        !           548:         $correct[$i]   =   $correct_by_concept{$concept->{'name'}};
        !           549:         $total += $correct_by_concept{$concept->{'name'}}+
        !           550:             $incorrect_by_concept{$concept->{'name'}};
        !           551:     }
        !           552:     if ($total == 0) { return ''; };
        !           553:     for (my $i=0;$i<=$#correct;$i++) { 
        !           554:         $correct[$i] = sprintf('%0f',$correct[$i]/$total*100);
        !           555:     }
        !           556:     my $xlabel = 'concept';
        !           557:     $title.= ' (N='.$total.')';
        !           558:     my $plot=  &Apache::loncommon::DrawBarGraph($title,
        !           559:                                                 $xlabel,
        !           560:                                                 'Percent Choosing',
        !           561:                                                 100,
        !           562:                                                 ['#33ff00','#ff3300'],
        !           563:                                                 undef,
        !           564:                                                 \@correct);
        !           565:     return $plot;
        !           566: }
        !           567: 
        !           568: 
        !           569: sub RR_create_percent_selected_plot {
        !           570:     my ($foils,$foil_data,$title) = @_;
        !           571:     #
        !           572:     my %foil_selections;
        !           573:     my %true;
        !           574:     foreach my $foil (@$foils) {
        !           575:         # foil_data has format $foil_data->{true_foil}->{selected foil}
        !           576:         next if (! exists($foil_data->{$foil}));
        !           577:         $true{$foil}++;
        !           578:         while (my ($f,$count)= each(%{$foil_data->{$foil}})) {
        !           579:             $foil_selections{$f}+=$count;
        !           580:         }
        !           581:     }
        !           582:     # 
        !           583:     # need arrays for incorrect and correct because we want to use different
        !           584:     # colors for them
        !           585:     my @correct;
        !           586:     my @incorrect;
        !           587:     #
        !           588:     my $total =0;
        !           589:     for (my $i=0;$i<scalar(@$foils);$i++) {
        !           590:         my $foil = $foils->[$i];
        !           591:         if ($true{$foil}) {
        !           592:             $correct[$i]   = $foil_selections{$foil};
        !           593:             $incorrect[$i] = 0;
        !           594:         } else {
        !           595:             $correct[$i]   = 0;
        !           596:             $incorrect[$i] = $foil_selections{$foil};
        !           597:         }
        !           598:         $total+=$foil_selections{$foil};
        !           599:     }
        !           600:     if ($total == 0) { return ''; };
        !           601:     for (my $i=0;$i<=$#correct;$i++) { 
        !           602:         $correct[$i] = sprintf('%0f',$correct[$i]/$total*100);
        !           603:     }
        !           604:     for (my $i=0;$i<=$#incorrect;$i++) {
        !           605:         $incorrect[$i] = sprintf('%0f',$incorrect[$i]/$total*100);
        !           606:     }
        !           607:     my $xlabel = 'foil chosen';
        !           608:     $title.= ' (N='.$total.')';
        !           609:     my $plot=  &Apache::loncommon::DrawBarGraph($title,
        !           610:                                                 $xlabel,
        !           611:                                                 'Percent Choosing',
        !           612:                                                 100,
        !           613:                                                 ['#33ff00','#ff3300'],
        !           614:                                                 undef,
        !           615:                                                 \@correct,
        !           616:                                                 \@incorrect);
        !           617:     return $plot;
        !           618: }
        !           619: 
        !           620: 
        !           621: sub RR_create_stacked_selection_plot {
        !           622:     my ($foils,$foil_data,$title)=@_;
        !           623:     #
        !           624:     my @correct_choice; # the green row
        !           625:     my @dataset; # array of array refs - multicolor rows.
        !           626:     my %filled;
        !           627:     my @labels;
        !           628:     my $count=-1;
        !           629:     my %column;
        !           630:     for (my $i=0;$i<scalar(@$foils);$i++) {
        !           631:         next if (! exists($foil_data->{$foils->[$i]}));
        !           632:         my $correct_foil = $foils->[$i];
        !           633:         push(@labels,$i+1);
        !           634:         $column{$correct_foil}= ++$count;
        !           635:         for (my $j=0;$j<scalar(@$foils);$j++) {
        !           636:             my $value = 0;
        !           637:             if ($i != $j ) {
        !           638:                 $value += $foil_data->{$correct_foil}->{$foils->[$j]};
        !           639:             }
        !           640:             $dataset[$j]->[$column{$correct_foil}]=$value;
        !           641:         }
        !           642:     }
        !           643:     #
        !           644:     return '' if (! scalar(keys(%column)));
        !           645:     #
        !           646:     my $grand_total = 0;
        !           647:     my %count_per_foil;
        !           648:     while (my ($foil,$bar) = each (%column)) {
        !           649:         my $bar_total = 0;
        !           650:         for (my $j=0;$j<scalar(@dataset);$j++) {
        !           651:             $bar_total += $dataset[$j]->[$bar];
        !           652:         }
        !           653:         next if ($bar_total == 0);
        !           654:         for (my $j=0;$j<scalar(@dataset);$j++) {
        !           655:             $dataset[$j]->[$bar] = 
        !           656:                 sprintf('%2f',$dataset[$j]->[$bar]/$bar_total * 100);
        !           657:         }
        !           658:         $count_per_foil{$foil}=' (N='.$bar_total.')';
        !           659:         $grand_total += $bar_total;
        !           660:     }
        !           661:     if ($grand_total == 0) {
        !           662:         return ('',undef);
        !           663:     }
        !           664:     my @empty_row = ();
        !           665:     foreach (@{$dataset[0]}) {
        !           666:         push(@empty_row,0);
        !           667:     }
        !           668:     #
        !           669:     $title .= ' (N='.$grand_total.')';
        !           670:     my $graph = &Apache::loncommon::DrawBarGraph
        !           671:         ($title,'Correct Foil','foils chosen Incorrectly',
        !           672:          100,$plotcolors,\@labels,\@empty_row,@dataset);
        !           673:     return ($graph,\%count_per_foil);
        !           674: }
        !           675: 
        !           676: 
        !           677: # if $correct is a hash ref, it is assumed to be indexed by student names.
        !           678: #    the values are assumed to be hash refs with a key of 'answer'.
        !           679: sub RR_classify_response_data {
        !           680:     my ($full_row_data,$correct,$function) = @_;
        !           681:     my %submission_data;
        !           682:     foreach my $row (@$full_row_data) {
        !           683:         my %subm = &hashify_attempt($row);
        !           684:         if (ref($correct) eq 'HASH') {
        !           685:             $subm{'correct'} = $correct->{$subm{'student'}};
        !           686:         } else {
        !           687:             $subm{'correct'} = $correct;
        !           688:         }
        !           689:         $subm{'submission'} =~ s/=\d+\s*$//;
        !           690:         if (&$function(\%subm)) {
        !           691:             $submission_data{$subm{'correct'}}->{$subm{'submission'}}++;
        !           692:         }
        !           693:     }
        !           694:     return \%submission_data;
        !           695: }
        !           696: 
        !           697: sub RR_static_time_analysis {
        !           698:     my ($r,$problem,$problem_analysis) = @_;
        !           699:     my ($resource,$partid,$respid) = ($problem->{'resource'},
        !           700:                                       $problem->{'part'},
        !           701:                                       $problem->{'respid'});
        !           702:     $r->print('<h3>The time answer you seek is static</h3>');
1.48      matthew   703:     my $analysis_html;
1.94    ! matthew   704:     # Gather student data
        !           705:     my $response_data = &Apache::loncoursedata::get_response_data
1.82      matthew   706:         (\@Apache::lonstatistics::SelectedSections,
                    707:          $Apache::lonstatistics::enrollment_status,
                    708:          $resource->{'symb'},$respid);
1.94    ! matthew   709:     if (! defined($response_data) || 
        !           710:         ref($response_data) ne 'ARRAY' ) {
1.48      matthew   711:         $analysis_html = '<h2>'.
                    712:             &mt('There is no submission data for this resource').
                    713:             '</h2>';
                    714:         $r->print($analysis_html);
1.47      matthew   715:         return;
                    716:     }
1.94    ! matthew   717:     # for time division
        !           718: 
        !           719:     #    limit to between start time & end time
        !           720:     #    loop through data, classifying it by
        !           721:     #         correct foil -> selected foil
        !           722:     #    if there is concept data
        !           723:     #         make a concept correct plot
        !           724:     #    for each correct foil
        !           725:     #         make a plot of the data
        !           726: 
        !           727: }
        !           728: 
        !           729: 
        !           730: =pod
        !           731: 
        !           732: 
        !           733:     } elsif ($ENV{'form.AnalyzeOver'} eq 'tries') {
        !           734:         $analysis_html .= &RR_tries_Analysis($r,$problem->{'resource'},
1.88      matthew   735:                                              $PerformanceData,$problem_data);
1.94    ! matthew   736:     } elsif ($ENV{'form.AnalyzeOver'} eq 'time') {
        !           737:         $analysis_html .= &RR_time_Analysis($r,$problem->{'resource'},
1.88      matthew   738:                                             $PerformanceData,$problem_data);
1.48      matthew   739:     } else {
                    740:         $analysis_html .= '<h2>'.
                    741:            &mt('The analysis you have selected is not supported at this time').
1.91      matthew   742:             '</h2>';
1.48      matthew   743:     }
                    744:     $r->print($analysis_html);
                    745: }
                    746: 
                    747: sub RR_Excel_output   { 
1.88      matthew   748:     my ($r,$PerformanceData,$problem_data) = @_;
1.48      matthew   749:     return '<h1>No!</h1>';
                    750: }
                    751: 
1.94    ! matthew   752: sub RR_tries_Analysis { 
1.88      matthew   753:     my ($r,$resource,$PerformanceData,$problem_data) = @_;
1.48      matthew   754:     my $analysis_html;
                    755:     my $mintries = 1;
                    756:     my $maxtries = $ENV{'form.NumPlots'};
1.88      matthew   757:     my ($table,$Foils,$Concepts) = &build_foil_index($problem_data);
                    758:     if ((! defined($Concepts)) || (@$Concepts < 2)) {
1.48      matthew   759:         $table = '<h3>'.
                    760:             &mt('Not enough data for concept analysis.  '.
                    761:                 'Performing Foil Analysis').
                    762:             '</h3>'.$table;
                    763:     }
                    764:     $analysis_html .= $table;
                    765:     my @TryData = &RR_tries_data_analysis($r,$PerformanceData);
1.94    ! matthew   766:         $analysis_html .= &RR_tries_Foil_Analysis($mintries,$maxtries,$Foils,
1.88      matthew   767:                                                  \@TryData,$problem_data);
1.48      matthew   768:     return $analysis_html;
                    769: }
                    770: 
                    771: sub RR_tries_data_analysis {
                    772:     my ($r,$Attempt_data) = @_;
                    773:     my @TryData;
                    774:     foreach my $attempt (@$Attempt_data) {
                    775:         my %Attempt = &hashify_attempt($attempt);
                    776:         my ($answer,undef) = split('=',$Attempt{'submission'});
                    777:         $TryData[$Attempt{'tries'}]->{$answer}++;
                    778:     }
                    779:     return @TryData;
                    780: }
                    781: 
1.94    ! matthew   782: sub RR_time_Analysis  { 
1.88      matthew   783:     my ($r,$PerformanceData,$problem_data) = @_;
1.48      matthew   784:     my $html;
                    785:     return $html;
                    786: }
                    787: 
1.94    ! matthew   788: sub RR_tries_Foil_Analysis {
1.88      matthew   789:     my ($min,$max,$Foils,$TryData,$problem_data) = @_;
1.48      matthew   790:     my $html;
                    791:     #
                    792:     # Compute the data neccessary to make the plots
                    793:     for (my $try=$min;$try<=$max;$try++) {
                    794:         my @PlotData_Correct; 
                    795:         my @PlotData_Incorrect;
                    796:         next if ($try > scalar(@{$TryData}));
1.56      matthew   797:         next if (! defined($TryData->[$try]));
                    798:         my %DataSet = %{$TryData->[$try]};
1.48      matthew   799:         my $total = 0;
                    800:         foreach my $foilid (@$Foils) {
                    801:             $total += $DataSet{$foilid};
                    802:         }
                    803:         foreach my $foilid (@$Foils) {
                    804:             if ($total == 0) {
                    805:                 push (@PlotData_Correct,0);
                    806:                 push (@PlotData_Incorrect,0);
                    807:             } else {
1.88      matthew   808:                 if ($problem_data->{'_Foils'}->{$foilid}->{'value'} eq 'true') {
1.48      matthew   809:                     push (@PlotData_Correct,
                    810:                           int(100*$DataSet{$foilid}/$total));
                    811:                     push (@PlotData_Incorrect,0);
                    812:                 } else {
                    813:                     push (@PlotData_Correct,0);
                    814:                     push (@PlotData_Incorrect,
                    815:                           int(100*$DataSet{$foilid}/$total));
                    816:                 }
                    817:             }
                    818:         }
1.91      matthew   819:         my $title='Attempt '.$try.' (N='.$total.')';
                    820:         my $xlabel = 'Foil Chosen';
1.48      matthew   821:         $html.=  &Apache::loncommon::DrawBarGraph($title,
                    822:                                                   $xlabel,
                    823:                                                   'Percent Choosing',
                    824:                                                   100,
                    825:                                                   ['#33ff00','#ff3300'],
1.64      matthew   826:                                                   undef,
1.48      matthew   827:                                                   \@PlotData_Correct,
                    828:                                                   \@PlotData_Incorrect);
                    829:     }
                    830:     return $html;
                    831: }
                    832: 
1.94    ! matthew   833: sub RR_tries_Concept_Analysis {
1.88      matthew   834:     my ($min,$max,$Concepts,$ResponseData,$problem_data) = @_;
1.48      matthew   835:     my $html;
                    836:     return $html;
                    837: }
                    838: 
1.94    ! matthew   839: sub RR_time_Foil_Analysis {
1.88      matthew   840:     my ($min,$max,$Foils,$ResponseData,$problem_data) = @_;
1.48      matthew   841:     my $html;
                    842:     return $html;
                    843: }
                    844: 
1.94    ! matthew   845: sub RR_time_Concept_Analysis {
1.88      matthew   846:     my ($min,$max,$Concepts,$ResponseData,$problem_data) = @_;
1.48      matthew   847:     my $html;
                    848:     return $html;
                    849: }
                    850: 
                    851: 
                    852: sub get_Radio_problem_data {
                    853:     my ($url) = @_;
                    854:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
                    855:     (my $garbage,$Answ)=split('_HASH_REF__',$Answ,2);
                    856:     my %Answer = &Apache::lonnet::str2hash($Answ);
                    857:     my %Partdata;
                    858:     foreach my $part (@{$Answer{'parts'}}) {
                    859:         while (my($key,$value) = each(%Answer)) {
                    860: #            if (ref($value) eq 'ARRAY') {
                    861: #                &Apache::lonnet::logthis('is ref part:'.$part.' '.$key.'='.join(',',@$value));
                    862: #            } else {
                    863: #                &Apache::lonnet::logthis('notref part:'.$part.' '.$key.'='.$value);
                    864: #            }                
                    865:             next if ($key !~ /^$part/);
                    866:             $key =~ s/^$part\.//;
                    867:             if ($key eq 'foils') {
                    868:                 $Partdata{$part}->{'_Foils'}=$value;
                    869:             } elsif ($key eq 'options') {
                    870:                 $Partdata{$part}->{'_Options'}=$value;
                    871:             } elsif ($key eq 'shown') {
                    872:                 $Partdata{$part}->{'_Shown'}=$value;
                    873:             } elsif ($key =~ /^foil.value.(.*)$/) {
                    874:                 $Partdata{$part}->{$1}->{'value'}=$value;
                    875:             } elsif ($key =~ /^foil.text.(.*)$/) {
                    876:                 $Partdata{$part}->{$1}->{'text'}=$value;
                    877:             }
                    878:         }
                    879:     }
                    880:     return %Partdata;
1.47      matthew   881: }
1.48      matthew   882: 
1.94    ! matthew   883: =cut
        !           884: 
1.33      matthew   885: #########################################################
                    886: #########################################################
                    887: ##
1.46      matthew   888: ##      Option Response Routines
1.33      matthew   889: ##
                    890: #########################################################
                    891: #########################################################
1.46      matthew   892: sub OptionResponseAnalysis {
1.88      matthew   893:     my ($r,$problem,$problem_data,$Students) = @_;
1.56      matthew   894:     my ($resource,$respid) = ($problem->{'resource'},
                    895:                               $problem->{'respid'});
                    896:     # Note: part data is not needed.
1.82      matthew   897:     my $PerformanceData = &Apache::loncoursedata::get_response_data
                    898:         (\@Apache::lonstatistics::SelectedSections,
                    899:          $Apache::lonstatistics::enrollment_status,
                    900:          $resource->{'symb'},$respid);
1.46      matthew   901:     if (! defined($PerformanceData) || 
                    902:         ref($PerformanceData) ne 'ARRAY' ) {
                    903:         $r->print('<h2>'.
                    904:                   &mt('There is no student data for this problem.').
                    905:                   '</h2>');
                    906:     }  else {
                    907:         $r->rflush();
                    908:         if (exists($ENV{'form.ExcelOutput'})) {
1.57      matthew   909:             my $result = &OR_excel_sheet($r,$resource,
                    910:                                          $PerformanceData,
1.88      matthew   911:                                          $problem_data);
1.46      matthew   912:             $r->print($result);
                    913:             $r->rflush();
1.39      matthew   914:         } else {
1.94    ! matthew   915:             if ($ENV{'form.AnalyzeOver'} eq 'tries') {
1.57      matthew   916:                 my $analysis_html = &OR_tries_analysis($r,
1.46      matthew   917:                                                     $PerformanceData,
1.88      matthew   918:                                                     $problem_data);
1.46      matthew   919:                 $r->print($analysis_html);
                    920:                 $r->rflush();
1.94    ! matthew   921:             } elsif ($ENV{'form.AnalyzeOver'} eq 'time') {
1.57      matthew   922:                 my $analysis_html = &OR_time_analysis($PerformanceData,
1.88      matthew   923:                                                    $problem_data);
1.46      matthew   924:                 $r->print($analysis_html);
                    925:                 $r->rflush();
1.39      matthew   926:             } else {
1.46      matthew   927:                 $r->print('<h2>'.
                    928:                           &mt('The analysis you have selected is '.
                    929:                               'not supported at this time').
                    930:                           '</h2>');
                    931:             }
1.36      matthew   932:         }
1.39      matthew   933:     }
                    934: }
                    935: 
1.33      matthew   936: #########################################################
1.46      matthew   937: #
1.94    ! matthew   938: #       Option Response:  tries Analysis
1.46      matthew   939: #
1.33      matthew   940: #########################################################
1.57      matthew   941: sub OR_tries_analysis {
1.43      matthew   942:     my ($r,$PerformanceData,$ORdata) = @_;
1.33      matthew   943:     my $mintries = 1;
                    944:     my $maxtries = $ENV{'form.NumPlots'};
1.39      matthew   945:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
1.88      matthew   946:     my %response_data = &OR_analyze_by_tries($r,$PerformanceData,
1.36      matthew   947:                                                      $mintries,$maxtries);
1.42      matthew   948:     my $analysis = '';
1.31      matthew   949:     #
1.88      matthew   950:     # Compute the data necessary to make the plots
                    951:     my @foil_plot; 
                    952:     my @concept_data;
                    953:     for (my $j=0;$j<=scalar(@$Concepts);$j++) {
                    954:         my $concept = $Concepts->[$j];
1.75      matthew   955:         foreach my $foilid (@{$concept->{'foils'}}) {
1.76      matthew   956:             for (my $try=$mintries;$try<=$maxtries;$try++) {
1.88      matthew   957:                 # concept analysis data
1.93      matthew   958:                 $concept_data[$j]->[$try]->{'_correct'} +=
1.88      matthew   959:                     $response_data{$foilid}->[$try]->{'_correct'};
1.93      matthew   960:                 $concept_data[$j]->[$try]->{'_total'} +=
1.88      matthew   961:                     $response_data{$foilid}->[$try]->{'_total'};
                    962:                 #
                    963:                 # foil analysis data
                    964:                 if ($response_data{$foilid}->[$try]->{'_total'} == 0) {
                    965:                     push(@{$foil_plot[$try]->{'_correct'}},0);
1.42      matthew   966:                 } else {
1.88      matthew   967:                     push(@{$foil_plot[$try]->{'_correct'}},
                    968:                          100*$response_data{$foilid}->[$try]->{'_correct'}/
                    969:                          $response_data{$foilid}->[$try]->{'_total'});
1.75      matthew   970:                 }
                    971:                 foreach my $option (@{$ORdata->{'_Options'}}) {
1.88      matthew   972:                     push(@{$foil_plot[$try]->{'_total'}},
                    973:                          $response_data{$foilid}->[$try]->{'_total'});
                    974:                     if ($response_data{$foilid}->[$try]->{'_total'} == 0) {
                    975:                         push (@{$foil_plot[$try]->{$option}},0);
1.39      matthew   976:                     } else {
1.88      matthew   977:                         if ($response_data{$foilid}->[$try]->{'_total'} ==
                    978:                             $response_data{$foilid}->[$try]->{'_correct'}) {
                    979:                             push(@{$foil_plot[$try]->{$option}},0);
1.75      matthew   980:                         } else {
1.88      matthew   981:                             push (@{$foil_plot[$try]->{$option}},
1.76      matthew   982:                                   100 * 
1.88      matthew   983:                                   $response_data{$foilid}->[$try]->{$option} / 
                    984:                                   ($response_data{$foilid}->[$try]->{'_total'} 
1.76      matthew   985:                                    - 
1.88      matthew   986:                                    $response_data{$foilid}->[$try]->{'_correct'}
1.76      matthew   987:                                    ));
1.75      matthew   988:                         }
1.39      matthew   989:                     }
1.76      matthew   990:                 } # End of foreach my $option
1.36      matthew   991:             }
1.76      matthew   992:         } # End of foreach my $foilid
1.88      matthew   993:     } # End of concept loops
1.42      matthew   994:     # 
                    995:     # Build a table for the plots
                    996:     my $analysis_html = "<table>\n";
1.75      matthew   997:     my $optionkey = &build_option_index($ORdata);
1.88      matthew   998:     my $num_concepts = 1;
                    999:     if (defined($Concepts)) { $num_concepts = scalar(@$Concepts); }
                   1000:     #
1.75      matthew  1001:     for (my $try=$mintries;$try<=$maxtries;$try++) {
1.92      matthew  1002:         if (! defined($response_data{'_total'}->[$try]) ||
                   1003:             $response_data{'_total'}->[$try] == 0) { 
                   1004:             if ($try > 1) {
                   1005:                 $analysis_html.= '<tr><td align="center" colspan="4"><b>'.
                   1006:                     &mt('None of the selected students attempted the problem more than [_1] times.',$try-1).
                   1007:                     '</b></td></tr>';
                   1008:             } else {
                   1009:                 $analysis_html.= '<tr><td colspan="4" align="center"><b>'.
                   1010:                     &mt('None of the selected students have attempted the problem').'</b></td></tr>';
                   1011:             }
                   1012:             last;
                   1013:         }
1.88      matthew  1014:         my $concept_graph='';
                   1015:         if ($num_concepts > 1) {
                   1016:             #
                   1017:             # Create concept plot
                   1018:             my @concept_plot_data;
                   1019:             for (my $j=0;$j<=$#concept_data;$j++) {
1.93      matthew  1020:                 my $total = $concept_data[$j]->[$try]->{'_total'};
1.88      matthew  1021:                 if ($total == 0) {
                   1022:                     $concept_plot_data[$j] = 0;
                   1023:                 } else {
                   1024:                     $concept_plot_data[$j] = 100 * 
                   1025:                         sprintf('%0.3f',
1.93      matthew  1026:                                 $concept_data[$j]->[$try]->{'_correct'} / 
                   1027:                                 $total);
1.88      matthew  1028:                 }
                   1029:             }
                   1030:             #
                   1031:             my $title;
                   1032:             my $count = $response_data{'_total'}->[$try];
                   1033:             $title = 'Attempt '.$try.' (N='.$count.')';
                   1034:             $concept_graph = &Apache::loncommon::DrawBarGraph
                   1035:                 ($title,'Concept Number','Percent Correct',
                   1036:                  100,$plotcolors,undef,\@concept_plot_data);
                   1037:         }
                   1038:         #
                   1039:         # Create Foil Plots
                   1040:         my $count = $response_data{'_total'}->[$try];
                   1041:         my $title = 'Attempt '.$try.' (N='.$count.')';
1.42      matthew  1042:         my @Datasets;
1.48      matthew  1043:         foreach my $option ('_correct',@{$ORdata->{'_Options'}}) {
1.88      matthew  1044:             next if (! exists($foil_plot[$try]->{$option}));
                   1045:             push(@Datasets,$foil_plot[$try]->{$option});
1.42      matthew  1046:         }
1.76      matthew  1047:         #
                   1048:         # Put a blank in the data set between concepts
                   1049:         for (my $set =0;$set<=$#Datasets;$set++) {
                   1050:             my @Data = @{$Datasets[$set]};
                   1051:             my $idx = 0;
                   1052:             foreach my $concept (@{$Concepts}) {
                   1053:                 foreach my $foilid (@{$concept->{'foils'}}) {
                   1054:                     $Datasets[$set]->[$idx++]=shift(@Data);
                   1055:                 }
                   1056:                 if ($concept->{'name'} ne $Concepts->[-1]->{'name'}) {
                   1057:                     $Datasets[$set]->[$idx++] = 0;
                   1058:                 }
                   1059:             }
                   1060:         }
                   1061:         #
                   1062:         # Set up the labels needed for the bar graph
                   1063:         my @Labels;
                   1064:         my $idx = 1;
                   1065:         foreach my $concept (@{$Concepts}) {
                   1066:             foreach my $foilid (@{$concept->{'foils'}}) {
                   1067:                 push(@Labels,$idx++);
                   1068:             }
                   1069:             push(@Labels,'');
                   1070:         }
                   1071:         #
1.88      matthew  1072:         my $correct_graph = &Apache::loncommon::DrawBarGraph
1.42      matthew  1073:             ($title,'Foil Number','Percent Correct',
1.76      matthew  1074:              100,$plotcolors,\@Labels,$Datasets[0]);
1.75      matthew  1075:         
                   1076:         #
                   1077:         #
1.57      matthew  1078:         next if (! defined($Datasets[0]));
1.42      matthew  1079:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
                   1080:             $Datasets[0]->[$i]=0;
                   1081:         }
1.88      matthew  1082:         $count = $response_data{'_total'}->[$try] - 
                   1083:                                            $response_data{'_correct'}->[$try];
                   1084:         $title = 'Attempt '.$try.' (N='.$count.')';
                   1085:         my $incorrect_graph = &Apache::loncommon::DrawBarGraph
1.42      matthew  1086:             ($title,'Foil Number','% Option Chosen Incorrectly',
1.76      matthew  1087:              100,$plotcolors,\@Labels,@Datasets);
1.88      matthew  1088:         $analysis_html.= 
                   1089:             '<tr>'.
                   1090:             '<td>'.$concept_graph.'</td>'.
                   1091:             '<td>'.$correct_graph.'</td>'.
                   1092:             '<td>'.$incorrect_graph.'</td>'.
                   1093:             '<td>'.$optionkey.'<td>'.
                   1094:             '</tr>'.$/;
1.42      matthew  1095:     }
                   1096:     $analysis_html .= "</table>\n";
1.88      matthew  1097:     $table .= $analysis_html;
                   1098:     return $table;
1.25      matthew  1099: }
                   1100: 
1.57      matthew  1101: sub OR_analyze_by_tries {
1.43      matthew  1102:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
1.25      matthew  1103:     my %Trydata;
                   1104:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
                   1105:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
1.26      matthew  1106:     foreach my $row (@$PerformanceData) {
                   1107:         next if (! defined($row));
1.47      matthew  1108:         my $tries = &get_tries_from_row($row);
1.46      matthew  1109:         my %Row   = &Process_OR_Row($row);
1.43      matthew  1110:         next if (! %Row);
1.42      matthew  1111:         while (my ($foilid,$href) = each(%Row)) {
                   1112:             if (! ref($href)) { 
                   1113:                 $Trydata{$foilid}->[$tries] += $href;
                   1114:                 next;
                   1115:             }
                   1116:             while (my ($option,$value) = each(%$href)) {
                   1117:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
1.25      matthew  1118:             }
                   1119:         }
                   1120:     }
                   1121:     return %Trydata;
                   1122: }
                   1123: 
1.33      matthew  1124: #########################################################
1.46      matthew  1125: #
                   1126: #     Option Response: Time Analysis
                   1127: #
1.33      matthew  1128: #########################################################
1.57      matthew  1129: sub OR_time_analysis {
1.88      matthew  1130:     my ($performance_data,$ORdata) = @_;
1.42      matthew  1131:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
1.88      matthew  1132:     my $foilkey = &build_option_index($ORdata);
                   1133:     my $num_concepts = 1;
                   1134:     if (defined($Concepts)) { $num_concepts = scalar(@$Concepts); }
                   1135:     #
                   1136:     if ($num_concepts < 2) {
1.42      matthew  1137:         $table = '<h3>'.
                   1138:             &mt('Not enough data for concept analysis.  '.
                   1139:                 'Performing Foil Analysis').
1.88      matthew  1140:                 '</h3>'.$table;
1.42      matthew  1141:     }
1.88      matthew  1142:     #
1.33      matthew  1143:     my $num_plots = $ENV{'form.NumPlots'};
1.88      matthew  1144:     my $num_data = scalar(@$performance_data)-1;
1.33      matthew  1145:     my $percent = sprintf('%2f',100/$num_plots);
1.42      matthew  1146:     #
1.37      matthew  1147:     $table .= "<table>\n";
1.33      matthew  1148:     for (my $i=0;$i<$num_plots;$i++) {
1.42      matthew  1149:         ##
1.34      matthew  1150:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
                   1151:             ('startdate_'.$i);
                   1152:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
                   1153:             ('enddate_'.$i);
                   1154:         if (! defined($starttime) || ! defined($endtime)) {
1.42      matthew  1155:             my $sec_in_day = 86400;
1.88      matthew  1156:             my $last_sub_time = &get_time_from_row($performance_data->[-1]);
1.42      matthew  1157:             my ($sday,$smon,$syear);
                   1158:             (undef,undef,undef,$sday,$smon,$syear) = 
                   1159:                 localtime($last_sub_time - $sec_in_day*$i);
                   1160:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
                   1161:             $endtime = $starttime + $sec_in_day;
                   1162:             if ($i == ($num_plots -1 )) {
1.88      matthew  1163:                 $starttime = &get_time_from_row($performance_data->[0]);
1.42      matthew  1164:             }
                   1165:         }
1.88      matthew  1166:         $table .= '<tr><td colspan="4" align="center">'.
                   1167:             &mt('Data from [_1] to [_2]',
                   1168:                 &Apache::lonlocal::locallocaltime($starttime),
                   1169:                 &Apache::lonlocal::locallocaltime($endtime)).'</td></tr>'.$/;
1.34      matthew  1170:         my $startdateform = &Apache::lonhtmlcommon::date_setter
                   1171:             ('Statistics','startdate_'.$i,$starttime);
                   1172:         my $enddateform = &Apache::lonhtmlcommon::date_setter
                   1173:             ('Statistics','enddate_'.$i,$endtime);
1.42      matthew  1174:         #
                   1175:         my $begin_index;
                   1176:         my $end_index;
                   1177:         my $j;
1.88      matthew  1178:         while (++$j < scalar(@$performance_data)) {
                   1179:             last if (&get_time_from_row($performance_data->[$j]) 
1.46      matthew  1180:                                                               > $starttime);
1.42      matthew  1181:         }
                   1182:         $begin_index = $j;
1.91      matthew  1183:         while ($j < scalar(@$performance_data)) {
                   1184:             if (&get_time_from_row($performance_data->[$j]) > $endtime) {
                   1185:                 last;
                   1186:             } else {
                   1187:                 $j++;
                   1188:             }
1.42      matthew  1189:         }
                   1190:         $end_index = $j;
                   1191:         ##
                   1192:         my $interval = {
                   1193:             begin_index => $begin_index,
                   1194:             end_index   => $end_index,
                   1195:             startdateform => $startdateform,
                   1196:             enddateform   => $enddateform,
                   1197:         };
1.88      matthew  1198:         my $concept_correct_plot = '';
                   1199:         if ($num_concepts > 1) {
                   1200:             $concept_correct_plot = 
                   1201:                 &OR_Concept_Time_Analysis($performance_data,
                   1202:                                           $ORdata,$Concepts,
1.42      matthew  1203:                                           $interval);
                   1204:         }
1.88      matthew  1205:         my ($foil_correct_plot,$foil_incorrect_plot) = 
                   1206:             &OR_Foil_Time_Analysis($performance_data,$ORdata,
                   1207:                                    $Foils,$Concepts,$interval);
                   1208:         $table .= '<tr>'.
                   1209:             '<td>'.$concept_correct_plot.'</td>'.
                   1210:             '<td>'.$foil_correct_plot.'</td>'.
                   1211:             '<td>'.$foil_incorrect_plot.'</td>'.
                   1212:             '<td align="left" valign="top">'.$foilkey.'</td></tr>'.$/;
                   1213:         $table .= '<tr><td colspan="4" align="center">'.
                   1214:             &mt('Start time: [_1]',
                   1215:                 $interval->{'startdateform'}).'<br />'.
                   1216:             &mt('End time: [_1]',
                   1217:                 $interval->{'enddateform'}).
                   1218:                 '</td></tr>'.$/;
                   1219:         $table.= '<tr><td colspan="4">&nbsp</td></tr>'.$/;
1.33      matthew  1220:     }
1.88      matthew  1221:     $table .= '</table>';
1.42      matthew  1222:     #
1.33      matthew  1223:     return $table;
                   1224: }
                   1225: 
1.57      matthew  1226: sub OR_Foil_Time_Analysis {
1.88      matthew  1227:     my ($performance_data,$ORdata,$Foils,$Concepts,$interval) = @_;
1.42      matthew  1228:     my $analysis_html;
                   1229:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
                   1230:                                     $interval->{'end_index'});
1.33      matthew  1231:     my %TimeData;
                   1232:     #
                   1233:     # Compute the number getting the foils correct or incorrects
1.42      matthew  1234:     for (my $j=$begin_index;$j<=$end_index;$j++) {
1.88      matthew  1235:         my $row = $performance_data->[$j];
1.33      matthew  1236:         next if (! defined($row));
1.46      matthew  1237:         my %Row = &Process_OR_Row($row);
1.42      matthew  1238:         while (my ($foilid,$href) = each(%Row)) {
                   1239:             if (! ref($href)) {
                   1240:                 $TimeData{$foilid} += $href;
                   1241:                 next;
                   1242:             }
                   1243:             while (my ($option,$value) = each(%$href)) {
                   1244:                 $TimeData{$foilid}->{$option}+=$value;
1.33      matthew  1245:             }
                   1246:         }
                   1247:     }
1.88      matthew  1248:     my @plotdata;
                   1249:     my @labels;
                   1250:     foreach my $concept (@{$Concepts}) {
                   1251:         foreach my $foil (@{$concept->{'foils'}}) {
                   1252:             push(@labels,scalar(@labels)+1);
                   1253:             my $total = $TimeData{$foil}->{'_total'};
                   1254:             if ($total == 0) {
                   1255:                 push(@{$plotdata[0]},0);
1.42      matthew  1256:             } else {
1.88      matthew  1257:                 push(@{$plotdata[0]},
                   1258:                      100 * $TimeData{$foil}->{'_correct'} / $total);
1.37      matthew  1259:             }
1.88      matthew  1260:             my $total_incorrect = $total - $TimeData{$foil}->{'_correct'};
                   1261:             for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
                   1262:                 my $option = $ORdata->{'_Options'}->[$i];
                   1263:                 if ($total_incorrect == 0) {
                   1264:                     push(@{$plotdata[$i+1]},0);
                   1265:                 } else {
                   1266:                     push(@{$plotdata[$i+1]},
                   1267:                          100 * $TimeData{$foil}->{$option} / $total_incorrect);
                   1268:                 }
                   1269:             }
                   1270:         }
                   1271:         # Put in a blank one
                   1272:         push(@labels,'');
                   1273:         push(@{$plotdata[0]},0);
                   1274:         for (my $i=0;$i<scalar(@{$ORdata->{'_Options'}});$i++) {
                   1275:             push(@{$plotdata[$i+1]},0);
1.39      matthew  1276:         }
1.42      matthew  1277:     }
                   1278:     #
                   1279:     # Create the plot
                   1280:     my $count = $end_index-$begin_index;
1.91      matthew  1281:     return ('No data','') if ($count <= 0);
1.42      matthew  1282:     my $title;
                   1283:     if ($count == 0) {
                   1284:         $title = 'no submissions';
                   1285:     } elsif ($count == 1) {
                   1286:         $title = 'one submission';
1.91      matthew  1287:     } elsif ($count > 1) {
1.42      matthew  1288:         $title = $count.' submissions';
                   1289:     }
1.88      matthew  1290:     my $correct_plot = &Apache::loncommon::DrawBarGraph($title,
1.46      matthew  1291:                                                        'Foil Number',
                   1292:                                                        'Percent Correct',
                   1293:                                                        100,
                   1294:                                                        $plotcolors,
1.64      matthew  1295:                                                        undef,
1.88      matthew  1296:                                                        $plotdata[0]);
                   1297:     for (my $j=0; $j< scalar(@{$plotdata[0]});$j++) {
                   1298:         $plotdata[0]->[$j]=0;
1.42      matthew  1299:     }
                   1300:     $count = $end_index-$begin_index-$TimeData{'_correct'};
                   1301:     if ($count == 0) {
                   1302:         $title = 'no submissions';
1.91      matthew  1303:     } elsif ($count < 0) {
                   1304:         return ($correct_plot,'');
1.42      matthew  1305:     } elsif ($count == 1) {
                   1306:         $title = 'one submission';
                   1307:     } else {
                   1308:         $title = $count.' submissions';
                   1309:     }
1.88      matthew  1310:     my $incorrect_plot = &Apache::loncommon::DrawBarGraph($title,
1.42      matthew  1311:                                                  'Foil Number',
                   1312:                                                  'Incorrect Option Choice',
                   1313:                                                  100,
                   1314:                                                  $plotcolors,
1.64      matthew  1315:                                                  undef,
1.91      matthew  1316:                                                  @plotdata);
1.88      matthew  1317:     return ($correct_plot,$incorrect_plot);
1.42      matthew  1318: }
                   1319: 
1.57      matthew  1320: sub OR_Concept_Time_Analysis {
1.88      matthew  1321:     my ($performance_data,$ORdata,$Concepts,$interval) = @_;
1.42      matthew  1322:     ##
                   1323:     ## Determine starttime, endtime, startindex, endindex
                   1324:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
                   1325:                                     $interval->{'end_index'});
                   1326:     my %TimeData;
                   1327:     #
                   1328:     # Compute the number getting the foils correct or incorrects
                   1329:     for (my $j=$begin_index;$j<=$end_index;$j++) {
1.88      matthew  1330:         my $row = $performance_data->[$j];
1.42      matthew  1331:         next if (! defined($row));
1.46      matthew  1332:         my %Row = &Process_OR_Row($row);
1.42      matthew  1333:         while (my ($foilid,$href) = each(%Row)) {
                   1334:             if (! ref($href)) {
                   1335:                 $TimeData{$foilid} += $href;
                   1336:                 next;
1.37      matthew  1337:             }
1.42      matthew  1338:             while (my ($option,$value) = each(%$href)) {
                   1339:                 $TimeData{$foilid}->{$option}+=$value;
1.37      matthew  1340:             }
1.33      matthew  1341:         }
                   1342:     }
                   1343:     #
1.42      matthew  1344:     # Put the data in plottable form
1.88      matthew  1345:     my @plotdata;
1.42      matthew  1346:     foreach my $concept (@$Concepts) {
                   1347:         my ($total,$correct);
                   1348:         foreach my $foil (@{$concept->{'foils'}}) {
                   1349:             $total += $TimeData{$foil}->{'_total'};
                   1350:             $correct += $TimeData{$foil}->{'_correct'};
                   1351:         }
                   1352:         if ($total == 0) {
1.88      matthew  1353:             push(@plotdata,0);
1.42      matthew  1354:         } else {
1.88      matthew  1355:             push(@plotdata,100 * $correct / $total);
1.42      matthew  1356:         }
                   1357:     }
                   1358:     #
1.33      matthew  1359:     # Create the plot
1.42      matthew  1360:     my $title = ($end_index - $begin_index).' submissions';
1.88      matthew  1361:     return &Apache::loncommon::DrawBarGraph($title,
                   1362:                                             'Concept Number',
                   1363:                                             'Percent Correct',
                   1364:                                             100,
                   1365:                                             $plotcolors,
                   1366:                                             undef,
                   1367:                                             \@plotdata);
1.42      matthew  1368: }
                   1369: 
                   1370: #########################################################
                   1371: #########################################################
                   1372: ##
                   1373: ##             Excel output 
                   1374: ##
                   1375: #########################################################
                   1376: #########################################################
1.57      matthew  1377: sub OR_excel_sheet {
1.88      matthew  1378:     my ($r,$resource,$performance_data,$ORdata) = @_;
1.44      matthew  1379:     my $response = '';
1.42      matthew  1380:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
1.43      matthew  1381:     #
                   1382:     # Create excel worksheet
                   1383:     my $filename = '/prtspool/'.
                   1384:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                   1385:         time.'_'.rand(1000000000).'.xls';
                   1386:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   1387:     if (! defined($workbook)) {
                   1388:         $r->log_error("Error creating excel spreadsheet $filename: $!");
                   1389:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
                   1390:                             "This error has been logged.  ".
                   1391:                             "Please alert your LON-CAPA administrator").
                   1392:                   '</p>');
                   1393:         return undef;
                   1394:     }
                   1395:     #
                   1396:     $workbook->set_tempdir('/home/httpd/perl/tmp');
1.69      matthew  1397:     my $format = &Apache::loncommon::define_excel_formats($workbook);
1.43      matthew  1398:     #
                   1399:     # Create and populate main worksheets
                   1400:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
1.69      matthew  1401:     my $student_data_sheet = &build_student_data_worksheet($workbook,$format);
1.43      matthew  1402:     my $response_data_sheet = $workbook->addworksheet('Response Data');
                   1403:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
                   1404:                        $response_data_sheet) {
                   1405:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
                   1406:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
                   1407:     }
                   1408:     #
                   1409:     my $result;
1.59      matthew  1410:     $result = &OR_build_problem_data_worksheet($problem_data_sheet,$format,
1.43      matthew  1411:                                             $Concepts,$ORdata);
                   1412:     if ($result ne 'okay') {
                   1413:         # Do something useful
                   1414:     }
1.59      matthew  1415:     $result = &OR_build_response_data_worksheet($response_data_sheet,$format,
1.88      matthew  1416:                                              $performance_data,$Foils,
1.43      matthew  1417:                                              $ORdata);
                   1418:     if ($result ne 'okay') {
                   1419:         # Do something useful
                   1420:     }
1.44      matthew  1421:     $response_data_sheet->activate();
1.43      matthew  1422:     #
                   1423:     # Close the excel file
                   1424:     $workbook->close();
                   1425:     #
                   1426:     # Write a link to allow them to download it
1.44      matthew  1427:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
                   1428:               '<p><a href="'.$filename.'">'.
                   1429:               &mt('Your Excel spreadsheet.').
                   1430:               '</a></p>'."\n";
1.43      matthew  1431:     return $result;
                   1432: }
                   1433: 
1.57      matthew  1434: sub OR_build_problem_data_worksheet {
1.43      matthew  1435:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
                   1436:     my $rows_output = 3;
                   1437:     my $cols_output = 0;
                   1438:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
                   1439:     ##
                   1440:     ##
                   1441:     my @Headers;
                   1442:     if (@$Concepts > 1) {
                   1443:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
                   1444:                     'Foil Name','Foil Text','Correct value');
                   1445:     } else {
                   1446:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
                   1447:     }
                   1448:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
1.48      matthew  1449:     my %Foildata = %{$ORdata->{'_Foils'}};
1.43      matthew  1450:     my $conceptindex = 1;
                   1451:     my $foilindex = 1;
                   1452:     foreach my $concept (@$Concepts) {
                   1453:         my @FoilsInConcept = @{$concept->{'foils'}};
                   1454:         my $firstfoil = shift(@FoilsInConcept);
                   1455:         if (@$Concepts > 1) {
                   1456:             $worksheet->write_row($rows_output++,0,
                   1457:                                   [$conceptindex,
                   1458:                                    $concept->{'name'},
                   1459:                                    $foilindex++,
                   1460:                                    $Foildata{$firstfoil}->{'name'},
                   1461:                                    $Foildata{$firstfoil}->{'text'},
                   1462:                                    $Foildata{$firstfoil}->{'value'},]);
                   1463:         } else {
                   1464:             $worksheet->write_row($rows_output++,0,
                   1465:                                   [ $foilindex++,
                   1466:                                     $Foildata{$firstfoil}->{'name'},
                   1467:                                     $Foildata{$firstfoil}->{'text'},
                   1468:                                     $Foildata{$firstfoil}->{'value'},]);
                   1469:         }
                   1470:         foreach my $foilid (@FoilsInConcept) {
                   1471:             if (@$Concepts > 1) {
                   1472:                 $worksheet->write_row($rows_output++,0,
                   1473:                                       ['',
                   1474:                                        '',
                   1475:                                        $foilindex,
                   1476:                                        $Foildata{$foilid}->{'name'},
                   1477:                                        $Foildata{$foilid}->{'text'},
                   1478:                                        $Foildata{$foilid}->{'value'},]);
                   1479:             } else {
                   1480:                 $worksheet->write_row($rows_output++,0,                
                   1481:                                       [$foilindex,
                   1482:                                        $Foildata{$foilid}->{'name'},
                   1483:                                        $Foildata{$foilid}->{'text'},
                   1484:                                        $Foildata{$foilid}->{'value'},]);
                   1485:             }                
                   1486:         } continue {
                   1487:             $foilindex++;
                   1488:         }
                   1489:     } continue {
                   1490:         $conceptindex++;
                   1491:     }
                   1492:     $rows_output++;
                   1493:     $rows_output++;
                   1494:     ##
                   1495:     ## Option data output
                   1496:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
1.48      matthew  1497:     foreach my $string (@{$ORdata->{'_Options'}}) {
1.43      matthew  1498:         $worksheet->write($rows_output++,0,$string);
                   1499:     }
                   1500:     return 'okay';
                   1501: }
                   1502: 
1.57      matthew  1503: sub OR_build_response_data_worksheet {
1.88      matthew  1504:     my ($worksheet,$format,$performance_data,$Foils,$ORdata)=@_;
1.43      matthew  1505:     my $rows_output = 3;
                   1506:     my $cols_output = 0;
                   1507:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
                   1508:     $worksheet->set_column(1,1,20);
                   1509:     $worksheet->set_column(2,2,13);
                   1510:     my @Headers = ('identifier','time','award detail','attempt');
                   1511:     foreach my $foil (@$Foils) {
                   1512:         push (@Headers,$foil.' submission');
                   1513:         push (@Headers,$foil.' grading');
                   1514:     }
                   1515:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
                   1516:     #
1.88      matthew  1517:     foreach my $row (@$performance_data) {
1.43      matthew  1518:         next if (! defined($row));
                   1519:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
                   1520:         my @Foilgrades = split('&',$grading);
                   1521:         my @Foilsubs   = split('&',$submission);
1.88      matthew  1522:         my %response_data;
1.43      matthew  1523:         for (my $j=0;$j<=$#Foilgrades;$j++) {
                   1524:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
                   1525:             my (undef,$submission) = split('=',$Foilsubs[$j]);
                   1526:             $submission = &Apache::lonnet::unescape($submission);
1.88      matthew  1527:             $response_data{$foilid.' submission'}=$submission;
                   1528:             $response_data{$foilid.' award'}=$correct;
1.43      matthew  1529:         }
                   1530:         $worksheet->write($rows_output,$cols_output++,$student);
                   1531:         $worksheet->write($rows_output,$cols_output++,
1.71      matthew  1532:              &Apache::lonstathelpers::calc_serial($time),$format->{'date'});
1.43      matthew  1533:         $worksheet->write($rows_output,$cols_output++,$award);
                   1534:         $worksheet->write($rows_output,$cols_output++,$tries);
                   1535:         foreach my $foilid (@$Foils) {
                   1536:             $worksheet->write($rows_output,$cols_output++,
1.88      matthew  1537:                               $response_data{$foilid.' submission'});
1.43      matthew  1538:             $worksheet->write($rows_output,$cols_output++,
1.88      matthew  1539:                               $response_data{$foilid.' award'});
1.43      matthew  1540:         }
                   1541:         $rows_output++;
                   1542:         $cols_output = 0;
                   1543:     }
                   1544:     return;
1.42      matthew  1545: }
                   1546: 
1.46      matthew  1547: sub build_foil_index {
                   1548:     my ($ORdata) = @_;
1.48      matthew  1549:     return if (! exists($ORdata->{'_Foils'}));
                   1550:     my %Foildata = %{$ORdata->{'_Foils'}};
1.46      matthew  1551:     my @Foils = sort(keys(%Foildata));
                   1552:     my %Concepts;
                   1553:     foreach my $foilid (@Foils) {
1.48      matthew  1554:         push(@{$Concepts{$Foildata{$foilid}->{'_Concept'}}},
1.46      matthew  1555:              $foilid);
                   1556:     }
                   1557:     undef(@Foils);
                   1558:     # Having gathered the concept information in a hash, we now translate it
                   1559:     # into an array because we need to be consistent about order.
                   1560:     # Also put the foils in order, too.
                   1561:     my $sortfunction = sub {
                   1562:         my %Numbers = (one   => 1,
                   1563:                        two   => 2,
                   1564:                        three => 3,
                   1565:                        four  => 4,
                   1566:                        five  => 5,
                   1567:                        six   => 6,
                   1568:                        seven => 7,
                   1569:                        eight => 8,
                   1570:                        nine  => 9,
                   1571:                        ten   => 10,);
                   1572:         my $a1 = lc($a); 
                   1573:         my $b1 = lc($b);
1.57      matthew  1574:         if (exists($Numbers{$a1})) {
1.67      matthew  1575:             $a1 = $Numbers{$a1};
1.57      matthew  1576:         }
                   1577:         if (exists($Numbers{$b1})) {
1.67      matthew  1578:             $b1 = $Numbers{$b1};
1.46      matthew  1579:         }
1.67      matthew  1580:         if (($a1 =~/^\d+$/) && ($b1 =~/^\d+$/)) {
                   1581:             return $a1 <=> $b1;
1.57      matthew  1582:         } else {
1.67      matthew  1583:             return $a1 cmp $b1;
1.46      matthew  1584:         }
                   1585:     };
                   1586:     my @Concepts;
                   1587:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
1.57      matthew  1588:         if (! defined($Concepts{$concept})) {
                   1589:             $Concepts{$concept}=[];
                   1590: #            next;
                   1591:         }
1.46      matthew  1592:         push(@Concepts,{ name => $concept,
                   1593:                         foils => [@{$Concepts{$concept}}]});
                   1594:         push(@Foils,(@{$Concepts{$concept}}));
                   1595:     }
                   1596:     #
                   1597:     # Build up the table of row labels.
                   1598:     my $table = '<table border="1" >'."\n";
                   1599:     if (@Concepts > 1) {
                   1600:         $table .= '<tr>'.
                   1601:             '<th>'.&mt('Concept Number').'</th>'.
                   1602:             '<th>'.&mt('Concept').'</th>'.
                   1603:             '<th>'.&mt('Foil Number').'</th>'.
                   1604:             '<th>'.&mt('Foil Name').'</th>'.
                   1605:             '<th>'.&mt('Foil Text').'</th>'.
                   1606:             '<th>'.&mt('Correct Value').'</th>'.
                   1607:             "</tr>\n";
                   1608:     } else {
                   1609:         $table .= '<tr>'.
                   1610:             '<th>'.&mt('Foil Number').'</th>'.
                   1611:             '<th>'.&mt('Foil Name').'</th>'.
                   1612:             '<th>'.&mt('Foil Text').'</th>'.
                   1613:             '<th>'.&mt('Correct Value').'</th>'.
                   1614:             "</tr>\n";
                   1615:     }        
                   1616:     my $conceptindex = 1;
                   1617:     my $foilindex = 1;
                   1618:     foreach my $concept (@Concepts) {
                   1619:         my @FoilsInConcept = @{$concept->{'foils'}};
                   1620:         my $firstfoil = shift(@FoilsInConcept);
                   1621:         if (@Concepts > 1) {
                   1622:             $table .= '<tr>'.
                   1623:                 '<td>'.$conceptindex.'</td>'.
1.83      albertel 1624:                 '<td>'.&HTML::Entities::encode($concept->{'name'},'<>&"').'</td>'.
1.46      matthew  1625:                 '<td>'.$foilindex++.'</td>'.
1.83      albertel 1626:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
1.65      matthew  1627:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
1.83      albertel 1628:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
1.46      matthew  1629:                 "</tr>\n";
                   1630:         } else {
                   1631:             $table .= '<tr>'.
                   1632:                 '<td>'.$foilindex++.'</td>'.
1.83      albertel 1633:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'name'},'<>&"').'</td>'.
1.65      matthew  1634:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
1.83      albertel 1635:                 '<td>'.&HTML::Entities::encode($Foildata{$firstfoil}->{'value'},'<>&"').'</td>'.
1.46      matthew  1636:                 "</tr>\n";
                   1637:         }
                   1638:         foreach my $foilid (@FoilsInConcept) {
                   1639:             if (@Concepts > 1) {
                   1640:                 $table .= '<tr>'.
                   1641:                     '<td></td>'.
                   1642:                     '<td></td>'.
                   1643:                     '<td>'.$foilindex.'</td>'.
1.83      albertel 1644:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
1.65      matthew  1645:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
1.84      albertel 1646:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
1.46      matthew  1647:                     "</tr>\n";
                   1648:             } else {
                   1649:                 $table .= '<tr>'.
                   1650:                     '<td>'.$foilindex.'</td>'.
1.83      albertel 1651:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'name'},'<>&"').'</td>'.
1.65      matthew  1652:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
1.83      albertel 1653:                     '<td>'.&HTML::Entities::encode($Foildata{$foilid}->{'value'},'<>&"').'</td>'.
1.46      matthew  1654:                     "</tr>\n";
                   1655:             }                
                   1656:         } continue {
                   1657:             $foilindex++;
                   1658:         }
                   1659:     } continue {
                   1660:         $conceptindex++;
                   1661:     }
                   1662:     $table .= "</table>\n";
                   1663:     #
                   1664:     # Build option index with color stuff
                   1665:     return ($table,\@Foils,\@Concepts);
                   1666: }
                   1667: 
                   1668: sub build_option_index {
                   1669:     my ($ORdata)= @_;
                   1670:     my $table = "<table>\n";
                   1671:     my $optionindex = 0;
                   1672:     my @Rows;
1.48      matthew  1673:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'_Options'}}) {
1.46      matthew  1674:         push (@Rows,
                   1675:               '<tr>'.
                   1676:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
                   1677:               ('&nbsp;'x4).'</td>'.
1.83      albertel 1678:               '<td>'.&HTML::Entities::encode($option,'<>&"').'</td>'.
1.46      matthew  1679:               "</tr>\n");
                   1680:     }
                   1681:     shift(@Rows); # Throw away 'correct option chosen' color
                   1682:     $table .= join('',reverse(@Rows));
                   1683:     $table .= "</table>\n";
                   1684: }
                   1685: 
1.94    ! matthew  1686: sub build_foil_key {
        !          1687:     my ($foils,$extra_data)= @_;
        !          1688:     if (! defined($extra_data)) { $extra_data = {}; }
        !          1689:     my $table = "<table>\n";
        !          1690:     my $foil_index = 0;
        !          1691:     my @rows;
        !          1692:     foreach my $foil (&mt('correct foil chosen'),@{$foils}) {
        !          1693:         push (@rows,
        !          1694:               '<tr>'.
        !          1695:               '<td bgcolor="'.$plotcolors->[$foil_index++].'">'.
        !          1696:               ('&nbsp;'x4).'</td>'.
        !          1697:               '<td>'.&HTML::Entities::encode($foil,'<>&"').
        !          1698:               ('&nbsp;'x2).$extra_data->{$foil}.'</td>'.
        !          1699:               "</tr>\n");
        !          1700:     }
        !          1701:     shift(@rows); # Throw away 'correct foil chosen' color
        !          1702:     $table .= join('',reverse(@rows));
        !          1703:     $table .= "</table>\n";
        !          1704: }
        !          1705: 
1.33      matthew  1706: #########################################################
                   1707: #########################################################
                   1708: ##
1.46      matthew  1709: ##   Generic Interface Routines
1.33      matthew  1710: ##
                   1711: #########################################################
                   1712: #########################################################
1.23      matthew  1713: sub CreateInterface {
1.28      matthew  1714:     ##
                   1715:     ## Environment variable initialization
1.36      matthew  1716:     if (! exists$ENV{'form.AnalyzeOver'}) {
1.94    ! matthew  1717:         $ENV{'form.AnalyzeOver'} = 'tries';
1.28      matthew  1718:     }
                   1719:     ##
                   1720:     ## Build the menu
1.7       stredwic 1721:     my $Str = '';
1.72      matthew  1722:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
1.74      matthew  1723:         (undef,'Detailed Problem Analysis');
1.23      matthew  1724:     $Str .= '<table cellspacing="5">'."\n";
                   1725:     $Str .= '<tr>';
                   1726:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
                   1727:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
1.31      matthew  1728: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
                   1729:     $Str .= '<td align="center">&nbsp;</td>';
1.23      matthew  1730:     $Str .= '</tr>'."\n";
1.31      matthew  1731:     ##
                   1732:     ## 
1.23      matthew  1733:     $Str .= '<tr><td align="center">'."\n";
                   1734:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1.28      matthew  1735:     $Str .= '</td>';
                   1736:     #
                   1737:     $Str .= '<td align="center">';
1.23      matthew  1738:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1.28      matthew  1739:     $Str .= '</td>';
                   1740:     #
1.31      matthew  1741: #    $Str .= '<td align="center">';
1.23      matthew  1742:     my $only_seq_with_assessments = sub { 
                   1743:         my $s=shift;
                   1744:         if ($s->{'num_assess'} < 1) { 
                   1745:             return 0;
                   1746:         } else { 
                   1747:             return 1;
                   1748:         }
                   1749:     };
1.31      matthew  1750:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
1.23      matthew  1751:                                               $only_seq_with_assessments);
1.36      matthew  1752:     ##
                   1753:     ##
1.28      matthew  1754:     $Str .= '<td>';
1.91      matthew  1755:     ##
                   1756:     my $showprob_checkbox = 
                   1757:         '<input type="checkbox" name="show_prob" value="true" ';
                   1758:     if ($ENV{'form.show_prob'} eq 'true') {
                   1759:         $showprob_checkbox .= 'checked ';
                   1760:     }
                   1761:     $showprob_checkbox.= ' />';
                   1762:     $Str.= '<nobr><label>'.
                   1763:         &mt('Show problem [_1]',$showprob_checkbox).
                   1764:         '</label></nobr><br />';
                   1765:     ##
1.90      matthew  1766:     my $analyze_selector = '<select name="AnalyzeOver" >';
1.94    ! matthew  1767:     $analyze_selector .= '<option value="tries" ';
1.90      matthew  1768:     if (! exists($ENV{'form.AnalyzeOver'}) || 
1.94    ! matthew  1769:         $ENV{'form.AnalyzeOver'} eq 'tries'){
        !          1770:         # Default to tries
1.90      matthew  1771:         $analyze_selector .= ' selected ';
                   1772:     }
                   1773:     $analyze_selector .= '>'.&mt('Tries').'</option>';
1.94    ! matthew  1774:     $analyze_selector .= '<option value="time" ';
        !          1775:     $analyze_selector .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'time');
1.90      matthew  1776:     $analyze_selector .= '>'.&mt('Time').'</option>';
                   1777:     $analyze_selector .= '</select>';
1.91      matthew  1778:     $Str .= '<nobr><label>'.
1.90      matthew  1779:         &mt('Analyze Over [_1] [_2]',
                   1780:             $analyze_selector,
                   1781:             &Apache::loncommon::help_open_topic('Analysis_Analyze_Over')).
1.91      matthew  1782:             '</label></nobr><br />'.$/;
1.90      matthew  1783:     ##
                   1784:     my $numplots_selector = '<select name="NumPlots">';
                   1785:     if (! exists($ENV{'form.NumPlots'}) 
                   1786:         || $ENV{'form.NumPlots'} < 1 
                   1787:         || $ENV{'form.NumPlots'} > 20) {
                   1788:         $ENV{'form.NumPlots'} = 5;
                   1789:     }
                   1790:     foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
                   1791:         $numplots_selector .= '<option value="'.$i.'" ';
                   1792:         if ($ENV{'form.NumPlots'} == $i) { $numplots_selector.=' selected '; }
                   1793:         $numplots_selector .= '>'.$i.'</option>';
                   1794:     }
                   1795:     $numplots_selector .= '</select></nobr><br />';
                   1796:     $Str .= '<nobr><label>'.&mt('Number of Plots [_1]',$numplots_selector).
                   1797:         '</label></nobr>';
                   1798:     ##
                   1799:     $Str .= '<nobr><label>'.&mt('Status: [_1]',
1.85      matthew  1800:                                  '<input type="text" '.
                   1801:                                  'name="stats_status" size="60" value="" />'
                   1802:                                  ).
1.90      matthew  1803:                     '</label></nobr>';
1.28      matthew  1804:     $Str .= '</td>';
1.36      matthew  1805:     ##
                   1806:     ##
1.28      matthew  1807:     $Str .= '</tr>'."\n";
1.23      matthew  1808:     $Str .= '</table>'."\n";
1.42      matthew  1809:     return $Str;
1.54      matthew  1810: }
1.47      matthew  1811: 
                   1812: #########################################################
                   1813: #########################################################
                   1814: ##
                   1815: ##              Misc Option Response functions
                   1816: ##
                   1817: #########################################################
                   1818: #########################################################
                   1819: sub get_time_from_row {
1.42      matthew  1820:     my ($row) = @_;
                   1821:     if (ref($row)) {
1.47      matthew  1822:         return $row->[&Apache::loncoursedata::RD_timestamp()];
1.42      matthew  1823:     } 
                   1824:     return undef;
                   1825: }
                   1826: 
1.47      matthew  1827: sub get_tries_from_row {
1.42      matthew  1828:     my ($row) = @_;
                   1829:     if (ref($row)) {
1.47      matthew  1830:         return $row->[&Apache::loncoursedata::RD_tries()];
1.42      matthew  1831:     }
                   1832:     return undef;
                   1833: }
                   1834: 
1.48      matthew  1835: sub hashify_attempt {
                   1836:     my ($row) = @_;
                   1837:     my %attempt;
1.94    ! matthew  1838:     $attempt{'student'}    = $row->[&Apache::loncoursedata::RD_sname()];
1.48      matthew  1839:     $attempt{'tries'}      = $row->[&Apache::loncoursedata::RD_tries()];
1.94    ! matthew  1840:     $attempt{'submission'} = &Apache::lonnet::unescape($row->[&Apache::loncoursedata::RD_submission()]);
1.48      matthew  1841:     $attempt{'award'}      = $row->[&Apache::loncoursedata::RD_awarddetail()];
                   1842:     $attempt{'timestamp'}  = $row->[&Apache::loncoursedata::RD_timestamp()];
                   1843:     return %attempt;
                   1844: }
                   1845: 
1.46      matthew  1846: sub Process_OR_Row {
1.42      matthew  1847:     my ($row) = @_;
                   1848:     my %RowData;
1.47      matthew  1849:     my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
                   1850:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
                   1851:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
                   1852:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
                   1853:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
                   1854:     my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
1.43      matthew  1855:     return undef if ($award eq 'MISSING_ANSWER');
1.42      matthew  1856:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
                   1857:         $RowData{'_correct'} = 1;
                   1858:     }
                   1859:     $RowData{'_total'} = 1;
                   1860:     my @Foilgrades = split('&',$grading);
                   1861:     my @Foilsubs   = split('&',$submission);
                   1862:     for (my $j=0;$j<=$#Foilgrades;$j++) {
                   1863:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
1.58      matthew  1864:         $foilid = &Apache::lonnet::unescape($foilid);
1.42      matthew  1865:         my (undef,$submission) = split('=',$Foilsubs[$j]);
                   1866:         if ($correct) {
                   1867:             $RowData{$foilid}->{'_correct'}++;
                   1868:         } else {
                   1869:             $submission = &Apache::lonnet::unescape($submission);
                   1870:             $RowData{$foilid}->{$submission}++;
                   1871:         }
                   1872:         $RowData{$foilid}->{'_total'}++;
                   1873:     }
                   1874:     return %RowData;
1.47      matthew  1875: }
                   1876: 
1.23      matthew  1877: 1;
1.1       stredwic 1878: 
                   1879: __END__

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