File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.69: download - view: text, annotated - select for diffs
Mon Mar 1 16:39:19 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
No longer translate before calling lonhtmlcommon::breadcrumbs
and lonhtmlcommong::add_breadcrumb.

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

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