File:  [LON-CAPA] / loncom / interface / statistics / lonproblemstatistics.pm
Revision 1.38: download - view: text, annotated - select for diffs
Fri Dec 6 21:56:22 2002 UTC (21 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD

- Fixes  BUG#1033 was getting 'Unable to tie database.2' on stats screens

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

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