File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.28: download - view: text, annotated - select for diffs
Wed Aug 14 21:51:51 2002 UTC (21 years, 10 months ago) by stredwic
Branches: MAIN
CVS tags: HEAD
Fixed the number not updating in the status window for update all students
button.  Also fixed some tieing to hash problems for lonproblemstatistics
and lonproblemanalysis.

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: lonproblemstatistics.pm,v 1.28 2002/08/14 21:51:51 stredwic Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (Navigate problems for statistical reports
   29: # YEAR=2001
   30: # 5/5,7/9,7/25/1,8/11,9/13,9/26,10/5,10/9,10/22,10/26 Behrouz Minaei
   31: # 11/1,11/4,11/16,12/14,12/16,12/18,12/20,12/31 Behrouz Minaei
   32: # YEAR=2002
   33: # 1/22,2/1,2/6,2/25,3/2,3/6,3/17,3/21,3/22,3/26,4/7,5/6 Behrouz Minaei
   34: # 5/12,5/14,5/15,5/19,5/26,7/16,7/25,7/29,8/5  Behrouz Minaei
   35: #
   36: ###
   37: 
   38: package Apache::lonproblemstatistics; 
   39: 
   40: use strict;
   41: use Apache::lonnet();
   42: use Apache::lonhtmlcommon;
   43: use Apache::loncoursedata;
   44: use GDBM_File;
   45: 
   46: my $jr;
   47: 
   48: sub InitializeProblemStatistics {
   49:     my ($cacheDB, $students, $courseID, $c, $r)=@_;
   50:     my %cache;
   51: 
   52:     $jr = $r;
   53: 
   54:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
   55:         $r->print('Unable to tie database1.');
   56:         return ('ERROR', undef);
   57:     }
   58: 
   59:     # Remove students who don't have the proper section.
   60:     my @sectionsSelected = split(':',$cache{'sectionsSelected'});
   61:     for(my $studentIndex=((scalar @$students)-1); $studentIndex>=0;
   62:         $studentIndex--) {
   63:         my $value = $cache{$students->[$studentIndex].':section'};
   64:         my $found = 0;
   65:         foreach (@sectionsSelected) {
   66:             if($_ eq 'none') {
   67:                 if($value eq '' || !defined($value) || $value eq ' ') {
   68:                     $found = 1;
   69:                     last;
   70:                 }
   71:             } else {
   72:                 if($value eq $_) {
   73:                     $found = 1;
   74:                     last;
   75:                 }
   76:             }
   77:         }
   78:         if($found == 0) {
   79:             splice(@$students, $studentIndex, 1);
   80:         }
   81:     }
   82: 
   83:     my $isNotCached = 0;
   84:     my $lastStatus = (defined($cache{'StatisticsLastStatus'})) ?
   85:                      $cache{'StatisticsLastStatus'} : 'Nothing';
   86:     my $whichStudents = join(':::',sort(@$students));
   87:     if(!defined($cache{'StatisticsCached'}) || 
   88:        $lastStatus ne $cache{'Status'} ||
   89:        $whichStudents ne $cache{'StatisticsWhichStudents'}) {
   90:         $isNotCached = 1;
   91:     }
   92: 
   93:     untie(%cache);
   94:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
   95:         $r->print('Unable to tie database.2');
   96:         return ('ERROR', undef);
   97:     }
   98:     if($isNotCached && defined($cache{'StatisticsCached'})) {
   99:         my @statkeys = split(':::', $cache{'StatisticsKeys'});
  100:         delete $cache{'StatisticsKeys'};
  101:         delete $cache{'StatisticsCached'};
  102:         foreach(@statkeys) {
  103:             delete $cache{$_};
  104:         }
  105:     }
  106: 
  107:     untie(%cache);
  108:     if($isNotCached) {
  109:         &Apache::loncoursedata::DownloadStudentCourseDataSeparate($students,
  110:                                                                   'true',
  111:                                                                   $cacheDB,
  112:                                                                   'true', 
  113:                                                                   'true',
  114:                                                                   $courseID,
  115:                                                                   $r, $c);
  116:     }
  117:     if($c->aborted()) { return ('ERROR', undef); }
  118: 
  119:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
  120:         $r->print('Unable to tie database.3');
  121:         return ('ERROR', undef);
  122:     }
  123:     my $problemData;
  124:     if($isNotCached) {
  125:         ($problemData) = &ExtractStudentData(\%cache, $students);
  126:         &CalculateStatistics($problemData, \%cache);
  127:     }
  128:     untie(%cache);
  129: 
  130:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
  131:         $r->print('Unable to tie database.4');
  132:         return ('ERROR', undef);
  133:     }
  134:     if($isNotCached) {
  135:         foreach(keys(%$problemData)) {
  136:             $cache{$_} = $problemData->{$_};
  137:         }
  138:         $cache{'StatisticsKeys'} = join(':::', keys(%$problemData));
  139:         $cache{'StatisticsCached'} = 'true';
  140:         $cache{'StatisticsLastStatus'} = $cache{'Status'};
  141:         $cache{'StatisticsWhichStudents'} = $whichStudents;
  142:     }
  143:     untie(%cache);
  144: 
  145:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
  146:         $r->print('Unable to tie database.5');
  147:         return ('ERROR', undef);
  148:     }
  149: 
  150:     my $orderedProblems = &SortProblems(\%cache, 
  151:                                         $cache{'ProblemStatisticsSort'},
  152:                                         $cache{'SortProblems'},
  153:                                         $cache{'ProblemStatisticsAscend'});
  154:     untie(%cache);
  155: 
  156:     return ('OK', $orderedProblems);
  157: }
  158: 
  159: sub BuildProblemStatisticsPage {
  160:     my ($cacheDB, $students, $courseID, $c, $r)=@_;
  161: 
  162:     my @Header = ("Homework Sets Order","#Stdnts","Tries","Mod",
  163:                   "Mean","#YES","#yes","%Wrng","DoDiff",
  164:                   "S.D.","Skew.","D.F.1st","D.F.2nd");
  165:     my $color=&setbgcolor(0);
  166:     my %cache;
  167: 
  168:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
  169:         $r->print('Unable to tie database.6');
  170:         return;
  171:     }
  172:     my $Ptr = '';
  173:     $Ptr .= '<table border="0" cellspacing="5"><tbody>';
  174:     $Ptr .= '<tr><td align="right"><b>Select Map</b></td>'."\n";
  175:     $Ptr .= '<td align="left">';
  176:     $Ptr .= &Apache::lonhtmlcommon::MapOptions(\%cache, 'ProblemStatistics',
  177:                                                'Statistics');
  178:     $Ptr .= '</td></tr>'."\n";
  179:     $Ptr .= '<tr><td align="right"><b>Sorting Type:</b></td>'."\n";
  180:     $Ptr .= '<td align="left">'."\n";
  181:     $Ptr .= &Apache::lonhtmlcommon::AscendOrderOptions(
  182:                                             $cache{'ProblemStatisticsAscend'}, 
  183:                                             'ProblemStatistics',
  184:                                             'Statistics');
  185:     $Ptr .= '</td></tr>'."\n";
  186:     $Ptr .= '<tr><td align="right"><b>Select Sections</b>';
  187:     $Ptr .= '</td>'."\n";
  188:     $Ptr .= '<td align="left">'."\n";
  189:     my @sections = split(':',$cache{'sectionList'});
  190:     my @sectionsSelected = split(':',$cache{'sectionsSelected'});
  191:     $Ptr .= &Apache::lonhtmlcommon::MultipleSectionSelect(\@sections,
  192:                                                           \@sectionsSelected,
  193:                                                           'Statistics');
  194:     $Ptr .= '</td></tr>'."\n";
  195:     $Ptr .= &ProblemStatisticsButtons($cache{'DisplayFormat'}, 
  196:                                       $cache{'DisplayLegend'},
  197:                                       $cache{'SortProblems'});
  198:     $Ptr .= '</table>';
  199:     if($cache{'DisplayLegend'} eq 'Show Legend') {
  200:         $Ptr .= &ProblemStatisticsLegend();
  201:     }
  202:     $r->print($Ptr);
  203:     $r->rflush();
  204:     untie(%cache);
  205: 
  206:     my ($result, $orderedProblems) =
  207:         &InitializeProblemStatistics($cacheDB, $students, $courseID, $c, $r);
  208:     if($result ne 'OK') {
  209:         return;
  210:     }
  211: 
  212:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
  213:         $r->print('Unable to tie database.6');
  214:         return;
  215:     }
  216:     &BuildStatisticsTable(\%cache, $cache{'DisplayFormat'}, 
  217:                           $cache{'SortProblems'}, $orderedProblems, 
  218:                           \@Header, $r, $color);
  219:     untie(%cache);
  220: 
  221:     return;
  222: }
  223: 
  224: sub BuildGraphicChart {
  225:     my ($graph,$cacheDB,$courseDescription,$students,$courseID,$r,$c)=@_;
  226:     my %cache;
  227:     my $max = 0;
  228: 
  229:     my ($result, undef) = 
  230:         &InitializeProblemStatistics($cacheDB, $students, $courseID, $c, $r);
  231:     if($result ne 'OK') {
  232:         return;
  233:     }
  234: 
  235:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
  236:         return 'Unable to tie database.7';
  237:     }
  238:    
  239:     my @problems = split(':::', $cache{'problemList'});
  240:     my @values = ();
  241:     foreach (@problems) {
  242:         my $data = 0;
  243:         if($graph eq 'DoDiffGraph') {
  244:             $data = sprintf("%.2f", $cache{$_.':degreeOfDifficulty'}),
  245:         } else {
  246:             $data = sprintf("%.1f", $cache{$_.':percentWrong'}),
  247:         }
  248:         if($max < $data) {
  249:             $max = $data;
  250:         }
  251:         push(@values, $data);
  252:     }
  253:     untie(%cache);
  254: 
  255:     my $sendValues = join(',', @values);
  256:     my $sendCount = scalar(@values);
  257: 
  258:     my $title = '';
  259:     if($graph eq 'DoDiffGraph') {
  260: 	$title = 'Degree-of-Difficulty';
  261:     } else {
  262: 	$title = 'Wrong-Percentage';
  263:     }
  264:     my @GData = ($courseDescription, 'Problems', $title, $max, $sendCount, 
  265:                  $sendValues);
  266: 
  267:     $r->print('</form>'."\n");
  268:     $r->print('<IMG src="/cgi-bin/graph.gif?'.(join('&', @GData)).
  269:               '" border="1" />');
  270:     $r->print('<form>'."\n");
  271: 
  272:     return;
  273: }
  274: 
  275: #---- Problem Statistics Web Page ---------------------------------------
  276: 
  277: sub CreateProblemStatisticsTableHeading {
  278:     my ($headings,$r)=@_;
  279: 
  280:     my $Str='';
  281:     $Str .= '<tr>'."\n";
  282:     $Str .= '<th bgcolor="#ffffe6">P#</th>'."\n";
  283:     foreach(@$headings) {
  284: 	$Str .= '<th bgcolor="#ffffe6">';
  285:         $Str .= '<a href="/adm/statistics?reportSelected=';
  286:         $Str .= &Apache::lonnet::escape('Problem Statistics');
  287:         $Str .= '&ProblemStatisticsSort=';
  288:         $Str .= &Apache::lonnet::escape($_).'">'.$_.'</a>&nbsp</th>'."\n";
  289:     }
  290:     $Str .= "\n".'</tr>'."\n";    
  291: 
  292:     return $Str;
  293: }
  294: 
  295: sub BuildStatisticsTable {
  296:     my ($cache,$displayFormat,$sortProblems,$orderedProblems,$headings,
  297:         $r,$color)=@_;
  298: 
  299:     my $count = 1;
  300:     my $currentSequence = -1;
  301:     foreach(@$orderedProblems) {
  302:         my ($sequence,$problem,$part)=split(':', $_);
  303:         if($cache->{'StatisticsMaps'} ne 'All Maps'  &&
  304:            $cache->{'StatisticsMaps'} ne $cache->{$sequence.':title'}) {
  305:             next;
  306:         }
  307: 
  308:         if($currentSequence == -1 || 
  309:            ($sortProblems eq 'Sort Within Sequence' && 
  310:             $currentSequence != $sequence)) {
  311:             if($displayFormat ne 'Display CSV Format') {
  312:                 if($currentSequence ne -1) {
  313:                     $r->print('</table>');
  314:                     $r->print('</td></tr></table><br>');
  315:                 }
  316:                 if($sortProblems eq 'Sort Within Sequence') {
  317:                     $r->print('<b>'.$cache->{$sequence.':title'}.'</b>');
  318:                 }
  319:                 $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
  320:                 $r->print('<table border="0" cellpadding="3">'."\n");
  321:                 $r->print(&CreateProblemStatisticsTableHeading($headings, $r));
  322:             } else {
  323:                 if($sortProblems eq 'Sort Within Sequence') {
  324:                     $r->print('"'.$cache->{$sequence.':title'}.'"');
  325:                 }
  326:                 $r->print('<br>');
  327:             }
  328:             $currentSequence = $sequence;
  329:         }
  330: 
  331:         my $ref = '<a href="'.$cache->{$problem.':source'}.
  332:                   '" target="_blank">'.$cache->{$problem.':title'}.'</a>';
  333:         my $title = $cache->{$problem.':title'};
  334:         if($part != 0) {
  335:             $title .= ' Part '.$part;
  336:         }
  337:         my $source = $cache->{$problem.':source'};
  338:         my $tableData = join('&', $ref, $title, $source,
  339:                        $cache->{$_.':studentCount'},
  340:                        $cache->{$_.':totalTries'},
  341:                        $cache->{$_.':maxTries'},
  342:                        $cache->{$_.':mean'},
  343:                        $cache->{$_.':correct'},
  344:                        $cache->{$_.':correctByOverride'},
  345:                        $cache->{$_.':percentWrong'},
  346:                        $cache->{$_.':degreeOfDifficulty'},
  347:                        $cache->{$_.':standardDeviation'},
  348:                        $cache->{$_.':skewness'},
  349:                        $cache->{$_.':discriminationFactor1'},
  350:                        $cache->{$_.':discriminationFactor2'});
  351: 
  352:         &TableRow($displayFormat,$tableData,$count,$r,$color);
  353: 
  354:         $count++;
  355:     }
  356:     if($displayFormat ne 'Display CSV Format') {
  357:         $r->print('</table>'."\n");
  358:         $r->print('</td></tr></table>');
  359:     } else {
  360:         $r->print('<br>');
  361:     }
  362: 
  363:     return;
  364: }
  365: 
  366: sub TableRow {
  367:     my ($displayFormat,$Str,$RealIdx,$r,$color)=@_;
  368:     my($ref,$title,$source,$StdNo,$TotalTries,$MxTries,$Avg,$YES,$Override,
  369:        $Wrng,$DoD,$SD,$Sk,$_D1,$_D2)=split(/\&/,$Str);	
  370:     my $Ptr;
  371:     if($displayFormat eq 'Display CSV Format') {
  372:         $Ptr='"'.$RealIdx.'",'."\n".
  373:              '"'.$title.'",'."\n".
  374:              '"'.$source.'",'."\n".
  375:              '"'.$StdNo.'",'."\n".
  376:              '"'.$TotalTries.'",'."\n".
  377:              '"'.$MxTries.'",'."\n".
  378:              '"'.$Avg.'",'."\n".
  379:              '"'.$YES.'",'."\n".
  380:              '"'.$Override.'",'."\n".
  381:              '"'.$Wrng.'",'."\n".
  382:              '"'.$DoD.'",'."\n".
  383:              '"'.$SD.'",'."\n".
  384:              '"'.$Sk.'",'."\n".
  385:              '"'.$_D1.'",'."\n".
  386:              '"'.$_D2.'"'."\n".
  387:              "<br>\n";
  388: 
  389:         $r->print("\n".$Ptr);
  390:     } else {
  391:         $Ptr='<tr>'."\n".
  392:              '<td bgcolor="#ffffe6">'.$RealIdx.'</td>'."\n".
  393:              '<td bgcolor="#ffffe6">'.$ref.'</td>'."\n".
  394:              '<td bgcolor='.$color->{"yellow"}.'> '.$StdNo.'</td>'."\n".
  395:              '<td bgcolor='.$color->{"yellow"}.'>'.$TotalTries.'</td>'."\n".
  396:              '<td bgcolor='.$color->{"yellow"}.'>'.$MxTries.'</td>'."\n".
  397:              '<td bgcolor='.$color->{"gb"}.'>'.$Avg.'</td>'."\n".
  398:              '<td bgcolor='.$color->{"gb"}.'> '.$YES.'</td>'."\n".
  399:              '<td bgcolor='.$color->{"gb"}.'> '.$Override.'</td>'."\n".
  400:              '<td bgcolor='.$color->{"red"}.'> '.$Wrng.'</td>'."\n".
  401:              '<td bgcolor='.$color->{"red"}.'> '.$DoD.'</td>'."\n".
  402:              '<td bgcolor='.$color->{"green"}.'> '.$SD.'</td>'."\n".
  403:              '<td bgcolor='.$color->{"green"}.'> '.$Sk.'</td>'."\n".
  404:              '<td bgcolor='.$color->{"purple"}.'> '.$_D1.'</td>'."\n".
  405: 	     '<td bgcolor='.$color->{"purple"}.'> '.$_D2.'</td>'."\n";
  406:         $r->print($Ptr.'</tr>'."\n");
  407:     }
  408: 
  409:     return;
  410: }
  411: 
  412: # For loading the colored table for display or un-colored for print
  413: sub setbgcolor {
  414:     my $PrintTable=shift;
  415:     my %color;
  416:     if ($PrintTable){
  417: 	$color{"gb"}="#FFFFFF";
  418: 	$color{"red"}="#FFFFFF";
  419: 	$color{"yellow"}="#FFFFFF";
  420: 	$color{"green"}="#FFFFFF";
  421: 	$color{"purple"}="#FFFFFF";
  422:     } else {
  423: 	$color{"gb"}="#DDFFFF";
  424: 	$color{"red"}="#FFDDDD";
  425: 	$color{"yellow"}="#EEFFCC";
  426: 	$color{"green"}="#DDFFDD";
  427: 	$color{"purple"}="#FFDDFF";
  428:     }
  429: 
  430:     return \%color;
  431: }
  432: 
  433: sub ProblemStatisticsButtons {
  434:     my ($displayFormat, $displayLegend, $sortProblems)=@_;
  435: 
  436:     my $Ptr = '<tr><td></td><td align="left">';
  437:     $Ptr .= '<input type="submit" name="DoDiffGraph" ';
  438:     $Ptr .= 'value="Degree of Difficulty" />'."\n";
  439:     $Ptr .= '</td><td align="left">';
  440:     $Ptr .= '<input type="submit" name="PercentWrongGraph" ';
  441:     $Ptr .= 'value="Percent Wrong" />'."\n";
  442:     $Ptr .= '</td></tr><tr><td></td><td>'."\n";
  443:     $Ptr .= '<input type="submit" name="SortProblems" ';
  444:     if($sortProblems eq 'Sort All Problems') {
  445:         $Ptr .= 'value="Sort Within Sequence" />'."\n";
  446:     } else {
  447:         $Ptr .= 'value="Sort All Problems" />'."\n";
  448:     }
  449:     $Ptr .= '</td><td align="left">';
  450:     $Ptr .= '<input type="submit" name="DisplayLegend" ';
  451:     if($displayLegend eq 'Show Legend') {
  452:         $Ptr .= 'value="Hide Legend" />'."\n";
  453:     } else {
  454:         $Ptr .= 'value="Show Legend" />'."\n";
  455:     }
  456:     $Ptr .= '</td><td align="left">';
  457:     $Ptr .= '<input type="submit" name="DisplayCSVFormat" ';
  458:     if($displayFormat eq 'Display CSV Format') {
  459:         $Ptr .= 'value="Display Table Format" />'."\n";
  460:     } else {
  461:         $Ptr .= 'value="Display CSV Format" />'."\n";
  462:     }
  463:     $Ptr .= '</td></tr>';
  464: 
  465:     return $Ptr;
  466: }
  467: 
  468: sub ProblemStatisticsLegend {
  469:     my $Ptr = '';
  470:     $Ptr = '<table border="0">';
  471:     $Ptr .= '<tr><td>';
  472:     $Ptr .= '<b>#Stdnts</b></td>';
  473:     $Ptr .= '<td>Total number of students attempted the problem.';
  474:     $Ptr .= '</td></tr><tr><td>';
  475:     $Ptr .= '<b>Tries</b></td>';
  476:     $Ptr .= '<td>Total number of tries for solving the problem.';
  477:     $Ptr .= '</td></tr><tr><td>';
  478:     $Ptr .= '<b>Mod</b></td>';
  479:     $Ptr .= '<td>Largest number of tries for solving the problem by a student.';
  480:     $Ptr .= '</td></tr><tr><td>';
  481:     $Ptr .= '<b>Mean</b></td>';
  482:     $Ptr .= '<td>Average number of tries. [ Tries / #Stdnts ]';
  483:     $Ptr .= '</td></tr><tr><td>';
  484:     $Ptr .= '<b>#YES</b></td>';
  485:     $Ptr .= '<td>Number of students solved the problem correctly.';
  486:     $Ptr .= '</td></tr><tr><td>';
  487:     $Ptr .= '<b>#yes</b></td>';
  488:     $Ptr .= '<td>Number of students solved the problem by override.';
  489:     $Ptr .= '</td></tr><tr><td>';
  490:     $Ptr .= '<b>%Wrong</b></td>';
  491:     $Ptr .= '<td>Percentage of students who tried to solve the problem ';
  492:     $Ptr .= 'but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]';
  493:     $Ptr .= '</td></tr><tr><td>';
  494:     $Ptr .= '<b>DoDiff</b></td>';
  495:     $Ptr .= '<td>Degree of Difficulty of the problem.  ';
  496:     $Ptr .= '[ 1 - ((#YES+#yes) / Tries) ]';
  497:     $Ptr .= '</td></tr><tr><td>';
  498:     $Ptr .= '<b>S.D.</b></td>';
  499:     $Ptr .= '<td>Standard Deviation of the tries.  ';
  500:     $Ptr .= '[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) ';
  501:     $Ptr .= 'where Xi denotes every student\'s tries ]';
  502:     $Ptr .= '</td></tr><tr><td>';
  503:     $Ptr .= '<b>Skew.</b></td>';
  504:     $Ptr .= '<td>Skewness of the students tries.';
  505:     $Ptr .= '[(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]';
  506:     $Ptr .= '</td></tr><tr><td>';
  507:     $Ptr .= '<b>Dis.F.</b></td>';
  508:     $Ptr .= '<td>Discrimination Factor: A Standard for evaluating the ';
  509:     $Ptr .= 'problem according to a Criterion<br>';
  510:     $Ptr .= '<b>[Applied Criterion in %27 Upper Students - ';
  511:     $Ptr .= 'Applied the same Criterion in %27 Lower Students]</b><br>';
  512:     $Ptr .= '<b>1st Criterion</b> for Sorting the Students: ';
  513:     $Ptr .= '<b>Sum of Partial Credit Awarded / Total Number of Tries</b><br>';
  514:     $Ptr .= '<b>2nd Criterion</b> for Sorting the Students: ';
  515:     $Ptr .= '<b>Total number of Correct Answers / Total Number of Tries</b>';
  516:     $Ptr .= '</td></tr>';
  517:     $Ptr .= '<tr><td><b>Disc.</b></td>';
  518:     $Ptr .= '<td>Number of Students had at least one discussion.';
  519:     $Ptr .= '</td></tr></table>';
  520: 
  521:     return $Ptr;
  522: }
  523: 
  524: sub ExtractStudentData {
  525:     my ($cache, $students)=@_;
  526: 
  527:     my @problemList=();
  528:     my %problemData;
  529:     foreach my $sequence (split(':', $cache->{'orderedSequences'})) {
  530:         foreach my $problemID (split(':', $cache->{$sequence.':problems'})) {
  531:             foreach my $part (split(/\:/,$cache->{$sequence.':'.
  532:                                                   $problemID.
  533:                                                   ':parts'})) {
  534:                 my $id = $sequence.':'.$problemID.':'.$part;
  535:                 push(@problemList, $id);
  536:                 my $totalTries = 0;
  537:                 my $totalAwarded = 0;
  538:                 my $correct = 0;
  539:                 my $correctByOverride = 0;
  540:                 my $studentCount = 0;
  541:                 my $maxTries = 0;
  542:                 my $totalFirst = 0;
  543:                 my @studentTries=();
  544:                 foreach(@$students) {
  545:                     my $code = $cache->{"$_:$problemID:$part:code"};
  546: 
  547:                     if(defined($cache->{$_.':error'}) || $code eq ' ' ||
  548:                        $cache->{"$_:$problemID:NoVersion"} eq 'true') {
  549:                         next;
  550:                     }
  551: 
  552:                     $studentCount++;
  553:                     my $tries =  $cache->{"$_:$problemID:$part:tries"};
  554:                     if($maxTries < $tries) {
  555:                         $maxTries = $tries;
  556:                     }
  557:                     $totalTries += $tries;
  558:                     push(@studentTries, $tries);
  559: 
  560:                     my $awarded = $cache->{"$_:$problemID:$part:awarded"};
  561:                     $totalAwarded += $awarded;
  562: 
  563:                     if($code eq '*') {
  564:                         $correct++;
  565:                         if($tries == 1) {
  566:                             $totalFirst++;
  567:                         }
  568:                     } elsif($code eq '+') {
  569:                         $correctByOverride++;
  570:                     }
  571:                 }
  572: 
  573:                 my $studentTriesJoined = join(':::', @studentTries);
  574:                 $problemData{$id.':sequenceTitle'} = 
  575:                     $cache->{$sequence.':title'};
  576:                 $problemData{$id.':studentCount'} = $studentCount;
  577:                 $problemData{$id.':totalTries'} = $totalTries;
  578:                 $problemData{$id.':studentTries'} = $studentTriesJoined;
  579:                 $problemData{$id.':totalAwarded'} = $totalAwarded;
  580:                 $problemData{$id.':correct'} = $correct;
  581:                 $problemData{$id.':correctByOverride'} = $correctByOverride;
  582:                 $problemData{$id.':wrong'} = $studentCount - 
  583:                                              ($correct + $correctByOverride);
  584:                 $problemData{$id.':maxTries'} = $maxTries;
  585:                 $problemData{$id.':totalFirst'} = $totalFirst;
  586:             }
  587:         }
  588:     }
  589: 
  590:     my @upperStudents1=();
  591:     my @lowerStudents1=();
  592:     my @upperStudents2=();
  593:     my @lowerStudents2=();
  594:     my $upperCount = int(0.27*scalar(@$students));
  595:     # Discriminant Factor criterion 1
  596:     my $sortedStudents = &SortDivideByTries($students,$cache,':totalAwarded');
  597: 
  598:     for(my $i=0; $i<$upperCount; $i++) {
  599:         push(@lowerStudents1, $sortedStudents->[$i]);
  600:         push(@upperStudents1, $sortedStudents->[(scalar(@$students)-$i-1)]);
  601:     }
  602: 
  603:     $problemData{'studentsUpperListCriterion1'}=join(':::', @upperStudents1);
  604:     $problemData{'studentsLowerListCriterion1'}=join(':::', @lowerStudents1);
  605: 
  606:     # Discriminant Factor criterion 2
  607:     $sortedStudents = &SortDivideByTries($students, $cache, ':totalSolved');
  608: 
  609:     for(my $i=0; $i<$upperCount; $i++) {
  610:         push(@lowerStudents2, $sortedStudents->[$i]);
  611:         push(@upperStudents2, $sortedStudents->[(scalar(@$students)-$i-1)]);
  612:     }
  613:     $problemData{'studentsUpperListCriterion2'}=join(':::', @upperStudents2);
  614:     $problemData{'studentsLowerListCriterion2'}=join(':::', @lowerStudents2);
  615: 
  616:     $problemData{'problemList'} = join(':::', @problemList);
  617: 
  618:     return \%problemData;
  619: }
  620: 
  621: sub SortDivideByTries {
  622:     my ($toSort, $data, $sortOn)=@_;
  623:     my @orderedData = sort { ($data->{$a.':totalTries'}) ? 
  624:                              ($data->{$a.$sortOn}/$data->{$a.':totalTries'}):0
  625:                              <=>
  626:                              ($data->{$b.':totalTries'}) ? 
  627:                              ($data->{$b.$sortOn}/$data->{$b.':totalTries'}):0
  628:                            } @$toSort;
  629: 
  630:     return \@orderedData;
  631: }
  632: 
  633: sub SortProblems {
  634:     my ($problemData,$sortBy,$sortProblems,$ascend)=@_;
  635: 
  636:     my @problems = split(':::', $problemData->{'problemList'});
  637:     if($sortBy eq "Homework Sets Order") {
  638:         return \@problems;
  639:     }
  640: 
  641:     my $data;
  642: 
  643:     if   ($sortBy eq "#Stdnts") { $data = ':studentCount'; }
  644:     elsif($sortBy eq "Tries")   { $data = ':totalTries'; }
  645:     elsif($sortBy eq "Mod")     { $data = ':maxTries'; }
  646:     elsif($sortBy eq "Mean")    { $data = ':mean'; }
  647:     elsif($sortBy eq "#YES")    { $data = ':correct'; }
  648:     elsif($sortBy eq "#yes")    { $data = ':correctByOverride'; }
  649:     elsif($sortBy eq "%Wrng")   { $data = ':percentWrong'; }
  650:     elsif($sortBy eq "DoDiff")  { $data = ':degreeOfDifficulty'; }
  651:     elsif($sortBy eq "S.D.")    { $data = ':standardDeviation'; }
  652:     elsif($sortBy eq "Skew.")   { $data = ':skewness'; }
  653:     elsif($sortBy eq "D.F.1st") { $data = ':discriminationFactor1'; }
  654:     elsif($sortBy eq "D.F.2nd") { $data = ':discriminationFactor2'; }
  655:     else                        { return \@problems; }
  656: 
  657:     my %temp;
  658:     my @sequenceList=();
  659:     foreach(@problems) {
  660:         my ($sequence) = split(':', $_);
  661: 
  662:         my @array=();
  663:         my $tempArray;
  664:         if(defined($temp{$sequence})) {
  665:             $tempArray = $temp{$sequence};
  666:         } else {
  667:             push(@sequenceList, $sequence);
  668:             $tempArray = \@array;
  669:             $temp{$sequence} = $tempArray;
  670:         }
  671: 
  672:         push(@$tempArray, $_);
  673:     }
  674: 
  675:     my @orderedProblems;
  676:     if($sortProblems eq "Sort Within Sequence") {
  677:         foreach(keys(%temp)) {
  678:             my $tempArray = $temp{$_};
  679:             my @tempOrder =
  680:                 sort { $problemData->{$a.$data} <=> $problemData->{$b.$data} }
  681:             @$tempArray;
  682:             $temp{$_} = \@tempOrder;
  683:         }
  684:         foreach(@sequenceList) {
  685:             my $tempArray = $temp{$_};
  686:             @orderedProblems = (@orderedProblems, @$tempArray);
  687:         }
  688:     } else {
  689:         @orderedProblems = 
  690:             sort { $problemData->{$a.$data} <=> $problemData->{$b.$data} }
  691:         @problems;
  692:     }
  693: 
  694:     if($ascend eq 'Descending') {
  695:         @orderedProblems = reverse(@orderedProblems);
  696:     }
  697: 
  698:     return \@orderedProblems;
  699: }
  700: 
  701: sub CalculateStatistics {
  702:     my ($data, $cache)=@_;
  703: 
  704:     my @problems = split(':::', $data->{'problemList'});
  705:     foreach(@problems) {
  706:         # Mean
  707:         my $mean = ($data->{$_.':studentCount'}) ? 
  708:             ($data->{$_.':totalTries'} / $data->{$_.':studentCount'}) : 0;
  709:         $data->{$_.':mean'} = sprintf("%.2f", $mean);
  710: 
  711:         # %Wrong
  712:         my $pw = ($data->{$_.':studentCount'}) ?
  713:             (($data->{$_.':wrong'} / $data->{$_.':studentCount'}) * 100.0) : 
  714:             100.0;
  715:         $data->{$_.':percentWrong'} = sprintf("%.1f", $pw);
  716: 
  717:         # Degree of Difficulty
  718:         my $dod = ($data->{$_.':totalTries'}) ?
  719:             (1 - (($data->{$_.':correct'} + $data->{$_.':correctByOverride'}) /
  720:                   $data->{$_.':totalTries'})) : 0;
  721: 
  722:         $data->{$_.':degreeOfDifficulty'} = sprintf("%.2f", $dod);
  723: 
  724:         # Factor in mean
  725:         my @studentTries = split(':::', $data->{$_.':studentTries'});
  726:         foreach(my $index=0; $index < scalar(@studentTries); $index++) {
  727:             $studentTries[$index] -= $mean;
  728:         }
  729:         my $sumSquared = 0;
  730:         my $sumCubed = 0;
  731:         foreach(@studentTries) {
  732:             my $squared = ($_ * $_);
  733:             my $cubed = ($squared * $_);
  734:             $sumSquared += $squared;
  735:             $sumCubed += $cubed;
  736:         }
  737: 
  738:         # Standard deviation
  739:         my $standardDeviation;
  740:         if($data->{$_.':studentCount'} - 1 > 0) {
  741:             $standardDeviation = (sqrt($sumSquared)) / 
  742:                                  ($data->{$_.':studentCount'} - 1);
  743:         } else {
  744:             $standardDeviation =  0.0;
  745:         }
  746:         $data->{$_.':standardDeviation'} = sprintf("%.1f", $standardDeviation);
  747: 
  748:         # Skewness
  749:         my $skew;
  750:         if($standardDeviation > 0.0999 && $data->{$_.':studentCount'} > 0) {
  751:             $skew = (((sqrt($sumSquared)) / $data->{$_.':studentCount'}) / 
  752:                      ($standardDeviation * 
  753:                       $standardDeviation * 
  754:                       $standardDeviation));
  755:         } else {
  756:             $skew = 0.0;
  757:         }
  758: 
  759:         $data->{$_.':skewness'} = sprintf("%.1f", $skew);
  760: 
  761:         # Discrimination Factor 1
  762:         my ($sequence, $problem, $part) = split(':', $_);
  763: 
  764:         my @upper1 = split(':::', $data->{'studentsUpperListCriterion1'});
  765:         my @lower1 = split(':::', $data->{'studentsLowerListCriterion1'});
  766: 
  767:         my $upper1Sum=0;
  768:         foreach my $name (@upper1) {
  769:             $upper1Sum += $cache->{"$name:$problem:$part:awarded"};
  770:         }
  771:         $upper1Sum = (scalar(@upper1)) ? ($upper1Sum/(scalar(@upper1))) : 0;
  772: 
  773:         my $lower1Sum=0;
  774:         foreach my $name (@lower1) {
  775:             $lower1Sum += $cache->{"$name:$problem:$part:awarded"};
  776:         }
  777:         $lower1Sum = (scalar(@lower1)) ? ($lower1Sum/(scalar(@lower1))) : 0;
  778: 
  779:         my $df1 = $upper1Sum - $lower1Sum;
  780:         $data->{$_.':discriminationFactor1'} = sprintf("%.2f", $df1);
  781: 
  782:         # Discrimination Factor 2
  783:         my @upper2 = split(':::', $data->{'studentsUpperListCriterion2'});
  784:         my @lower2 = split(':::', $data->{'studentsLowerListCriterion2'});
  785: 
  786:         my $upper2Sum=0;
  787:         foreach my $name (@upper2) {
  788:             $upper2Sum += $cache->{"$name:$problem:$part:awarded"};
  789:         }
  790:         $upper2Sum = (scalar(@upper2)) ? ($upper2Sum/(scalar(@upper2))) : 0;
  791: 
  792:         my $lower2Sum=0;
  793:         foreach my $name (@lower2) {
  794:             $lower2Sum += $cache->{"$name:$problem:$part:awarded"};
  795:         }
  796:         $lower2Sum = (scalar(@lower2)) ? ($lower2Sum/(scalar(@lower2))) : 0;
  797: 
  798:         my $df2 = $upper2Sum - $lower2Sum;
  799:         $data->{$_.':discriminationFactor2'} = sprintf("%.2f", $df2);
  800:     }
  801: 
  802:     return;
  803: }
  804: 
  805: #---- END Problem Statistics Web Page ----------------------------------------
  806: 
  807: 1;
  808: __END__

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