File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.94: download - view: text, annotated - select for diffs
Wed Oct 6 15:37:59 2004 UTC (19 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
More information for research only.

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

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