File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.81: download - view: text, annotated - select for diffs
Thu Apr 1 21:35:52 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
degrees and tries statistics plots: Change title based on width of plot,
changed names of plot that appear in selection box, now appear at the top
of the drop-down list instead of the bottom, added y-axis label to
the tries statistics plot.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemstatistics.pm,v 1.81 2004/04/01 21:35:52 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: my %SeqStat;    # keys are symbs, values are hash refs
   65: 
   66: ##
   67: ## Localization notes:
   68: ##
   69: ## in @Fields[0]->{'long_title'} is placed in Excel files and is used as the
   70: ## header for plots created with Graph.pm, both of which more than likely do
   71: ## not support localization.
   72: ##
   73: #
   74: #
   75: ##
   76: ## Description of Field attributes
   77: ##
   78: ## Attribute     Required   Value       Meaning or Use
   79: ##
   80: ## name            yes      any scalar  Used to uniquely identify field
   81: ## title           yes      any scalar  This is what the user sees to identify
   82: ##                                      the field.  Passed through &mt().
   83: ## long_title      yes      any scalar  Used as graph heading and in excel
   84: ##                                      output.  NOT translated
   85: ## align           no    (left|right|center)  HTML cell contents alignment
   86: ## color           yes      html color  HTML cell background color
   87: ##                                      used to visually group statistics
   88: ## special         no          (link)   Indicates a link, target is name.link
   89: ##                                      Currently set in &get_statistics()
   90: ## graphable       no      (yes|no)     Can a bar graph of the field be 
   91: ##                                      produced?
   92: ## sortable        no      (yes|no)     Should a sort link be put in the
   93: ##                                      column header?
   94: ## selectable      yes     (yes|no)     Can the column be removed from the
   95: ##                                      statistics display?
   96: ## selected        yes     (yes|no)     Is the column selected by default?
   97: ##
   98: my @Fields = (
   99:            { name => 'problem_num',
  100:              title => 'P#',
  101:              align => 'right',
  102:              color => '#FFFFE6',
  103:              selectable => 'no',
  104:              defaultselected => 'yes',
  105:            },
  106:            { name   => 'container',
  107:              title  => 'Sequence or Folder',
  108:              align  => 'left',
  109:              color  => '#FFFFE6',
  110:              sortable => 'yes',
  111:              selectable => 'no',
  112:              defaultselected => 'yes',
  113:            },
  114:            { name   => 'title',
  115:              title  => 'Title',
  116:              align  => 'left',
  117:              color  => '#FFFFE6',
  118:              special  => 'link',
  119:              sortable => 'yes', 
  120:              selectable => 'no',
  121:              defaultselected => 'yes',
  122:            },
  123:            { name   => 'part', 
  124:              title  => 'Part',
  125:              align  => 'left',
  126:              color  => '#FFFFE6',
  127:              selectable => 'no',
  128:              defaultselected => 'yes',
  129:            },
  130:            { name   => 'num_students',
  131:              title  => '#Stdnts',
  132:              align  => 'right',
  133:              color  => '#EEFFCC',
  134:              format => '%d',
  135:              sortable  => 'yes',
  136:              graphable => 'yes',
  137:              long_title => 'Number of Students Attempting Problem',
  138:              selectable => 'yes',
  139:              defaultselected => 'yes',
  140:            },
  141:            { name   => 'tries',
  142:              title  => 'Tries',
  143:              align  => 'right',
  144:              color  => '#EEFFCC',
  145:              format => '%d',
  146:              sortable  => 'yes',
  147:              graphable => 'yes',
  148:              long_title => 'Total Number of Tries',
  149:              selectable => 'yes',
  150:              defaultselected => 'yes',
  151:            },
  152:            { name   => 'max_tries',
  153:              title  => 'Max Tries',
  154:              align  => 'right',
  155:              color  => '#DDFFFF',
  156:              format => '%d',
  157:              sortable  => 'yes',
  158:              graphable => 'yes',
  159:              long_title => 'Maximum Number of Tries',
  160:              selectable => 'yes',
  161:              defaultselected => 'yes',
  162:            },
  163:            { name   => 'min_tries',
  164:              title  => 'Min Tries',
  165:              align  => 'right',
  166:              color  => '#DDFFFF',
  167:              format => '%d',
  168:              sortable  => 'yes',
  169:              graphable => 'yes',
  170:              long_title => 'Minumum Number of Tries',
  171:              selectable => 'yes',
  172:              defaultselected => 'yes',
  173:            },
  174:            { name   => 'mean_tries',
  175:              title  => 'Mean Tries',
  176:              align  => 'right',
  177:              color  => '#DDFFFF',
  178:              format => '%5.2f',
  179:              sortable  => 'yes',
  180:              graphable => 'yes',
  181:              long_title => 'Average Number of Tries',
  182:              selectable => 'yes',
  183:              defaultselected => 'yes',
  184:            },
  185:            { name   => 'std_tries',
  186:              title  => 'S.D. tries',
  187:              align  => 'right',
  188:              color  => '#DDFFFF',
  189:              format => '%5.2f',
  190:              sortable  => 'yes',
  191:              graphable => 'yes',
  192:              long_title => 'Standard Deviation of Number of Tries',
  193:              selectable => 'yes',
  194:              defaultselected => 'yes',
  195:            },
  196:            { name   => 'skew_tries',
  197:              title  => 'Skew Tries',
  198:              align  => 'right',
  199:              color  => '#DDFFFF',
  200:              format => '%5.2f',
  201:              sortable  => 'yes',
  202:              graphable => 'yes',
  203:              long_title => 'Skew of Number of Tries',
  204:              selectable => 'yes',
  205:              defaultselected => 'no',
  206:            },
  207:            { name   => 'num_solved',
  208:              title  => '#YES',
  209:              align  => 'right',
  210:              color  => '#FFDDDD',
  211:              format => '%4.1f',#             format => '%d',
  212:              sortable  => 'yes',
  213:              graphable => 'yes',
  214:              long_title => 'Number of Students able to Solve',
  215:              selectable => 'yes',
  216:              defaultselected => 'yes',
  217:            },
  218:            { name   => 'num_override',
  219:              title  => '#yes',
  220:              align  => 'right',
  221:              color  => '#FFDDDD',
  222:              format => '%4.1f',#             format => '%d',
  223:              sortable  => 'yes',
  224:              graphable => 'yes',
  225:              long_title => 'Number of Students given Override',
  226:              selectable => 'yes',
  227:              defaultselected => 'yes',
  228:            },
  229:            { name   => 'num_wrong',
  230:              title  => '#Wrng',
  231:              align  => 'right',
  232:              color  => '#FFDDDD',
  233:              format => '%4.1f',
  234:              sortable  => 'yes',
  235:              graphable => 'yes',
  236:              long_title => 'Percent of students whose final answer is wrong',
  237:              selectable => 'yes',
  238:              defaultselected => 'yes',
  239:            },
  240:            { name   => 'deg_of_diff',
  241:              title  => 'DoDiff',
  242:              align  => 'right',
  243:              color  => '#FFFFE6',
  244:              format => '%5.2f',
  245:              sortable  => 'yes',
  246:              graphable => 'yes',
  247:              long_title => 'Degree of Difficulty'.
  248:                            '[ 1 - ((#YES+#yes) / Tries) ]',
  249:              selectable => 'yes',
  250:              defaultselected => 'yes',
  251:            },
  252:            { name   => 'deg_of_disc',
  253:              title  => 'DoDisc',
  254:              align  => 'right',
  255:              color  => '#FFFFE6',
  256:              format => '%4.2f',
  257:              sortable  => 'yes',
  258:              graphable => 'yes',
  259:              long_title => 'Degree of Discrimination',
  260:              selectable => 'yes',
  261:              defaultselected => 'no',
  262:            },
  263: );
  264: 
  265: my @SeqFields = (
  266:            { name   => 'title',
  267:              title  => 'Sequence',
  268:              align  => 'left',
  269:              color  => '#FFFFE6',
  270:              special  => 'no',
  271:              sortable => 'no', 
  272:              selectable => 'yes',
  273:              defaultselected => 'no',
  274:            },
  275:            { name   => 'items',
  276:              title  => '#Items',
  277:              align  => 'right',
  278:              color  => '#FFFFE6',
  279:              format => '%4d',
  280:              sortable  => 'no',
  281:              graphable => 'no',
  282:              long_title => 'Number of Items in Sequence',
  283:              selectable => 'yes',
  284:              defaultselected => 'no',
  285:            },
  286:            { name   => 'scoremean',
  287:              title  => 'Score Mean',
  288:              align  => 'right',
  289:              color  => '#FFFFE6',
  290:              format => '%4.2f',
  291:              sortable  => 'no',
  292:              graphable => 'no',
  293:              long_title => 'Mean Sequence Score',
  294:              selectable => 'yes',
  295:              defaultselected => 'no',
  296:            },
  297:            { name   => 'scorestd',
  298:              title  => 'Score STD',
  299:              align  => 'right',
  300:              color  => '#FFFFE6',
  301:              format => '%4.2f',
  302:              sortable  => 'no',
  303:              graphable => 'no',
  304:              long_title => 'Standard Deviation of Sequence Scores',
  305:              selectable => 'yes',
  306:              defaultselected => 'no',
  307:            },
  308:            { name   => 'scoremax',
  309:              title  => 'Score Max',
  310:              align  => 'right',
  311:              color  => '#FFFFE6',
  312:              format => '%4.2f',
  313:              sortable  => 'no',
  314:              graphable => 'no',
  315:              long_title => 'Maximum Sequence Score',
  316:              selectable => 'yes',
  317:              defaultselected => 'no',
  318:            },
  319:            { name   => 'scoremin',
  320:              title  => 'Score Min',
  321:              align  => 'right',
  322:              color  => '#FFFFE6',
  323:              format => '%4.2f',
  324:              sortable  => 'no',
  325:              graphable => 'no',
  326:              long_title => 'Minumum Sequence Score',
  327:              selectable => 'yes',
  328:              defaultselected => 'no',
  329:            },
  330:            { name   => 'scorecount',
  331:              title  => 'Score N',
  332:              align  => 'right',
  333:              color  => '#FFFFE6',
  334:              format => '%4d',
  335:              sortable  => 'no',
  336:              graphable => 'no',
  337:              long_title => 'Number of Students in score computations',
  338:              selectable => 'yes',
  339:              defaultselected => 'no',
  340:            },
  341:            { name   => 'countmean',
  342:              title  => 'Count Mean',
  343:              align  => 'right',
  344:              color  => '#FFFFFF',
  345:              format => '%4.2f',
  346:              sortable  => 'no',
  347:              graphable => 'no',
  348:              long_title => 'Mean Sequence Score',
  349:              selectable => 'yes',
  350:              defaultselected => 'no',
  351:            },
  352:            { name   => 'countstd',
  353:              title  => 'Count STD',
  354:              align  => 'right',
  355:              color  => '#FFFFFF',
  356:              format => '%4.2f',
  357:              sortable  => 'no',
  358:              graphable => 'no',
  359:              long_title => 'Standard Deviation of Sequence Scores',
  360:              selectable => 'yes',
  361:              defaultselected => 'no',
  362:            },
  363:            { name   => 'countmax',
  364:              title  => 'Count Max',
  365:              align  => 'right',
  366:              color  => '#FFFFFF',
  367:              format => '%4.2f',
  368:              sortable  => 'no',
  369:              graphable => 'no',
  370:              long_title => 'Maximum Number of Correct Problems',
  371:              selectable => 'yes',
  372:              defaultselected => 'no',
  373:            },
  374:            { name   => 'countmin',
  375:              title  => 'Count Min',
  376:              align  => 'right',
  377:              color  => '#FFFFFF',
  378:              format => '%4.2f',
  379:              sortable  => 'no',
  380:              graphable => 'no',
  381:              long_title => 'Minumum Number of Correct Problems',
  382:              selectable => 'yes',
  383:              defaultselected => 'no',
  384:            },
  385:            { name   => 'count',
  386:              title  => 'Count N',
  387:              align  => 'right',
  388:              color  => '#FFFFFF',
  389:              format => '%4d',
  390:              sortable  => 'no',
  391:              graphable => 'no',
  392:              long_title => 'Number of Students in score computations',
  393:              selectable => 'yes',
  394:              defaultselected => 'no',
  395:            },
  396:            { name   => 'KR-21',
  397:              title  => 'KR-21',
  398:              align  => 'right',
  399:              color  => '#FFAAAA',
  400:              format => '%4.2f',
  401:              sortable  => 'no',
  402:              graphable => 'no',
  403:              long_title => 'KR-21 reliability statistic',
  404:              selectable => 'yes',
  405:              defaultselected => 'no',
  406:            },           
  407: );
  408: 
  409: my %SelectedFields;
  410: 
  411: sub parse_field_selection {
  412:     #
  413:     # Pull out the defaults
  414:     if (! defined($ENV{'form.fieldselections'})) {
  415:         $ENV{'form.fieldselections'} = [];
  416:         foreach my $field (@Fields) {
  417:             next if ($field->{'selectable'} ne 'yes');
  418:             if ($field->{'defaultselected'} eq 'yes') {
  419:                 push(@{$ENV{'form.fieldselections'}},$field->{'name'});
  420:             }
  421:         }
  422:     }
  423:     #
  424:     # Make sure the data we are plotting is there
  425:     my %NeededFields;
  426:     if (exists($ENV{'form.plot'}) && $ENV{'form.plot'} ne '' &&
  427:         $ENV{'form.plot'} ne 'none') {
  428:         if ($ENV{'form.plot'} eq 'degrees') {
  429:             $NeededFields{'deg_of_diff'}++;
  430:             $NeededFields{'deg_of_disc'}++;
  431:         } elsif ($ENV{'form.plot'} eq 'tries statistics') {
  432:             $NeededFields{'mean_tries'}++;
  433:             $NeededFields{'std_tries'}++;
  434:             $NeededFields{'problem_num'}++;
  435:         } else {
  436:             $NeededFields{$ENV{'form.plot'}}++;
  437:         }
  438:     }
  439:     #
  440:     # This should not happen, but in case it does...
  441:     if (ref($ENV{'form.fieldselections'}) ne 'ARRAY') {
  442:         $ENV{'form.fieldselections'} = [$ENV{'form.fieldselections'}];
  443:     }
  444:     #
  445:     # Set the field data and the selected fields (for easier checking)
  446:     undef(%SelectedFields);
  447:     foreach my $field (@Fields) {
  448:         if ($field->{'selectable'} ne 'yes') {
  449:             $field->{'selected'} = 'yes';
  450:         } else {
  451:             $field->{'selected'} = 'no';
  452:         }
  453:         if (exists($NeededFields{$field->{'name'}})) {
  454:             $field->{'selected'} = 'yes';
  455:             $SelectedFields{$field->{'name'}}++;
  456:         }
  457:         foreach my $selection (@{$ENV{'form.fieldselections'}}) {
  458:             if ($selection eq $field->{'name'} || $selection eq 'all') {
  459:                 $field->{'selected'} = 'yes';
  460:                 $SelectedFields{$field->{'name'}}++;
  461:             }
  462:         }
  463:     }
  464:     return;
  465: }
  466: 
  467: sub field_selection_input {
  468:     my $Str = '<select name="fieldselections" multiple size="5">'."\n";
  469:     $Str .= '<option value="all">all</option>'."\n";
  470:     foreach my $field (@Fields) {
  471:         next if ($field->{'selectable'} ne 'yes');
  472:         $Str .= '    <option value="'.$field->{'name'}.'" ';
  473:         if ($field->{'selected'} eq 'yes') {
  474:             $Str .= 'selected ';
  475:         }
  476:         $Str .= '>'.$field->{'title'}.'</option>'."\n";
  477:     }
  478:     $Str .= "</select>\n";
  479: }
  480: 
  481: ###############################################
  482: ###############################################
  483: 
  484: =pod 
  485: 
  486: =item &CreateInterface()
  487: 
  488: Create the main intereface for the statistics page.  Allows the user to
  489: select sections, maps, and output.
  490: 
  491: =cut
  492: 
  493: ###############################################
  494: ###############################################
  495: sub CreateInterface {
  496:     #
  497:     &parse_field_selection();
  498:     #
  499:     my $Str = '';
  500:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  501:         (undef,'Overall Problem Statistics','Statistics_Overall_Key');
  502:     $Str .= '<table cellspacing="5">'."\n";
  503:     $Str .= '<tr>';
  504:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  505:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  506:     $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
  507:     $Str .= '<td align="center"><b>'.&mt('Statistics').'</b></td>';
  508:     $Str .= '<td rowspan="2">'.
  509:         &Apache::lonstathelpers::limit_by_time_form().'</td>';
  510:     $Str .= '</tr>'."\n";
  511:     #
  512:     $Str .= '<tr><td align="center">'."\n";
  513:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  514:     $Str .= '</td><td align="center">';
  515:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  516:     $Str .= '</td><td align="center">';
  517:     #
  518:     my $only_seq_with_assessments = sub { 
  519:         my $s=shift;
  520:         if ($s->{'num_assess'} < 1) { 
  521:             return 0;
  522:         } else { 
  523:             return 1;
  524:         }
  525:     };
  526:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  527:                                               $only_seq_with_assessments);
  528:     $Str .= '</td><td>'.&field_selection_input();
  529:     $Str .= '</td></tr>'."\n";
  530:     $Str .= '</table>'."\n";
  531:     $Str .= '<input type="submit" name="GenerateStatistics" value="'.
  532:         &mt('Generate Statistics').'" />';
  533:     $Str .= '&nbsp;'x5;
  534:     $Str .= 'Plot '.&plot_dropdown().('&nbsp;'x10);
  535:     $Str .= '<input type="submit" name="ClearCache" value="'.
  536:         &mt('Clear Caches').'" />';
  537:     $Str .= '&nbsp;'x5;
  538:     $Str .= '<input type="submit" name="UpdateCache" value="'.
  539:         &mt('Update Student Data').'" />';
  540:     $Str .= '&nbsp;'x5;
  541:     $Str .= '<input type="submit" name="Excel" value="'.
  542:         &mt('Produce Excel Output').'" />';
  543:     $Str .= '&nbsp;'x5;
  544:     return $Str;
  545: }
  546: 
  547: ###############################################
  548: ###############################################
  549: 
  550: =pod 
  551: 
  552: =item &BuildProblemStatisticsPage()
  553: 
  554: Main interface to problem statistics.
  555: 
  556: =cut
  557: 
  558: ###############################################
  559: ###############################################
  560: sub BuildProblemStatisticsPage {
  561:     my ($r,$c)=@_;
  562:     #
  563:     my %Saveable_Parameters = ('Status' => 'scalar',
  564:                                'statsoutputmode' => 'scalar',
  565:                                'Section' => 'array',
  566:                                'StudentData' => 'array',
  567:                                'Maps' => 'array',
  568:                                'fieldselections'=> 'array');
  569:     &Apache::loncommon::store_course_settings('statistics',
  570:                                               \%Saveable_Parameters);
  571:     &Apache::loncommon::restore_course_settings('statistics',
  572:                                                 \%Saveable_Parameters);
  573:     #
  574:     &Apache::lonstatistics::PrepareClasslist();
  575:     #
  576:     # Clear the package variables
  577:     undef(@StatsArray);
  578:     undef(%SeqStat);
  579:     #
  580:     # Finally let the user know we are here
  581:     my $interface = &CreateInterface();
  582:     $r->print($interface);
  583:     $r->print('<input type="hidden" name="sortby" value="'.$ENV{'form.sortby'}.
  584:               '" />');
  585:     #
  586:     if (! exists($ENV{'form.statsfirstcall'})) {
  587:         $r->print('<input type="hidden" name="statsfirstcall" value="yes" />');
  588:         $r->print('<h3>'.
  589:                   &mt('Press "Generate Statistics" when you are ready.').
  590:                   '</h3><p>'.
  591:                   &mt('It may take some time to update the student data '.
  592:                       'for the first analysis.  Future analysis this session '.
  593:                       ' will not have this delay.').
  594:                   '</p>');
  595:         return;
  596:     } elsif ($ENV{'form.statsfirstcall'} eq 'yes' || 
  597:              exists($ENV{'form.UpdateCache'}) ||
  598:              exists($ENV{'form.ClearCache'}) ) {
  599:         $r->print('<input type="hidden" name="statsfirstcall" value="no" />');
  600:         &Apache::lonstatistics::Gather_Student_Data($r);
  601:     } else {
  602:         $r->print('<input type="hidden" name="statsfirstcall" value="no" />');
  603:     }
  604:     $r->rflush();
  605:     #
  606:     # This probably does not need to be done each time we are called, but
  607:     # it does not slow things down noticably.
  608:     &Apache::loncoursedata::populate_weight_table();
  609:     #
  610:     if (exists($ENV{'form.Excel'})) {
  611:         &Excel_output($r);
  612:     } else {
  613:         my $count = 0;
  614:         foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  615:             $count += $seq->{'num_assess_parts'};
  616:         }
  617:         if ($count > 10) {
  618:             $r->print('<h2>'.
  619:                       &mt('Compiling statistics for [_1] problems',$count).
  620:                       '</h2>');
  621:             if ($count > 30) {
  622:                 $r->print('<h3>'.&mt('This will take some time.').'</h3>');
  623:             }
  624:             $r->rflush();
  625:         }
  626:         #
  627:         my $sortby = $ENV{'form.sortby'};
  628:         $sortby = 'container' if (! defined($sortby) || $sortby =~ /^\s*$/);
  629:         my $plot = $ENV{'form.plot'};
  630:         if ($plot eq '' || $plot eq 'none') {
  631:             undef($plot);
  632:         }
  633:         if ($sortby eq 'container' && ! defined($plot)) {
  634:             &output_sequence_statistics($r);
  635:             &output_html_by_sequence($r);
  636:         } else {
  637:             if (defined($plot)) {
  638:                 &make_plot($r,$plot);
  639:             }
  640:             &output_html_stats($r);
  641:             &output_sequence_statistics($r);
  642:         }
  643:     }
  644:     return;
  645: }
  646: 
  647: sub output_sequence_statistics {
  648:     my ($r) = @_;
  649:     my $c=$r->connection();
  650:     $r->print('<h2>'.&mt('Sequence Statistics').'</h2>');
  651:     $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
  652:               '<table border="0" cellpadding="3">'."\n".
  653:               '<tr bgcolor="#FFFFE6">');
  654:     $r->print(&sequence_html_header());
  655:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  656:         last if ($c->aborted);
  657:         next if ($seq->{'num_assess'} < 1);
  658:         &compute_sequence_statistics($seq);
  659:         $r->print(&sequence_html_output($seq));
  660:     }
  661:     $r->print('</table>');
  662:     $r->print('</table>');
  663:     $r->rflush();
  664:     return;
  665: }
  666: 
  667: 
  668: ##########################################################
  669: ##########################################################
  670: ##
  671: ## HTML output routines
  672: ##
  673: ##########################################################
  674: ##########################################################
  675: sub output_html_by_sequence {
  676:     my ($r) = @_;
  677:     my $c = $r->connection();
  678:     $r->print(&html_preamble());
  679:     #
  680:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  681:         last if ($c->aborted);
  682:         next if ($seq->{'num_assess'} < 1);
  683:         $r->print("<h3>".$seq->{'title'}."</h3>".
  684:                   '<table border="0"><tr><td bgcolor="#777777">'."\n".
  685:                   '<table border="0" cellpadding="3">'."\n".
  686:                   '<tr bgcolor="#FFFFE6">'.
  687:                   &statistics_table_header('no container')."</tr>\n");
  688:         my @Data = &compute_statistics_on_sequence($seq);
  689:         foreach my $data (@Data) {
  690:             $r->print('<tr>'.&statistics_html_table_data($data,
  691:                                                          'no container').
  692:                       "</tr>\n");
  693:         }
  694:         $r->print('</table>'."\n".'</table>'."\n");
  695:         $r->rflush();
  696:     }
  697:     return;
  698: }
  699: 
  700: sub output_html_stats {
  701:     my ($r)=@_;
  702:     &compute_all_statistics($r);
  703:     $r->print(&html_preamble());
  704:     &sort_data($ENV{'form.sortby'});
  705:     #
  706:     my $count=0;
  707:     foreach my $data (@StatsArray) {
  708:         if ($count++ % 50 == 0) {
  709:             $r->print("</table>\n</table>\n");
  710:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
  711:                       '<table border="0" cellpadding="3">'."\n".
  712:                       '<tr bgcolor="#FFFFE6">'.
  713:                       '<tr bgcolor="#FFFFE6">'.
  714:                       &statistics_table_header().
  715:                       "</tr>\n");
  716:         }
  717:         $r->print('<tr>'.&statistics_html_table_data($data)."</tr>\n");
  718:     }
  719:     $r->print("</table>\n</table>\n");
  720:     return;
  721: }
  722: 
  723: sub html_preamble {
  724:     my $Str='';
  725:     $Str .= "<h2>".
  726:         $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  727:         "</h2>\n";
  728:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
  729:     if (defined($starttime) || defined($endtime)) {
  730:         # Inform the user what the time limits on the data are.
  731:         $Str .= '<h3>'.&mt('Statistics on submissions from [_1] to [_2]',
  732:                            &Apache::lonlocal::locallocaltime($starttime),
  733:                            &Apache::lonlocal::locallocaltime($endtime)
  734:                            ).'</h3>';
  735:     }
  736:     $Str .= "<h3>".&mt('Compiled on [_1]',
  737:                        &Apache::lonlocal::locallocaltime(time))."</h3>";
  738:     return $Str;
  739: }
  740: 
  741: 
  742: ###############################################
  743: ###############################################
  744: ##
  745: ## Misc HTML output routines
  746: ##
  747: ###############################################
  748: ###############################################
  749: sub statistics_html_table_data {
  750:     my ($data,$options) = @_;
  751:     my $row = '';
  752:     foreach my $field (@Fields) {
  753:         next if ($options =~ /no $field->{'name'}/);
  754:         next if ($field->{'selected'} ne 'yes');
  755:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  756:         if (exists($field->{'align'})) {
  757:             $row .= ' align="'.$field->{'align'}.'"';
  758:             }
  759:         $row .= '>';
  760:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  761:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
  762:         }
  763:         if (exists($field->{'format'})) {
  764:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  765:         } else {
  766:             $row .= $data->{$field->{'name'}};
  767:         }
  768:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  769:             $row.= '</a>';
  770:         }
  771:         $row .= '</td>';
  772:     }
  773:     return $row;
  774: }
  775: 
  776: sub statistics_table_header {
  777:     my ($options) = @_;
  778:     my $header_row;
  779:     foreach my $field (@Fields) {
  780:         next if ($options =~ /no $field->{'name'}/);
  781:         next if ($field->{'selected'} ne 'yes');
  782:         $header_row .= '<th>';
  783:         if (exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
  784:             $header_row .= '<a href="javascript:'.
  785:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
  786:                     ';document.Statistics.submit();">';
  787:         }
  788:         $header_row .= &mt($field->{'title'});
  789:         if ($options =~ /sortable/) {
  790:             $header_row.= '</a>';
  791:         }
  792:         if ($options !~ /no plots/        && 
  793:             exists($field->{'graphable'}) && 
  794:             $field->{'graphable'} eq 'yes') {
  795:             $header_row.=' (';
  796:             $header_row .= '<a href="javascript:'.
  797:                 "document.Statistics.plot.value='$field->{'name'}'".
  798:                     ';document.Statistics.submit();">';
  799:             $header_row .= &mt('plot').'</a>)';
  800:         }
  801:         $header_row .= '</th>';
  802:     }
  803:     return $header_row;
  804: }
  805: 
  806: sub sequence_html_header {
  807:     my $Str .= '<tr>';
  808:     foreach my $field (@SeqFields) {
  809: #        next if ($field->{'selected'} ne 'yes');
  810:         $Str .= '<th bgcolor="'.$field->{'color'}.'"';
  811:         $Str .= '>'.$field->{'title'}.'</th>';
  812:     }
  813:     $Str .= '</tr>';
  814:     return $Str;
  815: }
  816: 
  817: 
  818: sub sequence_html_output {
  819:     my ($seq) = @_;
  820:     my $data = $SeqStat{$seq->{'symb'}};
  821: #    $SeqStat{$symb}->{'max'}
  822: #    $SeqStat{$symb}->{'min'}
  823: #    $SeqStat{$symb}->{'mean'}
  824: #    $SeqStat{$symb}->{'std'}
  825: #    $SeqStat{$symb}->{'count'}
  826: #    $SeqStat{$symb}->{'max_possible'}
  827:     my $row = '<tr>';
  828:     foreach my $field (@SeqFields) {
  829: #        next if ($field->{'selected'} ne 'yes');
  830:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  831:         if (exists($field->{'align'})) {
  832:             $row .= ' align="'.$field->{'align'}.'"';
  833:         }
  834:         $row .= '>';
  835:         if (exists($field->{'format'})) {
  836:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  837:         } else {
  838:             $row .= $data->{$field->{'name'}};
  839:         }
  840:         $row .= '</td>';
  841:     }
  842:     $row .= '</tr>'."\n";
  843:     return $row;
  844: }
  845: 
  846: ####################################################
  847: ####################################################
  848: ##
  849: ##    Plotting Routines
  850: ##
  851: ####################################################
  852: ####################################################
  853: sub make_plot {
  854:     my ($r,$plot) = @_;
  855:     &compute_all_statistics($r);
  856:     &sort_data($ENV{'form.sortby'});
  857:     if ($plot eq 'degrees') {
  858:         &degrees_plot($r);
  859:     } elsif ($plot eq 'tries statistics') {
  860:         &tries_data_plot($r);
  861:     } else {
  862:         &make_single_stat_plot($r,$plot);
  863:     }
  864:     return;
  865: }
  866: 
  867: sub make_single_stat_plot {
  868:     my ($r,$datafield) = @_;
  869:     #
  870:     my $title; my $yaxis;
  871:     foreach my $field (@Fields) {
  872:         next if ($field->{'name'} ne $datafield);
  873:         $title = $field->{'long_title'};
  874:         $yaxis = $field->{'title'};
  875:         last;
  876:     }
  877:     if ($title eq '' || $yaxis eq '') {
  878:         # datafield is something we do not know enough about to plot
  879:         $r->print('<h3>'.
  880:                   &mt('Unable to plot the requested statistic.').
  881:                   '</h3>');
  882:         return;
  883:     }
  884:     #
  885:     # Build up the data sets to plot
  886:     my @Labels; 
  887:     my @Data;
  888:     my $max = 1;
  889:     foreach my $data (@StatsArray) {
  890:         push(@Labels,$data->{'problem_num'});
  891:         push(@Data,$data->{$datafield});
  892:         if ($data->{$datafield}>$max) {
  893:             $max = $data->{$datafield};
  894:         }
  895:     }
  896:     foreach (1,2,3,4,5,10,15,20,25,40,50,75,100,150,200,250,300,500,600,750,
  897:              1000,1500,2000,2500,3000,3500,4000,5000,7500,10000,15000,20000) {
  898:         if ($max <= $_) {
  899:             $max = $_;
  900:             last;
  901:         }
  902:     }
  903:     if ($max > 20000) {
  904:         $max = 10000*(int($max/10000)+1);
  905:     }
  906:     #
  907:     $r->print("<p>".&Apache::loncommon::DrawBarGraph($title,
  908:                                                      'Problem Number',
  909:                                                      $yaxis,
  910:                                                      $max,
  911:                                                      undef, # colors
  912:                                                      \@Labels,
  913:                                                      \@Data)."</p>\n");
  914:     return;
  915: }
  916: 
  917: sub degrees_plot {
  918:     my ($r)=@_;
  919:     my $count = scalar(@StatsArray);
  920:     my $width = 50 + 10*$count;
  921:     $width = 300 if ($width < 300);
  922:     my $height = 300;
  923:     my $plot = '';
  924:     my $ymax = 0;
  925:     my $ymin = 0;
  926:     my @Disc; my @Diff; my @Labels;    
  927:     foreach my $data (@StatsArray) {
  928:         push(@Labels,$data->{'problem_num'});
  929:         my $disc = $data->{'deg_of_disc'};
  930:         my $diff = $data->{'deg_of_diff'};
  931:         push(@Disc,$disc);
  932:         push(@Diff,$diff);
  933:         #
  934:         $ymin = $disc if ($ymin > $disc);
  935:         $ymin = $diff if ($ymin > $diff);
  936:         $ymax = $disc if ($ymax < $disc);
  937:         $ymax = $diff if ($ymax < $diff);
  938:     }
  939:     #
  940:     # Make sure we show relevant information.
  941:     if ($ymin < 0) {
  942:         if (abs($ymin) < 0.05) {
  943:             $ymin = 0;
  944:         } else {
  945:             $ymin = -1;
  946:         }
  947:     }
  948:     if ($ymax > 0) {
  949:         if (abs($ymax) < 0.05) {
  950:             $ymax = 0;
  951:         } else {
  952:             $ymax = 1;
  953:         }
  954:     }
  955:     #
  956:     my $xmax = $Labels[-1];
  957:     if ($xmax > 50) {
  958:         if ($xmax % 10 != 0) {
  959:             $xmax = 10 * (int($xmax/10)+1);
  960:         }
  961:     } else {
  962:         if ($xmax % 5 != 0) {
  963:             $xmax = 5 * (int($xmax/5)+1);
  964:         }
  965:     }
  966:     #
  967:     my $discdata .= '<data>'.join(',',@Labels).'</data>'.$/.
  968:                     '<data>'.join(',',@Disc).'</data>'.$/;
  969:     #
  970:     my $diffdata .= '<data>'.join(',',@Labels).'</data>'.$/.
  971:                     '<data>'.join(',',@Diff).'</data>'.$/;
  972:     #
  973:     my $title = 'Degree of Discrimination\nand Degree of Difficulty';
  974:     if ($xmax > 50) {
  975:         $title = 'Degree of Discrimination and Degree of Difficulty';
  976:     }
  977:     #
  978:     $plot=<<"END";
  979: <gnuplot 
  980:     texfont="10"
  981:     fgcolor="x000000"
  982:     plottype="Cartesian"
  983:     font="large"
  984:     grid="on"
  985:     align="center"
  986:     border="on"
  987:     transparent="on"
  988:     alttag="Degree of Discrimination and Degree of Difficulty Plot"
  989:     samples="100"
  990:     bgcolor="xffffff"
  991:     height="$height"
  992:     width="$width">
  993:     <key 
  994:         pos="top right"
  995:         title=""
  996:         box="off" />
  997:     <title>$title</title>
  998:     <axis xmin="0" ymin="$ymin" xmax="$xmax" ymax="$ymax" color="x000000" />
  999:     <xlabel>Problem Number</xlabel>
 1000:     <curve 
 1001:         linestyle="linespoints" 
 1002:         name="DoDisc" 
 1003:         pointtype="0" 
 1004:         color="x000000">
 1005:         $discdata
 1006:     </curve>
 1007:     <curve 
 1008:         linestyle="linespoints" 
 1009:         name="DoDiff" 
 1010:         pointtype="0" 
 1011:         color="xFF0000">
 1012:         $diffdata
 1013:     </curve>
 1014: </gnuplot>
 1015: END
 1016:     my $plotresult = 
 1017:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
 1018:     $r->print($plotresult);
 1019:     return;
 1020: }
 1021: 
 1022: sub tries_data_plot {
 1023:     my ($r)=@_;
 1024:     my $count = scalar(@StatsArray);
 1025:     my $width = 50 + 10*$count;
 1026:     $width = 300 if ($width < 300);
 1027:     my $height = 300;
 1028:     my $plot = '';
 1029:     my @STD;  my @Mean; my @Max; my @Min;
 1030:     my @Labels;
 1031:     my $ymax = 5;
 1032:     foreach my $data (@StatsArray) {
 1033:         my $max = $data->{'mean_tries'} + $data->{'std_tries'};
 1034:         $ymax = $max if ($ymax < $max);
 1035:         $ymax = $max if ($ymax < $max);
 1036:         push(@Labels,$data->{'problem_num'});
 1037:         push(@STD,$data->{'std_tries'});
 1038:         push(@Mean,$data->{'mean_tries'});
 1039:     }
 1040:     #
 1041:     # Make sure we show relevant information.
 1042:     my $xmax = $Labels[-1];
 1043:     if ($xmax > 50) {
 1044:         if ($xmax % 10 != 0) {
 1045:             $xmax = 10 * (int($xmax/10)+1);
 1046:         }
 1047:     } else {
 1048:         if ($xmax % 5 != 0) {
 1049:             $xmax = 5 * (int($xmax/5)+1);
 1050:         }
 1051:     }
 1052:     $ymax = int($ymax)+1+2;
 1053:     #
 1054:     my $std_data .= '<data>'.join(',',@Labels).'</data>'.$/.
 1055:                     '<data>'.join(',',@Mean).'</data>'.$/;
 1056:     #
 1057:     my $std_error_data .= '<data>'.join(',',@Labels).'</data>'.$/.
 1058:                           '<data>'.join(',',@Mean).'</data>'.$/.
 1059:                           '<data>'.join(',',@STD).'</data>'.$/;
 1060:     #
 1061:     my $title = 'Mean and S.D. of Tries';
 1062:     if ($xmax > 25) {
 1063:         $title = 'Mean and Standard Deviation of Tries';
 1064:     }
 1065:     #
 1066:     $plot=<<"END";
 1067: <gnuplot 
 1068:     texfont="10"
 1069:     fgcolor="x000000"
 1070:     plottype="Cartesian"
 1071:     font="large"
 1072:     grid="on"
 1073:     align="center"
 1074:     border="on"
 1075:     transparent="on"
 1076:     alttag="Mean and S.D of Tries Plot"
 1077:     samples="100"
 1078:     bgcolor="xffffff"
 1079:     height="$height"
 1080:     width="$width">
 1081:     <title>$title</title>
 1082:     <axis xmin="0" ymin="0" xmax="$xmax" ymax="$ymax" color="x000000" />
 1083:     <xlabel>Problem Number</xlabel>
 1084:     <ylabel>Number of Tries</ylabel>
 1085:     <curve 
 1086:         linestyle="yerrorbars"
 1087:         name="S.D. Tries" 
 1088:         pointtype="1" 
 1089:         color="x666666">
 1090:         $std_error_data
 1091:     </curve>
 1092:     <curve 
 1093:         linestyle="points"
 1094:         name="Mean Tries" 
 1095:         pointtype="1" 
 1096:         color="xCC4444">
 1097:         $std_data
 1098:     </curve>
 1099: </gnuplot>
 1100: END
 1101:     my $plotresult = 
 1102:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
 1103:     $r->print($plotresult);
 1104:     return;
 1105: }
 1106: 
 1107: sub plot_dropdown {
 1108:     my $current = '';
 1109:     #
 1110:     if (defined($ENV{'form.plot'})) {
 1111:         $current = $ENV{'form.plot'};
 1112:     }
 1113:     #
 1114:     my @Additional_Plots = (
 1115:                             { graphable=>'yes',
 1116:                               name => 'degrees',
 1117:                               title => 'Difficulty Indexes' },
 1118:                             { graphable=>'yes',
 1119:                               name => 'tries statistics',
 1120:                               title => 'Tries Statistics' });
 1121:     #
 1122:     my $Str= "\n".'<select name="plot" size="1">';
 1123:     $Str .= '<option name="none"></option>'."\n";
 1124:     $Str .= '<option name="none2">none</option>'."\n";
 1125:     foreach my $field (@Additional_Plots,@Fields) {
 1126:         if (! exists($field->{'graphable'}) ||
 1127:             $field->{'graphable'} ne 'yes') {
 1128:             next;
 1129:         }
 1130:         $Str .= '<option value="'.$field->{'name'}.'"';
 1131:         if ($field->{'name'} eq $current) {
 1132:             $Str .= ' selected ';
 1133:         }
 1134:         $Str.= '>'.&mt($field->{'title'}).'</option>'."\n";
 1135:     }
 1136:     $Str .= '</select>'."\n";
 1137:     return $Str;
 1138: }
 1139: 
 1140: ###############################################
 1141: ###############################################
 1142: ##
 1143: ## Excel output routines
 1144: ##
 1145: ###############################################
 1146: ###############################################
 1147: sub Excel_output {
 1148:     my ($r) = @_;
 1149:     $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
 1150:     ##
 1151:     ## Compute the statistics
 1152:     &compute_all_statistics($r);
 1153:     my $c = $r->connection;
 1154:     return if ($c->aborted());
 1155:     ##
 1156:     ## Create the excel workbook
 1157:     my $filename = '/prtspool/'.
 1158:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1159:         time.'_'.rand(1000000000).'.xls';
 1160:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1161:     #
 1162:     # Create sheet
 1163:     my $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1164:     #
 1165:     # Check for errors
 1166:     if (! defined($excel_workbook)) {
 1167:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1168:         $r->print(&mt("Problems creating new Excel file.  ".
 1169:                   "This error has been logged.  ".
 1170:                   "Please alert your LON-CAPA administrator."));
 1171:         return 0;
 1172:     }
 1173:     #
 1174:     # The excel spreadsheet stores temporary data in files, then put them
 1175:     # together.  If needed we should be able to disable this (memory only).
 1176:     # The temporary directory must be specified before calling 'addworksheet'.
 1177:     # File::Temp is used to determine the temporary directory.
 1178:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
 1179:     #
 1180:     # Add a worksheet
 1181:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1182:     if (length($sheetname) > 31) {
 1183:         $sheetname = substr($sheetname,0,31);
 1184:     }
 1185:     my $excel_sheet = $excel_workbook->addworksheet(
 1186:         &Apache::loncommon::clean_excel_name($sheetname));
 1187:     ##
 1188:     ## Begin creating excel sheet
 1189:     ##
 1190:     my ($rows_output,$cols_output) = (0,0);
 1191:     #
 1192:     # Put the course description in the header
 1193:     $excel_sheet->write($rows_output,$cols_output++,
 1194:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
 1195:     $cols_output += 3;
 1196:     #
 1197:     # Put a description of the sections listed
 1198:     my $sectionstring = '';
 1199:     my @Sections = @Apache::lonstatistics::SelectedSections;
 1200:     if (scalar(@Sections) > 1) {
 1201:         if (scalar(@Sections) > 2) {
 1202:             my $last = pop(@Sections);
 1203:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
 1204:         } else {
 1205:             $sectionstring = "Sections ".join(' and ',@Sections);
 1206:         }
 1207:     } else {
 1208:         if ($Sections[0] eq 'all') {
 1209:             $sectionstring = "All sections";
 1210:         } else {
 1211:             $sectionstring = "Section ".$Sections[0];
 1212:         }
 1213:     }
 1214:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
 1215:     $cols_output += scalar(@Sections);
 1216:     #
 1217:     # Time restrictions
 1218:     my $time_string;
 1219:     if (defined($starttime)) {
 1220:         # call localtime but not lonlocal:locallocaltime because excel probably
 1221:         # cannot handle localized text.  Probably.
 1222:         $time_string .= 'Data collected from '.localtime($time_string);
 1223:         if (defined($endtime)) {
 1224:             $time_string .= ' to '.localtime($endtime);
 1225:         }
 1226:         $time_string .= '.';
 1227:     } elsif (defined($endtime)) {
 1228:         # See note above about lonlocal:locallocaltime
 1229:         $time_string .= 'Data collected before '.localtime($endtime).'.';
 1230:     }
 1231:     #
 1232:     # Put the date in there too
 1233:     $excel_sheet->write($rows_output,$cols_output++,
 1234:                         'Compiled on '.localtime(time));
 1235:     #
 1236:     $rows_output++; 
 1237:     $cols_output=0;
 1238:     #
 1239:     # Long Headers
 1240:     foreach my $field (@Fields) {
 1241:         next if ($field->{'name'} eq 'problem_num');
 1242:         next if ($field->{'selected'} ne 'yes');
 1243:         if (exists($field->{'long_title'})) {
 1244:             $excel_sheet->write($rows_output,$cols_output++,
 1245:                                 $field->{'long_title'});
 1246:         } else {
 1247:             $excel_sheet->write($rows_output,$cols_output++,'');
 1248:         }
 1249:     }
 1250:     $rows_output++;
 1251:     $cols_output=0;
 1252:     # Brief headers
 1253:     foreach my $field (@Fields) {
 1254:         next if ($field->{'selected'} ne 'yes');
 1255:         next if ($field->{'name'} eq 'problem_num');
 1256:         # Use english for excel as I am not sure how well excel handles 
 1257:         # other character sets....
 1258:         $excel_sheet->write($rows_output,$cols_output++,$field->{'title'});
 1259:     }
 1260:     $rows_output++;
 1261:     foreach my $data (@StatsArray) {
 1262:         $cols_output=0;
 1263:         foreach my $field (@Fields) {
 1264:             next if ($field->{'selected'} ne 'yes');
 1265:             next if ($field->{'name'} eq 'problem_num');
 1266:             $excel_sheet->write($rows_output,$cols_output++,
 1267:                                 $data->{$field->{'name'}});
 1268:         }
 1269:         $rows_output++;
 1270:     }
 1271:     #
 1272:     $excel_workbook->close();
 1273:     #
 1274:     # Tell the user where to get their excel file
 1275:     $r->print('<br />'.
 1276:               '<a href="'.$filename.'">'.
 1277:               &mt('Your Excel Spreadsheet').'</a>'."\n");
 1278:     $r->rflush();
 1279:     return;
 1280: }
 1281: 
 1282: ##################################################
 1283: ##################################################
 1284: ##
 1285: ## Statistics Gathering and Manipulation Routines
 1286: ##
 1287: ##################################################
 1288: ##################################################
 1289: sub compute_statistics_on_sequence {
 1290:     my ($seq) = @_;
 1291:     my @Data;
 1292:     foreach my $res (@{$seq->{'contents'}}) {
 1293:         next if ($res->{'type'} ne 'assessment');
 1294:         foreach my $part (@{$res->{'parts'}}) {
 1295:             #
 1296:             # This is where all the work happens
 1297:             my $data = &get_statistics($seq,$res,$part,scalar(@StatsArray)+1);
 1298:             push (@Data,$data);
 1299:             push (@StatsArray,$data);
 1300:         }
 1301:     }
 1302:     return @Data;
 1303: }
 1304: 
 1305: sub compute_all_statistics {
 1306:     my ($r) = @_;
 1307:     if (@StatsArray > 0) {
 1308:         # Assume we have already computed the statistics
 1309:         return;
 1310:     }
 1311:     my $c = $r->connection;
 1312:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1313:         last if ($c->aborted);
 1314:         next if ($seq->{'num_assess'} < 1);
 1315:         &compute_statistics_on_sequence($seq);
 1316:     }
 1317: }
 1318: 
 1319: sub sort_data {
 1320:     my ($sortkey) = @_;
 1321:     return if (! @StatsArray);
 1322:     #
 1323:     # Sort the data
 1324:     my $sortby = undef;
 1325:     foreach my $field (@Fields) {
 1326:         if ($sortkey eq $field->{'name'}) {
 1327:             $sortby = $field->{'name'};
 1328:         }
 1329:     }
 1330:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
 1331:         $sortby = 'container';
 1332:     }
 1333:     if ($sortby ne 'container') {
 1334:         # $sortby is already defined, so we can charge ahead
 1335:         if ($sortby =~ /^(title|part)$/i) {
 1336:             # Alpha comparison
 1337:             @StatsArray = sort {
 1338:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
 1339:                 lc($a->{'title'}) cmp lc($b->{'title'}) ||
 1340:                 lc($a->{'part'}) cmp lc($b->{'part'});
 1341:             } @StatsArray;
 1342:         } else {
 1343:             # Numerical comparison
 1344:             @StatsArray = sort {
 1345:                 my $retvalue = 0;
 1346:                 if ($b->{$sortby} eq 'nan') {
 1347:                     if ($a->{$sortby} ne 'nan') {
 1348:                         $retvalue = -1;
 1349:                     } else {
 1350:                         $retvalue = 0;
 1351:                     }
 1352:                 }
 1353:                 if ($a->{$sortby} eq 'nan') {
 1354:                     if ($b->{$sortby} ne 'nan') {
 1355:                         $retvalue = 1;
 1356:                     }
 1357:                 }
 1358:                 if ($retvalue eq '0') {
 1359:                     $retvalue = $b->{$sortby} <=> $a->{$sortby}     ||
 1360:                             lc($a->{'title'}) <=> lc($b->{'title'}) ||
 1361:                             lc($a->{'part'})  <=> lc($b->{'part'});
 1362:                 }
 1363:                 $retvalue;
 1364:             } @StatsArray;
 1365:         }
 1366:     }
 1367:     #
 1368:     # Renumber the data set
 1369:     my $count;
 1370:     foreach my $data (@StatsArray) {
 1371:         $data->{'problem_num'} = ++$count;
 1372:     }
 1373:     return;
 1374: }
 1375: 
 1376: ########################################################
 1377: ########################################################
 1378: 
 1379: =pod
 1380: 
 1381: =item &get_statistics()
 1382: 
 1383: Wrapper routine from the call to loncoursedata::get_problem_statistics.  
 1384: Calls lonstathelpers::get_time_limits() to limit the data set by time
 1385: and &compute_discrimination_factor
 1386: 
 1387: Inputs: $sequence, $resource, $part, $problem_num
 1388: 
 1389: Returns: Hash reference with statistics data from 
 1390: loncoursedata::get_problem_statistics.
 1391: 
 1392: =cut
 1393: 
 1394: ########################################################
 1395: ########################################################
 1396: sub get_statistics {
 1397:     my ($sequence,$resource,$part,$problem_num) = @_;
 1398:     #
 1399:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1400:     my $symb = $resource->{'symb'};
 1401:     my $courseid = $ENV{'request.course.id'};
 1402:     #
 1403:     my $data = &Apache::loncoursedata::get_problem_statistics
 1404:                         (\@Apache::lonstatistics::SelectedSections,
 1405:                          $Apache::lonstatistics::enrollment_status,
 1406:                          $symb,$part,$courseid,$starttime,$endtime);
 1407:     $data->{'part'}        = $part;
 1408:     $data->{'problem_num'} = $problem_num;
 1409:     $data->{'container'}   = $sequence->{'title'};
 1410:     $data->{'title'}       = $resource->{'title'};
 1411:     $data->{'title.link'}  = $resource->{'src'}.'?symb='.
 1412:         &Apache::lonnet::escape($resource->{'symb'});
 1413:     #
 1414:     if ($SelectedFields{'deg_of_disc'}) {
 1415:         $data->{'deg_of_disc'} = 
 1416:             &compute_discrimination_factor($resource,$part,$sequence);
 1417:     }
 1418:     return $data;
 1419: }
 1420: 
 1421: ###############################################
 1422: ###############################################
 1423: 
 1424: =pod
 1425: 
 1426: =item &compute_discrimination_factor()
 1427: 
 1428: Inputs: $Resource, $Sequence
 1429: 
 1430: Returns: integer between -1 and 1
 1431: 
 1432: =cut
 1433: 
 1434: ###############################################
 1435: ###############################################
 1436: sub compute_discrimination_factor {
 1437:     my ($resource,$part,$sequence) = @_;
 1438:     my @Resources;
 1439:     foreach my $res (@{$sequence->{'contents'}}) {
 1440:         next if ($res->{'symb'} eq $resource->{'symb'});
 1441:         push (@Resources,$res->{'symb'});
 1442:     }
 1443:     #
 1444:     # rank
 1445:     my $ranking = 
 1446:         &Apache::loncoursedata::rank_students_by_scores_on_resources
 1447:         (\@Resources,
 1448:          \@Apache::lonstatistics::SelectedSections,
 1449:          $Apache::lonstatistics::enrollment_status,undef);
 1450:     #
 1451:     # compute their percent scores on the problems in the sequence,
 1452:     my $number_to_grab = int(scalar(@{$ranking})/4);
 1453:     my $num_students = scalar(@{$ranking});
 1454:     my @BottomSet = map { $_->[&Apache::loncoursedata::RNK_student()]; 
 1455:                       } @{$ranking}[0..$number_to_grab];
 1456:     my @TopSet    = 
 1457:         map { 
 1458:             $_->[&Apache::loncoursedata::RNK_student()]; 
 1459:           } @{$ranking}[($num_students-$number_to_grab)..($num_students-1)];
 1460:     my ($bottom_sum,$bottom_max) = 
 1461:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@BottomSet);
 1462:     my ($top_sum,$top_max) = 
 1463:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@TopSet);
 1464:     my $deg_of_disc;
 1465:     if ($top_max == 0 || $bottom_max==0) {
 1466:         $deg_of_disc = 'nan';
 1467:     } else {
 1468:         $deg_of_disc = ($top_sum/$top_max) - ($bottom_sum/$bottom_max);
 1469:     }
 1470:     #&Apache::lonnet::logthis('    '.$top_sum.'/'.$top_max.
 1471:     #                         ' - '.$bottom_sum.'/'.$bottom_max);
 1472:     return $deg_of_disc;
 1473: }
 1474: 
 1475: ###############################################
 1476: ###############################################
 1477: ##
 1478: ## Compute KR-21
 1479: ##
 1480: ## To compute KR-21, you need the following information:
 1481: ##
 1482: ## K=the number of items in your test
 1483: ## M=the mean score on the test
 1484: ## s=the standard deviation of the scores on your test 
 1485: ##
 1486: ## then:
 1487: ## 
 1488: ## KR-21 rk= [K/(K-1)] * [1- (M*(K-M))/(K*s^2))]
 1489: ##
 1490: ###############################################
 1491: ###############################################
 1492: sub compute_sequence_statistics {
 1493:     my ($seq) = @_;
 1494:     my $symb = $seq->{'symb'};
 1495:     my @Resources;
 1496:     foreach my $res (@{$seq->{'contents'}}) {
 1497:         next if ($res->{'type'} ne 'assessment');
 1498:         push (@Resources,$res->{'symb'});
 1499:     }
 1500:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1501:     #
 1502:     # First compute statistics based on student scores
 1503:     my ($smin,$smax,$sMean,$sSTD,$scount,$sMAX) = 
 1504:         &Apache::loncoursedata::score_stats
 1505:                     (\@Apache::lonstatistics::SelectedSections,
 1506:                      $Apache::lonstatistics::enrollment_status,
 1507:                      \@Resources,$starttime,$endtime,undef);
 1508:     $SeqStat{$symb}->{'title'}  = $seq->{'title'};
 1509:     $SeqStat{$symb}->{'scoremax'}  = $smax;
 1510:     $SeqStat{$symb}->{'scoremin'}  = $smin;
 1511:     $SeqStat{$symb}->{'scoremean'} = $sMean;
 1512:     $SeqStat{$symb}->{'scorestd'}  = $sSTD;
 1513:     $SeqStat{$symb}->{'scorecount'} = $scount;
 1514:     $SeqStat{$symb}->{'max_possible'} = $sMAX;
 1515:     #
 1516:     # Compute statistics based on the number of correct problems
 1517:     # 'correct' is taken to mean 
 1518:     my ($cmin,$cmax,$cMean,$cSTD,$ccount)=
 1519:         &Apache::loncoursedata::count_stats
 1520:         (\@Apache::lonstatistics::SelectedSections,
 1521:          $Apache::lonstatistics::enrollment_status,
 1522:          \@Resources,$starttime,$endtime,undef);
 1523:     my $K = $seq->{'num_assess_parts'};
 1524:     my $kr_21;
 1525:     if ($K > 1 && $cSTD > 0) {
 1526:         $kr_21 =  ($K/($K-1)) * (1 - $cMean*($K-$cMean)/($K*$cSTD**2));
 1527:     } else {
 1528:         $kr_21 = 'nan';
 1529:     }
 1530:     $SeqStat{$symb}->{'countmax'} = $cmax;
 1531:     $SeqStat{$symb}->{'countmin'} = $cmin;
 1532:     $SeqStat{$symb}->{'countstd'} = $cSTD;
 1533:     $SeqStat{$symb}->{'count'} = $ccount;
 1534:     $SeqStat{$symb}->{'items'} = $K;
 1535:     $SeqStat{$symb}->{'KR-21'}=$kr_21;
 1536: 
 1537:     return;
 1538: }
 1539: 
 1540: 
 1541: 
 1542: =pod 
 1543: 
 1544: =item ProblemStatisticsLegend
 1545: 
 1546: =over 4
 1547: 
 1548: =item #Stdnts
 1549: Total number of students attempted the problem.
 1550: 
 1551: =item Tries
 1552: Total number of tries for solving the problem.
 1553: 
 1554: =item Max Tries
 1555: Largest number of tries for solving the problem by a student.
 1556: 
 1557: =item Mean
 1558: Average number of tries. [ Tries / #Stdnts ]
 1559: 
 1560: =item #YES
 1561: Number of students solved the problem correctly.
 1562: 
 1563: =item #yes
 1564: Number of students solved the problem by override.
 1565: 
 1566: =item %Wrong
 1567: Percentage of students who tried to solve the problem 
 1568: but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]
 1569: 
 1570: =item DoDiff
 1571: Degree of Difficulty of the problem.  
 1572: [ 1 - ((#YES+#yes) / Tries) ]
 1573: 
 1574: =item S.D.
 1575: Standard Deviation of the tries.  
 1576: [ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) 
 1577: where Xi denotes every student\'s tries ]
 1578: 
 1579: =item Skew.
 1580: Skewness of the students tries.
 1581: [(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]
 1582: 
 1583: =item Dis.F.
 1584: Discrimination Factor: A Standard for evaluating the 
 1585: problem according to a Criterion<br>
 1586: 
 1587: =item [Criterion to group students into %27 Upper Students - 
 1588: and %27 Lower Students]
 1589: 1st Criterion for Sorting the Students: 
 1590: Sum of Partial Credit Awarded / Total Number of Tries
 1591: 2nd Criterion for Sorting the Students: 
 1592: Total number of Correct Answers / Total Number of Tries
 1593: 
 1594: =item Disc.
 1595: Number of Students had at least one discussion.
 1596: 
 1597: =back
 1598: 
 1599: =cut
 1600: 
 1601: ############################################################
 1602: ############################################################
 1603: 
 1604: 1;
 1605: __END__

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