File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.90: download - view: text, annotated - select for diffs
Mon Jul 12 19:30:12 2004 UTC (19 years, 11 months ago) by matthew
Branches: MAIN
CVS tags: version_1_1_99_3, HEAD
Added help link for statistics on sequences.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemstatistics.pm,v 1.90 2004/07/12 19:30:12 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 => 'yes',
  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:     my ($r) = @_;
  551:     #
  552:     &parse_field_selection();
  553:     #
  554:     my $Str = '';
  555:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  556:         (undef,'Overall Problem Statistics','Statistics_Overall_Key');
  557:     $Str .= '<table cellspacing="5">'."\n";
  558:     $Str .= '<tr>';
  559:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  560:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  561:     $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
  562:     $Str .= '<td align="center"><b>'.&mt('Statistics').'</b></td>';
  563:     $Str .= '<td rowspan="2">'.
  564:         &Apache::lonstathelpers::limit_by_time_form().'</td>';
  565:     $Str .= '</tr>'."\n";
  566:     #
  567:     $Str .= '<tr><td align="center">'."\n";
  568:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  569:     $Str .= '</td><td align="center">';
  570:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  571:     $Str .= '</td><td align="center">';
  572:     #
  573:     my $only_seq_with_assessments = sub { 
  574:         my $s=shift;
  575:         if ($s->{'num_assess'} < 1) { 
  576:             return 0;
  577:         } else { 
  578:             return 1;
  579:         }
  580:     };
  581:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  582:                                               $only_seq_with_assessments);
  583:     $Str .= '</td><td>'.&field_selection_input();
  584:     $Str .= '</td></tr>'."\n";
  585:     $Str .= '</table>'."\n";
  586:     #
  587:     $Str .= '<p>'.&mt('Status: [_1]',
  588:                          '<input type="text" '.
  589:                          'name="stats_status" size="60" value="" />'
  590:                          ).
  591:                          '</nobr></p>';
  592:     #
  593:     $Str .= '<input type="submit" name="GenerateStatistics" value="'.
  594:         &mt('Generate Statistics').'" />';
  595:     $Str .= '&nbsp;'x5;
  596:     $Str .= 'Plot '.&plot_dropdown().('&nbsp;'x10);
  597:     #
  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($r);
  636:     $r->print($interface);
  637:     $r->print('<input type="hidden" name="sortby" value="'.$ENV{'form.sortby'}.
  638:               '" />');
  639:     #
  640:     my @CacheButtonHTML = 
  641:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
  642:     my $Str;
  643:     foreach my $html (@CacheButtonHTML) {
  644:         $Str.=$html.('&nbsp;'x5);
  645:     }
  646:     #
  647:     $r->print($Str);
  648:     if (! exists($ENV{'form.firstrun'})) {
  649:         $r->print('<h3>'.
  650:                   &mt('Press "Generate Statistics" when you are ready.').
  651:                   '</h3><p>'.
  652:                   &mt('It may take some time to update the student data '.
  653:                       'for the first analysis.  Future analysis this session '.
  654:                       ' will not have this delay.').
  655:                   '</p>');
  656:         return;
  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:         $r->print('<input type="submit" name="Excel" value="'.
  668:                   &mt('Produce Excel Output').'" />'.'&nbsp;'x5);
  669:         $r->rflush();
  670:         my $count = 0;
  671:         foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  672:             $count += $seq->{'num_assess_parts'};
  673:         }
  674:         if ($count > 10) {
  675:             $r->print('<h2>'.
  676:                       &mt('Compiling statistics for [_1] problems',$count).
  677:                       '</h2>');
  678:             if ($count > 30) {
  679:                 $r->print('<h3>'.&mt('This will take some time.').'</h3>');
  680:             }
  681:             $r->rflush();
  682:         }
  683:         #
  684:         my $sortby = $ENV{'form.sortby'};
  685:         $sortby = 'container' if (! defined($sortby) || $sortby =~ /^\s*$/);
  686:         my $plot = $ENV{'form.plot'};
  687:         if ($plot eq '' || $plot eq 'none') {
  688:             undef($plot);
  689:         }
  690:         if ($sortby eq 'container' && ! defined($plot)) {
  691:             &output_sequence_statistics($r);
  692:             &output_html_by_sequence($r);
  693:         } else {
  694:             if (defined($plot)) {
  695:                 &make_plot($r,$plot);
  696:             }
  697:             &output_html_stats($r);
  698:             &output_sequence_statistics($r);
  699:         }
  700:     }
  701:     return;
  702: }
  703: 
  704: sub output_sequence_statistics {
  705:     my ($r) = @_;
  706:     my $c=$r->connection();
  707:     $r->print('<h2>'.&mt('Sequence Statistics').
  708: 	      &Apache::loncommon::help_open_topic('Statistics_Sequence').
  709: 	      '</h2>');
  710:     $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
  711:               '<table border="0" cellpadding="3">'."\n".
  712:               '<tr bgcolor="#FFFFE6">');
  713:     $r->print(&sequence_html_header());
  714:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  715:         last if ($c->aborted);
  716:         next if ($seq->{'num_assess'} < 1);
  717:         &compute_sequence_statistics($seq);
  718:         $r->print(&sequence_html_output($seq));
  719:     }
  720:     $r->print('</table>');
  721:     $r->print('</table>');
  722:     $r->rflush();
  723:     return;
  724: }
  725: 
  726: 
  727: ##########################################################
  728: ##########################################################
  729: ##
  730: ## HTML output routines
  731: ##
  732: ##########################################################
  733: ##########################################################
  734: sub output_html_by_sequence {
  735:     my ($r) = @_;
  736:     my $c = $r->connection();
  737:     $r->print(&html_preamble());
  738:     #
  739:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  740:         last if ($c->aborted);
  741:         next if ($seq->{'num_assess'} < 1);
  742:         $r->print("<h3>".$seq->{'title'}."</h3>".
  743:                   '<table border="0"><tr><td bgcolor="#777777">'."\n".
  744:                   '<table border="0" cellpadding="3">'."\n".
  745:                   '<tr bgcolor="#FFFFE6">'.
  746:                   &statistics_table_header('no container')."</tr>\n");
  747:         my @Data = &compute_statistics_on_sequence($seq);
  748:         foreach my $data (@Data) {
  749:             $r->print('<tr>'.&statistics_html_table_data($data,
  750:                                                          'no container').
  751:                       "</tr>\n");
  752:         }
  753:         $r->print('</table>'."\n".'</table>'."\n");
  754:         $r->rflush();
  755:     }
  756:     return;
  757: }
  758: 
  759: sub output_html_stats {
  760:     my ($r)=@_;
  761:     &compute_all_statistics($r);
  762:     $r->print(&html_preamble());
  763:     &sort_data($ENV{'form.sortby'});
  764:     #
  765:     my $count=0;
  766:     foreach my $data (@StatsArray) {
  767:         if ($count++ % 50 == 0) {
  768:             $r->print("</table>\n</table>\n");
  769:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
  770:                       '<table border="0" cellpadding="3">'."\n".
  771:                       '<tr bgcolor="#FFFFE6">'.
  772:                       '<tr bgcolor="#FFFFE6">'.
  773:                       &statistics_table_header().
  774:                       "</tr>\n");
  775:         }
  776:         $r->print('<tr>'.&statistics_html_table_data($data)."</tr>\n");
  777:     }
  778:     $r->print("</table>\n</table>\n");
  779:     return;
  780: }
  781: 
  782: sub html_preamble {
  783:     my $Str='';
  784:     $Str .= "<h2>".
  785:         $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  786:         "</h2>\n";
  787:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
  788:     if (defined($starttime) || defined($endtime)) {
  789:         # Inform the user what the time limits on the data are.
  790:         $Str .= '<h3>'.&mt('Statistics on submissions from [_1] to [_2]',
  791:                            &Apache::lonlocal::locallocaltime($starttime),
  792:                            &Apache::lonlocal::locallocaltime($endtime)
  793:                            ).'</h3>';
  794:     }
  795:     $Str .= "<h3>".&mt('Compiled on [_1]',
  796:                        &Apache::lonlocal::locallocaltime(time))."</h3>";
  797:     return $Str;
  798: }
  799: 
  800: 
  801: ###############################################
  802: ###############################################
  803: ##
  804: ## Misc HTML output routines
  805: ##
  806: ###############################################
  807: ###############################################
  808: sub statistics_html_table_data {
  809:     my ($data,$options) = @_;
  810:     my $row = '';
  811:     foreach my $field (@Fields) {
  812:         next if ($options =~ /no $field->{'name'}/);
  813:         next if ($field->{'selected'} ne 'yes');
  814:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  815:         if (exists($field->{'align'})) {
  816:             $row .= ' align="'.$field->{'align'}.'"';
  817:             }
  818:         $row .= '>';
  819:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  820:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
  821:         }
  822:         if (exists($field->{'format'})) {
  823:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  824:         } else {
  825:             $row .= $data->{$field->{'name'}};
  826:         }
  827:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  828:             $row.= '</a>';
  829:         }
  830:         $row .= '</td>';
  831:     }
  832:     return $row;
  833: }
  834: 
  835: sub statistics_table_header {
  836:     my ($options) = @_;
  837:     my $header_row;
  838:     foreach my $field (@Fields) {
  839:         next if ($options =~ /no $field->{'name'}/);
  840:         next if ($field->{'selected'} ne 'yes');
  841:         $header_row .= '<th>';
  842:         if (exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
  843:             $header_row .= '<a href="javascript:'.
  844:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
  845:                     ';document.Statistics.submit();">';
  846:         }
  847:         $header_row .= &mt($field->{'title'});
  848:         if ($options =~ /sortable/) {
  849:             $header_row.= '</a>';
  850:         }
  851:         if ($options !~ /no plots/        && 
  852:             exists($field->{'graphable'}) && 
  853:             $field->{'graphable'} eq 'yes') {
  854:             $header_row.=' (';
  855:             $header_row .= '<a href="javascript:'.
  856:                 "document.Statistics.plot.value='$field->{'name'}'".
  857:                     ';document.Statistics.submit();">';
  858:             $header_row .= &mt('plot').'</a>)';
  859:         }
  860:         $header_row .= '</th>';
  861:     }
  862:     return $header_row;
  863: }
  864: 
  865: sub sequence_html_header {
  866:     my $Str .= '<tr>';
  867:     foreach my $field (@SeqFields) {
  868: #        next if ($field->{'selected'} ne 'yes');
  869:         $Str .= '<th bgcolor="'.$field->{'color'}.'"';
  870:         $Str .= '>'.$field->{'title'}.'</th>';
  871:     }
  872:     $Str .= '</tr>';
  873:     return $Str;
  874: }
  875: 
  876: 
  877: sub sequence_html_output {
  878:     my ($seq) = @_;
  879:     my $data = $SeqStat{$seq->{'symb'}};
  880:     my $row = '<tr>';
  881:     foreach my $field (@SeqFields) {
  882:         next if ($field->{'selected'} ne 'yes');
  883:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  884:         if (exists($field->{'align'})) {
  885:             $row .= ' align="'.$field->{'align'}.'"';
  886:         }
  887:         $row .= '>';
  888:         if (exists($field->{'format'})) {
  889:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  890:         } else {
  891:             $row .= $data->{$field->{'name'}};
  892:         }
  893:         $row .= '</td>';
  894:     }
  895:     $row .= '</tr>'."\n";
  896:     return $row;
  897: }
  898: 
  899: ####################################################
  900: ####################################################
  901: ##
  902: ##    Plotting Routines
  903: ##
  904: ####################################################
  905: ####################################################
  906: sub make_plot {
  907:     my ($r,$plot) = @_;
  908:     &compute_all_statistics($r);
  909:     &sort_data($ENV{'form.sortby'});
  910:     if ($plot eq 'degrees') {
  911:         &degrees_plot($r);
  912:     } elsif ($plot eq 'tries statistics') {
  913:         &tries_data_plot($r);
  914:     } else {
  915:         &make_single_stat_plot($r,$plot);
  916:     }
  917:     return;
  918: }
  919: 
  920: sub make_single_stat_plot {
  921:     my ($r,$datafield) = @_;
  922:     #
  923:     my $title; my $yaxis;
  924:     foreach my $field (@Fields) {
  925:         next if ($field->{'name'} ne $datafield);
  926:         $title = $field->{'long_title'};
  927:         $yaxis = $field->{'title'};
  928:         last;
  929:     }
  930:     if ($title eq '' || $yaxis eq '') {
  931:         # datafield is something we do not know enough about to plot
  932:         $r->print('<h3>'.
  933:                   &mt('Unable to plot the requested statistic.').
  934:                   '</h3>');
  935:         return;
  936:     }
  937:     #
  938:     # Build up the data sets to plot
  939:     my @Labels; 
  940:     my @Data;
  941:     my $max = 1;
  942:     foreach my $data (@StatsArray) {
  943:         push(@Labels,$data->{'problem_num'});
  944:         push(@Data,$data->{$datafield});
  945:         if ($data->{$datafield}>$max) {
  946:             $max = $data->{$datafield};
  947:         }
  948:     }
  949:     foreach (1,2,3,4,5,10,15,20,25,40,50,75,100,150,200,250,300,500,600,750,
  950:              1000,1500,2000,2500,3000,3500,4000,5000,7500,10000,15000,20000) {
  951:         if ($max <= $_) {
  952:             $max = $_;
  953:             last;
  954:         }
  955:     }
  956:     if ($max > 20000) {
  957:         $max = 10000*(int($max/10000)+1);
  958:     }
  959:     #
  960:     $r->print("<p>".&Apache::loncommon::DrawBarGraph($title,
  961:                                                      'Problem Number',
  962:                                                      $yaxis,
  963:                                                      $max,
  964:                                                      undef, # colors
  965:                                                      \@Labels,
  966:                                                      \@Data)."</p>\n");
  967:     return;
  968: }
  969: 
  970: sub degrees_plot {
  971:     my ($r)=@_;
  972:     my $count = scalar(@StatsArray);
  973:     my $width = 50 + 10*$count;
  974:     $width = 300 if ($width < 300);
  975:     my $height = 300;
  976:     my $plot = '';
  977:     my $ymax = 0;
  978:     my $ymin = 0;
  979:     my @Disc; my @Diff; my @Labels;    
  980:     foreach my $data (@StatsArray) {
  981:         push(@Labels,$data->{'problem_num'});
  982:         my $disc = $data->{'deg_of_disc'};
  983:         my $diff = $data->{'deg_of_diff'};
  984:         push(@Disc,$disc);
  985:         push(@Diff,$diff);
  986:         #
  987:         $ymin = $disc if ($ymin > $disc);
  988:         $ymin = $diff if ($ymin > $diff);
  989:         $ymax = $disc if ($ymax < $disc);
  990:         $ymax = $diff if ($ymax < $diff);
  991:     }
  992:     #
  993:     # Make sure we show relevant information.
  994:     if ($ymin < 0) {
  995:         if (abs($ymin) < 0.05) {
  996:             $ymin = 0;
  997:         } else {
  998:             $ymin = -1;
  999:         }
 1000:     }
 1001:     if ($ymax > 0) {
 1002:         if (abs($ymax) < 0.05) {
 1003:             $ymax = 0;
 1004:         } else {
 1005:             $ymax = 1;
 1006:         }
 1007:     }
 1008:     #
 1009:     my $xmax = $Labels[-1];
 1010:     if ($xmax > 50) {
 1011:         if ($xmax % 10 != 0) {
 1012:             $xmax = 10 * (int($xmax/10)+1);
 1013:         }
 1014:     } else {
 1015:         if ($xmax % 5 != 0) {
 1016:             $xmax = 5 * (int($xmax/5)+1);
 1017:         }
 1018:     }
 1019:     #
 1020:     my $discdata .= '<data>'.join(',',@Labels).'</data>'.$/.
 1021:                     '<data>'.join(',',@Disc).'</data>'.$/;
 1022:     #
 1023:     my $diffdata .= '<data>'.join(',',@Labels).'</data>'.$/.
 1024:                     '<data>'.join(',',@Diff).'</data>'.$/;
 1025:     #
 1026:     my $title = 'Degree of Discrimination\nand Degree of Difficulty';
 1027:     if ($xmax > 50) {
 1028:         $title = 'Degree of Discrimination and Degree of Difficulty';
 1029:     }
 1030:     #
 1031:     $plot=<<"END";
 1032: <gnuplot 
 1033:     texfont="10"
 1034:     fgcolor="x000000"
 1035:     plottype="Cartesian"
 1036:     font="large"
 1037:     grid="on"
 1038:     align="center"
 1039:     border="on"
 1040:     transparent="on"
 1041:     alttag="Degree of Discrimination and Degree of Difficulty Plot"
 1042:     samples="100"
 1043:     bgcolor="xffffff"
 1044:     height="$height"
 1045:     width="$width">
 1046:     <key 
 1047:         pos="top right"
 1048:         title=""
 1049:         box="off" />
 1050:     <title>$title</title>
 1051:     <axis xmin="0" ymin="$ymin" xmax="$xmax" ymax="$ymax" color="x000000" />
 1052:     <xlabel>Problem Number</xlabel>
 1053:     <curve 
 1054:         linestyle="linespoints" 
 1055:         name="DoDisc" 
 1056:         pointtype="0" 
 1057:         color="x000000">
 1058:         $discdata
 1059:     </curve>
 1060:     <curve 
 1061:         linestyle="linespoints" 
 1062:         name="DoDiff" 
 1063:         pointtype="0" 
 1064:         color="xFF0000">
 1065:         $diffdata
 1066:     </curve>
 1067: </gnuplot>
 1068: END
 1069:     my $plotresult = 
 1070:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
 1071:     $r->print($plotresult);
 1072:     return;
 1073: }
 1074: 
 1075: sub tries_data_plot {
 1076:     my ($r)=@_;
 1077:     my $count = scalar(@StatsArray);
 1078:     my $width = 50 + 10*$count;
 1079:     $width = 300 if ($width < 300);
 1080:     my $height = 300;
 1081:     my $plot = '';
 1082:     my @STD;  my @Mean; my @Max; my @Min;
 1083:     my @Labels;
 1084:     my $ymax = 5;
 1085:     foreach my $data (@StatsArray) {
 1086:         my $max = $data->{'mean_tries'} + $data->{'std_tries'};
 1087:         $ymax = $max if ($ymax < $max);
 1088:         $ymax = $max if ($ymax < $max);
 1089:         push(@Labels,$data->{'problem_num'});
 1090:         push(@STD,$data->{'std_tries'});
 1091:         push(@Mean,$data->{'mean_tries'});
 1092:     }
 1093:     #
 1094:     # Make sure we show relevant information.
 1095:     my $xmax = $Labels[-1];
 1096:     if ($xmax > 50) {
 1097:         if ($xmax % 10 != 0) {
 1098:             $xmax = 10 * (int($xmax/10)+1);
 1099:         }
 1100:     } else {
 1101:         if ($xmax % 5 != 0) {
 1102:             $xmax = 5 * (int($xmax/5)+1);
 1103:         }
 1104:     }
 1105:     $ymax = int($ymax)+1+2;
 1106:     #
 1107:     my $std_data .= '<data>'.join(',',@Labels).'</data>'.$/.
 1108:                     '<data>'.join(',',@Mean).'</data>'.$/;
 1109:     #
 1110:     my $std_error_data .= '<data>'.join(',',@Labels).'</data>'.$/.
 1111:                           '<data>'.join(',',@Mean).'</data>'.$/.
 1112:                           '<data>'.join(',',@STD).'</data>'.$/;
 1113:     #
 1114:     my $title = 'Mean and S.D. of Tries';
 1115:     if ($xmax > 25) {
 1116:         $title = 'Mean and Standard Deviation of Tries';
 1117:     }
 1118:     #
 1119:     $plot=<<"END";
 1120: <gnuplot 
 1121:     texfont="10"
 1122:     fgcolor="x000000"
 1123:     plottype="Cartesian"
 1124:     font="large"
 1125:     grid="on"
 1126:     align="center"
 1127:     border="on"
 1128:     transparent="on"
 1129:     alttag="Mean and S.D of Tries Plot"
 1130:     samples="100"
 1131:     bgcolor="xffffff"
 1132:     height="$height"
 1133:     width="$width">
 1134:     <title>$title</title>
 1135:     <axis xmin="0" ymin="0" xmax="$xmax" ymax="$ymax" color="x000000" />
 1136:     <xlabel>Problem Number</xlabel>
 1137:     <ylabel>Number of Tries</ylabel>
 1138:     <curve 
 1139:         linestyle="yerrorbars"
 1140:         name="S.D. Tries" 
 1141:         pointtype="1" 
 1142:         color="x666666">
 1143:         $std_error_data
 1144:     </curve>
 1145:     <curve 
 1146:         linestyle="points"
 1147:         name="Mean Tries" 
 1148:         pointtype="1" 
 1149:         color="xCC4444">
 1150:         $std_data
 1151:     </curve>
 1152: </gnuplot>
 1153: END
 1154:     my $plotresult = 
 1155:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
 1156:     $r->print($plotresult);
 1157:     return;
 1158: }
 1159: 
 1160: sub plot_dropdown {
 1161:     my $current = '';
 1162:     #
 1163:     if (defined($ENV{'form.plot'})) {
 1164:         $current = $ENV{'form.plot'};
 1165:     }
 1166:     #
 1167:     my @Additional_Plots = (
 1168:                             { graphable=>'yes',
 1169:                               name => 'degrees',
 1170:                               title => 'Difficulty Indexes' },
 1171:                             { graphable=>'yes',
 1172:                               name => 'tries statistics',
 1173:                               title => 'Tries Statistics' });
 1174:     #
 1175:     my $Str= "\n".'<select name="plot" size="1">';
 1176:     $Str .= '<option name="none"></option>'."\n";
 1177:     $Str .= '<option name="none2">none</option>'."\n";
 1178:     foreach my $field (@Additional_Plots,@Fields) {
 1179:         if (! exists($field->{'graphable'}) ||
 1180:             $field->{'graphable'} ne 'yes') {
 1181:             next;
 1182:         }
 1183:         $Str .= '<option value="'.$field->{'name'}.'"';
 1184:         if ($field->{'name'} eq $current) {
 1185:             $Str .= ' selected ';
 1186:         }
 1187:         $Str.= '>'.&mt($field->{'title'}).'</option>'."\n";
 1188:     }
 1189:     $Str .= '</select>'."\n";
 1190:     return $Str;
 1191: }
 1192: 
 1193: ###############################################
 1194: ###############################################
 1195: ##
 1196: ## Excel output routines
 1197: ##
 1198: ###############################################
 1199: ###############################################
 1200: sub Excel_output {
 1201:     my ($r) = @_;
 1202:     $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
 1203:     ##
 1204:     ## Compute the statistics
 1205:     &compute_all_statistics($r);
 1206:     my $c = $r->connection;
 1207:     return if ($c->aborted());
 1208:     ##
 1209:     ## Create the excel workbook
 1210:     my $filename = '/prtspool/'.
 1211:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1212:         time.'_'.rand(1000000000).'.xls';
 1213:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1214:     #
 1215:     # Create sheet
 1216:     my $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1217:     #
 1218:     # Check for errors
 1219:     if (! defined($excel_workbook)) {
 1220:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1221:         $r->print(&mt("Problems creating new Excel file.  ".
 1222:                   "This error has been logged.  ".
 1223:                   "Please alert your LON-CAPA administrator."));
 1224:         return 0;
 1225:     }
 1226:     #
 1227:     # The excel spreadsheet stores temporary data in files, then put them
 1228:     # together.  If needed we should be able to disable this (memory only).
 1229:     # The temporary directory must be specified before calling 'addworksheet'.
 1230:     # File::Temp is used to determine the temporary directory.
 1231:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
 1232:     #
 1233:     # Add a worksheet
 1234:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1235:     if (length($sheetname) > 31) {
 1236:         $sheetname = substr($sheetname,0,31);
 1237:     }
 1238:     my $excel_sheet = $excel_workbook->addworksheet(
 1239:         &Apache::loncommon::clean_excel_name($sheetname));
 1240:     #
 1241:     my $format = &Apache::loncommon::define_excel_formats($excel_workbook);
 1242:     ##
 1243:     ## Begin creating excel sheet
 1244:     ##
 1245:     my ($rows_output,$cols_output) = (0,0);
 1246:     #
 1247:     # Put the course description in the header
 1248:     $excel_sheet->write($rows_output,$cols_output++,
 1249:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
 1250:                         $format->{'h1'});
 1251:     $cols_output += 3;
 1252:     #
 1253:     # Put a description of the sections listed
 1254:     my $sectionstring = '';
 1255:     $excel_sheet->write($rows_output,$cols_output++,
 1256:                         &Apache::lonstathelpers::sections_description
 1257:                             (@Apache::lonstatistics::SelectedSections),
 1258:                         $format->{'h3'});
 1259:     $cols_output += scalar(@Apache::lonstatistics::SelectedSections);
 1260:     #
 1261:     # Time restrictions
 1262:     my $time_string;
 1263:     if (defined($starttime)) {
 1264:         # call localtime but not lonlocal:locallocaltime because excel probably
 1265:         # cannot handle localized text.  Probably.
 1266:         $time_string .= 'Data collected from '.localtime($time_string);
 1267:         if (defined($endtime)) {
 1268:             $time_string .= ' to '.localtime($endtime);
 1269:         }
 1270:         $time_string .= '.';
 1271:     } elsif (defined($endtime)) {
 1272:         # See note above about lonlocal:locallocaltime
 1273:         $time_string .= 'Data collected before '.localtime($endtime).'.';
 1274:     }
 1275:     if (defined($time_string)) {
 1276:         $excel_sheet->write($rows_output,$cols_output++,$time_string);
 1277:         $cols_output+= 5;
 1278:     }
 1279:     #
 1280:     # Put the date in there too
 1281:     $excel_sheet->write($rows_output,$cols_output++,
 1282:                         'Compiled on '.localtime(time));
 1283:     #
 1284:     $rows_output++; 
 1285:     $cols_output=0;
 1286:     ##
 1287:     ## Sequence Statistics
 1288:     ## 
 1289:     &write_headers($excel_sheet,$format,\$rows_output,\$cols_output,
 1290:                    \@SeqFields);
 1291:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1292:         next if ($seq->{'num_assess'} < 1);
 1293:         my $data = $SeqStat{$seq->{'symb'}};
 1294:         $cols_output=0;
 1295:         foreach my $field (@SeqFields) {
 1296:             next if ($field->{'selected'} ne 'yes');
 1297:             my $fieldformat = undef;
 1298:             if (exists($field->{'excel_format'})) {
 1299:                 $fieldformat = $format->{$field->{'excel_format'}};
 1300:             }
 1301:             $excel_sheet->write($rows_output,$cols_output++,
 1302:                                 $data->{$field->{'name'}},$fieldformat);
 1303:         }
 1304:         $rows_output++;
 1305:         $cols_output=0;
 1306:     }
 1307:     ##
 1308:     ## Resource Statistics
 1309:     ##
 1310:     $rows_output++;
 1311:     $cols_output=0;
 1312:     &write_headers($excel_sheet,$format,\$rows_output,\$cols_output,
 1313:                    \@Fields);
 1314:     #
 1315:     foreach my $data (@StatsArray) {
 1316:         $cols_output=0;
 1317:         foreach my $field (@Fields) {
 1318:             next if ($field->{'selected'} ne 'yes');
 1319:             next if ($field->{'name'} eq 'problem_num');
 1320:             my $fieldformat = undef;
 1321:             if (exists($field->{'excel_format'})) {
 1322:                 $fieldformat = $format->{$field->{'excel_format'}};
 1323:             }
 1324:             $excel_sheet->write($rows_output,$cols_output++,
 1325:                                 $data->{$field->{'name'}},$fieldformat);
 1326:         }
 1327:         $rows_output++;
 1328:         $cols_output=0;
 1329:     }
 1330:     #
 1331:     $excel_workbook->close();
 1332:     #
 1333:     # Tell the user where to get their excel file
 1334:     $r->print('<br />'.
 1335:               '<a href="'.$filename.'">'.
 1336:               &mt('Your Excel Spreadsheet').'</a>'."\n");
 1337:     $r->rflush();
 1338:     return;
 1339: }
 1340: 
 1341: ##
 1342: ## &write_headers
 1343: ##
 1344: sub write_headers {
 1345:     my ($excel_sheet,$format,$rows_output,$cols_output,$Fields) = @_;
 1346:     ##
 1347:     ## First the long titles
 1348:     foreach my $field (@{$Fields}) {
 1349:         next if ($field->{'name'} eq 'problem_num');
 1350:         next if ($field->{'selected'} ne 'yes');
 1351:         if (exists($field->{'long_title'})) {
 1352:             $excel_sheet->write($$rows_output,${$cols_output},
 1353:                                 $field->{'long_title'},
 1354:                                 $format->{'bold'});
 1355:         } else {
 1356:             $excel_sheet->write($$rows_output,${$cols_output},'');
 1357:         }
 1358:         ${$cols_output}+= 1;
 1359:     }
 1360:     ${$cols_output} =0;
 1361:     ${$rows_output}+=1;
 1362:     ##
 1363:     ## Then the short titles
 1364:     foreach my $field (@{$Fields}) {
 1365:         next if ($field->{'selected'} ne 'yes');
 1366:         next if ($field->{'name'} eq 'problem_num');
 1367:         # Use english for excel as I am not sure how well excel handles 
 1368:         # other character sets....
 1369:         $excel_sheet->write($$rows_output,$$cols_output,
 1370:                             $field->{'title'},
 1371:                             $format->{'bold'});
 1372:         $$cols_output+=1;
 1373:     }
 1374:     ${$cols_output} =0;
 1375:     ${$rows_output}+=1;
 1376:     return;
 1377: }
 1378: 
 1379: ##################################################
 1380: ##################################################
 1381: ##
 1382: ## Statistics Gathering and Manipulation Routines
 1383: ##
 1384: ##################################################
 1385: ##################################################
 1386: sub compute_statistics_on_sequence {
 1387:     my ($seq) = @_;
 1388:     my @Data;
 1389:     foreach my $res (@{$seq->{'contents'}}) {
 1390:         next if ($res->{'type'} ne 'assessment');
 1391:         foreach my $part (@{$res->{'parts'}}) {
 1392:             next if ($res->{'partdata'}->{$part}->{'Survey'});
 1393:             #
 1394:             # This is where all the work happens
 1395:             my $data = &get_statistics($seq,$res,$part,scalar(@StatsArray)+1);
 1396:             push (@Data,$data);
 1397:             push (@StatsArray,$data);
 1398:         }
 1399:     }
 1400:     return @Data;
 1401: }
 1402: 
 1403: sub compute_all_statistics {
 1404:     my ($r) = @_;
 1405:     if (@StatsArray > 0) {
 1406:         # Assume we have already computed the statistics
 1407:         return;
 1408:     }
 1409:     my $c = $r->connection;
 1410:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1411:         last if ($c->aborted);
 1412:         next if ($seq->{'num_assess'} < 1);
 1413:         &compute_sequence_statistics($seq);
 1414:         &compute_statistics_on_sequence($seq);
 1415:     }
 1416: }
 1417: 
 1418: sub sort_data {
 1419:     my ($sortkey) = @_;
 1420:     return if (! @StatsArray);
 1421:     #
 1422:     # Sort the data
 1423:     my $sortby = undef;
 1424:     foreach my $field (@Fields) {
 1425:         if ($sortkey eq $field->{'name'}) {
 1426:             $sortby = $field->{'name'};
 1427:         }
 1428:     }
 1429:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
 1430:         $sortby = 'container';
 1431:     }
 1432:     if ($sortby ne 'container') {
 1433:         # $sortby is already defined, so we can charge ahead
 1434:         if ($sortby =~ /^(title|part)$/i) {
 1435:             # Alpha comparison
 1436:             @StatsArray = sort {
 1437:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
 1438:                 lc($a->{'title'}) cmp lc($b->{'title'}) ||
 1439:                 lc($a->{'part'}) cmp lc($b->{'part'});
 1440:             } @StatsArray;
 1441:         } else {
 1442:             # Numerical comparison
 1443:             @StatsArray = sort {
 1444:                 my $retvalue = 0;
 1445:                 if ($b->{$sortby} eq 'nan') {
 1446:                     if ($a->{$sortby} ne 'nan') {
 1447:                         $retvalue = -1;
 1448:                     } else {
 1449:                         $retvalue = 0;
 1450:                     }
 1451:                 }
 1452:                 if ($a->{$sortby} eq 'nan') {
 1453:                     if ($b->{$sortby} ne 'nan') {
 1454:                         $retvalue = 1;
 1455:                     }
 1456:                 }
 1457:                 if ($retvalue eq '0') {
 1458:                     $retvalue = $b->{$sortby} <=> $a->{$sortby}     ||
 1459:                             lc($a->{'title'}) <=> lc($b->{'title'}) ||
 1460:                             lc($a->{'part'})  <=> lc($b->{'part'});
 1461:                 }
 1462:                 $retvalue;
 1463:             } @StatsArray;
 1464:         }
 1465:     }
 1466:     #
 1467:     # Renumber the data set
 1468:     my $count;
 1469:     foreach my $data (@StatsArray) {
 1470:         $data->{'problem_num'} = ++$count;
 1471:     }
 1472:     return;
 1473: }
 1474: 
 1475: ########################################################
 1476: ########################################################
 1477: 
 1478: =pod
 1479: 
 1480: =item &get_statistics()
 1481: 
 1482: Wrapper routine from the call to loncoursedata::get_problem_statistics.  
 1483: Calls lonstathelpers::get_time_limits() to limit the data set by time
 1484: and &compute_discrimination_factor
 1485: 
 1486: Inputs: $sequence, $resource, $part, $problem_num
 1487: 
 1488: Returns: Hash reference with statistics data from 
 1489: loncoursedata::get_problem_statistics.
 1490: 
 1491: =cut
 1492: 
 1493: ########################################################
 1494: ########################################################
 1495: sub get_statistics {
 1496:     my ($sequence,$resource,$part,$problem_num) = @_;
 1497:     #
 1498:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1499:     my $symb = $resource->{'symb'};
 1500:     my $courseid = $ENV{'request.course.id'};
 1501:     #
 1502:     my $data = &Apache::loncoursedata::get_problem_statistics
 1503:                         (\@Apache::lonstatistics::SelectedSections,
 1504:                          $Apache::lonstatistics::enrollment_status,
 1505:                          $symb,$part,$courseid,$starttime,$endtime);
 1506:     $data->{'symb'}        = $symb;
 1507:     $data->{'part'}        = $part;
 1508:     $data->{'problem_num'} = $problem_num;
 1509:     $data->{'container'}   = $sequence->{'title'};
 1510:     $data->{'title'}       = $resource->{'title'};
 1511:     $data->{'title.link'}  = $resource->{'src'}.'?symb='.
 1512:         &Apache::lonnet::escape($resource->{'symb'});
 1513:     #
 1514:     if ($SelectedFields{'deg_of_disc'}) {
 1515:         $data->{'deg_of_disc'} = 
 1516:             &compute_discrimination_factor($resource,$part,$sequence);
 1517:     }
 1518:     #
 1519:     # Store in metadata if computations were done for all students
 1520:     if ($data->{'num_students'} > 1) {
 1521:         my @Sections = @Apache::lonstatistics::SelectedSections;
 1522:         my $sections = '"'.join(' ',@Sections).'"';
 1523:         $sections =~ s/&+/_/g;  # Ensure no special characters
 1524:         $data->{'sections'}=$sections;
 1525:         $data->{'course'} = $ENV{'request.course.id'};
 1526:         my $urlres=(&Apache::lonnet::decode_symb($resource->{'symb'}))[2];
 1527:         $data->{'urlres'}=$urlres;
 1528:         my %storestats = 
 1529:             &LONCAPA::lonmetadata::dynamic_metadata_storage($data);
 1530:         my ($dom,$user) = $urlres=~/^(\w+)\/(\w+)/; 
 1531:         &Apache::lonnet::put('nohist_resevaldata',\%storestats,$dom,$user);
 1532:     }
 1533:     #
 1534:     # Get the due date for research purposes (commented out most of the time)
 1535: #    $data->{'duedate'} = 
 1536: #        &Apache::lonnet::EXT('resource.'.$part.'.duedate',$symb);
 1537: #    $data->{'opendate'} = 
 1538: #        &Apache::lonnet::EXT('resource.'.$part.'.opendate',$symb);
 1539: #    $data->{'resptypes'} = join(',',@{$resource->{'partdata'}->{$part}->{'ResponseTypes'}});
 1540:     return $data;
 1541: }
 1542: 
 1543: ###############################################
 1544: ###############################################
 1545: 
 1546: =pod
 1547: 
 1548: =item &compute_discrimination_factor()
 1549: 
 1550: Inputs: $Resource, $Sequence
 1551: 
 1552: Returns: integer between -1 and 1
 1553: 
 1554: =cut
 1555: 
 1556: ###############################################
 1557: ###############################################
 1558: sub compute_discrimination_factor {
 1559:     my ($resource,$part,$sequence) = @_;
 1560:     my @Resources;
 1561:     foreach my $res (@{$sequence->{'contents'}}) {
 1562:         next if ($res->{'symb'} eq $resource->{'symb'});
 1563:         push (@Resources,$res->{'symb'});
 1564:     }
 1565:     #
 1566:     # rank
 1567:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1568:     my $ranking = 
 1569:         &Apache::loncoursedata::rank_students_by_scores_on_resources
 1570:         (\@Resources,
 1571:          \@Apache::lonstatistics::SelectedSections,
 1572:          $Apache::lonstatistics::enrollment_status,undef,
 1573:          $starttime,$endtime);
 1574:     #
 1575:     # compute their percent scores on the problems in the sequence,
 1576:     my $number_to_grab = int(scalar(@{$ranking})/4);
 1577:     my $num_students = scalar(@{$ranking});
 1578:     my @BottomSet = map { $_->[&Apache::loncoursedata::RNK_student()]; 
 1579:                       } @{$ranking}[0..$number_to_grab];
 1580:     my @TopSet    = 
 1581:         map { 
 1582:             $_->[&Apache::loncoursedata::RNK_student()]; 
 1583:           } @{$ranking}[($num_students-$number_to_grab)..($num_students-1)];
 1584:     my ($bottom_sum,$bottom_max) = 
 1585:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@BottomSet,
 1586:                                                   undef,$starttime,$endtime);
 1587:     my ($top_sum,$top_max) = 
 1588:         &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@TopSet,
 1589:                                                   undef,$starttime,$endtime);
 1590:     my $deg_of_disc;
 1591:     if ($top_max == 0 || $bottom_max==0) {
 1592:         $deg_of_disc = 'nan';
 1593:     } else {
 1594:         $deg_of_disc = ($top_sum/$top_max) - ($bottom_sum/$bottom_max);
 1595:     }
 1596:     #&Apache::lonnet::logthis('    '.$top_sum.'/'.$top_max.
 1597:     #                         ' - '.$bottom_sum.'/'.$bottom_max);
 1598:     return $deg_of_disc;
 1599: }
 1600: 
 1601: ###############################################
 1602: ###############################################
 1603: ##
 1604: ## Compute KR-21
 1605: ##
 1606: ## To compute KR-21, you need the following information:
 1607: ##
 1608: ## K=the number of items in your test
 1609: ## M=the mean score on the test
 1610: ## s=the standard deviation of the scores on your test 
 1611: ##
 1612: ## then:
 1613: ## 
 1614: ## KR-21 rk= [K/(K-1)] * [1- (M*(K-M))/(K*s^2))]
 1615: ##
 1616: ###############################################
 1617: ###############################################
 1618: sub compute_sequence_statistics {
 1619:     my ($seq) = @_;
 1620:     my $symb = $seq->{'symb'};
 1621:     my @Resources;
 1622:     foreach my $res (@{$seq->{'contents'}}) {
 1623:         next if ($res->{'type'} ne 'assessment');
 1624:         push (@Resources,$res->{'symb'});
 1625:     }
 1626:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
 1627:     #
 1628:     # First compute statistics based on student scores
 1629:     my ($smin,$smax,$sMean,$sSTD,$scount,$sMAX) = 
 1630:         &Apache::loncoursedata::score_stats
 1631:                     (\@Apache::lonstatistics::SelectedSections,
 1632:                      $Apache::lonstatistics::enrollment_status,
 1633:                      \@Resources,$starttime,$endtime,undef);
 1634:     $SeqStat{$symb}->{'title'}  = $seq->{'title'};
 1635:     $SeqStat{$symb}->{'scoremax'}  = $smax;
 1636:     $SeqStat{$symb}->{'scoremin'}  = $smin;
 1637:     $SeqStat{$symb}->{'scoremean'} = $sMean;
 1638:     $SeqStat{$symb}->{'scorestd'}  = $sSTD;
 1639:     $SeqStat{$symb}->{'scorecount'} = $scount;
 1640:     $SeqStat{$symb}->{'max_possible'} = $sMAX;
 1641:     #
 1642:     # Compute statistics based on the number of correct problems
 1643:     # 'correct' is taken to mean 
 1644:     my ($cmin,$cmax,$cMean,$cSTD,$ccount)=
 1645:         &Apache::loncoursedata::count_stats
 1646:         (\@Apache::lonstatistics::SelectedSections,
 1647:          $Apache::lonstatistics::enrollment_status,
 1648:          \@Resources,$starttime,$endtime,undef);
 1649:     my $K = $seq->{'num_assess_parts'};
 1650:     my $kr_21;
 1651:     if ($K > 1 && $cSTD > 0) {
 1652:         $kr_21 =  ($K/($K-1)) * (1 - $cMean*($K-$cMean)/($K*$cSTD**2));
 1653:     } else {
 1654:         $kr_21 = 'nan';
 1655:     }
 1656:     $SeqStat{$symb}->{'countmax'} = $cmax;
 1657:     $SeqStat{$symb}->{'countmin'} = $cmin;
 1658:     $SeqStat{$symb}->{'countstd'} = $cSTD;
 1659:     $SeqStat{$symb}->{'countmean'} = $cMean;
 1660:     $SeqStat{$symb}->{'count'} = $ccount;
 1661:     $SeqStat{$symb}->{'items'} = $K;
 1662:     $SeqStat{$symb}->{'KR-21'}=$kr_21;
 1663:     return;
 1664: }
 1665: 
 1666: 
 1667: 
 1668: =pod 
 1669: 
 1670: =item ProblemStatisticsLegend
 1671: 
 1672: =over 4
 1673: 
 1674: =item #Stdnts
 1675: Total number of students attempted the problem.
 1676: 
 1677: =item Tries
 1678: Total number of tries for solving the problem.
 1679: 
 1680: =item Max Tries
 1681: Largest number of tries for solving the problem by a student.
 1682: 
 1683: =item Mean
 1684: Average number of tries. [ Tries / #Stdnts ]
 1685: 
 1686: =item #YES
 1687: Number of students solved the problem correctly.
 1688: 
 1689: =item #yes
 1690: Number of students solved the problem by override.
 1691: 
 1692: =item %Wrong
 1693: Percentage of students who tried to solve the problem 
 1694: but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]
 1695: 
 1696: =item DoDiff
 1697: Degree of Difficulty of the problem.  
 1698: [ 1 - ((#YES+#yes) / Tries) ]
 1699: 
 1700: =item S.D.
 1701: Standard Deviation of the tries.  
 1702: [ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) 
 1703: where Xi denotes every student\'s tries ]
 1704: 
 1705: =item Skew.
 1706: Skewness of the students tries.
 1707: [(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]
 1708: 
 1709: =item Dis.F.
 1710: Discrimination Factor: A Standard for evaluating the 
 1711: problem according to a Criterion<br>
 1712: 
 1713: =item [Criterion to group students into %27 Upper Students - 
 1714: and %27 Lower Students]
 1715: 1st Criterion for Sorting the Students: 
 1716: Sum of Partial Credit Awarded / Total Number of Tries
 1717: 2nd Criterion for Sorting the Students: 
 1718: Total number of Correct Answers / Total Number of Tries
 1719: 
 1720: =item Disc.
 1721: Number of Students had at least one discussion.
 1722: 
 1723: =back
 1724: 
 1725: =cut
 1726: 
 1727: ############################################################
 1728: ############################################################
 1729: 
 1730: 1;
 1731: __END__

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