File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.86: download - view: text, annotated - select for diffs
Tue May 18 18:12:19 2004 UTC (20 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
More commented out code for data mining - the response types used in each
problem part can no be reported.

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

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