File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.93: download - view: text, annotated - select for diffs
Wed Aug 4 15:07:42 2004 UTC (19 years, 11 months ago) by matthew
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_1, version_1_2_0, version_1_1_99_5, HEAD
Bug 3262: %wrong plot is now re-implemented.

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

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