File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.100: download - view: text, annotated - select for diffs
Tue Mar 8 17:40:39 2005 UTC (19 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Make the problem weight a selectable output instead of outputting it every
    time statistics are computed.

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

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