File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.79: download - view: text, annotated - select for diffs
Thu Apr 1 20:02:56 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Bug 539: Report KR-21 reliability statistic

loncoursedata.pm:
  Added &limit_by_start_end_time to consolidate $starttime and $endtime code.
  Added &score_stats and &count_stats to return summary data on the scores
    and problem correct counts.

lonproblemstatistics:
  Added description of @Fields data structures
  Added @SeqFields, &output_sequence_statistics, &sequence_html_header,
    &sequence_html_output, and &compute_sequence_statistics.

Reporting statistics on sequences (instead of problems) is still being
fleshed out, interface wise.  No Excel output is available for sequence
statistics at this time.

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

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