File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.74: download - view: text, annotated - select for diffs
Mon Mar 29 15:58:33 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added "Mean and S.D. of Tries" plot.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemstatistics.pm,v 1.74 2004/03/29 15:58:33 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:     if (exists($ENV{'form.Excel'})) {
  314:         &Excel_output($r);
  315:     } else {
  316:         my $sortby = $ENV{'form.sortby'};
  317:         $sortby = 'container' if (! defined($sortby) || $sortby =~ /^\s*$/);
  318:         my $plot = $ENV{'form.plot'};
  319:         &Apache::lonnet::logthis('form.plot = '.$plot);
  320:         if ($sortby eq 'container' && ! defined($plot)) {
  321:             &output_html_by_sequence($r);
  322:         } else {
  323:             if (defined($plot)) {
  324:                 &Apache::lonnet::logthis('calling plot routine');
  325:                 &make_plot($r,$plot);
  326:             }
  327:             &output_html_stats($r);
  328:         }
  329:     }
  330:     return;
  331: }
  332: 
  333: ##########################################################
  334: ##########################################################
  335: ##
  336: ## HTML output routines
  337: ##
  338: ##########################################################
  339: ##########################################################
  340: sub output_html_by_sequence {
  341:     my ($r) = @_;
  342:     my $c = $r->connection();
  343:     $r->print(&html_preamble());
  344:     #
  345:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  346:         last if ($c->aborted);
  347:         next if ($seq->{'num_assess'} < 1);
  348:         $r->print("<h3>".$seq->{'title'}."</h3>".
  349:                   '<table border="0"><tr><td bgcolor="#777777">'."\n".
  350:                   '<table border="0" cellpadding="3">'."\n".
  351:                   '<tr bgcolor="#FFFFE6">'.
  352:                   &statistics_table_header('no container')."</tr>\n");
  353:         my @Data = &compute_statistics_on_sequence($seq);
  354:         foreach my $data (@Data) {
  355:             $r->print('<tr>'.&statistics_html_table_data($data,
  356:                                                          'no container').
  357:                       "</tr>\n");
  358:         }
  359:         $r->print('</table>'."\n".'</table>'."\n");
  360:         $r->rflush();
  361:     }
  362:     return;
  363: }
  364: 
  365: sub output_html_stats {
  366:     my ($r)=@_;
  367:     &compute_all_statistics($r);
  368:     $r->print(&html_preamble());
  369:     &sort_data($ENV{'form.sortby'});
  370:     #
  371:     my $count=0;
  372:     foreach my $data (@StatsArray) {
  373:         if ($count++ % 50 == 0) {
  374:             $r->print("</table>\n</table>\n");
  375:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
  376:                       '<table border="0" cellpadding="3">'."\n".
  377:                       '<tr bgcolor="#FFFFE6">'.
  378:                       '<tr bgcolor="#FFFFE6">'.
  379:                       &statistics_table_header().
  380:                       "</tr>\n");
  381:         }
  382:         $r->print('<tr>'.&statistics_html_table_data($data)."</tr>\n");
  383:     }
  384:     $r->print("</table>\n</table>\n");
  385:     return;
  386: }
  387: 
  388: 
  389: sub html_preamble {
  390:     my $Str='';
  391:     $Str .= "<h2>".
  392:         $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  393:         "</h2>\n";
  394:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
  395:     if (defined($starttime) || defined($endtime)) {
  396:         # Inform the user what the time limits on the data are.
  397:         $Str .= '<h3>'.&mt('Statistics on submissions from [_1] to [_2]',
  398:                            &Apache::lonlocal::locallocaltime($starttime),
  399:                            &Apache::lonlocal::locallocaltime($endtime)
  400:                            ).'</h3>';
  401:     }
  402:     $Str .= "<h3>".&mt('Compiled on [_1]',
  403:                        &Apache::lonlocal::locallocaltime(time))."</h3>";
  404:     return $Str;
  405: }
  406: 
  407: 
  408: ###############################################
  409: ###############################################
  410: ##
  411: ## Misc HTML output routines
  412: ##
  413: ###############################################
  414: ###############################################
  415: sub statistics_html_table_data {
  416:     my ($data,$options) = @_;
  417:     my $row = '';
  418:     foreach my $field (@Fields) {
  419:         next if ($options =~ /no $field->{'name'}/);
  420:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  421:         if (exists($field->{'align'})) {
  422:             $row .= ' align="'.$field->{'align'}.'"';
  423:             }
  424:         $row .= '>';
  425:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  426:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
  427:         }
  428:         if (exists($field->{'format'})) {
  429:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  430:         } else {
  431:             $row .= $data->{$field->{'name'}};
  432:         }
  433:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  434:             $row.= '</a>';
  435:         }
  436:         $row .= '</td>';
  437:     }
  438:     return $row;
  439: }
  440: 
  441: sub statistics_table_header {
  442:     my ($options) = @_;
  443:     my $header_row;
  444:     foreach my $field (@Fields) {
  445:         next if ($options =~ /no $field->{'name'}/);
  446:         $header_row .= '<th>';
  447:         if (exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
  448:             $header_row .= '<a href="javascript:'.
  449:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
  450:                     ';document.Statistics.submit();">';
  451:         }
  452:         $header_row .= &mt($field->{'title'});
  453:         if ($options =~ /sortable/) {
  454:             $header_row.= '</a>';
  455:         }
  456:         if ($options !~ /no plots/        && 
  457:             exists($field->{'graphable'}) && 
  458:             $field->{'graphable'} eq 'yes') {
  459:             $header_row.=' (';
  460:             $header_row .= '<a href="javascript:'.
  461:                 "document.Statistics.plot.value='$field->{'name'}'".
  462:                     ';document.Statistics.submit();">';
  463:             $header_row .= &mt('plot').'</a>)';
  464:         }
  465:         $header_row .= '</th>';
  466:     }
  467:     return $header_row;
  468: }
  469: 
  470: ####################################################
  471: ####################################################
  472: ##
  473: ##    Plotting Routines
  474: ##
  475: ####################################################
  476: ####################################################
  477: sub make_plot {
  478:     my ($r,$plot) = @_;
  479:     &compute_all_statistics($r);
  480:     &sort_data($ENV{'form.sortby'});
  481:     if ($plot eq 'degrees') {
  482:         &degrees_plot($r);
  483:     } elsif ($plot eq 'tries statistics') {
  484:         &tries_data_plot($r);
  485:     } else {
  486:         &make_single_stat_plot($r,$plot);
  487:     }
  488:     return;
  489: }
  490: 
  491: sub make_single_stat_plot {
  492:     my ($r,$datafield) = @_;
  493:     #
  494:     my $title; my $yaxis;
  495:     foreach my $field (@Fields) {
  496:         next if ($field->{'name'} ne $datafield);
  497:         $title = $field->{'long_title'};
  498:         $yaxis = $field->{'title'};
  499:         last;
  500:     }
  501:     if ($title eq '' || $yaxis eq '') {
  502:         # datafield is something we do not know enough about to plot
  503:         $r->print('<h3>'.
  504:                   &mt('Unable to plot the requested statistic.').
  505:                   '</h3>');
  506:         return;
  507:     }
  508:     #
  509:     # Build up the data sets to plot
  510:     my @Labels; 
  511:     my @Data;
  512:     my $max = 1;
  513:     foreach my $data (@StatsArray) {
  514:         push(@Labels,$data->{'problem_num'});
  515:         push(@Data,$data->{$datafield});
  516:         if ($data->{$datafield}>$max) {
  517:             $max = $data->{$datafield};
  518:         }
  519:     }
  520:     foreach (1,2,3,4,5,10,15,20,25,40,50,75,100,150,200,250,300,500,600,750,
  521:              1000,1500,2000,2500,3000,3500,4000,5000,7500,10000,15000,20000) {
  522:         if ($max <= $_) {
  523:             $max = $_;
  524:             last;
  525:         }
  526:     }
  527:     if ($max > 20000) {
  528:         $max = 10000*(int($max/10000)+1);
  529:     }
  530:     #
  531:     $r->print("<p>".&Apache::loncommon::DrawBarGraph($title,
  532:                                                      'Problem Number',
  533:                                                      $yaxis,
  534:                                                      $max,
  535:                                                      undef, # colors
  536:                                                      \@Labels,
  537:                                                      \@Data)."</p>\n");
  538:     return;
  539: }
  540: 
  541: sub degrees_plot {
  542:     my ($r)=@_;
  543:     my $count = scalar(@StatsArray);
  544:     my $width = 50 + 10*$count;
  545:     $width = 300 if ($width < 300);
  546:     my $height = 300;
  547:     my $plot = '';
  548:     my $ymax = 0;
  549:     my $ymin = 0;
  550:     my @Disc; my @Diff; my @Labels;    
  551:     foreach my $data (@StatsArray) {
  552:         push(@Labels,$data->{'problem_num'});
  553:         my $disc = $data->{'deg_of_disc'};
  554:         my $diff = $data->{'deg_of_diff'};
  555:         push(@Disc,$disc);
  556:         push(@Diff,$diff);
  557:         #
  558:         $ymin = $disc if ($ymin > $disc);
  559:         $ymin = $diff if ($ymin > $diff);
  560:         $ymax = $disc if ($ymax < $disc);
  561:         $ymax = $diff if ($ymax < $diff);
  562:     }
  563:     #
  564:     # Make sure we show relevant information.
  565:     if ($ymin < 0) {
  566:         if (abs($ymin) < 0.05) {
  567:             $ymin = 0;
  568:         } else {
  569:             $ymin = -1;
  570:         }
  571:     }
  572:     if ($ymax > 0) {
  573:         if (abs($ymax) < 0.05) {
  574:             $ymax = 0;
  575:         } else {
  576:             $ymax = 1;
  577:         }
  578:     }
  579:     #
  580:     my $xmax = $Labels[-1];
  581:     if ($xmax > 50) {
  582:         if ($xmax % 10 != 0) {
  583:             $xmax = 10 * (int($xmax/10)+1);
  584:         }
  585:     } else {
  586:         if ($xmax % 5 != 0) {
  587:             $xmax = 5 * (int($xmax/5)+1);
  588:         }
  589:     }
  590:     #
  591:     my $discdata .= '<data>'.join(',',@Labels).'</data>'.$/.
  592:                     '<data>'.join(',',@Disc).'</data>'.$/;
  593:     #
  594:     my $diffdata .= '<data>'.join(',',@Labels).'</data>'.$/.
  595:                     '<data>'.join(',',@Diff).'</data>'.$/;
  596:     #
  597:     $plot=<<"END";
  598: <gnuplot 
  599:     texfont="10"
  600:     fgcolor="x000000"
  601:     plottype="Cartesian"
  602:     font="large"
  603:     grid="on"
  604:     align="center"
  605:     border="on"
  606:     transparent="on"
  607:     alttag="Sample Plot"
  608:     samples="100"
  609:     bgcolor="xffffff"
  610:     height="$height"
  611:     width="$width">
  612:     <key 
  613:         pos="top right"
  614:         title=""
  615:         box="off" />
  616:     <title>Degree of Discrmination and Degree of Difficulty</title>
  617:     <axis xmin="0" ymin="$ymin" xmax="$xmax" ymax="$ymax" color="x000000" />
  618:     <xlabel>Problem Number</xlabel>
  619:     <curve 
  620:         linestyle="linespoints" 
  621:         name="DoDisc" 
  622:         pointtype="0" 
  623:         color="x000000">
  624:         $discdata
  625:     </curve>
  626:     <curve 
  627:         linestyle="linespoints" 
  628:         name="DoDiff" 
  629:         pointtype="0" 
  630:         color="xFF0000">
  631:         $diffdata
  632:     </curve>
  633: </gnuplot>
  634: END
  635:     my $plotresult = 
  636:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
  637:     $r->print($plotresult);
  638:     return;
  639: }
  640: 
  641: sub tries_data_plot {
  642:     my ($r)=@_;
  643:     my $count = scalar(@StatsArray);
  644:     my $width = 50 + 10*$count;
  645:     $width = 300 if ($width < 300);
  646:     my $height = 300;
  647:     my $plot = '';
  648:     my @STD;  my @Mean; my @Max; my @Min;
  649:     my @Labels;
  650:     my $ymax = 5;
  651:     foreach my $data (@StatsArray) {
  652:         my $max = $data->{'mean_tries'} + $data->{'std_tries'};
  653:         $ymax = $max if ($ymax < $max);
  654:         $ymax = $max if ($ymax < $max);
  655:         push(@Labels,$data->{'problem_num'});
  656:         push(@STD,$data->{'std_tries'});
  657:         push(@Mean,$data->{'mean_tries'});
  658:     }
  659:     #
  660:     # Make sure we show relevant information.
  661:     my $xmax = $Labels[-1];
  662:     if ($xmax > 50) {
  663:         if ($xmax % 10 != 0) {
  664:             $xmax = 10 * (int($xmax/10)+1);
  665:         }
  666:     } else {
  667:         if ($xmax % 5 != 0) {
  668:             $xmax = 5 * (int($xmax/5)+1);
  669:         }
  670:     }
  671:     $ymax = int($ymax)+1+2;
  672:     #
  673:     my $std_data .= '<data>'.join(',',@Labels).'</data>'.$/.
  674:                     '<data>'.join(',',@Mean).'</data>'.$/;
  675:     #
  676:     my $std_error_data .= '<data>'.join(',',@Labels).'</data>'.$/.
  677:                           '<data>'.join(',',@Mean).'</data>'.$/.
  678:                           '<data>'.join(',',@STD).'</data>'.$/;
  679:     #
  680:     $plot=<<"END";
  681: <gnuplot 
  682:     texfont="10"
  683:     fgcolor="x000000"
  684:     plottype="Cartesian"
  685:     font="large"
  686:     grid="on"
  687:     align="center"
  688:     border="on"
  689:     transparent="on"
  690:     alttag="Sample Plot"
  691:     samples="100"
  692:     bgcolor="xffffff"
  693:     height="$height"
  694:     width="$width">
  695:     <title>Mean and S.D. of Tries</title>
  696:     <axis xmin="0" ymin="0" xmax="$xmax" ymax="$ymax" color="x000000" />
  697:     <xlabel>Problem Number</xlabel>
  698:     <curve 
  699:         linestyle="yerrorbars"
  700:         name="S.D. Tries" 
  701:         pointtype="1" 
  702:         color="x666666">
  703:         $std_error_data
  704:     </curve>
  705:     <curve 
  706:         linestyle="points"
  707:         name="Mean Tries" 
  708:         pointtype="1" 
  709:         color="xCC4444">
  710:         $std_data
  711:     </curve>
  712: </gnuplot>
  713: END
  714:     my $plotresult = 
  715:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
  716:     $r->print($plotresult);
  717:     return;
  718: }
  719: 
  720: sub plot_dropdown {
  721:     my $current = '';
  722:     #
  723:     if (defined($ENV{'form.plot'})) {
  724:         $current = $ENV{'form.plot'};
  725:     }
  726:     #
  727:     my @Additional_Plots = (
  728:                             { graphable=>'yes',
  729:                               name => 'degrees',
  730:                               title => 'DoDisc and DoDiff' },
  731:                             { graphable=>'yes',
  732:                               name => 'tries statistics',
  733:                               title => 'Mean and S.D. of Tries' });
  734:     #
  735:     my $Str= "\n".'<select name="plot" size="1">';
  736:     $Str .= '<option name="none"></option>'."\n";
  737:     $Str .= '<option name="none2">none</option>'."\n";
  738:     foreach my $field (@Fields,@Additional_Plots) {
  739:         if (! exists($field->{'graphable'}) ||
  740:             $field->{'graphable'} ne 'yes') {
  741:             next;
  742:         }
  743:         $Str .= '<option value="'.$field->{'name'}.'"';
  744:         if ($field->{'name'} eq $current) {
  745:             $Str .= ' selected ';
  746:         }
  747:         $Str.= '>'.&mt($field->{'title'}).'</option>'."\n";
  748:     }
  749:     $Str .= '</select>'."\n";
  750:     return $Str;
  751: }
  752: 
  753: ###############################################
  754: ###############################################
  755: ##
  756: ## Excel output routines
  757: ##
  758: ###############################################
  759: ###############################################
  760: sub Excel_output {
  761:     my ($r) = @_;
  762:     $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
  763:     ##
  764:     ## Compute the statistics
  765:     &compute_all_statistics($r);
  766:     my $c = $r->connection;
  767:     return if ($c->aborted());
  768:     ##
  769:     ## Create the excel workbook
  770:     my $filename = '/prtspool/'.
  771:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  772:         time.'_'.rand(1000000000).'.xls';
  773:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
  774:     #
  775:     # Create sheet
  776:     my $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  777:     #
  778:     # Check for errors
  779:     if (! defined($excel_workbook)) {
  780:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  781:         $r->print(&mt("Problems creating new Excel file.  ".
  782:                   "This error has been logged.  ".
  783:                   "Please alert your LON-CAPA administrator."));
  784:         return 0;
  785:     }
  786:     #
  787:     # The excel spreadsheet stores temporary data in files, then put them
  788:     # together.  If needed we should be able to disable this (memory only).
  789:     # The temporary directory must be specified before calling 'addworksheet'.
  790:     # File::Temp is used to determine the temporary directory.
  791:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  792:     #
  793:     # Add a worksheet
  794:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  795:     if (length($sheetname) > 31) {
  796:         $sheetname = substr($sheetname,0,31);
  797:     }
  798:     my $excel_sheet = $excel_workbook->addworksheet(
  799:         &Apache::loncommon::clean_excel_name($sheetname));
  800:     ##
  801:     ## Begin creating excel sheet
  802:     ##
  803:     my ($rows_output,$cols_output) = (0,0);
  804:     #
  805:     # Put the course description in the header
  806:     $excel_sheet->write($rows_output,$cols_output++,
  807:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
  808:     $cols_output += 3;
  809:     #
  810:     # Put a description of the sections listed
  811:     my $sectionstring = '';
  812:     my @Sections = @Apache::lonstatistics::SelectedSections;
  813:     if (scalar(@Sections) > 1) {
  814:         if (scalar(@Sections) > 2) {
  815:             my $last = pop(@Sections);
  816:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
  817:         } else {
  818:             $sectionstring = "Sections ".join(' and ',@Sections);
  819:         }
  820:     } else {
  821:         if ($Sections[0] eq 'all') {
  822:             $sectionstring = "All sections";
  823:         } else {
  824:             $sectionstring = "Section ".$Sections[0];
  825:         }
  826:     }
  827:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
  828:     $cols_output += scalar(@Sections);
  829:     #
  830:     # Time restrictions
  831:     my $time_string;
  832:     if (defined($starttime)) {
  833:         # call localtime but not lonlocal:locallocaltime because excel probably
  834:         # cannot handle localized text.  Probably.
  835:         $time_string .= 'Data collected from '.localtime($time_string);
  836:         if (defined($endtime)) {
  837:             $time_string .= ' to '.localtime($endtime);
  838:         }
  839:         $time_string .= '.';
  840:     } elsif (defined($endtime)) {
  841:         # See note above about lonlocal:locallocaltime
  842:         $time_string .= 'Data collected before '.localtime($endtime).'.';
  843:     }
  844:     #
  845:     # Put the date in there too
  846:     $excel_sheet->write($rows_output,$cols_output++,
  847:                         'Compiled on '.localtime(time));
  848:     #
  849:     $rows_output++; 
  850:     $cols_output=0;
  851:     #
  852:     # Long Headers
  853:     foreach my $field (@Fields) {
  854:         next if ($field->{'name'} eq 'problem_num');
  855:         if (exists($field->{'long_title'})) {
  856:             $excel_sheet->write($rows_output,$cols_output++,
  857:                                 $field->{'long_title'});
  858:         } else {
  859:             $excel_sheet->write($rows_output,$cols_output++,'');
  860:         }
  861:     }
  862:     $rows_output++;
  863:     $cols_output=0;
  864:     # Brief headers
  865:     foreach my $field (@Fields) {
  866:         next if ($field->{'name'} eq 'problem_num');
  867:         # Use english for excel as I am not sure how well excel handles 
  868:         # other character sets....
  869:         $excel_sheet->write($rows_output,$cols_output++,$field->{'title'});
  870:     }
  871:     $rows_output++;
  872:     foreach my $data (@StatsArray) {
  873:         $cols_output=0;
  874:         foreach my $field (@Fields) {
  875:             next if ($field->{'name'} eq 'problem_num');
  876:             $excel_sheet->write($rows_output,$cols_output++,
  877:                                 $data->{$field->{'name'}});
  878:         }
  879:         $rows_output++;
  880:     }
  881:     #
  882:     $excel_workbook->close();
  883:     #
  884:     # Tell the user where to get their excel file
  885:     $r->print('<br />'.
  886:               '<a href="'.$filename.'">'.
  887:               &mt('Your Excel Spreadsheet').'</a>'."\n");
  888:     $r->rflush();
  889:     return;
  890: }
  891: 
  892: ##################################################
  893: ##################################################
  894: ##
  895: ## Statistics Gathering and Manipulation Routines
  896: ##
  897: ##################################################
  898: ##################################################
  899: sub compute_statistics_on_sequence {
  900:     my ($seq) = @_;
  901:     my @Data;
  902:     foreach my $res (@{$seq->{'contents'}}) {
  903:         next if ($res->{'type'} ne 'assessment');
  904:         foreach my $part (@{$res->{'parts'}}) {
  905:             #
  906:             # This is where all the work happens
  907:             my $data = &get_statistics($seq,$res,$part,scalar(@StatsArray)+1);
  908:             push (@Data,$data);
  909:             push (@StatsArray,$data);
  910:         }
  911:     }
  912:     return @Data;
  913: }
  914: 
  915: sub compute_all_statistics {
  916:     my ($r) = @_;
  917:     if (@StatsArray > 0) {
  918:         # Assume we have already computed the statistics
  919:         return;
  920:     }
  921:     my $c = $r->connection;
  922:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  923:         last if ($c->aborted);
  924:         next if ($seq->{'num_assess'} < 1);
  925:         &compute_statistics_on_sequence($seq);
  926:     }
  927: }
  928: 
  929: sub sort_data {
  930:     my ($sortkey) = @_;
  931:     return if (! @StatsArray);
  932:     #
  933:     # Sort the data
  934:     my $sortby = undef;
  935:     foreach my $field (@Fields) {
  936:         if ($sortkey eq $field->{'name'}) {
  937:             $sortby = $field->{'name'};
  938:         }
  939:     }
  940:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
  941:         $sortby = 'container';
  942:     }
  943:     if ($sortby ne 'container') {
  944:         # $sortby is already defined, so we can charge ahead
  945:         if ($sortby =~ /^(title|part)$/i) {
  946:             # Alpha comparison
  947:             @StatsArray = sort {
  948:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
  949:                 lc($a->{'title'}) cmp lc($b->{'title'}) ||
  950:                 lc($a->{'part'}) cmp lc($b->{'part'});
  951:             } @StatsArray;
  952:         } else {
  953:             # Numerical comparison
  954:             @StatsArray = sort {
  955:                 my $retvalue = 0;
  956:                 if ($b->{$sortby} eq 'nan') {
  957:                     if ($a->{$sortby} ne 'nan') {
  958:                         $retvalue = -1;
  959:                     } else {
  960:                         $retvalue = 0;
  961:                     }
  962:                 }
  963:                 if ($a->{$sortby} eq 'nan') {
  964:                     if ($b->{$sortby} ne 'nan') {
  965:                         $retvalue = 1;
  966:                     }
  967:                 }
  968:                 if ($retvalue eq '0') {
  969:                     $retvalue = $b->{$sortby} <=> $a->{$sortby}     ||
  970:                             lc($a->{'title'}) <=> lc($b->{'title'}) ||
  971:                             lc($a->{'part'})  <=> lc($b->{'part'});
  972:                 }
  973:                 $retvalue;
  974:             } @StatsArray;
  975:         }
  976:     }
  977:     #
  978:     # Renumber the data set
  979:     my $count;
  980:     foreach my $data (@StatsArray) {
  981:         $data->{'problem_num'} = ++$count;
  982:     }
  983:     return;
  984: }
  985: 
  986: ########################################################
  987: ########################################################
  988: 
  989: =pod
  990: 
  991: =item &get_statistics()
  992: 
  993: Wrapper routine from the call to loncoursedata::get_problem_statistics.  
  994: Calls lonstathelpers::get_time_limits() to limit the data set by time
  995: and &compute_discrimination_factor
  996: 
  997: Inputs: $sequence, $resource, $part, $problem_num
  998: 
  999: Returns: Hash reference with statistics data from 
 1000: loncoursedata::get_problem_statistics.
 1001: 
 1002: =cut
 1003: 
 1004: ########################################################
 1005: ########################################################
 1006: sub get_statistics {
 1007:     my ($sequence,$resource,$part,$problem_num) = @_;
 1008:     #
 1009:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1010:     my $symb = $resource->{'symb'};
 1011:     my $courseid = $ENV{'request.course.id'};
 1012:     #
 1013:     my $data = &Apache::loncoursedata::get_problem_statistics
 1014:                         (\@Apache::lonstatistics::SelectedSections,
 1015:                          $Apache::lonstatistics::enrollment_status,
 1016:                          $symb,$part,$courseid,$starttime,$endtime);
 1017:     $data->{'part'}        = $part;
 1018:     $data->{'problem_num'} = $problem_num;
 1019:     $data->{'container'}   = $sequence->{'title'};
 1020:     $data->{'title'}       = $resource->{'title'};
 1021:     $data->{'title.link'}  = $resource->{'src'}.'?symb='.
 1022:         &Apache::lonnet::escape($resource->{'symb'});
 1023:     #
 1024:     $data->{'deg_of_disc'} = &compute_discrimination_factor($resource,$part,$sequence);
 1025:     return $data;
 1026: }
 1027: 
 1028: 
 1029: ###############################################
 1030: ###############################################
 1031: 
 1032: =pod
 1033: 
 1034: =item &compute_discrimination_factor()
 1035: 
 1036: Inputs: $Resource, $Sequence
 1037: 
 1038: Returns: integer between -1 and 1
 1039: 
 1040: =cut
 1041: 
 1042: ###############################################
 1043: ###############################################
 1044: sub compute_discrimination_factor {
 1045:     my ($resource,$part,$sequence) = @_;
 1046:     my @Resources;
 1047:     foreach my $res (@{$sequence->{'contents'}}) {
 1048:         next if ($res->{'symb'} eq $resource->{'symb'});
 1049:         push (@Resources,$res->{'symb'});
 1050:     }
 1051:     #
 1052:     # rank
 1053:     my $ranking = 
 1054:         &Apache::loncoursedata::rank_students_by_scores_on_resources
 1055:         (\@Resources,
 1056:          \@Apache::lonstatistics::SelectedSections,
 1057:          $Apache::lonstatistics::enrollment_status,undef);
 1058:     #
 1059:     # compute their percent scores on the problems in the sequence,
 1060:     my $number_to_grab = int(scalar(@{$ranking})/4);
 1061:     my $num_students = scalar(@{$ranking});
 1062:     my @BottomSet = map { $_->[&Apache::loncoursedata::RNK_student()]; 
 1063:                       } @{$ranking}[0..$number_to_grab];
 1064:     my @TopSet    = 
 1065:         map { 
 1066:             $_->[&Apache::loncoursedata::RNK_student()]; 
 1067:           } @{$ranking}[($num_students-$number_to_grab)..($num_students-1)];
 1068:     my ($bottom_sum,$bottom_max) = 
 1069:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@BottomSet);
 1070:     my ($top_sum,$top_max) = 
 1071:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@TopSet);
 1072:     my $deg_of_disc;
 1073:     if ($top_max == 0 || $bottom_max==0) {
 1074:         $deg_of_disc = 'nan';
 1075:     } else {
 1076:         $deg_of_disc = ($top_sum/$top_max) - ($bottom_sum/$bottom_max);
 1077:     }
 1078:     #&Apache::lonnet::logthis('    '.$top_sum.'/'.$top_max.
 1079:     #                         ' - '.$bottom_sum.'/'.$bottom_max);
 1080:     return $deg_of_disc;
 1081: }
 1082: 
 1083: ###############################################
 1084: ###############################################
 1085: 
 1086: =pod 
 1087: 
 1088: =item ProblemStatisticsLegend
 1089: 
 1090: =over 4
 1091: 
 1092: =item #Stdnts
 1093: Total number of students attempted the problem.
 1094: 
 1095: =item Tries
 1096: Total number of tries for solving the problem.
 1097: 
 1098: =item Max Tries
 1099: Largest number of tries for solving the problem by a student.
 1100: 
 1101: =item Mean
 1102: Average number of tries. [ Tries / #Stdnts ]
 1103: 
 1104: =item #YES
 1105: Number of students solved the problem correctly.
 1106: 
 1107: =item #yes
 1108: Number of students solved the problem by override.
 1109: 
 1110: =item %Wrong
 1111: Percentage of students who tried to solve the problem 
 1112: but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]
 1113: 
 1114: =item DoDiff
 1115: Degree of Difficulty of the problem.  
 1116: [ 1 - ((#YES+#yes) / Tries) ]
 1117: 
 1118: =item S.D.
 1119: Standard Deviation of the tries.  
 1120: [ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) 
 1121: where Xi denotes every student\'s tries ]
 1122: 
 1123: =item Skew.
 1124: Skewness of the students tries.
 1125: [(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]
 1126: 
 1127: =item Dis.F.
 1128: Discrimination Factor: A Standard for evaluating the 
 1129: problem according to a Criterion<br>
 1130: 
 1131: =item [Criterion to group students into %27 Upper Students - 
 1132: and %27 Lower Students]
 1133: 1st Criterion for Sorting the Students: 
 1134: Sum of Partial Credit Awarded / Total Number of Tries
 1135: 2nd Criterion for Sorting the Students: 
 1136: Total number of Correct Answers / Total Number of Tries
 1137: 
 1138: =item Disc.
 1139: Number of Students had at least one discussion.
 1140: 
 1141: =back
 1142: 
 1143: =cut
 1144: 
 1145: 
 1146: ############################################################
 1147: ############################################################
 1148: ##
 1149: ##  How this all works:
 1150: ##     Statistics are computed by calling &get_statistics with the sequence,
 1151: ##     resource, and part id to run statistics on.  At various places within
 1152: ##     the loops which compute the statistics, as well as before and after 
 1153: ##     the entire process, subroutines can be called.  The subroutines are
 1154: ##     registered to the following hooks:
 1155: ##
 1156: ##         hook          subroutine inputs
 1157: ##     ----------------------------------------------------------
 1158: ##         pre           $r,$count
 1159: ##         pre_seq       $r,$count,$seq
 1160: ##         pre_res       $r,$count,$seq,$res
 1161: ##         calc          $r,$count,$seq,$res,$data
 1162: ##         post_res      $r,$count,$seq,$res
 1163: ##         post_seq      $r,$count,$seq
 1164: ##         post          $r,$count
 1165: ##
 1166: ##         abort         $r
 1167: ##
 1168: ##     subroutines will be called in the order in which they are registered.
 1169: ##   
 1170: ############################################################
 1171: ############################################################
 1172: {
 1173: 
 1174: my %hooks;
 1175: my $aborted = 0;
 1176: 
 1177: sub abort_computation {
 1178:     $aborted = 1;
 1179: }
 1180: 
 1181: sub clear_hooks {
 1182:     $aborted = 0;
 1183:     undef(%hooks);
 1184: }
 1185: 
 1186: sub register_hook {
 1187:     my ($hookname,$subref)=@_;
 1188:     if ($hookname !~ /^(pre|pre_seq|pre_res|post|post_seq|post_res|calc)$/){
 1189:         return;
 1190:     }
 1191:     if (ref($subref) ne 'CODE') {
 1192:         &Apache::lonnet::logthis('attempt to register hook to non-code: '.
 1193:                                  $hookname,' = '.$subref);
 1194:     } else {
 1195:         if (exists($hooks{$hookname})) {
 1196:             push(@{$hooks{$hookname}},$subref);
 1197:         } else {
 1198:             $hooks{$hookname} = [$subref];
 1199:         }
 1200:     }
 1201:     return;
 1202: }
 1203: 
 1204: sub run_hooks {
 1205:     my $context = shift();
 1206:     foreach my $hook (@{$hooks{$context}}) { 
 1207:         if ($aborted && $context ne 'abort') {
 1208:             last;
 1209:         }
 1210:         my $retvalue = $hook->(@_);
 1211:         if (defined($retvalue) && $retvalue eq '0') {
 1212:             $aborted = 1 if (! $aborted);
 1213:         }
 1214:     }
 1215: }
 1216: 
 1217: sub run_statistics {
 1218:     my ($r) = @_;
 1219:     my $count = 0;
 1220:     &run_hooks('pre',$r,$count);
 1221:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1222:         last if ($aborted);
 1223:         next if ($seq->{'num_assess'}<1);
 1224:         &run_hooks('pre_seq',$r,$count,$seq);
 1225:         foreach my $res (@{$seq->{'contents'}}) {
 1226:             last if ($aborted);
 1227:             next if ($res->{'type'} ne 'assessment');
 1228:             &run_hooks('pre_res',$r,$count,$seq,$res);            
 1229:             foreach my $part (@{$res->{'parts'}}) {
 1230:                 last if ($aborted);
 1231:                 #
 1232:                 # This is where all the work happens
 1233:                 my $data = &get_statistics($seq,$res,$part,++$count);
 1234:                 &run_hooks('calc',$r,$count,$seq,$res,$part,$data); 
 1235:             }
 1236:             &run_hooks('post_res',$r,$count,$seq,$res);
 1237:         }
 1238:         &run_hooks('post_seq',$r,$count,$seq);
 1239:     }
 1240:     if ($aborted) {
 1241:         &run_hooks('abort',$r);
 1242:     } else {
 1243:         &run_hooks('post',$r,$count);
 1244:     }
 1245:     return;
 1246: }
 1247: 
 1248: } # End of %hooks scope
 1249: 
 1250: ############################################################
 1251: ############################################################
 1252: 
 1253: 1;
 1254: __END__

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