File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.61: download - view: text, annotated - select for diffs
Tue Nov 11 22:14:28 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: version_1_0_99, HEAD
Use the new &Apache::loncommon::store_course_settings and
restore_course_settings subroutines to remember form settings.
lonstatistics.pm was changed to not parse the student enrollment status
form elements before calling the sub-module routines which present the
interface.  Now each of the sub-modules (statistics/*) must call
&Apache::lonstatistics::PrepareClasslist(); at the right time.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonproblemstatistics.pm,v 1.61 2003/11/11 22:14:28 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 %Saveable_Parameters = ('Status' => 'scalar',
  269:                                'statsoutputmode' => 'scalar',
  270:                                'Section' => 'array',
  271:                                'StudentData' => 'array',
  272:                                'Maps' => 'array');
  273:     &Apache::loncommon::store_course_settings('statistics',
  274:                                               \%Saveable_Parameters);
  275:     &Apache::loncommon::restore_course_settings('statistics',
  276:                                                 \%Saveable_Parameters);
  277:     #
  278:     &Apache::lonstatistics::PrepareClasslist();
  279:     #
  280:     my ($interface,$output_mode,$show) = &CreateInterface();
  281:     $r->print($interface);
  282:     $r->print('<input type="hidden" name="statsfirstcall" value="no" />');
  283:     $r->print('<input type="hidden" name="sortby" value="'.$ENV{'form.sortby'}.
  284:               '" />');
  285:     $r->print('<input type="hidden" name="plot" value="" />');
  286:     if (! exists($ENV{'form.statsfirstcall'})) {
  287:         return;
  288:     }
  289:     #
  290:     &Apache::lonstatistics::Gather_Student_Data($r);
  291:     #
  292:     #
  293:     if ($output_mode eq 'html') {
  294:         $r->print("<h2>".
  295:                   $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  296:                   "</h2>\n");
  297:         $r->print("<h3>".localtime(time)."</h3>");
  298:         $r->rflush();
  299:         if ($show eq 'grouped') {
  300:             &output_html_grouped_by_sequence($r);
  301:         } elsif ($show eq 'ungrouped') {
  302:             &output_html_ungrouped($r);
  303:         }
  304:     } elsif ($output_mode eq 'excel') {
  305:         $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
  306:         &output_excel($r);
  307:     } else {
  308:         $r->print('<h1>'.&mt('Not implemented').'</h1>');
  309:     }
  310:     return;
  311: }
  312: 
  313: ###############################################
  314: ###############################################
  315: 
  316: =pod 
  317: 
  318: =item &output_html_grouped_by_sequence()
  319: 
  320: Presents the statistics data as an html table organized by the order
  321: the assessments appear in the course.
  322: 
  323: =cut
  324: 
  325: ###############################################
  326: ###############################################
  327: sub output_html_grouped_by_sequence {
  328:     my ($r) = @_;
  329:     my $problem_num = 0;
  330:     #$r->print(&ProblemStatisticsLegend());
  331:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
  332:         next if ($sequence->{'num_assess'}<1);
  333:         $r->print("<h3>".$sequence->{'title'}."</h3>");
  334:         $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
  335:         $r->print('<table border="0" cellpadding="3">'."\n");
  336:         $r->print('<tr bgcolor="#FFFFE6">');
  337:         my $Str = &statistics_table_header('no container no plots');
  338:         $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
  339:         foreach my $resource (@{$sequence->{'contents'}}) {
  340:             next if ($resource->{'type'} ne 'assessment');
  341:             foreach my $part (@{$resource->{'parts'}}) {
  342:                 $problem_num++;
  343:                 my $data = &get_statistics($sequence,$resource,$part,
  344:                                            $problem_num);
  345:                 my $option = '';
  346:                 $r->print('<tr>'.&statistics_html_table_data($data,
  347:                                                              'no container').
  348:                           "</tr>\n");
  349:             }
  350:         }
  351:         $r->print("</table>\n");
  352:         $r->print("</td></tr></table>\n");
  353:         $r->rflush();
  354:     }
  355:     #
  356:     return;
  357: }
  358: 
  359: ###############################################
  360: ###############################################
  361: 
  362: =pod 
  363: 
  364: =item &output_html_ungrouped()
  365: 
  366: Presents the statistics data in a single html table which can be sorted by
  367: different columns.
  368: 
  369: =cut
  370: 
  371: ###############################################
  372: ###############################################
  373: sub output_html_ungrouped {
  374:     my ($r,$option) = @_;
  375:     #
  376:     if (exists($ENV{'form.plot'}) && $ENV{'form.plot'} ne '') {
  377:         &plot_statistics($r,$ENV{'form.plot'});
  378:     }
  379:     #
  380:     my $problem_num = 0;
  381:     my $show_container = 0;
  382:     my $show_part = 0;
  383:     #$r->print(&ProblemStatisticsLegend());
  384:     my $sortby = undef;
  385:     foreach my $field (@Fields) {
  386:         if ($ENV{'form.sortby'} eq $field->{'name'}) {
  387:             $sortby = $field->{'name'};
  388:         }
  389:     }
  390:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
  391:         $sortby = 'container';
  392:     }
  393:     # If there is more than one sequence, list their titles
  394:     my @Sequences = &Apache::lonstatistics::Sequences_with_Assess();
  395:     if (@Sequences < 1) {
  396:         $option .= ' no container';
  397:     }
  398:     #
  399:     # Compile the data
  400:     my @Statsarray;
  401:     foreach my $sequence (@Sequences) {
  402:         next if ($sequence->{'num_assess'}<1);
  403:         foreach my $resource (@{$sequence->{'contents'}}) {
  404:             next if ($resource->{'type'} ne 'assessment');
  405:             foreach my $part (@{$resource->{'parts'}}) {
  406:                 $problem_num++;
  407:                 my $data = &get_statistics($sequence,$resource,$part,
  408:                                            $problem_num);
  409:                 $show_part = 1 if ($part ne '0');
  410:                 #
  411:                 push (@Statsarray,$data);
  412:             }
  413:         }
  414:     }
  415:     #
  416:     # Sort the data
  417:     my @OutputOrder;
  418:     if ($sortby eq 'container') {
  419:         @OutputOrder = @Statsarray;
  420:     } else {
  421:         # $sortby is already defined, so we can charge ahead
  422:         if ($sortby =~ /^(title|part)$/i) {
  423:             # Alpha comparison
  424:             @OutputOrder = sort {
  425:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
  426:                     lc($a->{'title'}) cmp lc($b->{'title'}) ||
  427:                         lc($a->{'part'}) cmp lc($b->{'part'});
  428:             } @Statsarray;
  429:         } else {
  430:             # Numerical comparison
  431:             @OutputOrder = sort {
  432:                 my $retvalue = 0;
  433:                 if ($b->{$sortby} eq 'nan') {
  434:                     if ($a->{$sortby} ne 'nan') {
  435:                         $retvalue = -1;
  436:                     } else {
  437:                         $retvalue = 0;
  438:                     }
  439:                 }
  440:                 if ($a->{$sortby} eq 'nan') {
  441:                     if ($b->{$sortby} ne 'nan') {
  442:                         $retvalue = 1;
  443:                     }
  444:                 }
  445:                 if ($retvalue eq '0') {
  446:                     $retvalue = $b->{$sortby} <=> $a->{$sortby} ||
  447:                                 lc($a->{'title'}) <=> lc($b->{'title'}) ||
  448:                                 lc($a->{'part'})  <=> lc($b->{'part'});
  449:                 }
  450:                 $retvalue;
  451:             } @Statsarray;
  452:         }
  453:     }
  454:     $option .= 'no part' if (! $show_part);
  455:     my $num_output = 0;
  456:     #
  457:     # output the headers
  458:     $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
  459:     $r->print('<table border="0" cellpadding="3">'."\n");
  460:     my $Str = &statistics_table_header($option.' sortable');
  461:     $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
  462:     #
  463:     foreach my $rowdata (@OutputOrder) {
  464:         $num_output++;
  465:         if ($num_output % 25 == 0) {
  466:             $r->print("</table>\n</td></tr></table>\n");
  467:             #
  468:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
  469:             $r->print('<table border="0" cellpadding="3">'."\n");
  470:             my $Str = &statistics_table_header($option.' sortable');
  471:             $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
  472:             $r->rflush();
  473:         }
  474:         $r->print('<tr>'.&statistics_html_table_data($rowdata,$option).
  475:                   "</tr>\n");
  476:     }
  477:     $r->print("</table>\n");
  478:     $r->print("</td></tr></table>\n");
  479:     $r->rflush();
  480:     #
  481:     return;
  482: }
  483: 
  484: ###############################################
  485: ###############################################
  486: 
  487: =pod 
  488: 
  489: =item &output_excel()
  490: 
  491: Presents the statistical data in an Excel 95 compatable spreadsheet file.
  492: 
  493: =cut
  494: 
  495: ###############################################
  496: ###############################################
  497: sub output_excel {
  498:     my ($r) = @_;
  499:     my $filename = '/prtspool/'.
  500:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  501:             time.'_'.rand(1000000000).'.xls';
  502:     #
  503:     my $excel_workbook = undef;
  504:     my $excel_sheet = undef;
  505:     #
  506:     my $rows_output = 0;
  507:     my $cols_output = 0;
  508:     #
  509:     # Create sheet
  510:     $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  511:     #
  512:     # Check for errors
  513:     if (! defined($excel_workbook)) {
  514:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  515:         $r->print(&mt("Problems creating new Excel file.  ".
  516:                   "This error has been logged.  ".
  517:                   "Please alert your LON-CAPA administrator."));
  518:         return ;
  519:     }
  520:     #
  521:     # The excel spreadsheet stores temporary data in files, then put them
  522:     # together.  If needed we should be able to disable this (memory only).
  523:     # The temporary directory must be specified before calling 'addworksheet'.
  524:     # File::Temp is used to determine the temporary directory.
  525:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  526:     #
  527:     # Add a worksheet
  528:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  529:     if (length($sheetname) > 31) {
  530:         $sheetname = substr($sheetname,0,31);
  531:     }
  532:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
  533:     #
  534:     # Put the course description in the header
  535:     $excel_sheet->write($rows_output,$cols_output++,
  536:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
  537:     $cols_output += 3;
  538:     #
  539:     # Put a description of the sections listed
  540:     my $sectionstring = '';
  541:     my @Sections = @Apache::lonstatistics::SelectedSections;
  542:     if (scalar(@Sections) > 1) {
  543:         if (scalar(@Sections) > 2) {
  544:             my $last = pop(@Sections);
  545:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
  546:         } else {
  547:             $sectionstring = "Sections ".join(' and ',@Sections);
  548:         }
  549:     } else {
  550:         if ($Sections[0] eq 'all') {
  551:             $sectionstring = "All sections";
  552:         } else {
  553:             $sectionstring = "Section ".$Sections[0];
  554:         }
  555:     }
  556:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
  557:     $cols_output += scalar(@Sections);
  558:     #
  559:     # Put the date in there too
  560:     $excel_sheet->write($rows_output,$cols_output++,
  561:                         'Compiled on '.localtime(time));
  562:     #
  563:     $rows_output++; 
  564:     $cols_output=0;
  565:     #
  566:     # Long Headersheaders
  567:     foreach my $field (@Fields) {
  568:         next if ($field->{'name'} eq 'problem_num');
  569:         if (exists($field->{'long_title'})) {
  570:             $excel_sheet->write($rows_output,$cols_output++,
  571:                                 $field->{'long_title'});
  572:         } else {
  573:             $excel_sheet->write($rows_output,$cols_output++,'');
  574:         }
  575:     }
  576:     $rows_output++;
  577:     $cols_output=0;
  578:     # Brief headers
  579:     foreach my $field (@Fields) {
  580:         next if ($field->{'name'} eq 'problem_num');
  581:         # Use english for excel as I am not sure how well excel handles 
  582:         # other character sets....
  583:         $excel_sheet->write($rows_output,$cols_output++,$field->{'title'});
  584:     }
  585:     $rows_output++;
  586:     #
  587:     # Write the data
  588:     my $problem_num=0;
  589:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
  590:         next if ($sequence->{'num_assess'}<1);
  591:         foreach my $resource (@{$sequence->{'contents'}}) {
  592:             next if ($resource->{'type'} ne 'assessment');
  593:             foreach my $part (@{$resource->{'parts'}}) {
  594:                 $cols_output=0;
  595:                 $problem_num++;
  596:                 my $data = &get_statistics($sequence,$resource,$part,
  597:                                            $problem_num);
  598:                 #
  599:                 if (!defined($part) || $part eq '') {
  600:                     $part = ' ';
  601:                 }
  602:                 foreach my $field (@Fields) {
  603:                     next if ($field->{'name'} eq 'problem_num');
  604:                     $excel_sheet->write($rows_output,$cols_output++,
  605:                                         $data->{$field->{'name'}});
  606:                 }
  607:                 $rows_output++;
  608:             }
  609:         }
  610:     }
  611:     #
  612:     # Write the excel file
  613:     $excel_workbook->close();
  614:     # Tell the user where to get their excel file
  615:     $r->print('<br />'.
  616:               '<a href="'.$filename.'">'.
  617:               &mt('Your Excel Spreadsheet').'</a>'."\n");
  618:     $r->rflush();
  619:     return;
  620: }
  621: 
  622: ###############################################
  623: ###############################################
  624: 
  625: =pod 
  626: 
  627: =item &statistics_html_table_data()
  628: 
  629: Help function used to format the rows for HTML table output.
  630: 
  631: =cut
  632: 
  633: ###############################################
  634: ###############################################
  635: sub statistics_html_table_data {
  636:     my ($data,$options) = @_;
  637:     my $row = '';
  638:     foreach my $field (@Fields) {
  639:         next if ($options =~ /no $field->{'name'}/);
  640:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
  641:         if (exists($field->{'align'})) {
  642:             $row .= ' align="'.$field->{'align'}.'"';
  643:             }
  644:         $row .= '>';
  645:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  646:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
  647:         }
  648:         if (exists($field->{'format'})) {
  649:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
  650:         } else {
  651:             $row .= $data->{$field->{'name'}};
  652:         }
  653:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
  654:             $row.= '</a>';
  655:         }
  656:         $row .= '</td>';
  657:     }
  658:     return $row;
  659: }
  660: 
  661: sub statistics_table_header {
  662:     my ($options) = @_;
  663:     my $header_row;
  664:     foreach my $field (@Fields) {
  665:         next if ($options =~ /no $field->{'name'}/);
  666:         $header_row .= '<th>';
  667:         if ($options =~ /sortable/ && 
  668:             exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
  669:             $header_row .= '<a href="javascript:'.
  670:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
  671:                     ';document.Statistics.submit();">';
  672:         }
  673:         $header_row .= &mt($field->{'title'});
  674:         if ($options =~ /sortable/) {
  675:             $header_row.= '</a>';
  676:         }
  677:         if ($options !~ /no plots/        && 
  678:             exists($field->{'graphable'}) && 
  679:             $field->{'graphable'} eq 'yes') {
  680:             $header_row.=' (';
  681:             $header_row .= '<a href="javascript:'.
  682:                 "document.Statistics.plot.value='$field->{'name'}'".
  683:                     ';document.Statistics.submit();">';
  684:             $header_row .= &mt('plot').'</a>)';
  685:         }
  686:         $header_row .= '</th>';
  687:     }
  688:     return $header_row;
  689: }
  690: 
  691: ###############################################
  692: ###############################################
  693: 
  694: =pod 
  695: 
  696: =item &plot_statistics()
  697: 
  698: =cut
  699: 
  700: ###############################################
  701: ###############################################
  702: sub plot_statistics {
  703:     my ($r,$datafield) = @_;
  704:     my @Data;
  705:     #
  706:     #
  707:     my $sortfield = undef;
  708:     my $title = undef;
  709:     foreach my $field (@Fields) {
  710:         if ($datafield eq $field->{'name'} &&
  711:             exists($field->{'graphable'}) && $field->{'graphable'} eq 'yes') {
  712:             $sortfield = $field->{'name'};
  713:             $title = $field->{'long_title'};
  714:         }
  715:     }
  716:     return if (! defined($sortfield) || $sortfield eq '');
  717:     #
  718:     my $Max = 0;
  719:     my $problem_num = 0;
  720:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
  721:         next if ($sequence->{'num_assess'}<1);
  722:         foreach my $resource (@{$sequence->{'contents'}}) {
  723:             next if ($resource->{'type'} ne 'assessment');
  724:             foreach my $part (@{$resource->{'parts'}}) {
  725:                 my $problem_number++;
  726:                 my $data = &get_statistics($sequence,$resource,$part,
  727:                                            $problem_num);
  728:                 my $value = $data->{$sortfield};
  729:                 $Max = $value if ($Max < $value);
  730:                 push (@Data,$value);
  731:             }
  732:         }
  733:     }
  734:     #
  735:     # Print out plot request
  736:     my $yaxis = '';
  737:     if ($sortfield eq 'per_wrong') {
  738:         $yaxis = 'Percent';
  739:     }
  740:     #
  741:     # Determine appropriate value for $Max
  742:     if ($sortfield eq 'deg_of_diff') {
  743:         if ($Max > 0.5) {
  744:             $Max = 1;
  745:         } elsif ($Max > 0.2) {
  746:             $Max = 0.5;
  747:         } elsif ($Max > 0.1) {
  748:             $Max = 0.2;
  749:         }
  750:     } elsif ($sortfield eq 'per_wrong') {
  751:         if ($Max > 50) {
  752:             $Max = 100;
  753:         } elsif ($Max > 25) {
  754:             $Max = 50;
  755:         } elsif ($Max > 20) {
  756:             $Max = 25;
  757:         } elsif ($Max > 10) {
  758:             $Max = 20;
  759:         } elsif ($Max > 5) {
  760:             $Max = 10;
  761:         } else {
  762:             $Max = 5;
  763:         }
  764:     }
  765:     
  766:     $r->print("<p>".&Apache::loncommon::DrawBarGraph($title,
  767:                                                      'Problem Number',
  768:                                                      $yaxis,
  769:                                                      $Max,
  770:                                                      undef,
  771:                                                      \@Data)."</p>\n");
  772:     #
  773:     # Print out the data
  774:     $ENV{'form.sortby'} = 'Contents';
  775: #    &output_html_ungrouped($r);
  776:     return;
  777: }
  778: 
  779: sub get_statistics {
  780:     my ($sequence,$resource,$part,$problem_num) = @_;
  781:     #
  782:     my $symb = $resource->{'symb'};
  783:     my $courseid = $ENV{'request.course.id'};
  784:     #
  785:     my $students = \@Apache::lonstatistics::Students;
  786:     if ($Apache::lonstatistics::SelectedSections[0] eq 'all') {
  787:         $students = undef;
  788:     }
  789:     my $data = &Apache::loncoursedata::get_problem_statistics
  790:                         ($students,$symb,$part,$courseid);
  791:     $data->{'part'}        = $part;
  792:     $data->{'problem_num'} = $problem_num;
  793:     $data->{'container'}   = $sequence->{'title'};
  794:     $data->{'title'}       = $resource->{'title'};
  795:     $data->{'title.link'}  = $resource->{'src'}.'?symb='.
  796:         &Apache::lonnet::escape($resource->{'symb'});
  797:     #
  798:     return $data;
  799: }
  800: 
  801: ###############################################
  802: ###############################################
  803: 
  804: =pod 
  805: 
  806: =item &ProblemStatisticsLegend()
  807: 
  808: HELP  This needs to be localized, or at least generated automatically.
  809: 
  810: =cut
  811: 
  812: ###############################################
  813: ###############################################
  814: sub ProblemStatisticsLegend {
  815:     my $Ptr = '';
  816:     $Ptr = '<table border="0">';
  817:     $Ptr .= '<tr><td>';
  818:     $Ptr .= '<b>#Stdnts</b></td>';
  819:     $Ptr .= '<td>Total number of students attempted the problem.';
  820:     $Ptr .= '</td></tr><tr><td>';
  821:     $Ptr .= '<b>Tries</b></td>';
  822:     $Ptr .= '<td>Total number of tries for solving the problem.';
  823:     $Ptr .= '</td></tr><tr><td>';
  824:     $Ptr .= '<b>Max Tries</b></td>';
  825:     $Ptr .= '<td>Largest number of tries for solving the problem by a student.';
  826:     $Ptr .= '</td></tr><tr><td>';
  827:     $Ptr .= '<b>Mean</b></td>';
  828:     $Ptr .= '<td>Average number of tries. [ Tries / #Stdnts ]';
  829:     $Ptr .= '</td></tr><tr><td>';
  830:     $Ptr .= '<b>#YES</b></td>';
  831:     $Ptr .= '<td>Number of students solved the problem correctly.';
  832:     $Ptr .= '</td></tr><tr><td>';
  833:     $Ptr .= '<b>#yes</b></td>';
  834:     $Ptr .= '<td>Number of students solved the problem by override.';
  835:     $Ptr .= '</td></tr><tr><td>';
  836:     $Ptr .= '<b>%Wrong</b></td>';
  837:     $Ptr .= '<td>Percentage of students who tried to solve the problem ';
  838:     $Ptr .= 'but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]';
  839:     $Ptr .= '</td></tr><tr><td>';
  840:     $Ptr .= '<b>DoDiff</b></td>';
  841:     $Ptr .= '<td>Degree of Difficulty of the problem.  ';
  842:     $Ptr .= '[ 1 - ((#YES+#yes) / Tries) ]';
  843:     $Ptr .= '</td></tr><tr><td>';
  844:     $Ptr .= '<b>S.D.</b></td>';
  845:     $Ptr .= '<td>Standard Deviation of the tries.  ';
  846:     $Ptr .= '[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) ';
  847:     $Ptr .= 'where Xi denotes every student\'s tries ]';
  848:     $Ptr .= '</td></tr><tr><td>';
  849:     $Ptr .= '<b>Skew.</b></td>';
  850:     $Ptr .= '<td>Skewness of the students tries.';
  851:     $Ptr .= '[(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]';
  852:     $Ptr .= '</td></tr><tr><td>';
  853:     $Ptr .= '<b>Dis.F.</b></td>';
  854:     $Ptr .= '<td>Discrimination Factor: A Standard for evaluating the ';
  855:     $Ptr .= 'problem according to a Criterion<br>';
  856:     $Ptr .= '<b>[Criterion to group students into %27 Upper Students - ';
  857:     $Ptr .= 'and %27 Lower Students]</b><br>';
  858:     $Ptr .= '<b>1st Criterion</b> for Sorting the Students: ';
  859:     $Ptr .= '<b>Sum of Partial Credit Awarded / Total Number of Tries</b><br>';
  860:     $Ptr .= '<b>2nd Criterion</b> for Sorting the Students: ';
  861:     $Ptr .= '<b>Total number of Correct Answers / Total Number of Tries</b>';
  862:     $Ptr .= '</td></tr>';
  863:     $Ptr .= '<tr><td><b>Disc.</b></td>';
  864:     $Ptr .= '<td>Number of Students had at least one discussion.';
  865:     $Ptr .= '</td></tr></table>';
  866:     return $Ptr;
  867: }
  868: 
  869: #---- END Problem Statistics Web Page ----------------------------------------
  870: 
  871: 1;
  872: __END__

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