File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.105: download - view: text, annotated - select for diffs
Fri Aug 26 21:53:23 2005 UTC (18 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, HEAD
- some stat reports were not cleaningup the navmap cglobal which menat the course hashes weren't untied

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

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