File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.122: download - view: text, annotated - select for diffs
Thu May 3 11:21:33 2012 UTC (12 years, 2 months ago) by goltermann
Branches: MAIN
CVS tags: version_2_11_X, HEAD
- changed headlines to normal text with info/error/warning style where necessary
- changed tables to data tables to unify UI

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

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