File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.47: download - view: text, annotated - select for diffs
Thu Oct 30 16:24:36 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Works with new version of loncoursedata.pm.  Radio response analysis is
not fully functional, although you can select those resources.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemanalysis.pm,v 1.47 2003/10/30 16:24:36 matthew Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: package Apache::lonproblemanalysis;
   29: 
   30: use strict;
   31: use Apache::lonnet();
   32: use Apache::loncommon();
   33: use Apache::lonhtmlcommon();
   34: use Apache::loncoursedata();
   35: use Apache::lonstatistics;
   36: use Apache::lonlocal;
   37: use HTML::Entities();
   38: use Time::Local();
   39: use Spreadsheet::WriteExcel();
   40: 
   41: my $plotcolors = ['#33ff00', 
   42:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
   43:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
   44:                   ]; 
   45: 
   46: my @SubmitButtons = ({ name => 'ProblemAnalyis',
   47:                        text => 'Analyze Problem Again' },
   48:                      { name => 'ClearCache',
   49:                        text => 'Clear Caches' },
   50:                      { name => 'updatecaches',
   51:                        text => 'Update Student Data' },
   52:                      { name => 'SelectAnother',
   53:                        text => 'Choose a different resource' },
   54:                      { name => 'ExcelOutput',
   55:                        text => 'Produce Excel Output' });
   56: 
   57: sub render_resource {
   58:     my ($resource) = @_;
   59:     ##
   60:     ## Render the problem
   61:     my $base;
   62:     ($base,undef) = ($resource->{'src'} =~ m|(.*/)[^/]*$|);
   63:     $base = "http://".$ENV{'SERVER_NAME'}.$base;
   64:     my $rendered_problem = 
   65:         &Apache::lonnet::ssi_body($resource->{'src'});
   66:     $rendered_problem =~ s/<\s*form\s*/<nop /g;
   67:     $rendered_problem =~ s|(<\s*/form\s*>)|<\/nop>|g;
   68:     return '<table bgcolor="ffffff"><tr><td>'.
   69:         '<base href="'.$base.'" />'.
   70:         $rendered_problem.
   71:         '</td></tr></table>';
   72: }
   73: 
   74: sub BuildProblemAnalysisPage {
   75:     my ($r,$c)=@_;
   76:     $r->print('<h2>'.&mt('Option Response Problem Analysis').'</h2>');
   77:     $r->print(&CreateInterface());
   78:     #
   79:     my @Students = @Apache::lonstatistics::Students;
   80:     #
   81:     if (@Students < 1) {
   82:         $r->print('<h2>There are no students in the sections selected</h2>');
   83:     }
   84:     #
   85:     &Apache::loncoursedata::clear_internal_caches();
   86:     if (exists($ENV{'form.ClearCache'}) || 
   87:         exists($ENV{'form.updatecaches'}) ||
   88:         (exists($ENV{'form.firstanalysis'}) &&
   89:          $ENV{'form.firstanalysis'} ne 'no')) {
   90:         &Apache::lonstatistics::Gather_Full_Student_Data($r);
   91:     }
   92:     if (! exists($ENV{'form.firstanalysis'})) {
   93:         $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
   94:     } else {
   95:         $r->print('<input type="hidden" name="firstanalysis" value="no" />');
   96:     }
   97:     $r->rflush();
   98:     #
   99:     if (exists($ENV{'form.problemchoice'}) && 
  100:         ! exists($ENV{'form.SelectAnother'})) {
  101:         foreach my $button (@SubmitButtons) {
  102:             $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
  103:                       'value="'.&mt($button->{'text'}).'" />');
  104:             $r->print('&nbsp;'x5);
  105:         }
  106:         $r->print('<input type="hidden" name="problemchoice" value="'.
  107:                   $ENV{'form.problemchoice'}.'" />');
  108:         #
  109:         $r->print('<hr />');
  110:         #
  111:         my ($symb,$part,$resid,$resptype) = &get_problem_symb(
  112:                   &Apache::lonnet::unescape($ENV{'form.problemchoice'}));
  113:         $r->rflush();
  114:         #
  115:         my $resource = &get_resource_from_symb($symb);
  116:         if (! defined($resource) || ! defined($resptype)) {
  117:             $r->print('resource is undefined');
  118:         } else {
  119:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  120:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  121:             $r->print(&render_resource($resource));
  122:             $r->rflush();
  123:             if ($resptype eq 'option') {
  124:                 my %Data = &get_OR_problem_data($resource->{'src'});
  125:                 my $ProblemData = $Data{$part.'.'.$resid};
  126:                 &OptionResponseAnalysis($r,$resource,$resid,$ProblemData,
  127:                                         \@Students);
  128:             } elsif ($resptype eq 'radiobutton') {
  129:                 $r->print('<h2>This analysis is not supported</h2>');
  130:                 my %Data = &get_Radio_problem_data($resource->{'src'});
  131:                 my $ProblemData = $Data{$part.'.'.$resid};
  132:                 &RadioResponseAnalysis($r,$resource,$resid,$ProblemData,
  133:                                        \@Students);
  134:             } else {
  135:                 $r->print('<h2>This analysis is not supported</h2>');
  136:             }
  137:         }
  138:         $r->print('<hr />');
  139:     } else {
  140:         $r->print('<input type="submit" name="ProblemAnalysis" value="'.
  141:                   &mt('Analyze Problem').'" />');
  142:         $r->print('&nbsp;'x5);
  143:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
  144:         $r->print(&ProblemSelector());
  145:     }
  146: }
  147: 
  148: 
  149: #########################################################
  150: #########################################################
  151: ##
  152: ##      Radio Response Routines
  153: ##
  154: #########################################################
  155: #########################################################
  156: sub RadioResponseAnalysis {
  157:     my ($r,$resource,$resid,$ProblemData,$Students) = @_;
  158:     my $PerformanceData = 
  159:         &Apache::loncoursedata::get_response_data
  160:         ($Students,$resource->{'symb'},$resid);
  161:     if (! ref($PerformanceData)) {
  162:         $r->print('<h2>There is no submission data for this resource</h2>');
  163:         return;
  164:     }
  165: #    foreach my $row (@$PerformanceData) {
  166: #        &Apache::lonnet::logthis('row = '.join(',',@$row));
  167: #    }
  168:     return;
  169: }
  170: #########################################################
  171: #########################################################
  172: ##
  173: ##      Option Response Routines
  174: ##
  175: #########################################################
  176: #########################################################
  177: sub OptionResponseAnalysis {
  178:     my ($r,$resource,$resid,$ProblemData,$Students) = @_;
  179:     my $PerformanceData = 
  180:         &Apache::loncoursedata::get_response_data
  181:         ($Students,$resource->{'symb'},$resid);
  182:     if (! defined($PerformanceData) || 
  183:         ref($PerformanceData) ne 'ARRAY' ) {
  184:         $r->print('<h2>'.
  185:                   &mt('There is no student data for this problem.').
  186:                   '</h2>');
  187:     }  else {
  188:         $r->rflush();
  189:         if (exists($ENV{'form.ExcelOutput'})) {
  190:             my $result = &prepare_optionresponse_excel_sheet($r,$resource,
  191:                                                              $PerformanceData,
  192:                                                              $ProblemData);
  193:             $r->print($result);
  194:             $r->rflush();
  195:         } else {
  196:             if ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  197:                 my $analysis_html = &tries_analysis($r,
  198:                                                     $PerformanceData,
  199:                                                     $ProblemData);
  200:                 $r->print($analysis_html);
  201:                 $r->rflush();
  202:             } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  203:                 my $analysis_html = &time_analysis($PerformanceData,
  204:                                                    $ProblemData);
  205:                 $r->print($analysis_html);
  206:                 $r->rflush();
  207:             } else {
  208:                 $r->print('<h2>'.
  209:                           &mt('The analysis you have selected is '.
  210:                               'not supported at this time').
  211:                           '</h2>');
  212:             }
  213:         }
  214:     }
  215: }
  216: 
  217: #########################################################
  218: #
  219: #       Option Response:  Tries Analysis
  220: #
  221: #########################################################
  222: sub tries_analysis {
  223:     my ($r,$PerformanceData,$ORdata) = @_;
  224:     my $mintries = 1;
  225:     my $maxtries = $ENV{'form.NumPlots'};
  226:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  227:     if ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils')) {
  228:         $table = '<h3>'.
  229:             &mt('Not enough data for concept analysis.  '.
  230:                 'Performing Foil Analysis').
  231:             '</h3>'.$table;
  232:         $ENV{'form.AnalyzeAs'} = 'Foils';
  233:     }
  234:     my %ResponseData = &analyze_option_data_by_tries($r,$PerformanceData,
  235:                                                      $mintries,$maxtries);
  236:     my $analysis = '';
  237:     if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  238:         $analysis = &Tries_Foil_Analysis($mintries,$maxtries,$Foils,
  239:                                          \%ResponseData,$ORdata);
  240:     } else {
  241:         $analysis = &Tries_Concept_Analysis($mintries,$maxtries,
  242:                                             $Concepts,\%ResponseData,$ORdata);
  243:     }
  244:     $table .= $analysis;
  245:     return $table;
  246: }
  247: 
  248: sub Tries_Foil_Analysis {
  249:     my ($mintries,$maxtries,$Foils,$respdat,$ORdata) = @_;
  250:     my %ResponseData = %$respdat;
  251:     #
  252:     # Compute the data neccessary to make the plots
  253:     my @PlotData; 
  254:     foreach my $foilid (@$Foils) {
  255:         for (my $i=$mintries;$i<=$maxtries;$i++) {
  256:             if ($ResponseData{$foilid}->[$i]->{'_total'} == 0) {
  257:                 push(@{$PlotData[$i]->{'_correct'}},0);
  258:             } else {
  259:                 push(@{$PlotData[$i]->{'_correct'}},
  260:                      100*$ResponseData{$foilid}->[$i]->{'_correct'}/
  261:                      $ResponseData{$foilid}->[$i]->{'_total'});
  262:             }
  263:             foreach my $option (@{$ORdata->{'Options'}}) {
  264:                 push(@{$PlotData[$i]->{'_total'}},
  265:                      $ResponseData{$foilid}->[$i]->{'_total'});
  266:                 if ($ResponseData{$foilid}->[$i]->{'_total'} == 0) {
  267:                     push (@{$PlotData[$i]->{$option}},0);
  268:                 } else {
  269:                     if ($ResponseData{$foilid}->[$i]->{'_total'} ==
  270:                         $ResponseData{$foilid}->[$i]->{'_correct'}) {
  271:                         push(@{$PlotData[$i]->{$option}},0);
  272:                     } else {
  273:                         push (@{$PlotData[$i]->{$option}},
  274:                               100 * $ResponseData{$foilid}->[$i]->{$option} / 
  275:                               ($ResponseData{$foilid}->[$i]->{'_total'} - 
  276:                                $ResponseData{$foilid}->[$i]->{'_correct'}));
  277:                     }
  278:                 }
  279:             }
  280:         }
  281:     }
  282:     # 
  283:     # Build a table for the plots
  284:     my $analysis_html = "<table>\n";
  285:     my $foilkey = &build_option_index($ORdata);
  286:     for (my $i=$mintries;$i<=$maxtries;$i++) {
  287:         my $count = $ResponseData{'_total'}->[$i];
  288:         if ($count == 0) {
  289:             $count = 'no submissions';
  290:         } elsif ($count == 1) {
  291:             $count = '1 submission';
  292:         } else {
  293:             $count = $count.' submissions';
  294:         }
  295:         my $title = 'Attempt '.$i.', '.$count;
  296:         my @Datasets;
  297:         foreach my $option ('_correct',@{$ORdata->{'Options'}}) {
  298:             next if (! exists($PlotData[$i]->{$option}));
  299:             push(@Datasets,$PlotData[$i]->{$option});
  300:         }
  301:         my $correctgraph = &Apache::loncommon::DrawBarGraph
  302:             ($title,'Foil Number','Percent Correct',
  303:              100,$plotcolors,$Datasets[0]);
  304:         $analysis_html.= '<tr><td>'.$correctgraph.'</td>';
  305:         ##
  306:         ##
  307:         for (my $i=0; $i< scalar(@{$Datasets[0]});$i++) {
  308:             $Datasets[0]->[$i]=0;
  309:         }
  310:         $count = $ResponseData{'_total'}->[$i]-$ResponseData{'_correct'}->[$i];
  311:         if ($count == 0) {
  312:             $count = 'no submissions';
  313:         } elsif ($count == 1) {
  314:             $count = '1 submission';
  315:         } else {
  316:             $count = $count.' submissions';
  317:         }
  318:         $title = 'Attempt '.$i.', '.$count;
  319:         my $incorrectgraph = &Apache::loncommon::DrawBarGraph
  320:             ($title,'Foil Number','% Option Chosen Incorrectly',
  321:              100,$plotcolors,@Datasets);
  322:         $analysis_html.= '<td>'.$incorrectgraph.'</td>';
  323:         $analysis_html.= '<td>'.$foilkey."<td></tr>\n";
  324:     }
  325:     $analysis_html .= "</table>\n";
  326:     return $analysis_html;
  327: }
  328: 
  329: sub Tries_Concept_Analysis {
  330:     my ($mintries,$maxtries,$Concepts,$respdat,$ORdata) = @_;
  331:     my %ResponseData = %$respdat;
  332:     my $analysis_html = "<table>\n";
  333:     #
  334:     # Compute the data neccessary to make the plots
  335:     my @PlotData;
  336:     # Concept analysis
  337:     #
  338:     # Note: we do not bother with characterizing the students incorrect
  339:     # answers at the concept level because an incorrect answer for one foil
  340:     # may be a correct answer for another foil.
  341:     my %ConceptData;
  342:     foreach my $concept (@{$Concepts}) {
  343:         for (my $i=$mintries;$i<=$maxtries;$i++) {
  344:             #
  345:             # Gather the per-attempt data
  346:             my $cdata = $ConceptData{$concept}->[$i];
  347:             foreach my $foilid (@{$concept->{'foils'}}) {
  348:                 $cdata->{'_correct'} += 
  349:                     $ResponseData{$foilid}->[$i]->{'_correct'};
  350:                 $cdata->{'_total'}   += 
  351:                     $ResponseData{$foilid}->[$i]->{'_total'};
  352:             }
  353:             push (@{$PlotData[$i]->{'_total'}},$cdata->{'_total'});
  354:             if ($cdata->{'_total'} == 0) {
  355:                 push (@{$PlotData[$i]->{'_correct'}},0);
  356:             } else {
  357:                 push (@{$PlotData[$i]->{'_correct'}},
  358:                       100*$cdata->{'_correct'}/$cdata->{'_total'});
  359:                 }
  360:         }
  361:     }
  362:     # Build a table for the plots
  363:     for (my $i=$mintries;$i<=$maxtries;$i++) {
  364:         my $minstu = $PlotData[$i]->{'_total'}->[0];
  365:         my $maxstu = $PlotData[$i]->{'_total'}->[0];
  366:         foreach my $count (@{$PlotData[$i]->{'_total'}}) {
  367:             if ($minstu > $count) {
  368:                 $minstu = $count;
  369:             }
  370:             if ($maxstu < $count) {
  371:                 $maxstu = $count;
  372:             }
  373:         }
  374:         $maxstu = 0 if (! defined($maxstu));
  375:         $minstu = 0 if (! defined($minstu));
  376:         my $title;
  377:         my $count = $ResponseData{'_total'}->[$i];
  378:         if ($count == 0) {
  379:             $count = 'no submissions';
  380:         } elsif ($count == 1) {
  381:             $count = '1 submission';
  382:         } else {
  383:             $count = $count.' submissions';
  384:         }
  385:         $title = 'Attempt '.$i.', '.$count;
  386:         my $graphlink = &Apache::loncommon::DrawBarGraph
  387:             ($title,'Concept Number','Percent Correct',
  388:              100,$plotcolors,$PlotData[$i]->{'_correct'});
  389:         $analysis_html.= '<tr><td>'.$graphlink."</td></tr>\n";
  390:     }
  391:     $analysis_html .= "</table>\n";
  392:     return $analysis_html;
  393: }
  394: 
  395: sub analyze_option_data_by_tries {
  396:     my ($r,$PerformanceData,$mintries,$maxtries) = @_;
  397:     my %Trydata;
  398:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
  399:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
  400:     foreach my $row (@$PerformanceData) {
  401:         next if (! defined($row));
  402:         my $tries = &get_tries_from_row($row);
  403:         my %Row   = &Process_OR_Row($row);
  404:         next if (! %Row);
  405:         while (my ($foilid,$href) = each(%Row)) {
  406:             if (! ref($href)) { 
  407:                 $Trydata{$foilid}->[$tries] += $href;
  408:                 next;
  409:             }
  410:             while (my ($option,$value) = each(%$href)) {
  411:                 $Trydata{$foilid}->[$tries]->{$option}+=$value;
  412:             }
  413:         }
  414:     }
  415:     return %Trydata;
  416: }
  417: 
  418: #########################################################
  419: #
  420: #     Option Response: Time Analysis
  421: #
  422: #########################################################
  423: sub time_analysis {
  424:     my ($PerformanceData,$ORdata) = @_;
  425:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  426:     if ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils')) {
  427:         $table = '<h3>'.
  428:             &mt('Not enough data for concept analysis.  '.
  429:                 'Performing Foil Analysis').
  430:             '</h3>'.$table;
  431:         $ENV{'form.AnalyzeAs'} = 'Foils';
  432:     }
  433:     my $num_plots = $ENV{'form.NumPlots'};
  434:     my $num_data = scalar(@$PerformanceData)-1;
  435:     my $percent = sprintf('%2f',100/$num_plots);
  436:     #
  437:     $table .= "<table>\n";
  438:     for (my $i=0;$i<$num_plots;$i++) {
  439:         ##
  440:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  441:             ('startdate_'.$i);
  442:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  443:             ('enddate_'.$i);
  444:         if (! defined($starttime) || ! defined($endtime)) {
  445:             my $sec_in_day = 86400;
  446:             my $last_sub_time = &get_time_from_row($PerformanceData->[-1]);
  447:             my ($sday,$smon,$syear);
  448:             (undef,undef,undef,$sday,$smon,$syear) = 
  449:                 localtime($last_sub_time - $sec_in_day*$i);
  450:             $starttime = &Time::Local::timelocal(0,0,0,$sday,$smon,$syear);
  451:             $endtime = $starttime + $sec_in_day;
  452:             if ($i == ($num_plots -1 )) {
  453:                 $starttime = &get_time_from_row($PerformanceData->[0]);
  454:             }
  455:         }
  456:         my $startdateform = &Apache::lonhtmlcommon::date_setter
  457:             ('Statistics','startdate_'.$i,$starttime);
  458:         my $enddateform = &Apache::lonhtmlcommon::date_setter
  459:             ('Statistics','enddate_'.$i,$endtime);
  460:         #
  461:         my $begin_index;
  462:         my $end_index;
  463:         my $j;
  464:         while (++$j < scalar(@$PerformanceData)) {
  465:             last if (&get_time_from_row($PerformanceData->[$j]) 
  466:                                                               > $starttime);
  467:         }
  468:         $begin_index = $j;
  469:         while (++$j < scalar(@$PerformanceData)) {
  470:             last if (&get_time_from_row($PerformanceData->[$j]) > $endtime);
  471:         }
  472:         $end_index = $j;
  473:         ##
  474:         my $interval = {
  475:             begin_index => $begin_index,
  476:             end_index   => $end_index,
  477:             startdateform => $startdateform,
  478:             enddateform   => $enddateform,
  479:         };
  480:         if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  481:             $table .= &Foil_Time_Analysis($PerformanceData,$ORdata,$Foils,
  482:                                           $interval);
  483:         } else {
  484:             $table .= &Concept_Time_Analysis($PerformanceData,$ORdata,
  485:                                              $Concepts,$interval);
  486:         }
  487:     }
  488:     #
  489:     return $table;
  490: }
  491: 
  492: sub Foil_Time_Analysis {
  493:     my ($PerformanceData,$ORdata,$Foils,$interval) = @_;
  494:     my $analysis_html;
  495:     my $foilkey = &build_option_index($ORdata);
  496:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  497:                                     $interval->{'end_index'});
  498:     my %TimeData;
  499:     #
  500:     # Compute the number getting the foils correct or incorrects
  501:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  502:         my $row = $PerformanceData->[$j];
  503:         next if (! defined($row));
  504:         my %Row = &Process_OR_Row($row);
  505:         while (my ($foilid,$href) = each(%Row)) {
  506:             if (! ref($href)) {
  507:                 $TimeData{$foilid} += $href;
  508:                 next;
  509:             }
  510:             while (my ($option,$value) = each(%$href)) {
  511:                 $TimeData{$foilid}->{$option}+=$value;
  512:             }
  513:         }
  514:     }
  515:     my @Plotdata;
  516:     foreach my $foil (@$Foils) {
  517:         my $total = $TimeData{$foil}->{'_total'};
  518:         if ($total == 0) {
  519:             push(@{$Plotdata[0]},0);
  520:         } else {
  521:             push(@{$Plotdata[0]},
  522:                  100 * $TimeData{$foil}->{'_correct'} / $total);
  523:         }
  524:         my $total_incorrect = $total - $TimeData{$foil}->{'_correct'};
  525:         my $optionidx = 1;
  526:         foreach my $option (@{$ORdata->{'Options'}}) {
  527:             if ($total_incorrect == 0) {
  528:                 push(@{$Plotdata[$optionidx]},0);
  529:             } else {
  530:                 push(@{$Plotdata[$optionidx]},
  531:                      100 * $TimeData{$foil}->{$option} / $total_incorrect);
  532:             }
  533:         } continue {
  534:             $optionidx++;
  535:         }
  536:     }
  537:     #
  538:     # Create the plot
  539:     my $count = $end_index-$begin_index;
  540:     my $title;
  541:     if ($count == 0) {
  542:         $title = 'no submissions';
  543:     } elsif ($count == 1) {
  544:         $title = 'one submission';
  545:     } else {
  546:         $title = $count.' submissions';
  547:     }
  548:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
  549:                                                        'Foil Number',
  550:                                                        'Percent Correct',
  551:                                                        100,
  552:                                                        $plotcolors,
  553:                                                        $Plotdata[0]);
  554:     for (my $j=0; $j< scalar(@{$Plotdata[0]});$j++) {
  555:         $Plotdata[0]->[$j]=0;
  556:     }
  557:     $count = $end_index-$begin_index-$TimeData{'_correct'};
  558:     if ($count == 0) {
  559:         $title = 'no submissions';
  560:     } elsif ($count == 1) {
  561:         $title = 'one submission';
  562:     } else {
  563:         $title = $count.' submissions';
  564:     }
  565:     my $incorrectplot = &Apache::loncommon::DrawBarGraph($title,
  566:                                                  'Foil Number',
  567:                                                  'Incorrect Option Choice',
  568:                                                  100,
  569:                                                  $plotcolors,
  570:                                                  @Plotdata);        
  571:     $analysis_html.='<tr>'.
  572:         '<td>'.$correctplot.'</td>'.
  573:         '<td>'.$incorrectplot.'</td>'.
  574:         '<td align="left" valign="top">'.$foilkey.'</td>'."</tr>\n";
  575:     $analysis_html.= '<tr>'.'<td colspan="3">'.
  576:         '<b>Start Time</b>:'.
  577:         ' &nbsp;'.$interval->{'startdateform'}.'<br />'.
  578:         '<b>End Time</b>&nbsp;&nbsp;: '.
  579:         '&nbsp;'.$interval->{'enddateform'}.'<br />'.
  580: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.
  581: #        ("&nbsp;"x3).$interval->{'titleform'}.
  582:         '</td>'.
  583:         "</tr>\n";
  584:     return $analysis_html;
  585: }
  586: 
  587: sub Concept_Time_Analysis {
  588:     my ($PerformanceData,$ORdata,$Concepts,$interval) = @_;
  589:     my $analysis_html;
  590:     ##
  591:     ## Determine starttime, endtime, startindex, endindex
  592:     my ($begin_index,$end_index) = ($interval->{'begin_index'},
  593:                                     $interval->{'end_index'});
  594:     my %TimeData;
  595:     #
  596:     # Compute the number getting the foils correct or incorrects
  597:     for (my $j=$begin_index;$j<=$end_index;$j++) {
  598:         my $row = $PerformanceData->[$j];
  599:         next if (! defined($row));
  600:         my %Row = &Process_OR_Row($row);
  601:         while (my ($foilid,$href) = each(%Row)) {
  602:             if (! ref($href)) {
  603:                 $TimeData{$foilid} += $href;
  604:                 next;
  605:             }
  606:             while (my ($option,$value) = each(%$href)) {
  607:                 $TimeData{$foilid}->{$option}+=$value;
  608:             }
  609:         }
  610:     }
  611:     #
  612:     # Put the data in plottable form
  613:     my @Plotdata;
  614:     foreach my $concept (@$Concepts) {
  615:         my ($total,$correct);
  616:         foreach my $foil (@{$concept->{'foils'}}) {
  617:             $total += $TimeData{$foil}->{'_total'};
  618:             $correct += $TimeData{$foil}->{'_correct'};
  619:         }
  620:         if ($total == 0) {
  621:             push(@Plotdata,0);
  622:         } else {
  623:             push(@Plotdata,100 * $correct / $total);
  624:         }
  625:     }
  626:     #
  627:     # Create the plot
  628:     my $title = ($end_index - $begin_index).' submissions';
  629:     my $correctplot = &Apache::loncommon::DrawBarGraph($title,
  630:                                                     'Concept Number',
  631:                                                     'Percent Correct',
  632:                                                     100,
  633:                                                     $plotcolors,
  634:                                                     \@Plotdata);
  635:     $analysis_html.='<tr>'.
  636:         '<td>'.$correctplot.'</td>'.
  637:         '<td align="left" valign="top">'.
  638:         '<b>Start Time</b>: &nbsp;'.$interval->{'startdateform'}.'<br />'.
  639:         '<b>End Time</b>&nbsp;&nbsp;: '.
  640:            '&nbsp;'.$interval->{'enddateform'}.'<br />'.
  641: #        '<b>Plot Title</b>&nbsp;&nbsp;:'.("&nbsp;"x3).
  642: #            $interval->{'titleform'}.
  643:         '</td>'.
  644:         "</tr>\n";
  645:     return $analysis_html;
  646: }
  647: 
  648: #########################################################
  649: #########################################################
  650: ##
  651: ##             Excel output 
  652: ##
  653: #########################################################
  654: #########################################################
  655: sub prepare_optionresponse_excel_sheet {
  656:     my ($r,$resource,$PerformanceData,$ORdata) = @_;
  657:     my $response = '';
  658:     my (undef,$Foils,$Concepts) = &build_foil_index($ORdata);
  659:     #
  660:     # Create excel worksheet
  661:     my $filename = '/prtspool/'.
  662:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  663:         time.'_'.rand(1000000000).'.xls';
  664:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  665:     if (! defined($workbook)) {
  666:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  667:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
  668:                             "This error has been logged.  ".
  669:                             "Please alert your LON-CAPA administrator").
  670:                   '</p>');
  671:         return undef;
  672:     }
  673:     #
  674:     $workbook->set_tempdir('/home/httpd/perl/tmp');
  675:     #
  676:     # Define some potentially useful formats
  677:     my $format;
  678:     $format->{'header'} = $workbook->add_format(bold      => 1, 
  679:                                                 bottom    => 1,
  680:                                                 align     => 'center');
  681:     $format->{'bold'} = $workbook->add_format(bold=>1);
  682:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
  683:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
  684:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
  685:     $format->{'date'} = $workbook->add_format(num_format=>
  686:                                               'mmm d yyyy hh:mm AM/PM');
  687:     #
  688:     # Create and populate main worksheets
  689:     my $problem_data_sheet  = $workbook->addworksheet('Problem Data');
  690:     my $student_data_sheet  = $workbook->addworksheet('Student Data');
  691:     my $response_data_sheet = $workbook->addworksheet('Response Data');
  692:     foreach my $sheet ($problem_data_sheet,$student_data_sheet,
  693:                        $response_data_sheet) {
  694:         $sheet->write(0,0,$resource->{'title'},$format->{'h2'});
  695:         $sheet->write(1,0,$resource->{'src'},$format->{'h3'});
  696:     }
  697:     #
  698:     my $result;
  699:     $result = &build_problem_data_worksheet($problem_data_sheet,$format,
  700:                                             $Concepts,$ORdata);
  701:     if ($result ne 'okay') {
  702:         # Do something useful
  703:     }
  704:     $result = &build_student_data_worksheet($student_data_sheet,$format);
  705:     if ($result ne 'okay') {
  706:         # Do something useful
  707:     }
  708:     $result = &build_response_data_worksheet($response_data_sheet,$format,
  709:                                              $PerformanceData,$Foils,
  710:                                              $ORdata);
  711:     if ($result ne 'okay') {
  712:         # Do something useful
  713:     }
  714:     $response_data_sheet->activate();
  715:     #
  716:     # Close the excel file
  717:     $workbook->close();
  718:     #
  719:     # Write a link to allow them to download it
  720:     $result .= '<h2>'.&mt('Excel Raw Data Output').'</h2>'.
  721:               '<p><a href="'.$filename.'">'.
  722:               &mt('Your Excel spreadsheet.').
  723:               '</a></p>'."\n";
  724:     return $result;
  725: }
  726: 
  727: sub build_problem_data_worksheet {
  728:     my ($worksheet,$format,$Concepts,$ORdata) = @_;
  729:     my $rows_output = 3;
  730:     my $cols_output = 0;
  731:     $worksheet->write($rows_output++,0,'Problem Structure',$format->{'h3'});
  732:     ##
  733:     ##
  734:     my @Headers;
  735:     if (@$Concepts > 1) {
  736:         @Headers = ("Concept\nNumber",'Concept',"Foil\nNumber",
  737:                     'Foil Name','Foil Text','Correct value');
  738:     } else {
  739:         @Headers = ('Foil Number','FoilName','Foil Text','Correct value');
  740:     }
  741:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
  742:     my %Foildata = %{$ORdata->{'Foils'}};
  743:     my $conceptindex = 1;
  744:     my $foilindex = 1;
  745:     foreach my $concept (@$Concepts) {
  746:         my @FoilsInConcept = @{$concept->{'foils'}};
  747:         my $firstfoil = shift(@FoilsInConcept);
  748:         if (@$Concepts > 1) {
  749:             $worksheet->write_row($rows_output++,0,
  750:                                   [$conceptindex,
  751:                                    $concept->{'name'},
  752:                                    $foilindex++,
  753:                                    $Foildata{$firstfoil}->{'name'},
  754:                                    $Foildata{$firstfoil}->{'text'},
  755:                                    $Foildata{$firstfoil}->{'value'},]);
  756:         } else {
  757:             $worksheet->write_row($rows_output++,0,
  758:                                   [ $foilindex++,
  759:                                     $Foildata{$firstfoil}->{'name'},
  760:                                     $Foildata{$firstfoil}->{'text'},
  761:                                     $Foildata{$firstfoil}->{'value'},]);
  762:         }
  763:         foreach my $foilid (@FoilsInConcept) {
  764:             if (@$Concepts > 1) {
  765:                 $worksheet->write_row($rows_output++,0,
  766:                                       ['',
  767:                                        '',
  768:                                        $foilindex,
  769:                                        $Foildata{$foilid}->{'name'},
  770:                                        $Foildata{$foilid}->{'text'},
  771:                                        $Foildata{$foilid}->{'value'},]);
  772:             } else {
  773:                 $worksheet->write_row($rows_output++,0,                
  774:                                       [$foilindex,
  775:                                        $Foildata{$foilid}->{'name'},
  776:                                        $Foildata{$foilid}->{'text'},
  777:                                        $Foildata{$foilid}->{'value'},]);
  778:             }                
  779:         } continue {
  780:             $foilindex++;
  781:         }
  782:     } continue {
  783:         $conceptindex++;
  784:     }
  785:     $rows_output++;
  786:     $rows_output++;
  787:     ##
  788:     ## Option data output
  789:     $worksheet->write($rows_output++,0,'Options',$format->{'header'});
  790:     foreach my $string (@{$ORdata->{'Options'}}) {
  791:         $worksheet->write($rows_output++,0,$string);
  792:     }
  793:     return 'okay';
  794: }
  795: 
  796: sub build_student_data_worksheet {
  797:     my ($worksheet,$format) = @_;
  798:     my $rows_output = 3;
  799:     my $cols_output = 0;
  800:     $worksheet->write($rows_output++,0,'Student Data',$format->{'h3'});
  801:     my @Headers = ('full name','username','domain','section',
  802:                    "student\nnumber",'identifier');
  803:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
  804:     my @Students = @Apache::lonstatistics::Students;
  805:     my $studentrows = &Apache::loncoursedata::get_student_data(\@Students);
  806:     my %ids;
  807:     foreach my $row (@$studentrows) {
  808:         my ($mysqlid,$student) = @$row;
  809:         $ids{$student}=$mysqlid;
  810:     }
  811:     foreach my $student (@Students) {
  812:         my $name_domain = $student->{'username'}.':'.$student->{'domain'};
  813:         $worksheet->write_row($rows_output++,0,
  814:                           [$student->{'fullname'},
  815:                            $student->{'username'},$student->{'domain'},
  816:                            $student->{'section'},$student->{'id'},
  817:                            $ids{$name_domain}]);
  818:     }
  819:     return;
  820: }
  821: 
  822: sub build_response_data_worksheet {
  823:     my ($worksheet,$format,$PerformanceData,$Foils,$ORdata)=@_;
  824:     my $rows_output = 3;
  825:     my $cols_output = 0;
  826:     $worksheet->write($rows_output++,0,'Response Data',$format->{'h3'});
  827:     $worksheet->set_column(1,1,20);
  828:     $worksheet->set_column(2,2,13);
  829:     my @Headers = ('identifier','time','award detail','attempt');
  830:     foreach my $foil (@$Foils) {
  831:         push (@Headers,$foil.' submission');
  832:         push (@Headers,$foil.' grading');
  833:     }
  834:     $worksheet->write_row($rows_output++,0,\@Headers,$format->{'header'});
  835:     #
  836:     foreach my $row (@$PerformanceData) {
  837:         next if (! defined($row));
  838:         my ($student,$award,$grading,$submission,$time,$tries) = @$row;
  839:         my @Foilgrades = split('&',$grading);
  840:         my @Foilsubs   = split('&',$submission);
  841:         my %ResponseData;
  842:         for (my $j=0;$j<=$#Foilgrades;$j++) {
  843:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
  844:             my (undef,$submission) = split('=',$Foilsubs[$j]);
  845:             $submission = &Apache::lonnet::unescape($submission);
  846:             $ResponseData{$foilid.' submission'}=$submission;
  847:             $ResponseData{$foilid.' award'}=$correct;
  848:         }
  849:         $worksheet->write($rows_output,$cols_output++,$student);
  850:         $worksheet->write($rows_output,$cols_output++,
  851:                           &calc_serial($time),$format->{'date'});
  852:         $worksheet->write($rows_output,$cols_output++,$award);
  853:         $worksheet->write($rows_output,$cols_output++,$tries);
  854:         foreach my $foilid (@$Foils) {
  855:             $worksheet->write($rows_output,$cols_output++,
  856:                               $ResponseData{$foilid.' submission'});
  857:             $worksheet->write($rows_output,$cols_output++,
  858:                               $ResponseData{$foilid.' award'});
  859:         }
  860:         $rows_output++;
  861:         $cols_output = 0;
  862:     }
  863:     return;
  864: }
  865: 
  866: 
  867: ##
  868: ## The following is copied from datecalc1.pl, part of the 
  869: ## Spreadsheet::WriteExcel CPAN module.
  870: ##
  871: ##
  872: ######################################################################
  873: #
  874: # Demonstration of writing date/time cells to Excel spreadsheets,
  875: # using UNIX/Perl time as source of date/time.
  876: #
  877: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
  878: #
  879: ######################################################################
  880: #
  881: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
  882: # measured in seconds.
  883: #
  884: # An Excel file can use exactly one of two different date/time systems.
  885: # In these systems, a floating point number represents the number of days
  886: # (and fractional parts of the day) since a start point. The floating point
  887: # number is referred to as a 'serial'.
  888: # The two systems ('1900' and '1904') use different starting points:
  889: #  '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
  890: #          a leap year - see:
  891: #            http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
  892: #          for the excuse^H^H^H^H^H^Hreason.
  893: #  '1904'; '1.00' is 2 Jan 1904.
  894: #
  895: # The '1904' system is the default for Apple Macs. Windows versions of
  896: # Excel have the option to use the '1904' system.
  897: #
  898: # Note that Visual Basic's "DateSerial" function does NOT erroneously
  899: # regard 1900 as a leap year, and thus its serials do not agree with
  900: # the 1900 serials of Excel for dates before 1 Mar 1900.
  901: #
  902: # Note that StarOffice (at least at version 5.2) does NOT erroneously
  903: # regard 1900 as a leap year, and thus its serials do not agree with
  904: # the 1900 serials of Excel for dates before 1 Mar 1900.
  905: #
  906: ######################################################################
  907: #
  908: # Calculation description
  909: # =======================
  910: #
  911: # 1900 system
  912: # -----------
  913: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
  914: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
  915: # were leap years with an extra day.
  916: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
  917: # 1 Jan 1970.
  918: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
  919: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
  920: #
  921: # 1904 system
  922: # -----------
  923: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
  924: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
  925: # were leap years with an extra day.
  926: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
  927: # 1 Jan 1970.
  928: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
  929: #
  930: ######################################################################
  931: #
  932: # Copyright (c) 2000, Andrew Benham.
  933: # This program is free software. It may be used, redistributed and/or
  934: # modified under the same terms as Perl itself.
  935: #
  936: # Andrew Benham, adsb@bigfoot.com
  937: # London, United Kingdom
  938: # 11 Nov 2000
  939: #
  940: ######################################################################
  941: 
  942: # Use 1900 date system on all platforms other than Apple Mac (for which
  943: # use 1904 date system).
  944: my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
  945: 
  946: #-----------------------------------------------------------
  947: # calc_serial()
  948: #
  949: # Called with (up to) 2 parameters.
  950: #   1.  Unix timestamp.  If omitted, uses current time.
  951: #   2.  GMT flag. Set to '1' to return serial in GMT.
  952: #       If omitted, returns serial in appropriate timezone.
  953: #
  954: # Returns date/time serial according to $DATE_SYSTEM selected
  955: #-----------------------------------------------------------
  956: sub calc_serial {
  957:         my $time = (defined $_[0]) ? $_[0] : time();
  958:         my $gmtflag = (defined $_[1]) ? $_[1] : 0;
  959: 
  960:         # Divide timestamp by number of seconds in a day.
  961:         # This gives a date serial with '0' on 1 Jan 1970.
  962:         my $serial = $time / 86400;
  963: 
  964:         # Adjust the date serial by the offset appropriate to the
  965:         # currently selected system (1900/1904).
  966:         if ($DATE_SYSTEM == 0) {        # use 1900 system
  967:                 $serial += 25569;
  968:         } else {                        # use 1904 system
  969:                 $serial += 24107;
  970:         }
  971: 
  972:         unless ($gmtflag) {
  973:                 # Now have a 'raw' serial with the right offset. But this
  974:                 # gives a serial in GMT, which is false unless the timezone
  975:                 # is GMT. We need to adjust the serial by the appropriate
  976:                 # timezone offset.
  977:                 # Calculate the appropriate timezone offset by seeing what
  978:                 # the differences between localtime and gmtime for the given
  979:                 # time are.
  980: 
  981:                 my @gmtime = gmtime($time);
  982:                 my @ltime  = localtime($time);
  983: 
  984:                 # For the first 7 elements of the two arrays, adjust the
  985:                 # date serial where the elements differ.
  986:                 for (0 .. 6) {
  987:                         my $diff = $ltime[$_] - $gmtime[$_];
  988:                         if ($diff) {
  989:                                 $serial += _adjustment($diff,$_);
  990:                         }
  991:                 }
  992:         }
  993: 
  994:         # Perpetuate the error that 1900 was a leap year by decrementing
  995:         # the serial if we're using the 1900 system and the date is prior to
  996:         # 1 Mar 1900. This has the effect of making serial value '60'
  997:         # 29 Feb 1900.
  998: 
  999:         # This fix only has any effect if UNIX/Perl time on the platform
 1000:         # can represent 1900. Many can't.
 1001: 
 1002:         unless ($DATE_SYSTEM) {
 1003:                 $serial-- if ($serial < 61);    # '61' is 1 Mar 1900
 1004:         }
 1005:         return $serial;
 1006: }
 1007: 
 1008: sub _adjustment {
 1009:         # Based on the difference in the localtime/gmtime array elements
 1010:         # number, return the adjustment required to the serial.
 1011: 
 1012:         # We only look at some elements of the localtime/gmtime arrays:
 1013:         #    seconds    unlikely to be different as all known timezones
 1014:         #               have an offset of integral multiples of 15 minutes,
 1015:         #               but it's easy to do.
 1016:         #    minutes    will be different for timezone offsets which are
 1017:         #               not an exact number of hours.
 1018:         #    hours      very likely to be different.
 1019:         #    weekday    will differ when localtime/gmtime difference
 1020:         #               straddles midnight.
 1021:         #
 1022:         # Assume that difference between localtime and gmtime is less than
 1023:         # 5 days, then don't have to do maths for day of month, month number,
 1024:         # year number, etc...
 1025: 
 1026:         my ($delta,$element) = @_;
 1027:         my $adjust = 0;
 1028: 
 1029:         if ($element == 0) {            # Seconds
 1030:                 $adjust = $delta/86400;         # 60 * 60 * 24
 1031:         } elsif ($element == 1) {       # Minutes
 1032:                 $adjust = $delta/1440;          # 60 * 24
 1033:         } elsif ($element == 2) {       # Hours
 1034:                 $adjust = $delta/24;            # 24
 1035:         } elsif ($element == 6) {       # Day of week number
 1036:                 # Catch difference straddling Sat/Sun in either direction
 1037:                 $delta += 7 if ($delta < -4);
 1038:                 $delta -= 7 if ($delta > 4);
 1039: 
 1040:                 $adjust = $delta;
 1041:         }
 1042:         return $adjust;
 1043: }
 1044: 
 1045: sub build_foil_index {
 1046:     my ($ORdata) = @_;
 1047:     return if (! exists($ORdata->{'Foils'}));
 1048:     my %Foildata = %{$ORdata->{'Foils'}};
 1049:     my @Foils = sort(keys(%Foildata));
 1050:     my %Concepts;
 1051:     foreach my $foilid (@Foils) {
 1052:         push(@{$Concepts{$Foildata{$foilid}->{'Concept'}}},
 1053:              $foilid);
 1054:     }
 1055:     undef(@Foils);
 1056:     # Having gathered the concept information in a hash, we now translate it
 1057:     # into an array because we need to be consistent about order.
 1058:     # Also put the foils in order, too.
 1059:     my $sortfunction = sub {
 1060:         my %Numbers = (one   => 1,
 1061:                        two   => 2,
 1062:                        three => 3,
 1063:                        four  => 4,
 1064:                        five  => 5,
 1065:                        six   => 6,
 1066:                        seven => 7,
 1067:                        eight => 8,
 1068:                        nine  => 9,
 1069:                        ten   => 10,);
 1070:         my $a1 = lc($a); 
 1071:         my $b1 = lc($b);
 1072:         if (exists($Numbers{$a})) {
 1073:             $a1 = $Numbers{$a};
 1074:         }
 1075:         if (exists($Numbers{$b})) {
 1076:             $b1 = $Numbers{$b};
 1077:         }
 1078:         $a1 cmp $b1;
 1079:     };
 1080:     my @Concepts;
 1081:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
 1082:         push(@Concepts,{ name => $concept,
 1083:                         foils => [@{$Concepts{$concept}}]});
 1084:         push(@Foils,(@{$Concepts{$concept}}));
 1085:     }
 1086:     #
 1087:     # Build up the table of row labels.
 1088:     my $table = '<table border="1" >'."\n";
 1089:     if (@Concepts > 1) {
 1090:         $table .= '<tr>'.
 1091:             '<th>'.&mt('Concept Number').'</th>'.
 1092:             '<th>'.&mt('Concept').'</th>'.
 1093:             '<th>'.&mt('Foil Number').'</th>'.
 1094:             '<th>'.&mt('Foil Name').'</th>'.
 1095:             '<th>'.&mt('Foil Text').'</th>'.
 1096:             '<th>'.&mt('Correct Value').'</th>'.
 1097:             "</tr>\n";
 1098:     } else {
 1099:         $table .= '<tr>'.
 1100:             '<th>'.&mt('Foil Number').'</th>'.
 1101:             '<th>'.&mt('Foil Name').'</th>'.
 1102:             '<th>'.&mt('Foil Text').'</th>'.
 1103:             '<th>'.&mt('Correct Value').'</th>'.
 1104:             "</tr>\n";
 1105:     }        
 1106:     my $conceptindex = 1;
 1107:     my $foilindex = 1;
 1108:     foreach my $concept (@Concepts) {
 1109:         my @FoilsInConcept = @{$concept->{'foils'}};
 1110:         my $firstfoil = shift(@FoilsInConcept);
 1111:         if (@Concepts > 1) {
 1112:             $table .= '<tr>'.
 1113:                 '<td>'.$conceptindex.'</td>'.
 1114:                 '<td>'.$concept->{'name'}.'</td>'.
 1115:                 '<td>'.$foilindex++.'</td>'.
 1116:                 '<td>'.$Foildata{$firstfoil}->{'name'}.'</td>'.
 1117:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1118:                 '<td>'.$Foildata{$firstfoil}->{'value'}.'</td>'.
 1119:                 "</tr>\n";
 1120:         } else {
 1121:             $table .= '<tr>'.
 1122:                 '<td>'.$foilindex++.'</td>'.
 1123:                 '<td>'.$Foildata{$firstfoil}->{'name'}.'</td>'.
 1124:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
 1125:                 '<td>'.$Foildata{$firstfoil}->{'value'}.'</td>'.
 1126:                 "</tr>\n";
 1127:         }
 1128:         foreach my $foilid (@FoilsInConcept) {
 1129:             if (@Concepts > 1) {
 1130:                 $table .= '<tr>'.
 1131:                     '<td></td>'.
 1132:                     '<td></td>'.
 1133:                     '<td>'.$foilindex.'</td>'.
 1134:                     '<td>'.$Foildata{$foilid}->{'name'}.'</td>'.
 1135:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1136:                     '<td>'.$Foildata{$foilid}->{'value'}.'</td>'.
 1137:                     "</tr>\n";
 1138:             } else {
 1139:                 $table .= '<tr>'.
 1140:                     '<td>'.$foilindex.'</td>'.
 1141:                     '<td>'.$Foildata{$foilid}->{'name'}.'</td>'.
 1142:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
 1143:                     '<td>'.$Foildata{$foilid}->{'value'}.'</td>'.
 1144:                     "</tr>\n";
 1145:             }                
 1146:         } continue {
 1147:             $foilindex++;
 1148:         }
 1149:     } continue {
 1150:         $conceptindex++;
 1151:     }
 1152:     $table .= "</table>\n";
 1153:     #
 1154:     # Build option index with color stuff
 1155:     return ($table,\@Foils,\@Concepts);
 1156: }
 1157: 
 1158: sub build_option_index {
 1159:     my ($ORdata)= @_;
 1160:     my $table = "<table>\n";
 1161:     my $optionindex = 0;
 1162:     my @Rows;
 1163:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'Options'}}) {
 1164:         push (@Rows,
 1165:               '<tr>'.
 1166:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
 1167:               ('&nbsp;'x4).'</td>'.
 1168:               '<td>'.$option.'</td>'.
 1169:               "</tr>\n");
 1170:     }
 1171:     shift(@Rows); # Throw away 'correct option chosen' color
 1172:     $table .= join('',reverse(@Rows));
 1173:     $table .= "</table>\n";
 1174: }
 1175: 
 1176: #########################################################
 1177: #########################################################
 1178: ##
 1179: ##   Generic Interface Routines
 1180: ##
 1181: #########################################################
 1182: #########################################################
 1183: sub CreateInterface {
 1184:     ##
 1185:     ## Environment variable initialization
 1186:     if (! exists$ENV{'form.AnalyzeOver'}) {
 1187:         $ENV{'form.AnalyzeOver'} = 'Tries';
 1188:     }
 1189:     ##
 1190:     ## Build the menu
 1191:     my $Str = '';
 1192:     $Str .= '<table cellspacing="5">'."\n";
 1193:     $Str .= '<tr>';
 1194:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
 1195:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
 1196: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
 1197:     $Str .= '<td align="center">&nbsp;</td>';
 1198:     $Str .= '</tr>'."\n";
 1199:     ##
 1200:     ## 
 1201:     $Str .= '<tr><td align="center">'."\n";
 1202:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
 1203:     $Str .= '</td>';
 1204:     #
 1205:     $Str .= '<td align="center">';
 1206:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
 1207:     $Str .= '</td>';
 1208:     #
 1209: #    $Str .= '<td align="center">';
 1210:     my $only_seq_with_assessments = sub { 
 1211:         my $s=shift;
 1212:         if ($s->{'num_assess'} < 1) { 
 1213:             return 0;
 1214:         } else { 
 1215:             return 1;
 1216:         }
 1217:     };
 1218:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
 1219:                                               $only_seq_with_assessments);
 1220:     ##
 1221:     ##
 1222:     $Str .= '<td>';
 1223:     { # These braces are here to organize the code, not scope it.
 1224:         {
 1225:             $Str .= '<nobr>'.&mt('Analyze Over ');
 1226:             $Str .='<select name="AnalyzeOver" >';
 1227:             $Str .= '<option value="Tries" ';
 1228:             if (! exists($ENV{'form.AnalyzeOver'}) || 
 1229:                 $ENV{'form.AnalyzeOver'} eq 'Tries'){
 1230:                 # Default to Tries
 1231:                 $Str .= ' selected ';
 1232:             }
 1233:             $Str .= '>'.&mt('Tries').'</option>';
 1234:             $Str .= '<option value="Time" ';
 1235:             $Str .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'Time');
 1236:             $Str .= '>'.&mt('Time').'</option>';
 1237:             $Str .= '</select></nobr><br />';
 1238:         }
 1239:         {
 1240:             $Str .= '<nobr>'.&mt('Analyze as ');
 1241:             $Str .='<select name="AnalyzeAs" >';
 1242:             $Str .= '<option value="Concepts" ';
 1243:             if (! exists($ENV{'form.AnalyzeAs'}) || 
 1244:                 $ENV{'form.AnalyzeAs'} eq 'Concepts'){
 1245:                 # Default to Concepts
 1246:                 $Str .= ' selected ';
 1247:             }
 1248:             $Str .= '>'.&mt('Concepts').'</option>';
 1249:             $Str .= '<option value="Foils" ';
 1250:             $Str .= ' selected ' if ($ENV{'form.AnalyzeAs'} eq 'Foils');
 1251:             $Str .= '>'.&mt('Foils').'</option>';
 1252:             $Str .= '</select></nobr><br />';
 1253:         }
 1254:         {
 1255:             $Str .= '<br /><nobr>'.&mt('Number of Plots:');
 1256:             $Str .= '<select name="NumPlots">';
 1257:             if (! exists($ENV{'form.NumPlots'}) 
 1258:                 || $ENV{'form.NumPlots'} < 1 
 1259:                 || $ENV{'form.NumPlots'} > 20) {
 1260:                 $ENV{'form.NumPlots'} = 5;
 1261:             }
 1262:             foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
 1263:                 $Str .= '<option value="'.$i.'" ';
 1264:                 if ($ENV{'form.NumPlots'} == $i) { $Str.=' selected '; }
 1265:                 $Str .= '>'.$i.'</option>';
 1266:             }
 1267:             $Str .= '</select></nobr>';
 1268:         }
 1269:     }
 1270:     $Str .= '</td>';
 1271:     ##
 1272:     ##
 1273:     $Str .= '</tr>'."\n";
 1274:     $Str .= '</table>'."\n";
 1275:     return $Str;
 1276: }
 1277: 
 1278: sub ProblemSelector {
 1279:     my $Str;
 1280:     $Str = "\n<table>\n";
 1281:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1282:         next if ($seq->{'num_assess'}<1);
 1283:         my $seq_str = '';
 1284:         foreach my $res (@{$seq->{'contents'}}) {
 1285:             next if ($res->{'type'} ne 'assessment');
 1286:             foreach my $part (@{$res->{'parts'}}) {
 1287:                 my $partdata = $res->{'partdata'}->{$part};
 1288: #                &Apache::lonnet::logthis('----------------');
 1289: #                while (my ($k,$v)=each(%$partdata)) {
 1290: #                    if (ref($v) eq 'ARRAY') {
 1291: #                        &Apache::lonnet::logthis($k.' = '.join(',',@$v));
 1292: #                    } else {
 1293: #                        &Apache::lonnet::logthis($k.' = '.$v);
 1294: #                    }
 1295: #                }
 1296:                 if ((! exists($partdata->{'option'}) || 
 1297:                      $partdata->{'option'} == 0      ) &&
 1298:                     (! exists($partdata->{'radiobutton'}) ||
 1299:                      $partdata->{'radiobutton'} == 0)) {
 1300:                     next;
 1301:                 }
 1302:                 for (my $i=0;$i<scalar(@{$partdata->{'ResponseTypes'}});$i++){
 1303:                     my $respid = $partdata->{'ResponseIds'}->[$i];
 1304:                     my $resptype = $partdata->{'ResponseTypes'}->[$i];
 1305:                     if ($resptype eq 'option' || $resptype eq 'radiobutton') {
 1306:                         my $value = &Apache::lonnet::escape($res->{'symb'}.':'.$part.':'.$respid.':'.$resptype);
 1307:                         my $checked = '';
 1308:                         if ($ENV{'form.problemchoice'} eq $value) {
 1309:                             $checked = 'checked ';
 1310:                         }
 1311:                         $seq_str .= '<tr><td>'.
 1312:   '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
 1313:   '</td><td>'.
 1314:   '<a href="'.$res->{'src'}.'">'.$resptype.' '.$res->{'title'}.'</a> ';
 1315:                         if ($partdata->{'option'} > 1) {
 1316:                             $seq_str .= &mt('response').' '.$respid;
 1317:                         }
 1318:                         $seq_str .= "</td></tr>\n";
 1319:                     }
 1320:                 }
 1321:             }
 1322:         }
 1323:         if ($seq_str ne '') {
 1324:             $Str .= '<tr><td>&nbsp</td><td><b>'.$seq->{'title'}.'</b></td>'.
 1325:                 "</tr>\n".$seq_str;
 1326:         }
 1327:     }
 1328:     $Str .= "</table>\n";
 1329:     return $Str;
 1330: }
 1331: 
 1332: #########################################################
 1333: #########################################################
 1334: ##
 1335: ##              Misc functions
 1336: ##
 1337: #########################################################
 1338: #########################################################
 1339: sub get_problem_symb {
 1340:     my $problemstring = shift();
 1341:     my ($symb,$partid,$resid,$resptype) = 
 1342:         ($problemstring=~ /^(.*):([^:]*):([^:]*):([^:]*)$/);
 1343:     return ($symb,$partid,$resid,$resptype);
 1344: }
 1345: 
 1346: sub get_resource_from_symb {
 1347:     my ($symb) = @_;
 1348:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1349:         foreach my $res (@{$seq->{'contents'}}) {
 1350:             if ($res->{'symb'} eq $symb) {
 1351:                 return $res;
 1352:             }
 1353:         }
 1354:     }
 1355:     return undef;
 1356: }
 1357: 
 1358: 
 1359: #########################################################
 1360: #########################################################
 1361: ##
 1362: ##              Misc Option Response functions
 1363: ##
 1364: #########################################################
 1365: #########################################################
 1366: sub get_time_from_row {
 1367:     my ($row) = @_;
 1368:     if (ref($row)) {
 1369:         return $row->[&Apache::loncoursedata::RD_timestamp()];
 1370:     } 
 1371:     return undef;
 1372: }
 1373: 
 1374: sub get_tries_from_row {
 1375:     my ($row) = @_;
 1376:     if (ref($row)) {
 1377:         return $row->[&Apache::loncoursedata::RD_tries()];
 1378:     }
 1379:     return undef;
 1380: }
 1381: 
 1382: sub Process_OR_Row {
 1383:     my ($row) = @_;
 1384:     my %RowData;
 1385:     my $student_id = $row->[&Apache::loncoursedata::RD_student_id()];
 1386:     my $award      = $row->[&Apache::loncoursedata::RD_awarddetail()];
 1387:     my $grading    = $row->[&Apache::loncoursedata::RD_response_eval()];
 1388:     my $submission = $row->[&Apache::loncoursedata::RD_submission()];
 1389:     my $time       = $row->[&Apache::loncoursedata::RD_timestamp()];
 1390:     my $tries      = $row->[&Apache::loncoursedata::RD_tries()];
 1391:     return undef if ($award eq 'MISSING_ANSWER');
 1392:     if ($award =~ /(APPROX_ANS|EXACT_ANS)/) {
 1393:         $RowData{'_correct'} = 1;
 1394:     }
 1395:     $RowData{'_total'} = 1;
 1396:     my @Foilgrades = split('&',$grading);
 1397:     my @Foilsubs   = split('&',$submission);
 1398:     for (my $j=0;$j<=$#Foilgrades;$j++) {
 1399:         my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
 1400:         my (undef,$submission) = split('=',$Foilsubs[$j]);
 1401:         if ($correct) {
 1402:             $RowData{$foilid}->{'_correct'}++;
 1403:         } else {
 1404:             $submission = &Apache::lonnet::unescape($submission);
 1405:             $RowData{$foilid}->{$submission}++;
 1406:         }
 1407:         $RowData{$foilid}->{'_total'}++;
 1408:     }
 1409:     return %RowData;
 1410: }
 1411: 
 1412: ##
 1413: ## get problem data and put it into a useful data structure.
 1414: ## note: we must force each foil and option to not begin or end with
 1415: ##       spaces as they are stored without such data.
 1416: ##
 1417: sub get_OR_problem_data {
 1418:     my ($url) = @_;
 1419:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
 1420:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
 1421:     my %Answer;
 1422:     %Answer=&Apache::lonnet::str2hash($Answ);
 1423:     my %Partdata;
 1424:     foreach my $part (@{$Answer{'parts'}}) {
 1425:         while (my($key,$value) = each(%Answer)) {
 1426:             next if ($key !~ /^$part/);
 1427:             $key =~ s/^$part\.//;
 1428:             if (ref($value) eq 'ARRAY') {
 1429:                 if ($key eq 'options') {
 1430:                     $Partdata{$part}->{'Options'}=$value;
 1431:                 } elsif ($key eq 'concepts') {
 1432:                     $Partdata{$part}->{'Concepts'}=$value;
 1433:                 } elsif ($key =~ /^concept\.(.*)$/) {
 1434:                     my $concept = $1;
 1435:                     foreach my $foil (@$value) {
 1436:                         $Partdata{$part}->{'Foils'}->{$foil}->{'Concept'}=
 1437:                                                                       $concept;
 1438:                     }
 1439:                 }
 1440:             } else {
 1441:                 if ($key=~ /^foil\.text\.(.*)$/) {
 1442:                     my $foil = $1;
 1443:                     $Partdata{$part}->{'Foils'}->{$foil}->{'name'}=$foil;
 1444:                     $value =~ s/(\s*$|^\s*)//g;
 1445:                     $Partdata{$part}->{'Foils'}->{$foil}->{'text'}=$value;
 1446:                 } elsif ($key =~ /^foil\.value\.(.*)$/) {
 1447:                     my $foil = $1;
 1448:                     $Partdata{$part}->{'Foils'}->{$foil}->{'value'}=$value;
 1449:                 }
 1450:             }
 1451:         }
 1452:     }
 1453:     return %Partdata;
 1454: }
 1455: 
 1456: #########################################################
 1457: #########################################################
 1458: ##
 1459: ##              Misc Radio Response functions
 1460: ##
 1461: #########################################################
 1462: #########################################################
 1463: sub get_Radio_problem_student_data {
 1464:     my ($row) = @_;
 1465: }
 1466: 
 1467: sub get_Radio_problem_data {
 1468:     my ($url) = @_;
 1469:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
 1470:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
 1471:     my %Answer;
 1472:     %Answer=&Apache::lonnet::str2hash($Answ);
 1473:     my %Partdata;
 1474:     &Apache::lonnet::logthis('url = '.$url);
 1475:     foreach my $part (@{$Answer{'parts'}}) {
 1476:         while (my($key,$value) = each(%Answer)) {
 1477:             if (ref($value) eq 'ARRAY') {
 1478:                 &Apache::lonnet::logthis('is ref part:'.$part.' '.$key.'='.join(',',@$value));
 1479:             } else {
 1480:                 &Apache::lonnet::logthis('notref part:'.$part.' '.$key.'='.$value);
 1481:             }                
 1482:             next if ($key !~ /^$part/);
 1483:             $key =~ s/^$part\.//;
 1484:             if ($key eq 'foils') {
 1485:                 $Partdata{$part}->{'_Foils'}=$value;
 1486:             } elsif ($key eq 'options') {
 1487:                 $Partdata{$part}->{'_Options'}=$value;
 1488:             } elsif ($key eq 'shown') {
 1489:                 $Partdata{$part}->{'_Shown'}=$value;
 1490:             } elsif ($key =~ /^foil.value.(.*)$/) {
 1491:                 $Partdata{$part}->{$1}->{'value'}=$value;
 1492:             } elsif ($key =~ /^foil.text.(.*)$/) {
 1493:                 $Partdata{$part}->{$1}->{'text'}=$value;
 1494:             }
 1495:         }
 1496:     }
 1497:     return %Partdata;
 1498: }
 1499: 
 1500: 1;
 1501: 
 1502: __END__

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