File:  [LON-CAPA] / loncom / interface / statistics / lonproblemanalysis.pm
Revision 1.41: download - view: text, annotated - select for diffs
Thu Oct 16 15:24:49 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Removed code which strips leading and tailing whitespace from problem data
because Guy told me to.  Minor changes otherwise.

    1: # The LearningOnline Network with CAPA
    2: #
    3: 
    4: # $Id: lonproblemanalysis.pm,v 1.41 2003/10/16 15:24:49 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonproblemanalysis;
   30: 
   31: use strict;
   32: use Apache::lonnet();
   33: use Apache::loncommon();
   34: use Apache::lonhtmlcommon();
   35: use Apache::loncoursedata();
   36: use Apache::lonstatistics;
   37: use Apache::lonlocal;
   38: use HTML::Entities();
   39: 
   40: my $plotcolors = ['#33ff00', 
   41:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
   42:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
   43:                   ]; 
   44: 
   45: sub BuildProblemAnalysisPage {
   46:     my ($r,$c)=@_;
   47:     $r->print('<h2>'.&mt('Option Response Problem Analysis').'</h2>');
   48:     $r->print(&CreateInterface());
   49:     #
   50:     my @Students = @Apache::lonstatistics::Students;
   51:     #
   52:     if (exists($ENV{'form.ClearCache'}) || 
   53:         exists($ENV{'form.updatecaches'}) ||
   54:         (exists($ENV{'form.firstanalysis'}) &&
   55:          $ENV{'form.firstanalysis'} ne 'no')) {
   56:         &Apache::lonstatistics::Gather_Full_Student_Data($r);
   57:     }
   58:     if (! exists($ENV{'form.firstanalysis'})) {
   59:         $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
   60:     } else {
   61:         $r->print('<input type="hidden" name="firstanalysis" value="no" />');
   62:     }
   63:     $r->rflush();
   64:     if (exists($ENV{'form.problemchoice'}) && 
   65:         ! exists($ENV{'form.SelectAnother'})) {
   66:         $r->print('<input type="submit" name="ProblemAnalysis" value="'.
   67:                   &mt('Analyze Problem Again').'" />');
   68:         $r->print('&nbsp;'x5);
   69:         $r->print('<input type="submit" name="ClearCache" value="'.
   70:                   &mt('Clear Caches').'" />');
   71:         $r->print('&nbsp;'x5);
   72:         $r->print('<input type="submit" name="updatecaches" value="'.
   73:                   &mt('Update Student Data').'" />');
   74:         $r->print('&nbsp;'x5);
   75:         $r->print('<input type="hidden" name="problemchoice" value="'.
   76:                   $ENV{'form.problemchoice'}.'" />');
   77:         $r->print('<input type="submit" name="SelectAnother" value="'.
   78:                   &mt('Choose a different resource').'" />');
   79:         $r->print('&nbsp;'x5);
   80:         #
   81:         $r->print('<hr />');
   82:         #
   83:         my ($symb,$part,$resid) = &get_problem_symb(
   84:                      &Apache::lonnet::unescape($ENV{'form.problemchoice'})
   85:                                            );
   86:         #
   87:         my $resource = &get_resource_from_symb($symb);
   88:         if (defined($resource)) {
   89:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
   90:             my %Data = &get_problem_data($resource->{'src'});
   91:             my $ORdata = $Data{$part.'.'.$resid};
   92:             ##
   93:             ## Render the problem
   94:             my $base;
   95:             ($base,undef) = ($resource->{'src'} =~ m|(.*/)[^/]*$|);
   96:             $base = "http://".$ENV{'SERVER_NAME'}.$base;
   97:             my $rendered_problem = 
   98:                 &Apache::lonnet::ssi_body($resource->{'src'});
   99:             $rendered_problem =~ s/<\s*form\s*/<nop /g;
  100:             $rendered_problem =~ s|(<\s*/form\s*>)|<\/nop>|g;
  101:             $r->print('<table bgcolor="ffffff"><tr><td>'.
  102:                       '<base href="'.$base.'" />'.
  103:                       $rendered_problem.
  104:                       '</td></tr></table>');
  105:             ##
  106:             ## Analyze the problem
  107:             my $PerformanceData = 
  108:                 &Apache::loncoursedata::get_optionresponse_data
  109:                                            (\@Students,$symb,$resid);
  110:             if (defined($PerformanceData) && 
  111:                 ref($PerformanceData) eq 'ARRAY') {
  112:                 if ($ENV{'form.AnalyzeOver'} eq 'Tries') {
  113:                     my $analysis_html = &tries_analysis($PerformanceData,
  114:                                                          $ORdata);
  115:                     $r->print($analysis_html);
  116:                 } elsif ($ENV{'form.AnalyzeOver'} eq 'Time') {
  117:                     my $analysis_html = &time_analysis($PerformanceData,
  118:                                                          $ORdata);
  119:                 $r->print($analysis_html);
  120:                 } else {
  121:                     $r->print('<h2>'.
  122:                               &mt('The analysis you have selected is '.
  123:                                          'not supported at this time').
  124:                               '</h2>');
  125:                 }
  126:             } else {
  127:                 $r->print('<h2>'.
  128:                           &mt('There is no student data for this problem.').
  129:                           '</h2>');
  130:             }
  131:         } else {
  132:             $r->print('resource is undefined');
  133:         }
  134:         $r->print('<hr />');
  135:     } else {
  136:         $r->print('<input type="submit" name="ProblemAnalysis" value="'.
  137:                   &mt('Analyze Problem').'" />');
  138:         $r->print('&nbsp;'x5);
  139:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
  140:         $r->print(&OptionResponseProblemSelector());
  141:     }
  142: }
  143: 
  144: 
  145: #########################################################
  146: #########################################################
  147: ##
  148: ##      Misc interface routines use by analysis code
  149: ##
  150: #########################################################
  151: #########################################################
  152: sub build_foil_index {
  153:     my ($ORdata) = @_;
  154:     return if (! exists($ORdata->{'Foils'}));
  155:     my %Foildata = %{$ORdata->{'Foils'}};
  156:     my @Foils = sort(keys(%Foildata));
  157:     my %Concepts;
  158:     foreach my $foilid (@Foils) {
  159:         push(@{$Concepts{$Foildata{$foilid}->{'Concept'}}},
  160:              $foilid);
  161:     }
  162:     undef(@Foils);
  163:     # Having gathered the concept information in a hash, we now translate it
  164:     # into an array because we need to be consistent about order.
  165:     # Also put the foils in order, too.
  166:     my $sortfunction = sub {
  167:         my %Numbers = (one   => 1,
  168:                        two   => 2,
  169:                        three => 3,
  170:                        four  => 4,
  171:                        five  => 5,
  172:                        six   => 6,
  173:                        seven => 7,
  174:                        eight => 8,
  175:                        nine  => 9,
  176:                        ten   => 10,);
  177:         my $a1 = $a; 
  178:         my $b1 = $b;
  179:         if (exists($Numbers{lc($a)})) {
  180:             $a1 = $Numbers{lc($a)};
  181:         }
  182:         if (exists($Numbers{lc($b)})) {
  183:             $b1 = $Numbers{lc($b)};
  184:         }
  185:         $a1 cmp $b1;
  186:     };
  187:     my @Concepts;
  188:     foreach my $concept (sort $sortfunction (keys(%Concepts))) {
  189:         push(@Concepts,{ name => $concept,
  190:                         foils => [@{$Concepts{$concept}}]});
  191:         push(@Foils,(@{$Concepts{$concept}}));
  192:     }
  193:     #
  194:     # Build up the table of row labels.
  195:     my $table = '<table border="1" >'."\n";
  196:     if (@Concepts > 1) {
  197:         $table .= '<tr>'.
  198:             '<th>'.&mt('Concept Number').'</th>'.
  199:             '<th>'.&mt('Concept').'</th>'.
  200:             '<th>'.&mt('Foil Number').'</th>'.
  201:             '<th>'.&mt('Foil Name').'</th>'.
  202:             '<th>'.&mt('Foil Text').'</th>'.
  203:             '<th>'.&mt('Correct Value').'</th>'.
  204:             "</tr>\n";
  205:     } else {
  206:         $table .= '<tr>'.
  207:             '<th>'.&mt('Foil Number').'</th>'.
  208:             '<th>'.&mt('Foil Name').'</th>'.
  209:             '<th>'.&mt('Foil Text').'</th>'.
  210:             '<th>'.&mt('Correct Value').'</th>'.
  211:             "</tr>\n";
  212:     }        
  213:     my $conceptindex = 1;
  214:     my $foilindex = 1;
  215:     foreach my $concept (@Concepts) {
  216:         my @FoilsInConcept = @{$concept->{'foils'}};
  217:         my $firstfoil = shift(@FoilsInConcept);
  218:         if (@Concepts > 1) {
  219:             $table .= '<tr>'.
  220:                 '<td>'.$conceptindex.'</td>'.
  221:                 '<td>'.$concept->{'name'}.'</td>'.
  222:                 '<td>'.$foilindex++.'</td>'.
  223:                 '<td>'.$Foildata{$firstfoil}->{'name'}.'</td>'.
  224:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
  225:                 '<td>'.$Foildata{$firstfoil}->{'value'}.'</td>'.
  226:                 "</tr>\n";
  227:         } else {
  228:             $table .= '<tr>'.
  229:                 '<td>'.$foilindex++.'</td>'.
  230:                 '<td>'.$Foildata{$firstfoil}->{'name'}.'</td>'.
  231:                 '<td>'.$Foildata{$firstfoil}->{'text'}.'</td>'.
  232:                 '<td>'.$Foildata{$firstfoil}->{'value'}.'</td>'.
  233:                 "</tr>\n";
  234:         }
  235:         foreach my $foilid (@FoilsInConcept) {
  236:             if (@Concepts > 1) {
  237:                 $table .= '<tr>'.
  238:                     '<td></td>'.
  239:                     '<td></td>'.
  240:                     '<td>'.$foilindex.'</td>'.
  241:                     '<td>'.$Foildata{$foilid}->{'name'}.'</td>'.
  242:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
  243:                     '<td>'.$Foildata{$foilid}->{'value'}.'</td>'.
  244:                     "</tr>\n";
  245:             } else {
  246:                 $table .= '<tr>'.
  247:                     '<td>'.$foilindex.'</td>'.
  248:                     '<td>'.$Foildata{$foilid}->{'name'}.'</td>'.
  249:                     '<td>'.$Foildata{$foilid}->{'text'}.'</td>'.
  250:                     '<td>'.$Foildata{$foilid}->{'value'}.'</td>'.
  251:                     "</tr>\n";
  252:             }                
  253:         } continue {
  254:             $foilindex++;
  255:         }
  256:     } continue {
  257:         $conceptindex++;
  258:     }
  259:     $table .= "</table>\n";
  260:     #
  261:     # Build option index with color stuff
  262:     return ($table,\@Foils,\@Concepts);
  263: }
  264: 
  265: sub build_option_index {
  266:     my ($ORdata)= @_;
  267:     my $table = "<table>\n";
  268:     my $optionindex = 0;
  269:     my @Rows;
  270:     foreach my $option (&mt('correct option chosen'),@{$ORdata->{'Options'}}) {
  271:         push (@Rows,
  272:               '<tr>'.
  273:               '<td bgcolor="'.$plotcolors->[$optionindex++].'">'.
  274:               ('&nbsp;'x4).'</td>'.
  275:               '<td>'.$option.'</td>'.
  276:               "</tr>\n");
  277:     }
  278:     $table .= join('',reverse(@Rows));
  279:     $table .= "</table>\n";
  280: }
  281: 
  282: #########################################################
  283: #########################################################
  284: ##
  285: ##         Tries Analysis
  286: ##
  287: #########################################################
  288: #########################################################
  289: sub tries_analysis {
  290:     my ($PerformanceData,$ORdata) = @_;
  291:     my $mintries = 1;
  292:     my $maxtries = $ENV{'form.NumPlots'};
  293:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  294:     if ((@$Concepts < 2) && ($ENV{'form.AnalyzeAs'} ne 'Foils')) {
  295:         $table = '<h3>'.
  296:             &mt('Not enough data for concept analysis.  '.
  297:                 'Performing Foil Analysis').
  298:             '</h3>'.$table;
  299:         $ENV{'form.AnalyzeAs'} = 'Foils';
  300:     }
  301:     my %ResponseData = &analyze_option_data_by_tries($PerformanceData,
  302:                                                      $mintries,$maxtries);
  303:     #
  304:     # Compute the data neccessary to make the plots
  305:     my @PlotData;   # Array which holds the data for each plot
  306:                     # @{$PlotData[$try]->{'datasetname'}} holds the data for
  307:                     # try $try with respect to 'datasetname'.  The array is
  308:                     # filled either with per-foil or per-concept data.
  309:     my ($extrakey,$xlabel,$ylabel);
  310:     if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  311:         $extrakey = &build_option_index($ORdata);
  312:         $xlabel = 'Foil Number';
  313:         $ylabel = 'Percent Choosing Option';
  314:         foreach my $foilid (@$Foils) {
  315:             for (my $i=$mintries;$i<=$maxtries;$i++) {
  316:                 foreach my $option ('_correct',@{$ORdata->{'Options'}}) {
  317:                     push(@{$PlotData[$i]->{'_total'}},
  318:                          $ResponseData{$foilid}->[$i]->{'_total'});
  319:                     if ($ResponseData{$foilid}->[$i]->{'_total'} == 0) {
  320:                         push (@{$PlotData[$i]->{$option}},0);
  321:                     } else {
  322:                         push (@{$PlotData[$i]->{$option}},
  323:                               100 * $ResponseData{$foilid}->[$i]->{$option} / 
  324:                                     $ResponseData{$foilid}->[$i]->{'_total'});
  325:                     }
  326:                 }
  327:             }
  328:         }
  329:     } else {
  330:         # Concept analysis
  331:         #
  332:         # Note: we do not bother with characterizing the students incorrect
  333:         # answers at the concept level because an incorrect answer for one foil
  334:         # may be a correct answer for another foil.
  335:         $extrakey = '';
  336:         $xlabel = 'Concept Number';
  337:         $ylabel = 'Percent Correct';
  338:         my %ConceptData;
  339:         foreach my $concept (@{$Concepts}) {
  340:             for (my $i=$mintries;$i<=$maxtries;$i++) {
  341:                 #
  342:                 # Gather the per-attempt data
  343:                 my $cdata = $ConceptData{$concept}->[$i];
  344:                 foreach my $foilid (@{$concept->{'foils'}}) {
  345:                     $cdata->{'_correct'} += 
  346:                         $ResponseData{$foilid}->[$i]->{'_correct'};
  347:                     $cdata->{'_total'}   += 
  348:                         $ResponseData{$foilid}->[$i]->{'_total'};
  349:                 }
  350:                 push (@{$PlotData[$i]->{'_total'}},$cdata->{'_total'});
  351:                 if ($cdata->{'_total'} == 0) {
  352:                     push (@{$PlotData[$i]->{'_correct'}},0);
  353:                 } else {
  354:                     push (@{$PlotData[$i]->{'_correct'}},
  355:                           100*$cdata->{'_correct'}/$cdata->{'_total'});
  356:                 }
  357:             }
  358:         }
  359:     } # End of work to fill @PlotData
  360:     # 
  361:     # Build a table for the plots
  362:     $table .= "<table>\n";
  363:     my @Plots;
  364:     for (my $i=$mintries;$i<=$maxtries;$i++) {
  365:         my $minstu = $PlotData[$i]->{'_total'}->[0];
  366:         my $maxstu = $PlotData[$i]->{'_total'}->[0];
  367:         foreach my $count (@{$PlotData[$i]->{'_total'}}) {
  368:             if ($minstu > $count) {
  369:                 $minstu = $count;
  370:             }
  371:             if ($maxstu < $count) {
  372:                 $maxstu = $count;
  373:             }
  374:         }
  375:         $maxstu = 0 if (! defined($maxstu));
  376:         $minstu = 0 if (! defined($minstu));
  377:         my $title;
  378:         if ($maxstu == $minstu) {
  379:             $title = 'Attempt '.$i.', '.$maxstu.' students';
  380:         } else {
  381:             $title = 'Attempt '.$i.', '.$minstu.'-'.$maxstu.' students';
  382:         }
  383:         my @Datasets;
  384:         foreach my $option ('_correct',@{$ORdata->{'Options'}}) {
  385:             next if (! exists($PlotData[$i]->{$option}));
  386:             push(@Datasets,$PlotData[$i]->{$option});
  387:         }
  388:         my $graphlink = &Apache::loncommon::DrawGraph($title,
  389:                                                       $xlabel,
  390:                                                       $ylabel,
  391:                                                       100,
  392:                                                       $plotcolors,
  393:                                                       @Datasets);
  394:         push(@Plots,$graphlink);
  395:     }
  396:     #
  397:     # Should this be something the user can set?  Too many dialogs!
  398:     while (my $plotlink = shift(@Plots)) {
  399:         $table .= '<tr><td>'.$plotlink.'</td><td>'.$extrakey."</td></tr>\n";
  400:     }
  401:     $table .= "</table>\n";
  402:     return ($table);
  403: }
  404: 
  405: sub analyze_option_data_by_tries {
  406:     my ($PerformanceData,$mintries,$maxtries) = @_;
  407:     my %Trydata;
  408:     $mintries = 1         if (! defined($mintries) || $mintries < 1);
  409:     $maxtries = $mintries if (! defined($maxtries) || $maxtries < $mintries);
  410:     foreach my $row (@$PerformanceData) {
  411:         next if (! defined($row));
  412:         my ($grading,$submission,$time,$tries) = @$row;
  413:         next if ($grading eq 'MISSING_ANSWER');
  414:         my @Foilgrades = split('&',$grading);
  415:         my @Foilsubs   = split('&',$submission);
  416:         for (my $numtries = 1; $numtries <= $maxtries; $numtries++) {
  417:             if ($tries == $numtries) {
  418:                 for (my $i=0;$i<=$#Foilgrades;$i++) {
  419:                     my ($foilid,$correct)  = split('=',$Foilgrades[$i]);
  420:                     my (undef,$submission) = split('=',$Foilsubs[$i]);
  421:                     $submission = &Apache::lonnet::unescape($submission);
  422:                     if ($correct) {
  423:                         $Trydata{$foilid}->[$numtries]->{'_correct'}++;
  424:                     } else {
  425:                         $Trydata{$foilid}->[$numtries]->{$submission}++;
  426:                     }                        
  427:                     $Trydata{$foilid}->[$numtries]->{'_total'}++;
  428:                 }
  429:             }
  430:         }
  431:     }
  432:     return %Trydata;
  433: }
  434: 
  435: #########################################################
  436: #########################################################
  437: ##
  438: ##                 Time Analysis
  439: ##
  440: #########################################################
  441: #########################################################
  442: sub time_analysis {
  443:     my ($PerformanceData,$ORdata) = @_;
  444:     my $num_plots = $ENV{'form.NumPlots'};
  445:     my ($table,$Foils,$Concepts) = &build_foil_index($ORdata);
  446:     my $num_data = scalar(@$PerformanceData)-1;
  447:     my $percent = sprintf('%2f',100/$num_plots);
  448:     my $extratable = '';
  449:     if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  450:         $extratable = &build_option_index($ORdata);
  451:     }
  452:     $table .= "<table>\n";
  453:     for (my $i=0;$i<$num_plots;$i++) {
  454:         my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  455:             ('startdate_'.$i);
  456:         my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  457:             ('enddate_'.$i);
  458:         my ($begin_index,$end_index,$plottitle,$plothtml,$data);
  459:         if (! defined($starttime) || ! defined($endtime)) {
  460:             $begin_index = $i*int($num_data/$num_plots);
  461:             $end_index = ($i+1)*int($num_data/$num_plots);
  462:             my $lownum  = sprintf('%2.1f',$i*$percent);
  463:             $lownum =~ s/(\.0)$//;
  464:             my $highnum = sprintf('%2.1f',($i+1)*$percent);
  465:             $highnum =~ s/(\.0)$//;
  466:             $plottitle = $lownum.'% to '.$highnum.'% of submissions';
  467:         } else {
  468:             my $j;
  469:             while (++$j < scalar(@$PerformanceData)) {
  470:                 last if ($PerformanceData->[$j]->[2] > $starttime);
  471:             }
  472:             $begin_index = $j;
  473:             while (++$j < scalar(@$PerformanceData)) {
  474:                 last if ($PerformanceData->[$j]->[2] > $endtime);
  475:             }
  476:             $end_index = $j;
  477:             $plottitle = $ENV{'form.plottitle_'.$i};
  478:         }
  479:         ($plothtml,$starttime,$endtime,$data) = 
  480:             &analyze_option_data_by_time($PerformanceData,
  481:                                          $begin_index,$end_index,
  482:                                          $plottitle,$Foils,
  483:                                          $Concepts,$ORdata);
  484:         my $startdateform = &Apache::lonhtmlcommon::date_setter
  485:             ('Statistics','startdate_'.$i,$starttime);
  486:         my $enddateform = &Apache::lonhtmlcommon::date_setter
  487:             ('Statistics','enddate_'.$i,$endtime);
  488:         $table.="<tr><td>".$plothtml.'</td><td align="left" valign="top">'.
  489:             "<b>Start Time</b>: &nbsp;".$startdateform."<br />".
  490:             "<b>End Time</b>&nbsp;&nbsp;: "."&nbsp;".$enddateform."<br />".
  491:             '<b>Plot Title</b>&nbsp;&nbsp;:'.("&nbsp;"x3).
  492:             '<input type="text" size="30" name="plottitle_'.$i.'" value="'.
  493:                   &HTML::Entities::encode($plottitle).'" /><br />'.$extratable.
  494:             "</td></tr>\n";
  495:     }
  496:     $table .="</table>\n";
  497:     return $table;
  498: }
  499: 
  500: sub analyze_option_data_by_time {
  501:     my ($PerformanceData,$begin_index,
  502:         $end_index,$description,$Foils,$Concepts,$ORdata) = @_;
  503:     my %TimeData;
  504:     #
  505:     # Get the start and end times for this segment of the plot
  506:     my $starttime = $PerformanceData->[$begin_index]->[2];
  507:     my $endtime   = $PerformanceData->[$end_index  ]->[2];
  508:     #
  509:     # Compute the number getting the foils correct or incorrects
  510:     for (my $i=$begin_index;$i<=$end_index;$i++) {
  511:         my $row = $PerformanceData->[$i];
  512:         next if (! defined($row));
  513:         my ($grading,$submission,$time,$tries) = @$row;
  514:         next if ($grading eq 'MISSING_ANSWER');
  515:         my @Foilgrades = split('&',$grading);
  516:         my @Foilsubs   = split('&',$submission);
  517:         for (my $j=0;$j<=$#Foilgrades;$j++) {
  518:             my ($foilid,$correct)  = split('=',$Foilgrades[$j]);
  519:             my (undef,$submission) = split('=',$Foilsubs[$j]);
  520:             if ($correct) {
  521:                 $TimeData{$foilid}->{'_correct'}++;
  522:             } else {
  523:                 $submission = &HTML::Entities::decode($submission);
  524:                 $submission =~ s/\%20/ /g;
  525:                 $TimeData{$foilid}->{$submission}++;
  526:             }
  527:             $TimeData{$foilid}->{'_total'}++;
  528:         }
  529:     }
  530:     #
  531:     # Compute the total and percent correct
  532:     my @Plotdata;
  533:     my ($xlabel,$ylabel);
  534:     if ($ENV{'form.AnalyzeAs'} eq 'Foils') {
  535:         $xlabel = 'Foil Number';
  536:         $ylabel = 'Option Chosen';
  537:         foreach my $foil (@$Foils) {
  538:             my $total = $TimeData{$foil}->{'_total'};
  539:             my $optionidx = 0;
  540:             foreach my $option ('_correct',@{$ORdata->{'Options'}}) {
  541:                 if ($total > 0) {
  542:                     push(@{$Plotdata[$optionidx]},
  543:                          100 * $TimeData{$foil}->{$option} / $total);
  544:                 } else {
  545:                     push(@{$Plotdata[$optionidx]},0);
  546:                 }
  547:             } continue {
  548:                 $optionidx++;
  549:             }
  550:         }
  551:     } else {
  552:         $xlabel = 'Concept Number';
  553:         $ylabel = 'Percent Correct';
  554:         foreach my $concept (@$Concepts) {
  555:             my $correct;
  556:             my $total;
  557:             foreach my $foil (@{$concept->{'foils'}}) {
  558:                 $correct+=$TimeData{$foil}->{'_correct'};
  559:                 $total  +=$TimeData{$foil}->{'_total'};
  560:             }
  561:             if ($total > 0) {
  562:                 push(@{$Plotdata[0]},100 * $correct / $total);
  563:             } else {
  564:                 push(@{$Plotdata[0]},0);
  565:             }
  566:         }
  567:     }
  568:     #
  569:     # Create the plot
  570:     my $graphlink = &Apache::loncommon::DrawGraph
  571:         ($description,#'Time Interval Analysis',
  572:          $xlabel,
  573:          $ylabel,
  574:          100,
  575:          $plotcolors,
  576:          @Plotdata);
  577:     #
  578:     return ($graphlink,$starttime,$endtime,\%TimeData);
  579: }
  580: 
  581: #########################################################
  582: #########################################################
  583: ##
  584: ##             Interface 
  585: ##
  586: #########################################################
  587: #########################################################
  588: sub CreateInterface {
  589:     ##
  590:     ## Environment variable initialization
  591:     if (! exists$ENV{'form.AnalyzeOver'}) {
  592:         $ENV{'form.AnalyzeOver'} = 'Tries';
  593:     }
  594:     ##
  595:     ## Build the menu
  596:     my $Str = '';
  597:     $Str .= '<table cellspacing="5">'."\n";
  598:     $Str .= '<tr>';
  599:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  600:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  601: #    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
  602:     $Str .= '<td align="center">&nbsp;</td>';
  603:     $Str .= '</tr>'."\n";
  604:     ##
  605:     ## 
  606:     $Str .= '<tr><td align="center">'."\n";
  607:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  608:     $Str .= '</td>';
  609:     #
  610:     $Str .= '<td align="center">';
  611:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  612:     $Str .= '</td>';
  613:     #
  614: #    $Str .= '<td align="center">';
  615:     my $only_seq_with_assessments = sub { 
  616:         my $s=shift;
  617:         if ($s->{'num_assess'} < 1) { 
  618:             return 0;
  619:         } else { 
  620:             return 1;
  621:         }
  622:     };
  623:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  624:                                               $only_seq_with_assessments);
  625:     ##
  626:     ##
  627:     $Str .= '<td>';
  628:     { # These braces are here to organize the code, not scope it.
  629:         {
  630:             $Str .= '<nobr>'.&mt('Analyze Over ');
  631:             $Str .='<select name="AnalyzeOver" >';
  632:             $Str .= '<option value="Tries" ';
  633:             if (! exists($ENV{'form.AnalyzeOver'}) || 
  634:                 $ENV{'form.AnalyzeOver'} eq 'Tries'){
  635:                 # Default to Tries
  636:                 $Str .= ' selected ';
  637:             }
  638:             $Str .= '>'.&mt('Tries').'</option>';
  639:             $Str .= '<option value="Time" ';
  640:             $Str .= ' selected ' if ($ENV{'form.AnalyzeOver'} eq 'Time');
  641:             $Str .= '>'.&mt('Time').'</option>';
  642:             $Str .= '</select></nobr><br />';
  643:         }
  644:         {
  645:             $Str .= '<nobr>'.&mt('Analyze as ');
  646:             $Str .='<select name="AnalyzeAs" >';
  647:             $Str .= '<option value="Concepts" ';
  648:             if (! exists($ENV{'form.AnalyzeAs'}) || 
  649:                 $ENV{'form.AnalyzeAs'} eq 'Concepts'){
  650:                 # Default to Concepts
  651:                 $Str .= ' selected ';
  652:             }
  653:             $Str .= '>'.&mt('Concepts').'</option>';
  654:             $Str .= '<option value="Foils" ';
  655:             $Str .= ' selected ' if ($ENV{'form.AnalyzeAs'} eq 'Foils');
  656:             $Str .= '>'.&mt('Foils').'</option>';
  657:             $Str .= '</select></nobr><br />';
  658:         }
  659:         {
  660:             $Str .= '<br /><nobr>'.&mt('Number of Plots:');
  661:             $Str .= '<select name="NumPlots">';
  662:             if (! exists($ENV{'form.NumPlots'}) 
  663:                 || $ENV{'form.NumPlots'} < 1 
  664:                 || $ENV{'form.NumPlots'} > 20) {
  665:                 $ENV{'form.NumPlots'} = 5;
  666:             }
  667:             foreach my $i (1,2,3,4,5,6,7,8,10,15,20) {
  668:                 $Str .= '<option value="'.$i.'" ';
  669:                 if ($ENV{'form.NumPlots'} == $i) { $Str.=' selected '; }
  670:                 $Str .= '>'.$i.'</option>';
  671:             }
  672:             $Str .= '</select></nobr>';
  673:         }
  674:     }
  675:     $Str .= '</td>';
  676:     ##
  677:     ##
  678:     $Str .= '</tr>'."\n";
  679:     $Str .= '</table>'."\n";
  680:     return ($Str);
  681: }
  682: 
  683: sub OptionResponseProblemSelector {
  684:     my $Str;
  685:     $Str = "\n<table>\n";
  686:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  687:         next if ($seq->{'num_assess'}<1);
  688:         my $seq_str = '';
  689:         foreach my $res (@{$seq->{'contents'}}) {
  690:             next if ($res->{'type'} ne 'assessment');
  691:             foreach my $part (@{$res->{'parts'}}) {
  692:                 my $partdata = $res->{'partdata'}->{$part};
  693:                 if (! exists($partdata->{'option'}) || 
  694:                     $partdata->{'option'} == 0) {
  695:                     next;
  696:                 }
  697:                 for (my $i=0;$i<scalar(@{$partdata->{'ResponseTypes'}});$i++){
  698:                     my $respid = $partdata->{'ResponseIds'}->[$i];
  699:                     my $resptype = $partdata->{'ResponseTypes'}->[$i];
  700:                     if ($resptype eq 'option') {
  701:                         my $value = &Apache::lonnet::escape($res->{'symb'}.':'.$part.':'.$respid);
  702:                         my $checked = '';
  703:                         if ($ENV{'form.problemchoice'} eq $value) {
  704:                             $checked = 'checked ';
  705:                         }
  706:                         $seq_str .= '<tr><td>'.
  707:   '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
  708:   '</td><td>'.
  709:   '<a href="'.$res->{'src'}.'">'.$res->{'title'}.'</a> ';
  710:                         if ($partdata->{'option'} > 1) {
  711:                             $seq_str .= &mt('response').' '.$respid;
  712:                         }
  713:                         $seq_str .= "</td></tr>\n";
  714:                     }
  715:                 }
  716:             }
  717:         }
  718:         if ($seq_str ne '') {
  719:             $Str .= '<tr><td>&nbsp</td><td><b>'.$seq->{'title'}.'</b></td>'.
  720:                 "</tr>\n".$seq_str;
  721:         }
  722:     }
  723:     $Str .= "</table>\n";
  724:     return $Str;
  725: }
  726: 
  727: #########################################################
  728: #########################################################
  729: ##
  730: ##              Misc functions
  731: ##
  732: #########################################################
  733: #########################################################
  734: sub get_problem_symb {
  735:     my $problemstring = shift();
  736:     my ($symb,$partid,$resid) = ($problemstring=~ /^(.*):([^:]*):([^:]*)$/);
  737:     return ($symb,$partid,$resid);
  738: }
  739: 
  740: sub get_resource_from_symb {
  741:     my ($symb) = @_;
  742:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  743:         foreach my $res (@{$seq->{'contents'}}) {
  744:             if ($res->{'symb'} eq $symb) {
  745:                 return $res;
  746:             }
  747:         }
  748:     }
  749:     return undef;
  750: }
  751: 
  752: ##
  753: ## get problem data and put it into a useful data structure.
  754: ## note: we must force each foil and option to not begin or end with
  755: ##       spaces as they are stored without such data.
  756: ##
  757: sub get_problem_data {
  758:     my ($url) = @_;
  759:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
  760:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
  761:     my %Answer;
  762:     %Answer=&Apache::lonnet::str2hash($Answ);
  763:     my %Partdata;
  764:     foreach my $part (@{$Answer{'parts'}}) {
  765:         while (my($key,$value) = each(%Answer)) {
  766:             next if ($key !~ /^$part/);
  767:             $key =~ s/^$part\.//;
  768:             if (ref($value) eq 'ARRAY') {
  769:                 if ($key eq 'options') {
  770:                     $Partdata{$part}->{'Options'}=$value;
  771:                 } elsif ($key eq 'concepts') {
  772:                     $Partdata{$part}->{'Concepts'}=$value;
  773:                 } elsif ($key =~ /^concept\.(.*)$/) {
  774:                     my $concept = $1;
  775:                     foreach my $foil (@$value) {
  776:                         $Partdata{$part}->{'Foils'}->{$foil}->{'Concept'}=
  777:                                                                       $concept;
  778:                     }
  779:                 }
  780:             } else {
  781:                 if ($key=~ /^foil\.text\.(.*)$/) {
  782:                     my $foil = $1;
  783:                     $Partdata{$part}->{'Foils'}->{$foil}->{'name'}=$foil;
  784:                     $Partdata{$part}->{'Foils'}->{$foil}->{'text'}=$value;
  785:                 } elsif ($key =~ /^foil\.value\.(.*)$/) {
  786:                     my $foil = $1;
  787:                     $Partdata{$part}->{'Foils'}->{$foil}->{'value'}=$value;
  788:                 }
  789:             }
  790:         }
  791:     }
  792:     return %Partdata;
  793: }
  794: 
  795: 1;
  796: 
  797: __END__

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