File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.122.2.3: download - view: text, annotated - select for diffs
Mon Mar 3 20:45:05 2014 UTC (10 years, 4 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_2_uiuc, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0
- For 2.11
  - Backport 1.125, 1.126.

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

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