File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.75: download - view: text, annotated - select for diffs
Mon Mar 29 18:22:28 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Removed some logging code.  Fixed bug with stats display always being in
'ungrouped' mode.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemstatistics.pm,v 1.75 2004/03/29 18:22:28 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: # (Navigate problems for statistical reports
   28: #
   29: ###############################################
   30: ###############################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: lonproblemstatistics
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Routines to present problem statistics to instructors via tables,
   41: Excel files, and plots.
   42: 
   43: =over 4
   44: 
   45: =cut
   46: 
   47: ###############################################
   48: ###############################################
   49: 
   50: package Apache::lonproblemstatistics;
   51: 
   52: use strict;
   53: use Apache::lonnet();
   54: use Apache::loncommon();
   55: use Apache::lonhtmlcommon;
   56: use Apache::loncoursedata;
   57: use Apache::lonstatistics;
   58: use Apache::lonlocal;
   59: use Spreadsheet::WriteExcel;
   60: use Apache::lonstathelpers();
   61: use Time::HiRes;
   62: 
   63: my @StatsArray;
   64: 
   65: ##
   66: ## Localization notes:
   67: ##
   68: ## in @Fields[0]->{'long_title'} is placed in Excel files and is used as the
   69: ## header for plots created with Graph.pm, both of which more than likely do
   70: ## not support localization.
   71: ##
   72: my @Fields = (
   73:            { name => 'problem_num',
   74:              title => 'P#',
   75:              align => 'right',
   76:              color => '#FFFFE6' },
   77:            { name   => 'container',
   78:              title  => 'Sequence or Folder',
   79:              align  => 'left',
   80:              color  => '#FFFFE6',
   81:              sortable => 'yes' },
   82:            { name   => 'title',
   83:              title  => 'Title',
   84:              align  => 'left',
   85:              color  => '#FFFFE6',
   86:              special  => 'link',
   87:              sortable => 'yes', },
   88:            { name   => 'part', 
   89:              title  => 'Part',
   90:              align  => 'left',
   91:              color  => '#FFFFE6',
   92:              },
   93:            { name   => 'num_students',
   94:              title  => '#Stdnts',
   95:              align  => 'right',
   96:              color  => '#EEFFCC',
   97:              format => '%d',
   98:              sortable  => 'yes',
   99:              graphable => 'yes',
  100:              long_title => 'Number of Students Attempting Problem' },
  101:            { name   => 'tries',
  102:              title  => 'Tries',
  103:              align  => 'right',
  104:              color  => '#EEFFCC',
  105:              format => '%d',
  106:              sortable  => 'yes',
  107:              graphable => 'yes',
  108:              long_title => 'Total Number of Tries' },
  109:            { name   => 'max_tries',
  110:              title  => 'Max Tries',
  111:              align  => 'right',
  112:              color  => '#DDFFFF',
  113:              format => '%d',
  114:              sortable  => 'yes',
  115:              graphable => 'yes',
  116:              long_title => 'Maximum Number of Tries' },
  117:            { name   => 'min_tries',
  118:              title  => 'Min Tries',
  119:              align  => 'right',
  120:              color  => '#DDFFFF',
  121:              format => '%d',
  122:              sortable  => 'yes',
  123:              graphable => 'yes',
  124:              long_title => 'Minumum Number of Tries' },
  125:            { name   => 'mean_tries',
  126:              title  => 'Mean Tries',
  127:              align  => 'right',
  128:              color  => '#DDFFFF',
  129:              format => '%5.2f',
  130:              sortable  => 'yes',
  131:              graphable => 'yes',
  132:              long_title => 'Average Number of Tries' },
  133:            { name   => 'std_tries',
  134:              title  => 'S.D. tries',
  135:              align  => 'right',
  136:              color  => '#DDFFFF',
  137:              format => '%5.2f',
  138:              sortable  => 'yes',
  139:              graphable => 'yes',
  140:              long_title => 'Standard Deviation of Number of Tries' },
  141:            { name   => 'skew_tries',
  142:              title  => 'Skew Tries',
  143:              align  => 'right',
  144:              color  => '#DDFFFF',
  145:              format => '%5.2f',
  146:              sortable  => 'yes',
  147:              graphable => 'yes',
  148:              long_title => 'Skew of Number of Tries' },
  149:            { name   => 'num_solved',
  150:              title  => '#YES',
  151:              align  => 'right',
  152:              color  => '#FFDDDD',
  153:              format => '%4.1f',#             format => '%d',
  154:              sortable  => 'yes',
  155:              graphable => 'yes',
  156:              long_title => 'Number of Students able to Solve' },
  157:            { name   => 'num_override',
  158:              title  => '#yes',
  159:              align  => 'right',
  160:              color  => '#FFDDDD',
  161:              format => '%4.1f',#             format => '%d',
  162:              sortable  => 'yes',
  163:              graphable => 'yes',
  164:              long_title => 'Number of Students given Override' },
  165:            { name   => 'num_wrong',
  166:              title  => '#Wrng',
  167:              align  => 'right',
  168:              color  => '#FFDDDD',
  169:              format => '%4.1f',
  170:              sortable  => 'yes',
  171:              graphable => 'yes',
  172:              long_title => 'Percent of students whose final answer is wrong' },
  173:            { name   => 'deg_of_diff',
  174:              title  => 'DoDiff',
  175:              align  => 'right',
  176:              color  => '#FFFFE6',
  177:              format => '%5.2f',
  178:              sortable  => 'yes',
  179:              graphable => 'yes',
  180:              long_title => 'Degree of Difficulty'.
  181:                            '[ 1 - ((#YES+#yes) / Tries) ]'},
  182:            { name   => 'deg_of_disc',
  183:              title  => 'DoDisc',
  184:              align  => 'right',
  185:              color  => '#FFFFE6',
  186:              format => '%4.2f',
  187:              sortable  => 'yes',
  188:              graphable => 'yes',
  189:              long_title => 'Degree of Discrimination' },
  190: );
  191: 
  192: ###############################################
  193: ###############################################
  194: 
  195: =pod 
  196: 
  197: =item &CreateInterface()
  198: 
  199: Create the main intereface for the statistics page.  Allows the user to
  200: select sections, maps, and output.
  201: 
  202: =cut
  203: 
  204: ###############################################
  205: ###############################################
  206: sub CreateInterface {
  207:     my $Str = '';
  208:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  209:         (undef,'Overall Problem Statistics','Statistics_Overall_Key');
  210:     $Str .= '<table cellspacing="5">'."\n";
  211:     $Str .= '<tr>';
  212:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  213:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  214:     $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
  215:     $Str .= '<td rowspan="2">'.
  216:         &Apache::lonstathelpers::limit_by_time_form().'</td>';
  217:     $Str .= '</tr>'."\n";
  218:     #
  219:     $Str .= '<tr><td align="center">'."\n";
  220:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  221:     $Str .= '</td><td align="center">';
  222:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  223:     $Str .= '</td><td align="center">';
  224:     #
  225:     my $only_seq_with_assessments = sub { 
  226:         my $s=shift;
  227:         if ($s->{'num_assess'} < 1) { 
  228:             return 0;
  229:         } else { 
  230:             return 1;
  231:         }
  232:     };
  233:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  234:                                               $only_seq_with_assessments);
  235:     $Str .= '</td></tr>'."\n";
  236:     $Str .= '</table>'."\n";
  237:     $Str .= '<input type="submit" name="GenerateStatistics" value="'.
  238:         &mt('Generate Statistics').'" />';
  239:     $Str .= '&nbsp;'x5;
  240:     $Str .= 'Plot '.&plot_dropdown().('&nbsp;'x10);
  241:     $Str .= '<input type="submit" name="ClearCache" value="'.
  242:         &mt('Clear Caches').'" />';
  243:     $Str .= '&nbsp;'x5;
  244:     $Str .= '<input type="submit" name="UpdateCache" value="'.
  245:         &mt('Update Student Data').'" />';
  246:     $Str .= '&nbsp;'x5;
  247:     $Str .= '<input type="submit" name="Excel" value="'.
  248:         &mt('Produce Excel Output').'" />';
  249:     $Str .= '&nbsp;'x5;
  250:     return $Str;
  251: }
  252: 
  253: ###############################################
  254: ###############################################
  255: 
  256: =pod 
  257: 
  258: =item &BuildProblemStatisticsPage()
  259: 
  260: Main interface to problem statistics.
  261: 
  262: =cut
  263: 
  264: ###############################################
  265: ###############################################
  266: sub BuildProblemStatisticsPage {
  267:     my ($r,$c)=@_;
  268:     #
  269:     my %Saveable_Parameters = ('Status' => 'scalar',
  270:                                'statsoutputmode' => 'scalar',
  271:                                'Section' => 'array',
  272:                                'StudentData' => 'array',
  273:                                'Maps' => 'array');
  274:     &Apache::loncommon::store_course_settings('statistics',
  275:                                               \%Saveable_Parameters);
  276:     &Apache::loncommon::restore_course_settings('statistics',
  277:                                                 \%Saveable_Parameters);
  278:     #
  279:     &Apache::lonstatistics::PrepareClasslist();
  280:     #
  281:     # Clear the package variables
  282:     undef(@StatsArray);
  283:     #
  284:     # Finally let the user know we are here
  285:     my $interface = &CreateInterface();
  286:     $r->print($interface);
  287:     $r->print('<input type="hidden" name="sortby" value="'.$ENV{'form.sortby'}.
  288:               '" />');
  289:     #
  290:     if (! exists($ENV{'form.statsfirstcall'})) {
  291:         $r->print('<input type="hidden" name="statsfirstcall" value="yes" />');
  292:         $r->print('<h3>'.
  293:                   &mt('Press "Generate Statistics" when you are ready.').
  294:                   '</h3><p>'.
  295:                   &mt('It may take some time to update the student data '.
  296:                       'for the first analysis.  Future analysis this session '.
  297:                       ' will not have this delay.').
  298:                   '</p>');
  299:         return;
  300:     } elsif ($ENV{'form.statsfirstcall'} eq 'yes' || 
  301:              exists($ENV{'form.UpdateCache'}) ||
  302:              exists($ENV{'form.ClearCache'}) ) {
  303:         $r->print('<input type="hidden" name="statsfirstcall" value="no" />');
  304:         &Apache::lonstatistics::Gather_Student_Data($r);
  305:     } else {
  306:         $r->print('<input type="hidden" name="statsfirstcall" value="no" />');
  307:     }
  308:     $r->rflush();
  309:     #
  310:     # This probably does not need to be done each time we are called, but
  311:     # it does not slow things down noticably.
  312:     &Apache::loncoursedata::populate_weight_table();
  313:     #
  314:     if (exists($ENV{'form.Excel'})) {
  315:         &Excel_output($r);
  316:     } else {
  317:         my $count = 0;
  318:         foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  319:             $count += $seq->{'num_assess'};
  320:         }
  321:         if ($count > 10) {
  322:             $r->print('<h2>'.
  323:                       &mt('Compiling statistics for [_1] problems',$count).
  324:                       '</h2>');
  325:             if ($count > 30) {
  326:                 $r->print('<h3>'.&mt('This will take some time.').'</h3>');
  327:             }
  328:             $r->rflush();
  329:         }
  330:         #
  331:         my $sortby = $ENV{'form.sortby'};
  332:         $sortby = 'container' if (! defined($sortby) || $sortby =~ /^\s*$/);
  333:         my $plot = $ENV{'form.plot'};
  334:         if ($plot eq '' || $plot eq 'none') {
  335:             undef($plot);
  336:         }
  337:         if ($sortby eq 'container' && ! defined($plot)) {
  338:             &output_html_by_sequence($r);
  339:         } else {
  340:             if (defined($plot)) {
  341:                 &make_plot($r,$plot);
  342:             }
  343:             &output_html_stats($r);
  344:         }
  345:     }
  346:     return;
  347: }
  348: 
  349: ##########################################################
  350: ##########################################################
  351: ##
  352: ## HTML output routines
  353: ##
  354: ##########################################################
  355: ##########################################################
  356: sub output_html_by_sequence {
  357:     my ($r) = @_;
  358:     my $c = $r->connection();
  359:     $r->print(&html_preamble());
  360:     #
  361:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  362:         last if ($c->aborted);
  363:         next if ($seq->{'num_assess'} < 1);
  364:         $r->print("<h3>".$seq->{'title'}."</h3>".
  365:                   '<table border="0"><tr><td bgcolor="#777777">'."\n".
  366:                   '<table border="0" cellpadding="3">'."\n".
  367:                   '<tr bgcolor="#FFFFE6">'.
  368:                   &statistics_table_header('no container')."</tr>\n");
  369:         my @Data = &compute_statistics_on_sequence($seq);
  370:         foreach my $data (@Data) {
  371:             $r->print('<tr>'.&statistics_html_table_data($data,
  372:                                                          'no container').
  373:                       "</tr>\n");
  374:         }
  375:         $r->print('</table>'."\n".'</table>'."\n");
  376:         $r->rflush();
  377:     }
  378:     return;
  379: }
  380: 
  381: sub output_html_stats {
  382:     my ($r)=@_;
  383:     &compute_all_statistics($r);
  384:     $r->print(&html_preamble());
  385:     &sort_data($ENV{'form.sortby'});
  386:     #
  387:     my $count=0;
  388:     foreach my $data (@StatsArray) {
  389:         if ($count++ % 50 == 0) {
  390:             $r->print("</table>\n</table>\n");
  391:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
  392:                       '<table border="0" cellpadding="3">'."\n".
  393:                       '<tr bgcolor="#FFFFE6">'.
  394:                       '<tr bgcolor="#FFFFE6">'.
  395:                       &statistics_table_header().
  396:                       "</tr>\n");
  397:         }
  398:         $r->print('<tr>'.&statistics_html_table_data($data)."</tr>\n");
  399:     }
  400:     $r->print("</table>\n</table>\n");
  401:     return;
  402: }
  403: 
  404: sub html_preamble {
  405:     my $Str='';
  406:     $Str .= "<h2>".
  407:         $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  408:         "</h2>\n";
  409:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
  410:     if (defined($starttime) || defined($endtime)) {
  411:         # Inform the user what the time limits on the data are.
  412:         $Str .= '<h3>'.&mt('Statistics on submissions from [_1] to [_2]',
  413:                            &Apache::lonlocal::locallocaltime($starttime),
  414:                            &Apache::lonlocal::locallocaltime($endtime)
  415:                            ).'</h3>';
  416:     }
  417:     $Str .= "<h3>".&mt('Compiled on [_1]',
  418:                        &Apache::lonlocal::locallocaltime(time))."</h3>";
  419:     return $Str;
  420: }
  421: 
  422: 
  423: ###############################################
  424: ###############################################
  425: ##
  426: ## Misc HTML output routines
  427: ##
  428: ###############################################
  429: ###############################################
  430: sub statistics_html_table_data {
  431:     my ($data,$options) = @_;
  432:     my $row = '';
  433:     foreach my $field (@Fields) {
  434:         next if ($options =~ /no $field->{'name'}/);
  435:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  436:         if (exists($field->{'align'})) {
  437:             $row .= ' align="'.$field->{'align'}.'"';
  438:             }
  439:         $row .= '>';
  440:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  441:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
  442:         }
  443:         if (exists($field->{'format'})) {
  444:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  445:         } else {
  446:             $row .= $data->{$field->{'name'}};
  447:         }
  448:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  449:             $row.= '</a>';
  450:         }
  451:         $row .= '</td>';
  452:     }
  453:     return $row;
  454: }
  455: 
  456: sub statistics_table_header {
  457:     my ($options) = @_;
  458:     my $header_row;
  459:     foreach my $field (@Fields) {
  460:         next if ($options =~ /no $field->{'name'}/);
  461:         $header_row .= '<th>';
  462:         if (exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
  463:             $header_row .= '<a href="javascript:'.
  464:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
  465:                     ';document.Statistics.submit();">';
  466:         }
  467:         $header_row .= &mt($field->{'title'});
  468:         if ($options =~ /sortable/) {
  469:             $header_row.= '</a>';
  470:         }
  471:         if ($options !~ /no plots/        && 
  472:             exists($field->{'graphable'}) && 
  473:             $field->{'graphable'} eq 'yes') {
  474:             $header_row.=' (';
  475:             $header_row .= '<a href="javascript:'.
  476:                 "document.Statistics.plot.value='$field->{'name'}'".
  477:                     ';document.Statistics.submit();">';
  478:             $header_row .= &mt('plot').'</a>)';
  479:         }
  480:         $header_row .= '</th>';
  481:     }
  482:     return $header_row;
  483: }
  484: 
  485: ####################################################
  486: ####################################################
  487: ##
  488: ##    Plotting Routines
  489: ##
  490: ####################################################
  491: ####################################################
  492: sub make_plot {
  493:     my ($r,$plot) = @_;
  494:     &compute_all_statistics($r);
  495:     &sort_data($ENV{'form.sortby'});
  496:     if ($plot eq 'degrees') {
  497:         &degrees_plot($r);
  498:     } elsif ($plot eq 'tries statistics') {
  499:         &tries_data_plot($r);
  500:     } else {
  501:         &make_single_stat_plot($r,$plot);
  502:     }
  503:     return;
  504: }
  505: 
  506: sub make_single_stat_plot {
  507:     my ($r,$datafield) = @_;
  508:     #
  509:     my $title; my $yaxis;
  510:     foreach my $field (@Fields) {
  511:         next if ($field->{'name'} ne $datafield);
  512:         $title = $field->{'long_title'};
  513:         $yaxis = $field->{'title'};
  514:         last;
  515:     }
  516:     if ($title eq '' || $yaxis eq '') {
  517:         # datafield is something we do not know enough about to plot
  518:         $r->print('<h3>'.
  519:                   &mt('Unable to plot the requested statistic.').
  520:                   '</h3>');
  521:         return;
  522:     }
  523:     #
  524:     # Build up the data sets to plot
  525:     my @Labels; 
  526:     my @Data;
  527:     my $max = 1;
  528:     foreach my $data (@StatsArray) {
  529:         push(@Labels,$data->{'problem_num'});
  530:         push(@Data,$data->{$datafield});
  531:         if ($data->{$datafield}>$max) {
  532:             $max = $data->{$datafield};
  533:         }
  534:     }
  535:     foreach (1,2,3,4,5,10,15,20,25,40,50,75,100,150,200,250,300,500,600,750,
  536:              1000,1500,2000,2500,3000,3500,4000,5000,7500,10000,15000,20000) {
  537:         if ($max <= $_) {
  538:             $max = $_;
  539:             last;
  540:         }
  541:     }
  542:     if ($max > 20000) {
  543:         $max = 10000*(int($max/10000)+1);
  544:     }
  545:     #
  546:     $r->print("<p>".&Apache::loncommon::DrawBarGraph($title,
  547:                                                      'Problem Number',
  548:                                                      $yaxis,
  549:                                                      $max,
  550:                                                      undef, # colors
  551:                                                      \@Labels,
  552:                                                      \@Data)."</p>\n");
  553:     return;
  554: }
  555: 
  556: sub degrees_plot {
  557:     my ($r)=@_;
  558:     my $count = scalar(@StatsArray);
  559:     my $width = 50 + 10*$count;
  560:     $width = 300 if ($width < 300);
  561:     my $height = 300;
  562:     my $plot = '';
  563:     my $ymax = 0;
  564:     my $ymin = 0;
  565:     my @Disc; my @Diff; my @Labels;    
  566:     foreach my $data (@StatsArray) {
  567:         push(@Labels,$data->{'problem_num'});
  568:         my $disc = $data->{'deg_of_disc'};
  569:         my $diff = $data->{'deg_of_diff'};
  570:         push(@Disc,$disc);
  571:         push(@Diff,$diff);
  572:         #
  573:         $ymin = $disc if ($ymin > $disc);
  574:         $ymin = $diff if ($ymin > $diff);
  575:         $ymax = $disc if ($ymax < $disc);
  576:         $ymax = $diff if ($ymax < $diff);
  577:     }
  578:     #
  579:     # Make sure we show relevant information.
  580:     if ($ymin < 0) {
  581:         if (abs($ymin) < 0.05) {
  582:             $ymin = 0;
  583:         } else {
  584:             $ymin = -1;
  585:         }
  586:     }
  587:     if ($ymax > 0) {
  588:         if (abs($ymax) < 0.05) {
  589:             $ymax = 0;
  590:         } else {
  591:             $ymax = 1;
  592:         }
  593:     }
  594:     #
  595:     my $xmax = $Labels[-1];
  596:     if ($xmax > 50) {
  597:         if ($xmax % 10 != 0) {
  598:             $xmax = 10 * (int($xmax/10)+1);
  599:         }
  600:     } else {
  601:         if ($xmax % 5 != 0) {
  602:             $xmax = 5 * (int($xmax/5)+1);
  603:         }
  604:     }
  605:     #
  606:     my $discdata .= '<data>'.join(',',@Labels).'</data>'.$/.
  607:                     '<data>'.join(',',@Disc).'</data>'.$/;
  608:     #
  609:     my $diffdata .= '<data>'.join(',',@Labels).'</data>'.$/.
  610:                     '<data>'.join(',',@Diff).'</data>'.$/;
  611:     #
  612:     $plot=<<"END";
  613: <gnuplot 
  614:     texfont="10"
  615:     fgcolor="x000000"
  616:     plottype="Cartesian"
  617:     font="large"
  618:     grid="on"
  619:     align="center"
  620:     border="on"
  621:     transparent="on"
  622:     alttag="Sample Plot"
  623:     samples="100"
  624:     bgcolor="xffffff"
  625:     height="$height"
  626:     width="$width">
  627:     <key 
  628:         pos="top right"
  629:         title=""
  630:         box="off" />
  631:     <title>Degree of Discrmination and Degree of Difficulty</title>
  632:     <axis xmin="0" ymin="$ymin" xmax="$xmax" ymax="$ymax" color="x000000" />
  633:     <xlabel>Problem Number</xlabel>
  634:     <curve 
  635:         linestyle="linespoints" 
  636:         name="DoDisc" 
  637:         pointtype="0" 
  638:         color="x000000">
  639:         $discdata
  640:     </curve>
  641:     <curve 
  642:         linestyle="linespoints" 
  643:         name="DoDiff" 
  644:         pointtype="0" 
  645:         color="xFF0000">
  646:         $diffdata
  647:     </curve>
  648: </gnuplot>
  649: END
  650:     my $plotresult = 
  651:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
  652:     $r->print($plotresult);
  653:     return;
  654: }
  655: 
  656: sub tries_data_plot {
  657:     my ($r)=@_;
  658:     my $count = scalar(@StatsArray);
  659:     my $width = 50 + 10*$count;
  660:     $width = 300 if ($width < 300);
  661:     my $height = 300;
  662:     my $plot = '';
  663:     my @STD;  my @Mean; my @Max; my @Min;
  664:     my @Labels;
  665:     my $ymax = 5;
  666:     foreach my $data (@StatsArray) {
  667:         my $max = $data->{'mean_tries'} + $data->{'std_tries'};
  668:         $ymax = $max if ($ymax < $max);
  669:         $ymax = $max if ($ymax < $max);
  670:         push(@Labels,$data->{'problem_num'});
  671:         push(@STD,$data->{'std_tries'});
  672:         push(@Mean,$data->{'mean_tries'});
  673:     }
  674:     #
  675:     # Make sure we show relevant information.
  676:     my $xmax = $Labels[-1];
  677:     if ($xmax > 50) {
  678:         if ($xmax % 10 != 0) {
  679:             $xmax = 10 * (int($xmax/10)+1);
  680:         }
  681:     } else {
  682:         if ($xmax % 5 != 0) {
  683:             $xmax = 5 * (int($xmax/5)+1);
  684:         }
  685:     }
  686:     $ymax = int($ymax)+1+2;
  687:     #
  688:     my $std_data .= '<data>'.join(',',@Labels).'</data>'.$/.
  689:                     '<data>'.join(',',@Mean).'</data>'.$/;
  690:     #
  691:     my $std_error_data .= '<data>'.join(',',@Labels).'</data>'.$/.
  692:                           '<data>'.join(',',@Mean).'</data>'.$/.
  693:                           '<data>'.join(',',@STD).'</data>'.$/;
  694:     #
  695:     $plot=<<"END";
  696: <gnuplot 
  697:     texfont="10"
  698:     fgcolor="x000000"
  699:     plottype="Cartesian"
  700:     font="large"
  701:     grid="on"
  702:     align="center"
  703:     border="on"
  704:     transparent="on"
  705:     alttag="Sample Plot"
  706:     samples="100"
  707:     bgcolor="xffffff"
  708:     height="$height"
  709:     width="$width">
  710:     <title>Mean and S.D. of Tries</title>
  711:     <axis xmin="0" ymin="0" xmax="$xmax" ymax="$ymax" color="x000000" />
  712:     <xlabel>Problem Number</xlabel>
  713:     <curve 
  714:         linestyle="yerrorbars"
  715:         name="S.D. Tries" 
  716:         pointtype="1" 
  717:         color="x666666">
  718:         $std_error_data
  719:     </curve>
  720:     <curve 
  721:         linestyle="points"
  722:         name="Mean Tries" 
  723:         pointtype="1" 
  724:         color="xCC4444">
  725:         $std_data
  726:     </curve>
  727: </gnuplot>
  728: END
  729:     my $plotresult = 
  730:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
  731:     $r->print($plotresult);
  732:     return;
  733: }
  734: 
  735: sub plot_dropdown {
  736:     my $current = '';
  737:     #
  738:     if (defined($ENV{'form.plot'})) {
  739:         $current = $ENV{'form.plot'};
  740:     }
  741:     #
  742:     my @Additional_Plots = (
  743:                             { graphable=>'yes',
  744:                               name => 'degrees',
  745:                               title => 'DoDisc and DoDiff' },
  746:                             { graphable=>'yes',
  747:                               name => 'tries statistics',
  748:                               title => 'Mean and S.D. of Tries' });
  749:     #
  750:     my $Str= "\n".'<select name="plot" size="1">';
  751:     $Str .= '<option name="none"></option>'."\n";
  752:     $Str .= '<option name="none2">none</option>'."\n";
  753:     foreach my $field (@Fields,@Additional_Plots) {
  754:         if (! exists($field->{'graphable'}) ||
  755:             $field->{'graphable'} ne 'yes') {
  756:             next;
  757:         }
  758:         $Str .= '<option value="'.$field->{'name'}.'"';
  759:         if ($field->{'name'} eq $current) {
  760:             $Str .= ' selected ';
  761:         }
  762:         $Str.= '>'.&mt($field->{'title'}).'</option>'."\n";
  763:     }
  764:     $Str .= '</select>'."\n";
  765:     return $Str;
  766: }
  767: 
  768: ###############################################
  769: ###############################################
  770: ##
  771: ## Excel output routines
  772: ##
  773: ###############################################
  774: ###############################################
  775: sub Excel_output {
  776:     my ($r) = @_;
  777:     $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
  778:     ##
  779:     ## Compute the statistics
  780:     &compute_all_statistics($r);
  781:     my $c = $r->connection;
  782:     return if ($c->aborted());
  783:     ##
  784:     ## Create the excel workbook
  785:     my $filename = '/prtspool/'.
  786:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  787:         time.'_'.rand(1000000000).'.xls';
  788:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
  789:     #
  790:     # Create sheet
  791:     my $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  792:     #
  793:     # Check for errors
  794:     if (! defined($excel_workbook)) {
  795:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  796:         $r->print(&mt("Problems creating new Excel file.  ".
  797:                   "This error has been logged.  ".
  798:                   "Please alert your LON-CAPA administrator."));
  799:         return 0;
  800:     }
  801:     #
  802:     # The excel spreadsheet stores temporary data in files, then put them
  803:     # together.  If needed we should be able to disable this (memory only).
  804:     # The temporary directory must be specified before calling 'addworksheet'.
  805:     # File::Temp is used to determine the temporary directory.
  806:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  807:     #
  808:     # Add a worksheet
  809:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  810:     if (length($sheetname) > 31) {
  811:         $sheetname = substr($sheetname,0,31);
  812:     }
  813:     my $excel_sheet = $excel_workbook->addworksheet(
  814:         &Apache::loncommon::clean_excel_name($sheetname));
  815:     ##
  816:     ## Begin creating excel sheet
  817:     ##
  818:     my ($rows_output,$cols_output) = (0,0);
  819:     #
  820:     # Put the course description in the header
  821:     $excel_sheet->write($rows_output,$cols_output++,
  822:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
  823:     $cols_output += 3;
  824:     #
  825:     # Put a description of the sections listed
  826:     my $sectionstring = '';
  827:     my @Sections = @Apache::lonstatistics::SelectedSections;
  828:     if (scalar(@Sections) > 1) {
  829:         if (scalar(@Sections) > 2) {
  830:             my $last = pop(@Sections);
  831:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
  832:         } else {
  833:             $sectionstring = "Sections ".join(' and ',@Sections);
  834:         }
  835:     } else {
  836:         if ($Sections[0] eq 'all') {
  837:             $sectionstring = "All sections";
  838:         } else {
  839:             $sectionstring = "Section ".$Sections[0];
  840:         }
  841:     }
  842:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
  843:     $cols_output += scalar(@Sections);
  844:     #
  845:     # Time restrictions
  846:     my $time_string;
  847:     if (defined($starttime)) {
  848:         # call localtime but not lonlocal:locallocaltime because excel probably
  849:         # cannot handle localized text.  Probably.
  850:         $time_string .= 'Data collected from '.localtime($time_string);
  851:         if (defined($endtime)) {
  852:             $time_string .= ' to '.localtime($endtime);
  853:         }
  854:         $time_string .= '.';
  855:     } elsif (defined($endtime)) {
  856:         # See note above about lonlocal:locallocaltime
  857:         $time_string .= 'Data collected before '.localtime($endtime).'.';
  858:     }
  859:     #
  860:     # Put the date in there too
  861:     $excel_sheet->write($rows_output,$cols_output++,
  862:                         'Compiled on '.localtime(time));
  863:     #
  864:     $rows_output++; 
  865:     $cols_output=0;
  866:     #
  867:     # Long Headers
  868:     foreach my $field (@Fields) {
  869:         next if ($field->{'name'} eq 'problem_num');
  870:         if (exists($field->{'long_title'})) {
  871:             $excel_sheet->write($rows_output,$cols_output++,
  872:                                 $field->{'long_title'});
  873:         } else {
  874:             $excel_sheet->write($rows_output,$cols_output++,'');
  875:         }
  876:     }
  877:     $rows_output++;
  878:     $cols_output=0;
  879:     # Brief headers
  880:     foreach my $field (@Fields) {
  881:         next if ($field->{'name'} eq 'problem_num');
  882:         # Use english for excel as I am not sure how well excel handles 
  883:         # other character sets....
  884:         $excel_sheet->write($rows_output,$cols_output++,$field->{'title'});
  885:     }
  886:     $rows_output++;
  887:     foreach my $data (@StatsArray) {
  888:         $cols_output=0;
  889:         foreach my $field (@Fields) {
  890:             next if ($field->{'name'} eq 'problem_num');
  891:             $excel_sheet->write($rows_output,$cols_output++,
  892:                                 $data->{$field->{'name'}});
  893:         }
  894:         $rows_output++;
  895:     }
  896:     #
  897:     $excel_workbook->close();
  898:     #
  899:     # Tell the user where to get their excel file
  900:     $r->print('<br />'.
  901:               '<a href="'.$filename.'">'.
  902:               &mt('Your Excel Spreadsheet').'</a>'."\n");
  903:     $r->rflush();
  904:     return;
  905: }
  906: 
  907: ##################################################
  908: ##################################################
  909: ##
  910: ## Statistics Gathering and Manipulation Routines
  911: ##
  912: ##################################################
  913: ##################################################
  914: sub compute_statistics_on_sequence {
  915:     my ($seq) = @_;
  916:     my @Data;
  917:     foreach my $res (@{$seq->{'contents'}}) {
  918:         next if ($res->{'type'} ne 'assessment');
  919:         foreach my $part (@{$res->{'parts'}}) {
  920:             #
  921:             # This is where all the work happens
  922:             my $data = &get_statistics($seq,$res,$part,scalar(@StatsArray)+1);
  923:             push (@Data,$data);
  924:             push (@StatsArray,$data);
  925:         }
  926:     }
  927:     return @Data;
  928: }
  929: 
  930: sub compute_all_statistics {
  931:     my ($r) = @_;
  932:     if (@StatsArray > 0) {
  933:         # Assume we have already computed the statistics
  934:         return;
  935:     }
  936:     my $c = $r->connection;
  937:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  938:         last if ($c->aborted);
  939:         next if ($seq->{'num_assess'} < 1);
  940:         &compute_statistics_on_sequence($seq);
  941:     }
  942: }
  943: 
  944: sub sort_data {
  945:     my ($sortkey) = @_;
  946:     return if (! @StatsArray);
  947:     #
  948:     # Sort the data
  949:     my $sortby = undef;
  950:     foreach my $field (@Fields) {
  951:         if ($sortkey eq $field->{'name'}) {
  952:             $sortby = $field->{'name'};
  953:         }
  954:     }
  955:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
  956:         $sortby = 'container';
  957:     }
  958:     if ($sortby ne 'container') {
  959:         # $sortby is already defined, so we can charge ahead
  960:         if ($sortby =~ /^(title|part)$/i) {
  961:             # Alpha comparison
  962:             @StatsArray = sort {
  963:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
  964:                 lc($a->{'title'}) cmp lc($b->{'title'}) ||
  965:                 lc($a->{'part'}) cmp lc($b->{'part'});
  966:             } @StatsArray;
  967:         } else {
  968:             # Numerical comparison
  969:             @StatsArray = sort {
  970:                 my $retvalue = 0;
  971:                 if ($b->{$sortby} eq 'nan') {
  972:                     if ($a->{$sortby} ne 'nan') {
  973:                         $retvalue = -1;
  974:                     } else {
  975:                         $retvalue = 0;
  976:                     }
  977:                 }
  978:                 if ($a->{$sortby} eq 'nan') {
  979:                     if ($b->{$sortby} ne 'nan') {
  980:                         $retvalue = 1;
  981:                     }
  982:                 }
  983:                 if ($retvalue eq '0') {
  984:                     $retvalue = $b->{$sortby} <=> $a->{$sortby}     ||
  985:                             lc($a->{'title'}) <=> lc($b->{'title'}) ||
  986:                             lc($a->{'part'})  <=> lc($b->{'part'});
  987:                 }
  988:                 $retvalue;
  989:             } @StatsArray;
  990:         }
  991:     }
  992:     #
  993:     # Renumber the data set
  994:     my $count;
  995:     foreach my $data (@StatsArray) {
  996:         $data->{'problem_num'} = ++$count;
  997:     }
  998:     return;
  999: }
 1000: 
 1001: ########################################################
 1002: ########################################################
 1003: 
 1004: =pod
 1005: 
 1006: =item &get_statistics()
 1007: 
 1008: Wrapper routine from the call to loncoursedata::get_problem_statistics.  
 1009: Calls lonstathelpers::get_time_limits() to limit the data set by time
 1010: and &compute_discrimination_factor
 1011: 
 1012: Inputs: $sequence, $resource, $part, $problem_num
 1013: 
 1014: Returns: Hash reference with statistics data from 
 1015: loncoursedata::get_problem_statistics.
 1016: 
 1017: =cut
 1018: 
 1019: ########################################################
 1020: ########################################################
 1021: sub get_statistics {
 1022:     my ($sequence,$resource,$part,$problem_num) = @_;
 1023:     #
 1024:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1025:     my $symb = $resource->{'symb'};
 1026:     my $courseid = $ENV{'request.course.id'};
 1027:     #
 1028:     my $data = &Apache::loncoursedata::get_problem_statistics
 1029:                         (\@Apache::lonstatistics::SelectedSections,
 1030:                          $Apache::lonstatistics::enrollment_status,
 1031:                          $symb,$part,$courseid,$starttime,$endtime);
 1032:     $data->{'part'}        = $part;
 1033:     $data->{'problem_num'} = $problem_num;
 1034:     $data->{'container'}   = $sequence->{'title'};
 1035:     $data->{'title'}       = $resource->{'title'};
 1036:     $data->{'title.link'}  = $resource->{'src'}.'?symb='.
 1037:         &Apache::lonnet::escape($resource->{'symb'});
 1038:     #
 1039:     $data->{'deg_of_disc'} = &compute_discrimination_factor($resource,$part,$sequence);
 1040:     return $data;
 1041: }
 1042: 
 1043: 
 1044: ###############################################
 1045: ###############################################
 1046: 
 1047: =pod
 1048: 
 1049: =item &compute_discrimination_factor()
 1050: 
 1051: Inputs: $Resource, $Sequence
 1052: 
 1053: Returns: integer between -1 and 1
 1054: 
 1055: =cut
 1056: 
 1057: ###############################################
 1058: ###############################################
 1059: sub compute_discrimination_factor {
 1060:     my ($resource,$part,$sequence) = @_;
 1061:     my @Resources;
 1062:     foreach my $res (@{$sequence->{'contents'}}) {
 1063:         next if ($res->{'symb'} eq $resource->{'symb'});
 1064:         push (@Resources,$res->{'symb'});
 1065:     }
 1066:     #
 1067:     # rank
 1068:     my $ranking = 
 1069:         &Apache::loncoursedata::rank_students_by_scores_on_resources
 1070:         (\@Resources,
 1071:          \@Apache::lonstatistics::SelectedSections,
 1072:          $Apache::lonstatistics::enrollment_status,undef);
 1073:     #
 1074:     # compute their percent scores on the problems in the sequence,
 1075:     my $number_to_grab = int(scalar(@{$ranking})/4);
 1076:     my $num_students = scalar(@{$ranking});
 1077:     my @BottomSet = map { $_->[&Apache::loncoursedata::RNK_student()]; 
 1078:                       } @{$ranking}[0..$number_to_grab];
 1079:     my @TopSet    = 
 1080:         map { 
 1081:             $_->[&Apache::loncoursedata::RNK_student()]; 
 1082:           } @{$ranking}[($num_students-$number_to_grab)..($num_students-1)];
 1083:     my ($bottom_sum,$bottom_max) = 
 1084:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@BottomSet);
 1085:     my ($top_sum,$top_max) = 
 1086:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@TopSet);
 1087:     my $deg_of_disc;
 1088:     if ($top_max == 0 || $bottom_max==0) {
 1089:         $deg_of_disc = 'nan';
 1090:     } else {
 1091:         $deg_of_disc = ($top_sum/$top_max) - ($bottom_sum/$bottom_max);
 1092:     }
 1093:     #&Apache::lonnet::logthis('    '.$top_sum.'/'.$top_max.
 1094:     #                         ' - '.$bottom_sum.'/'.$bottom_max);
 1095:     return $deg_of_disc;
 1096: }
 1097: 
 1098: ###############################################
 1099: ###############################################
 1100: 
 1101: =pod 
 1102: 
 1103: =item ProblemStatisticsLegend
 1104: 
 1105: =over 4
 1106: 
 1107: =item #Stdnts
 1108: Total number of students attempted the problem.
 1109: 
 1110: =item Tries
 1111: Total number of tries for solving the problem.
 1112: 
 1113: =item Max Tries
 1114: Largest number of tries for solving the problem by a student.
 1115: 
 1116: =item Mean
 1117: Average number of tries. [ Tries / #Stdnts ]
 1118: 
 1119: =item #YES
 1120: Number of students solved the problem correctly.
 1121: 
 1122: =item #yes
 1123: Number of students solved the problem by override.
 1124: 
 1125: =item %Wrong
 1126: Percentage of students who tried to solve the problem 
 1127: but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]
 1128: 
 1129: =item DoDiff
 1130: Degree of Difficulty of the problem.  
 1131: [ 1 - ((#YES+#yes) / Tries) ]
 1132: 
 1133: =item S.D.
 1134: Standard Deviation of the tries.  
 1135: [ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) 
 1136: where Xi denotes every student\'s tries ]
 1137: 
 1138: =item Skew.
 1139: Skewness of the students tries.
 1140: [(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]
 1141: 
 1142: =item Dis.F.
 1143: Discrimination Factor: A Standard for evaluating the 
 1144: problem according to a Criterion<br>
 1145: 
 1146: =item [Criterion to group students into %27 Upper Students - 
 1147: and %27 Lower Students]
 1148: 1st Criterion for Sorting the Students: 
 1149: Sum of Partial Credit Awarded / Total Number of Tries
 1150: 2nd Criterion for Sorting the Students: 
 1151: Total number of Correct Answers / Total Number of Tries
 1152: 
 1153: =item Disc.
 1154: Number of Students had at least one discussion.
 1155: 
 1156: =back
 1157: 
 1158: =cut
 1159: 
 1160: 
 1161: ############################################################
 1162: ############################################################
 1163: ##
 1164: ##  How this all works:
 1165: ##     Statistics are computed by calling &get_statistics with the sequence,
 1166: ##     resource, and part id to run statistics on.  At various places within
 1167: ##     the loops which compute the statistics, as well as before and after 
 1168: ##     the entire process, subroutines can be called.  The subroutines are
 1169: ##     registered to the following hooks:
 1170: ##
 1171: ##         hook          subroutine inputs
 1172: ##     ----------------------------------------------------------
 1173: ##         pre           $r,$count
 1174: ##         pre_seq       $r,$count,$seq
 1175: ##         pre_res       $r,$count,$seq,$res
 1176: ##         calc          $r,$count,$seq,$res,$data
 1177: ##         post_res      $r,$count,$seq,$res
 1178: ##         post_seq      $r,$count,$seq
 1179: ##         post          $r,$count
 1180: ##
 1181: ##         abort         $r
 1182: ##
 1183: ##     subroutines will be called in the order in which they are registered.
 1184: ##   
 1185: ############################################################
 1186: ############################################################
 1187: {
 1188: 
 1189: my %hooks;
 1190: my $aborted = 0;
 1191: 
 1192: sub abort_computation {
 1193:     $aborted = 1;
 1194: }
 1195: 
 1196: sub clear_hooks {
 1197:     $aborted = 0;
 1198:     undef(%hooks);
 1199: }
 1200: 
 1201: sub register_hook {
 1202:     my ($hookname,$subref)=@_;
 1203:     if ($hookname !~ /^(pre|pre_seq|pre_res|post|post_seq|post_res|calc)$/){
 1204:         return;
 1205:     }
 1206:     if (ref($subref) ne 'CODE') {
 1207:         &Apache::lonnet::logthis('attempt to register hook to non-code: '.
 1208:                                  $hookname,' = '.$subref);
 1209:     } else {
 1210:         if (exists($hooks{$hookname})) {
 1211:             push(@{$hooks{$hookname}},$subref);
 1212:         } else {
 1213:             $hooks{$hookname} = [$subref];
 1214:         }
 1215:     }
 1216:     return;
 1217: }
 1218: 
 1219: sub run_hooks {
 1220:     my $context = shift();
 1221:     foreach my $hook (@{$hooks{$context}}) { 
 1222:         if ($aborted && $context ne 'abort') {
 1223:             last;
 1224:         }
 1225:         my $retvalue = $hook->(@_);
 1226:         if (defined($retvalue) && $retvalue eq '0') {
 1227:             $aborted = 1 if (! $aborted);
 1228:         }
 1229:     }
 1230: }
 1231: 
 1232: sub run_statistics {
 1233:     my ($r) = @_;
 1234:     my $count = 0;
 1235:     &run_hooks('pre',$r,$count);
 1236:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1237:         last if ($aborted);
 1238:         next if ($seq->{'num_assess'}<1);
 1239:         &run_hooks('pre_seq',$r,$count,$seq);
 1240:         foreach my $res (@{$seq->{'contents'}}) {
 1241:             last if ($aborted);
 1242:             next if ($res->{'type'} ne 'assessment');
 1243:             &run_hooks('pre_res',$r,$count,$seq,$res);            
 1244:             foreach my $part (@{$res->{'parts'}}) {
 1245:                 last if ($aborted);
 1246:                 #
 1247:                 # This is where all the work happens
 1248:                 my $data = &get_statistics($seq,$res,$part,++$count);
 1249:                 &run_hooks('calc',$r,$count,$seq,$res,$part,$data); 
 1250:             }
 1251:             &run_hooks('post_res',$r,$count,$seq,$res);
 1252:         }
 1253:         &run_hooks('post_seq',$r,$count,$seq);
 1254:     }
 1255:     if ($aborted) {
 1256:         &run_hooks('abort',$r);
 1257:     } else {
 1258:         &run_hooks('post',$r,$count);
 1259:     }
 1260:     return;
 1261: }
 1262: 
 1263: } # End of %hooks scope
 1264: 
 1265: ############################################################
 1266: ############################################################
 1267: 
 1268: 1;
 1269: __END__

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