File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.119: download - view: text, annotated - select for diffs
Mon Jan 17 00:19:41 2011 UTC (13 years, 4 months ago) by www
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
Add statistics and reports under "grading"

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

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