File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.116: download - view: text, annotated - select for diffs
Tue Jan 20 16:38:44 2009 UTC (15 years, 5 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
XHTML conform multiple selection

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

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