File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.37: download - view: text, annotated - select for diffs
Tue Nov 26 16:59:03 2002 UTC (21 years, 7 months ago) by minaeibi
Branches: MAIN
CVS tags: HEAD
cleaning the code

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

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