File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.59: download - view: text, annotated - select for diffs
Mon Sep 29 16:20:18 2003 UTC (20 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Localization of lonproblemstatistics.pm.

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

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