File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.97: download - view: text, annotated - select for diffs
Tue Mar 1 00:07:21 2005 UTC (19 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
loncoursedata.pm: changed &get_sum_of_scores to take symb instead of a
'resource' hash.
lonproblemstatistics: navmapification is complete.  Required addition of
package variables $navmap and @sequences.

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

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