File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.109: download - view: text, annotated - select for diffs
Tue May 30 12:46:50 2006 UTC (18 years, 1 month ago) by www
Branches: MAIN
CVS tags: version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

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

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