File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.96: download - view: text, annotated - select for diffs
Tue Jan 11 19:45:18 2005 UTC (19 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added problem weight output for research.

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

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