File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.44: download - view: text, annotated - select for diffs
Wed Mar 26 17:03:41 2003 UTC (21 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
&output_excel which writes the data to an Excel file.

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

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