File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.125: download - view: text, annotated - select for diffs
Mon Feb 3 18:52:30 2014 UTC (10 years, 4 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
- Corrected unbalanced tags
- XHTML
- Internationalization:
    - Added missing &mt() calls
    - Time window info for Excel:
        - Improved output
        - Corrected start date (use correct variable)

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

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