File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.56: download - view: text, annotated - select for diffs
Wed Aug 27 21:33:33 2003 UTC (20 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Moving &Gather_Student_data to lonstatistics as it will be needed by
lonclassifystudent.pm.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemstatistics.pm,v 1.56 2003/08/27 21:33:33 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::lonhtmlcommon;
   55: use Apache::loncoursedata;
   56: use Apache::lonstatistics;
   57: use Spreadsheet::WriteExcel;
   58: 
   59: my @Fields = (
   60:            { name => 'problem_num',
   61:              title => 'P#',
   62:              align => 'right',
   63:              color => '#FFFFE6' },
   64:            { name   => 'container',
   65:              title  => 'Sequence or Folder',
   66:              align  => 'left',
   67:              color  => '#FFFFE6',
   68:              sortable => 'yes' },
   69:            { name   => 'title',
   70:              title  => 'Title',
   71:              align  => 'left',
   72:              color  => '#FFFFE6',
   73:              special  => 'link',
   74:              sortable => 'yes', },
   75:            { name   => 'part', 
   76:              title  => 'Part',
   77:              align  => 'left',
   78:              color  => '#FFFFE6',
   79:              },
   80:            { name   => 'num_students',
   81:              title  => '#Stdnts',
   82:              align  => 'right',
   83:              color  => '#EEFFCC',
   84:              format => '%d',
   85:              sortable  => 'yes',
   86:              graphable => 'yes',
   87:              long_title => 'Number of Students Attempting Problem' },
   88:            { name   => 'tries',
   89:              title  => 'Tries',
   90:              align  => 'right',
   91:              color  => '#EEFFCC',
   92:              format => '%d',
   93:              sortable  => 'yes',
   94:              graphable => 'yes',
   95:              long_title => 'Total Number of Tries' },
   96:            { name   => 'max_tries',
   97:              title  => 'Max Tries',
   98:              align  => 'right',
   99:              color  => '#DDFFFF',
  100:              format => '%d',
  101:              sortable  => 'yes',
  102:              graphable => 'yes',
  103:              long_title => 'Maximum Number of Tries' },
  104:            { name   => 'mean_tries',
  105:              title  => 'Mean Tries',
  106:              align  => 'right',
  107:              color  => '#DDFFFF',
  108:              format => '%5.2f',
  109:              sortable  => 'yes',
  110:              graphable => 'yes',
  111:              long_title => 'Average Number of Tries' },
  112:            { name   => 'std_tries',
  113:              title  => 'S.D. tries',
  114:              align  => 'right',
  115:              color  => '#DDFFFF',
  116:              format => '%5.2f',
  117:              sortable  => 'yes',
  118:              graphable => 'yes',
  119:              long_title => 'Standard Deviation of Number of Tries' },
  120:            { name   => 'skew_tries',
  121:              title  => 'Skew Tries',
  122:              align  => 'right',
  123:              color  => '#DDFFFF',
  124:              format => '%5.2f',
  125:              sortable  => 'yes',
  126:              graphable => 'yes',
  127:              long_title => 'Skew of Number of Tries' },
  128:            { name   => 'deg_of_diff',
  129:              title  => 'DoDiff',
  130:              align  => 'right',
  131:              color  => '#DDFFFF',
  132:              format => '%5.2f',
  133:              sortable  => 'yes',
  134:              graphable => 'yes',
  135:              long_title => 'Degree of Difficulty'.
  136:                            '[ 1 - ((#YES+#yes) / Tries) ]'},
  137:            { name   => 'num_solved',
  138:              title  => '#YES',
  139:              align  => 'right',
  140:              color  => '#FFDDDD',
  141:              format => '%d',
  142:              sortable  => 'yes',
  143:              graphable => 'yes',
  144:              long_title => 'Number of Students able to Solve' },
  145:            { name   => 'num_override',
  146:              title  => '#yes',
  147:              align  => 'right',
  148:              color  => '#FFDDDD',
  149:              format => '%d',
  150:              sortable  => 'yes',
  151:              graphable => 'yes',
  152:              long_title => 'Number of Students given Override' },
  153:            { name   => 'per_wrong',
  154:              title  => '%Wrng',
  155:              align  => 'right',
  156:              color  => '#FFFFE6',
  157:              format => '%4.1f',
  158:              sortable  => 'yes',
  159:              graphable => 'yes',
  160:              long_title => 'Percent of students whose final answer is wrong' },
  161: );
  162: 
  163: ###############################################
  164: ###############################################
  165: 
  166: =pod 
  167: 
  168: =item &CreateInterface()
  169: 
  170: Create the main intereface for the statistics page.  Allows the user to
  171: select sections, maps, and output.
  172: 
  173: =cut
  174: 
  175: ###############################################
  176: ###############################################
  177: sub CreateInterface {
  178:     my $Str = '';
  179:     $Str .= '<table cellspacing="5">'."\n";
  180:     $Str .= '<tr>';
  181:     $Str .= '<td align="center"><b>Sections</b></td>';
  182:     $Str .= '<td align="center"><b>Enrollment Status</b></td>';
  183:     $Str .= '<td align="center"><b>Sequences and Folders</b></td>';
  184:     $Str .= '<td align="center"><b>Output</b></td>';
  185:     $Str .= '</tr>'."\n";
  186:     #
  187:     $Str .= '<tr><td align="center">'."\n";
  188:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  189:     $Str .= '</td><td align="center">';
  190:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  191:     $Str .= '</td><td align="center">';
  192:     #
  193:     my $only_seq_with_assessments = sub { 
  194:         my $s=shift;
  195:         if ($s->{'num_assess'} < 1) { 
  196:             return 0;
  197:         } else { 
  198:             return 1;
  199:         }
  200:     };
  201:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  202:                                               $only_seq_with_assessments);
  203:     $Str .= '</td><td>'."\n";
  204:     $Str .= &CreateAndParseOutputSelector();
  205:     $Str .= '</td></tr>'."\n";
  206:     $Str .= '</table>'."\n";
  207:     $Str .= '<input type="submit" value="Generate Statistics" />';
  208:     $Str .= '&nbsp;'x5;
  209:     $Str .= '<input type="submit" name="ClearCache" value="Clear Caches" />';
  210:     $Str .= '&nbsp;'x5;
  211:     return $Str;
  212: }
  213: 
  214: #######################################################
  215: #######################################################
  216: 
  217: =pod
  218: 
  219: =item &CreateAndParseOutputSelector()
  220: 
  221: Construct a selection list of options for output and parse output selections.
  222: The current output selected is indicated by the values of the two package
  223: variables $output_mode and $show.  @OutputOptions holds the descriptions of
  224: the output options and the values for $output_mode and $show.
  225: 
  226: Based on code from lonstudentassessment.pm.
  227: 
  228: =cut
  229: 
  230: #######################################################
  231: #######################################################
  232: my $output_mode;
  233: my $show;
  234: 
  235: my @OutputOptions = 
  236:     (
  237:      { name  => 'problem statistics grouped by sequence',
  238:        value => 'HTML problem statistics grouped',
  239:        description => 'Output statistics for the problem parts.',
  240:        mode => 'html',
  241:        show => 'grouped',
  242:      },
  243:      { name  => 'problem statistics ungrouped',
  244:        value => 'HTML problem statistics ungrouped',
  245:        description => 'Output statistics for the problem parts.',
  246:        mode => 'html',
  247:        show => 'ungrouped',
  248:      },
  249:      { name  => 'problem statistics, Excel',
  250:        value => 'Excel problem statistics',
  251:        description => 'Output statistics for the problem parts '.
  252:            'in an Excel workbook',
  253:        mode => 'excel',
  254:        show => 'all',
  255:      },
  256:      );
  257: 
  258: sub OutputDescriptions {
  259:     my $Str = '';
  260:     $Str .= "<h2>Output Modes</h2>\n";
  261:     $Str .= "<dl>\n";
  262:     foreach my $outputmode (@OutputOptions) {
  263: 	$Str .="    <dt>".$outputmode->{'name'}."</dt>\n";
  264: 	$Str .="        <dd>".$outputmode->{'description'}."</dd>\n";
  265:     }
  266:     $Str .= "</dl>\n";
  267:     return $Str;
  268: }
  269: 
  270: sub CreateAndParseOutputSelector {
  271:     my $Str = '';
  272:     my $elementname = 'statsoutputmode';
  273:     #
  274:     # Format for output options is 'mode, restrictions';
  275:     my $selected = 'HTML problem statistics grouped';
  276:     if (exists($ENV{'form.'.$elementname})) {
  277:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
  278:             $selected = $ENV{'form.'.$elementname}->[0];
  279:         } else {
  280:             $selected = $ENV{'form.'.$elementname};
  281:         }
  282:     }
  283:     #
  284:     # Set package variables describing output mode
  285:     $output_mode = 'html';
  286:     $show        = 'all';
  287:     foreach my $option (@OutputOptions) {
  288:         next if ($option->{'value'} ne $selected);
  289:         $output_mode = $option->{'mode'};
  290:         $show        = $option->{'show'};
  291:     }
  292:     #
  293:     # Build the form element
  294:     $Str = qq/<select size="5" name="$elementname">/;
  295:     foreach my $option (@OutputOptions) {
  296:         if (exists($option->{'special'}) && 
  297:             $option->{'special'} =~ /do not show/) {
  298:             next;
  299:         }
  300:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
  301:         $Str .= " selected " if ($option->{'value'} eq $selected);
  302:         $Str .= ">".$option->{'name'}."<\/option>";
  303:     }
  304:     $Str .= "\n</select>";
  305:     return $Str;
  306: }
  307: 
  308: ###############################################
  309: ###############################################
  310: 
  311: =pod 
  312: 
  313: =item &BuildProblemStatisticsPage()
  314: 
  315: Main interface to problem statistics.
  316: 
  317: =cut
  318: 
  319: ###############################################
  320: ###############################################
  321: sub BuildProblemStatisticsPage {
  322:     my ($r,$c)=@_;
  323:     #
  324:     $output_mode = 'html';
  325:     $show = 'grouped';
  326:     #
  327:     $r->print(&CreateInterface());
  328:     $r->print('<input type="hidden" name="statsfirstcall" value="no" />');
  329:     $r->print('<input type="hidden" name="sortby" value="'.$ENV{'form.sortby'}.
  330:               '" />');
  331:     $r->print('<input type="hidden" name="plot" value="" />');
  332:     if (! exists($ENV{'form.statsfirstcall'})) {
  333:         return;
  334:     }
  335:     #
  336:     &Apache::lonstatistics::Gather_Student_Data($r);
  337:     #
  338:     #
  339:     if ($output_mode eq 'html') {
  340:         $r->print("<h2>".
  341:                   $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  342:                   "</h2>\n");
  343:         $r->print("<h3>".localtime(time)."</h3>");
  344:         $r->rflush();
  345:         if ($show eq 'grouped') {
  346:             &output_html_grouped_by_sequence($r);
  347:         } elsif ($show eq 'ungrouped') {
  348:             &output_html_ungrouped($r);
  349:         }
  350:     } elsif ($output_mode eq 'excel') {
  351:         $r->print("<h2>Preparing Excel Spreadsheet</h2>");
  352:         &output_excel($r);
  353:     } else {
  354:         $r->print("<h1>Not implemented</h1>");
  355:     }
  356:     return;
  357: }
  358: 
  359: ###############################################
  360: ###############################################
  361: 
  362: =pod 
  363: 
  364: =item &output_html_grouped_by_sequence()
  365: 
  366: Presents the statistics data as an html table organized by the order
  367: the assessments appear in the course.
  368: 
  369: =cut
  370: 
  371: ###############################################
  372: ###############################################
  373: sub output_html_grouped_by_sequence {
  374:     my ($r) = @_;
  375:     my $problem_num = 0;
  376:     #$r->print(&ProblemStatisticsLegend());
  377:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
  378:         next if ($sequence->{'num_assess'}<1);
  379:         $r->print("<h3>".$sequence->{'title'}."</h3>");
  380:         $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
  381:         $r->print('<table border="0" cellpadding="3">'."\n");
  382:         $r->print('<tr bgcolor="#FFFFE6">');
  383:         my $Str = &statistics_table_header('no container no plots');
  384:         $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
  385:         foreach my $resource (@{$sequence->{'contents'}}) {
  386:             next if ($resource->{'type'} ne 'assessment');
  387:             foreach my $part (@{$resource->{'parts'}}) {
  388:                 $problem_num++;
  389:                 my $data = &get_statistics($sequence,$resource,$part,
  390:                                            $problem_num);
  391:                 my $option = '';
  392:                 $r->print('<tr>'.&statistics_html_table_data($data,
  393:                                                              'no container').
  394:                           "</tr>\n");
  395:             }
  396:         }
  397:         $r->print("</table>\n");
  398:         $r->print("</td></tr></table>\n");
  399:         $r->rflush();
  400:     }
  401:     #
  402:     return;
  403: }
  404: 
  405: ###############################################
  406: ###############################################
  407: 
  408: =pod 
  409: 
  410: =item &output_html_ungrouped()
  411: 
  412: Presents the statistics data in a single html table which can be sorted by
  413: different columns.
  414: 
  415: =cut
  416: 
  417: ###############################################
  418: ###############################################
  419: sub output_html_ungrouped {
  420:     my ($r,$option) = @_;
  421:     #
  422:     if (exists($ENV{'form.plot'}) && $ENV{'form.plot'} ne '') {
  423:         &plot_statistics($r,$ENV{'form.plot'});
  424:     }
  425:     #
  426:     my $problem_num = 0;
  427:     my $show_container = 0;
  428:     my $show_part = 0;
  429:     #$r->print(&ProblemStatisticsLegend());
  430:     my $sortby = undef;
  431:     foreach my $field (@Fields) {
  432:         if ($ENV{'form.sortby'} eq $field->{'name'}) {
  433:             $sortby = $field->{'name'};
  434:         }
  435:     }
  436:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
  437:         $sortby = 'container';
  438:     }
  439:     # If there is more than one sequence, list their titles
  440:     my @Sequences = &Apache::lonstatistics::Sequences_with_Assess();
  441:     if (@Sequences < 1) {
  442:         $option .= ' no container';
  443:     }
  444:     #
  445:     # Compile the data
  446:     my @Statsarray;
  447:     foreach my $sequence (@Sequences) {
  448:         next if ($sequence->{'num_assess'}<1);
  449:         foreach my $resource (@{$sequence->{'contents'}}) {
  450:             next if ($resource->{'type'} ne 'assessment');
  451:             foreach my $part (@{$resource->{'parts'}}) {
  452:                 $problem_num++;
  453:                 my $data = &get_statistics($sequence,$resource,$part,
  454:                                            $problem_num);
  455:                 $show_part = 1 if ($part ne '0');
  456:                 #
  457:                 push (@Statsarray,$data);
  458:             }
  459:         }
  460:     }
  461:     #
  462:     # Sort the data
  463:     my @OutputOrder;
  464:     if ($sortby eq 'container') {
  465:         @OutputOrder = @Statsarray;
  466:     } else {
  467:         # $sortby is already defined, so we can charge ahead
  468:         if ($sortby =~ /^(title|part)$/i) {
  469:             # Alpha comparison
  470:             @OutputOrder = sort {
  471:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
  472:                     lc($a->{'title'}) cmp lc($b->{'title'}) ||
  473:                         lc($a->{'part'}) cmp lc($b->{'part'});
  474:             } @Statsarray;
  475:         } else {
  476:             # Numerical comparison
  477:             @OutputOrder = sort {
  478:                 my $retvalue = 0;
  479:                 if ($b->{$sortby} eq 'nan') {
  480:                     if ($a->{$sortby} ne 'nan') {
  481:                         $retvalue = -1;
  482:                     } else {
  483:                         $retvalue = 0;
  484:                     }
  485:                 }
  486:                 if ($a->{$sortby} eq 'nan') {
  487:                     if ($b->{$sortby} ne 'nan') {
  488:                         $retvalue = 1;
  489:                     }
  490:                 }
  491:                 if ($retvalue eq '0') {
  492:                     $retvalue = $b->{$sortby} <=> $a->{$sortby} ||
  493:                                 lc($a->{'title'}) <=> lc($b->{'title'}) ||
  494:                                 lc($a->{'part'})  <=> lc($b->{'part'});
  495:                 }
  496:                 $retvalue;
  497:             } @Statsarray;
  498:         }
  499:     }
  500:     $option .= 'no part' if (! $show_part);
  501:     my $num_output = 0;
  502:     #
  503:     # output the headers
  504:     $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
  505:     $r->print('<table border="0" cellpadding="3">'."\n");
  506:     my $Str = &statistics_table_header($option.' sortable');
  507:     $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
  508:     #
  509:     foreach my $rowdata (@OutputOrder) {
  510:         $num_output++;
  511:         if ($num_output % 25 == 0) {
  512:             $r->print("</table>\n</td></tr></table>\n");
  513:             #
  514:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
  515:             $r->print('<table border="0" cellpadding="3">'."\n");
  516:             my $Str = &statistics_table_header($option.' sortable');
  517:             $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
  518:             $r->rflush();
  519:         }
  520:         $r->print('<tr>'.&statistics_html_table_data($rowdata,$option).
  521:                   "</tr>\n");
  522:     }
  523:     $r->print("</table>\n");
  524:     $r->print("</td></tr></table>\n");
  525:     $r->rflush();
  526:     #
  527:     return;
  528: }
  529: 
  530: ###############################################
  531: ###############################################
  532: 
  533: =pod 
  534: 
  535: =item &output_excel()
  536: 
  537: Presents the statistical data in an Excel 95 compatable spreadsheet file.
  538: 
  539: =cut
  540: 
  541: ###############################################
  542: ###############################################
  543: sub output_excel {
  544:     my ($r) = @_;
  545:     my $filename = '/prtspool/'.
  546:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  547:             time.'_'.rand(1000000000).'.xls';
  548:     #
  549:     my $excel_workbook = undef;
  550:     my $excel_sheet = undef;
  551:     #
  552:     my $rows_output = 0;
  553:     my $cols_output = 0;
  554:     #
  555:     # Create sheet
  556:     $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  557:     #
  558:     # Check for errors
  559:     if (! defined($excel_workbook)) {
  560:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  561:         $r->print("Problems creating new Excel file.  ".
  562:                   "This error has been logged.  ".
  563:                   "Please alert your LON-CAPA administrator");
  564:         return ;
  565:     }
  566:     #
  567:     # The excel spreadsheet stores temporary data in files, then put them
  568:     # together.  If needed we should be able to disable this (memory only).
  569:     # The temporary directory must be specified before calling 'addworksheet'.
  570:     # File::Temp is used to determine the temporary directory.
  571:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  572:     #
  573:     # Add a worksheet
  574:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  575:     if (length($sheetname) > 31) {
  576:         $sheetname = substr($sheetname,0,31);
  577:     }
  578:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
  579:     #
  580:     # Put the course description in the header
  581:     $excel_sheet->write($rows_output,$cols_output++,
  582:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
  583:     $cols_output += 3;
  584:     #
  585:     # Put a description of the sections listed
  586:     my $sectionstring = '';
  587:     my @Sections = @Apache::lonstatistics::SelectedSections;
  588:     if (scalar(@Sections) > 1) {
  589:         if (scalar(@Sections) > 2) {
  590:             my $last = pop(@Sections);
  591:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
  592:         } else {
  593:             $sectionstring = "Sections ".join(' and ',@Sections);
  594:         }
  595:     } else {
  596:         if ($Sections[0] eq 'all') {
  597:             $sectionstring = "All sections";
  598:         } else {
  599:             $sectionstring = "Section ".$Sections[0];
  600:         }
  601:     }
  602:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
  603:     $cols_output += scalar(@Sections);
  604:     #
  605:     # Put the date in there too
  606:     $excel_sheet->write($rows_output,$cols_output++,
  607:                         'Compiled on '.localtime(time));
  608:     #
  609:     $rows_output++; 
  610:     $cols_output=0;
  611:     #
  612:     # Long Headersheaders
  613:     foreach my $field (@Fields) {
  614:         next if ($field->{'name'} eq 'problem_num');
  615:         if (exists($field->{'long_title'})) {
  616:             $excel_sheet->write($rows_output,$cols_output++,
  617:                                 $field->{'long_title'});
  618:         } else {
  619:             $excel_sheet->write($rows_output,$cols_output++,'');
  620:         }
  621:     }
  622:     $rows_output++;
  623:     $cols_output=0;
  624:     # Brief headers
  625:     foreach my $field (@Fields) {
  626:         next if ($field->{'name'} eq 'problem_num');
  627:         $excel_sheet->write($rows_output,$cols_output++,$field->{'title'});
  628:     }
  629:     $rows_output++;
  630:     #
  631:     # Write the data
  632:     my $problem_num=0;
  633:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
  634:         next if ($sequence->{'num_assess'}<1);
  635:         foreach my $resource (@{$sequence->{'contents'}}) {
  636:             next if ($resource->{'type'} ne 'assessment');
  637:             foreach my $part (@{$resource->{'parts'}}) {
  638:                 $cols_output=0;
  639:                 $problem_num++;
  640:                 my $data = &get_statistics($sequence,$resource,$part,
  641:                                            $problem_num);
  642:                 #
  643:                 if (!defined($part) || $part eq '') {
  644:                     $part = ' ';
  645:                 }
  646:                 foreach my $field (@Fields) {
  647:                     next if ($field->{'name'} eq 'problem_num');
  648:                     $excel_sheet->write($rows_output,$cols_output++,
  649:                                         $data->{$field->{'name'}});
  650:                 }
  651:                 $rows_output++;
  652:             }
  653:         }
  654:     }
  655:     #
  656:     # Write the excel file
  657:     $excel_workbook->close();
  658:     # Tell the user where to get their excel file
  659:     $r->print('<br />'.
  660:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
  661:     $r->rflush();
  662:     return;
  663: }
  664: 
  665: ###############################################
  666: ###############################################
  667: 
  668: =pod 
  669: 
  670: =item &statistics_html_table_data()
  671: 
  672: Help function used to format the rows for HTML table output.
  673: 
  674: =cut
  675: 
  676: ###############################################
  677: ###############################################
  678: sub statistics_html_table_data {
  679:     my ($data,$options) = @_;
  680:     my $row = '';
  681:     foreach my $field (@Fields) {
  682:         next if ($options =~ /no $field->{'name'}/);
  683:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  684:         if (exists($field->{'align'})) {
  685:             $row .= ' align="'.$field->{'align'}.'"';
  686:             }
  687:         $row .= '>';
  688:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  689:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
  690:         }
  691:         if (exists($field->{'format'})) {
  692:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  693:         } else {
  694:             $row .= $data->{$field->{'name'}};
  695:         }
  696:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  697:             $row.= '</a>';
  698:         }
  699:         $row .= '</td>';
  700:     }
  701:     return $row;
  702: }
  703: 
  704: sub statistics_table_header {
  705:     my ($options) = @_;
  706:     my $header_row;
  707:     foreach my $field (@Fields) {
  708:         next if ($options =~ /no $field->{'name'}/);
  709:         $header_row .= '<th>';
  710:         if ($options =~ /sortable/ && 
  711:             exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
  712:             $header_row .= '<a href="javascript:'.
  713:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
  714:                     ';document.Statistics.submit();">';
  715:         }
  716:         $header_row .= $field->{'title'};
  717:         if ($options =~ /sortable/) {
  718:             $header_row.= '</a>';
  719:         }
  720:         if ($options !~ /no plots/        && 
  721:             exists($field->{'graphable'}) && 
  722:             $field->{'graphable'} eq 'yes') {
  723:             $header_row.=' (';
  724:             $header_row .= '<a href="javascript:'.
  725:                 "document.Statistics.plot.value='$field->{'name'}'".
  726:                     ';document.Statistics.submit();">';
  727:             $header_row .= 'plot</a>)';
  728:         }
  729:         $header_row .= '</th>';
  730:     }
  731:     return $header_row;
  732: }
  733: 
  734: ###############################################
  735: ###############################################
  736: 
  737: =pod 
  738: 
  739: =item &plot_statistics()
  740: 
  741: =cut
  742: 
  743: ###############################################
  744: ###############################################
  745: sub plot_statistics {
  746:     my ($r,$datafield) = @_;
  747:     my @Data;
  748:     #
  749:     #
  750:     my $sortfield = undef;
  751:     my $title = undef;
  752:     foreach my $field (@Fields) {
  753:         if ($datafield eq $field->{'name'} &&
  754:             exists($field->{'graphable'}) && $field->{'graphable'} eq 'yes') {
  755:             $sortfield = $field->{'name'};
  756:             $title = $field->{'long_title'};
  757:         }
  758:     }
  759:     return if (! defined($sortfield) || $sortfield eq '');
  760:     #
  761:     my $Max = 0;
  762:     my $problem_num = 0;
  763:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
  764:         next if ($sequence->{'num_assess'}<1);
  765:         foreach my $resource (@{$sequence->{'contents'}}) {
  766:             next if ($resource->{'type'} ne 'assessment');
  767:             foreach my $part (@{$resource->{'parts'}}) {
  768:                 my $problem_number++;
  769:                 my $data = &get_statistics($sequence,$resource,$part,
  770:                                            $problem_num);
  771:                 my $value = $data->{$sortfield};
  772:                 $Max = $value if ($Max < $value);
  773:                 push (@Data,$value);
  774:             }
  775:         }
  776:     }
  777:     #
  778:     # Print out plot request
  779:     my $yaxis = '';
  780:     if ($sortfield eq 'per_wrong') {
  781:         $yaxis = 'Percent';
  782:     }
  783:     #
  784:     # Determine appropriate value for $Max
  785:     if ($sortfield eq 'deg_of_diff') {
  786:         if ($Max > 0.5) {
  787:             $Max = 1;
  788:         } elsif ($Max > 0.2) {
  789:             $Max = 0.5;
  790:         } elsif ($Max > 0.1) {
  791:             $Max = 0.2;
  792:         }
  793:     } elsif ($sortfield eq 'per_wrong') {
  794:         if ($Max > 50) {
  795:             $Max = 100;
  796:         } elsif ($Max > 25) {
  797:             $Max = 50;
  798:         } elsif ($Max > 20) {
  799:             $Max = 25;
  800:         } elsif ($Max > 10) {
  801:             $Max = 20;
  802:         } elsif ($Max > 5) {
  803:             $Max = 10;
  804:         } else {
  805:             $Max = 5;
  806:         }
  807:     }
  808:     
  809:     $r->print("<p>".&DrawGraph(\@Data,$title,'Problem Number',$yaxis,
  810:                                $Max)."</p>\n");
  811:     #
  812:     # Print out the data
  813:     $ENV{'form.sortby'} = 'Contents';
  814: #    &output_html_ungrouped($r);
  815:     return;
  816: }
  817: 
  818: ###############################################
  819: ###############################################
  820: 
  821: =pod 
  822: 
  823: =item &DrawGraph()
  824: 
  825: =cut
  826: 
  827: ###############################################
  828: ###############################################
  829: sub DrawGraph {
  830:     my ($values,$title,$xaxis,$yaxis,$Max)=@_;
  831:     $title = '' if (! defined($title));
  832:     $xaxis = '' if (! defined($xaxis));
  833:     $yaxis = '' if (! defined($yaxis));
  834:     #
  835:     my $sendValues = join(',', @$values);
  836:     my $sendCount = scalar(@$values);
  837:     $Max =1 if ($Max < 1);
  838:     if ( int($Max) < $Max ) {
  839:         $Max++;
  840:         $Max = int($Max);
  841:     }
  842:     my @GData = ($title,$xaxis,$yaxis,$Max,$sendCount,$sendValues);
  843:     return '<IMG src="/cgi-bin/graph.png?'.
  844:         (join('&', @GData)).'" border="1" />';
  845: }
  846: 
  847: sub get_statistics {
  848:     my ($sequence,$resource,$part,$problem_num) = @_;
  849:     #
  850:     my $symb = $resource->{'symb'};
  851:     my $courseid = $ENV{'request.course.id'};
  852:     #
  853:     my $students = \@Apache::lonstatistics::Students;
  854:     if ($Apache::lonstatistics::SelectedSections[0] eq 'all') {
  855:         $students = undef;
  856:     }
  857:     my $data = &Apache::loncoursedata::get_problem_statistics
  858:                         ($students,$symb,$part,$courseid);
  859:     $data->{'part'}        = $part;
  860:     $data->{'problem_num'} = $problem_num;
  861:     $data->{'container'}   = $sequence->{'title'};
  862:     $data->{'title'}       = $resource->{'title'};
  863:     $data->{'title.link'}  = $resource->{'src'}.'?symb='.
  864:         &Apache::lonnet::escape($resource->{'symb'});
  865:     #
  866:     return $data;
  867: }
  868: 
  869: ###############################################
  870: ###############################################
  871: 
  872: =pod 
  873: 
  874: =item &ProblemStatisticsLegend()
  875: 
  876: =cut
  877: 
  878: ###############################################
  879: ###############################################
  880: sub ProblemStatisticsLegend {
  881:     my $Ptr = '';
  882:     $Ptr = '<table border="0">';
  883:     $Ptr .= '<tr><td>';
  884:     $Ptr .= '<b>#Stdnts</b></td>';
  885:     $Ptr .= '<td>Total number of students attempted the problem.';
  886:     $Ptr .= '</td></tr><tr><td>';
  887:     $Ptr .= '<b>Tries</b></td>';
  888:     $Ptr .= '<td>Total number of tries for solving the problem.';
  889:     $Ptr .= '</td></tr><tr><td>';
  890:     $Ptr .= '<b>Max Tries</b></td>';
  891:     $Ptr .= '<td>Largest number of tries for solving the problem by a student.';
  892:     $Ptr .= '</td></tr><tr><td>';
  893:     $Ptr .= '<b>Mean</b></td>';
  894:     $Ptr .= '<td>Average number of tries. [ Tries / #Stdnts ]';
  895:     $Ptr .= '</td></tr><tr><td>';
  896:     $Ptr .= '<b>#YES</b></td>';
  897:     $Ptr .= '<td>Number of students solved the problem correctly.';
  898:     $Ptr .= '</td></tr><tr><td>';
  899:     $Ptr .= '<b>#yes</b></td>';
  900:     $Ptr .= '<td>Number of students solved the problem by override.';
  901:     $Ptr .= '</td></tr><tr><td>';
  902:     $Ptr .= '<b>%Wrong</b></td>';
  903:     $Ptr .= '<td>Percentage of students who tried to solve the problem ';
  904:     $Ptr .= 'but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]';
  905:     $Ptr .= '</td></tr><tr><td>';
  906:     $Ptr .= '<b>DoDiff</b></td>';
  907:     $Ptr .= '<td>Degree of Difficulty of the problem.  ';
  908:     $Ptr .= '[ 1 - ((#YES+#yes) / Tries) ]';
  909:     $Ptr .= '</td></tr><tr><td>';
  910:     $Ptr .= '<b>S.D.</b></td>';
  911:     $Ptr .= '<td>Standard Deviation of the tries.  ';
  912:     $Ptr .= '[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) ';
  913:     $Ptr .= 'where Xi denotes every student\'s tries ]';
  914:     $Ptr .= '</td></tr><tr><td>';
  915:     $Ptr .= '<b>Skew.</b></td>';
  916:     $Ptr .= '<td>Skewness of the students tries.';
  917:     $Ptr .= '[(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]';
  918:     $Ptr .= '</td></tr><tr><td>';
  919:     $Ptr .= '<b>Dis.F.</b></td>';
  920:     $Ptr .= '<td>Discrimination Factor: A Standard for evaluating the ';
  921:     $Ptr .= 'problem according to a Criterion<br>';
  922:     $Ptr .= '<b>[Criterion to group students into %27 Upper Students - ';
  923:     $Ptr .= 'and %27 Lower Students]</b><br>';
  924:     $Ptr .= '<b>1st Criterion</b> for Sorting the Students: ';
  925:     $Ptr .= '<b>Sum of Partial Credit Awarded / Total Number of Tries</b><br>';
  926:     $Ptr .= '<b>2nd Criterion</b> for Sorting the Students: ';
  927:     $Ptr .= '<b>Total number of Correct Answers / Total Number of Tries</b>';
  928:     $Ptr .= '</td></tr>';
  929:     $Ptr .= '<tr><td><b>Disc.</b></td>';
  930:     $Ptr .= '<td>Number of Students had at least one discussion.';
  931:     $Ptr .= '</td></tr></table>';
  932:     return $Ptr;
  933: }
  934: 
  935: #---- END Problem Statistics Web Page ----------------------------------------
  936: 
  937: 1;
  938: __END__

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