File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.122.2.4.2.1: download - view: text, annotated - select for diffs
Fri Sep 8 00:07:30 2023 UTC (9 months, 4 weeks ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.122.2.4: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.127

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

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