File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.122.2.1: download - view: text, annotated - select for diffs
Sat May 12 03:17:43 2012 UTC (12 years, 2 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_0_RC1
- For 2.11
  - Reverse changes for 1.119 - they require "3.0" grading interface.

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

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