Annotation of loncom/interface/lonquickgrades.pm, revision 1.51

1.1       bowersj2    1: # The LearningOnline Network with CAPA
                      2: # Quick Student Grades Display
                      3: #
1.51    ! www         4: # $Id: lonquickgrades.pm,v 1.50 2010/11/27 20:51:26 www Exp $
1.1       bowersj2    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: 
                     29: package Apache::lonquickgrades;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common :http);
1.5       bowersj2   33: use POSIX;
1.25      www        34: use Apache::loncommon;
                     35: use Apache::lonlocal;
1.36      albertel   36: use Apache::lonnet;
1.38      bowersj2   37: use Apache::grades;
1.1       bowersj2   38: 
                     39: sub handler {
                     40:     my $r = shift;
1.5       bowersj2   41:     return real_handler($r);
                     42: }
                     43: 
                     44: sub real_handler {
                     45:     my $r = shift;
1.1       bowersj2   46: 
                     47:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
                     48: 
                     49:     # Handle header-only request
1.40      albertel   50:     if ($env{'browser.mathml'}) {
                     51: 	&Apache::loncommon::content_type($r,'text/xml');
                     52:     } else {
                     53: 	&Apache::loncommon::content_type($r,'text/html');
                     54:     }
1.1       bowersj2   55:     if ($r->header_only) {
1.40      albertel   56: 	$r->send_http_header;
1.1       bowersj2   57:         return OK;
                     58:     }
                     59: 
                     60:     # Send header, don't cache this page
                     61:     &Apache::loncommon::no_cache($r);
                     62:     $r->send_http_header;
                     63: 
1.22      matthew    64:     my $showPoints = 
1.36      albertel   65:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
1.29      albertel   66:     my $notshowSPRSlink = 
1.49      www        67:         (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
                     68:       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals'));
                     69:     my $notshowTotals=
                     70:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
1.50      www        71:     my $showCategories=
                     72:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
                     73: 
1.17      bowersj2   74: 
1.1       bowersj2   75:     # Header
1.47      schafran   76:     my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
1.46      raeburn    77:     my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
                     78:     $r->print(&Apache::loncommon::start_page($title,undef,
                     79:                                             {'bread_crumbs' => $brcrum})
                     80:              );
1.1       bowersj2   81: 
1.29      albertel   82:     if (!$showPoints && !$notshowSPRSlink ) {
1.43      bisitz     83:         $r->print('<p>'
                     84:                  .&mt('This screen shows how many problems (or problem parts) you have completed'
                     85:                      .', and how many you have not yet done.'
                     86:                      .' You can also look at [_1]a detailed score sheet[_2].'
                     87:                      ,'<a href="/adm/studentcalc">','</a>')
                     88:                  .'</p>');
1.17      bowersj2   89:     }
                     90: 
1.43      bisitz     91:     $r->print('<p class="LC_info">'.&mt('This may take a few moments to display.').'</p>');
1.17      bowersj2   92: 
                     93:     $r->rflush();
1.51    ! www        94:     my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=&getData($showPoints);
        !            95:     &outputTable($r,$showPoints,$notshowTotals,
        !            96:                  $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
        !            97:     return OK;
        !            98: 
        !            99: }
1.2       bowersj2  100: 
1.51    ! www       101: sub getData {
        !           102: 
        !           103:     my ($showPoints)=@_;
        !           104: 
        !           105:     # Create the nav map
        !           106:     my $navmap = Apache::lonnavmaps::navmap->new();
        !           107: 
        !           108:     my $res = $navmap->firstResource(); # temp resource to access constants
1.1       bowersj2  109: 
                    110:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
                    111:     my $depth = 1;
                    112:     $iterator->next(); # ignore first BEGIN_MAP
                    113:     my $curRes = $iterator->next();
1.4       bowersj2  114:     
1.5       bowersj2  115:     # General overview of the following: Walk along the course resources.
                    116:     # For every problem in the resource, tell its parent maps how many
                    117:     # parts and how many parts correct it has. After that, each map will
                    118:     # have a count of the total parts underneath it, correct and otherwise.
                    119:     # After that, we will walk through the course again and read off
                    120:     # maps in order, with their data. 
                    121:     # (If in the future people decide not to be cumulative, only add
                    122:     #  the counts to the parent map.)
1.17      bowersj2  123:     # For convenience, "totalParts" is also "totalPoints" when we're looking
                    124:     #  at points; I can't come up with a variable name that makes sense
                    125:     #  equally for both cases.
1.5       bowersj2  126: 
                    127:     my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
1.28      bowersj2  128:     my $totalAttempted = 0;
1.14      bowersj2  129:     my $now = time();
1.28      bowersj2  130:     my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
1.5       bowersj2  131: 
                    132:     # Pre-run: Count parts correct
1.1       bowersj2  133:     while ( $depth > 0 ) {
                    134:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    135:         if ($curRes == $iterator->END_MAP()) { $depth--; }
                    136: 
1.7       bowersj2  137:         if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
1.5       bowersj2  138:         {
                    139:             # Get number of correct, incorrect parts
                    140:             my $parts = $curRes->parts();
                    141:             my $partsRight = 0;
1.17      bowersj2  142: 	    my $partsCount = 0;
1.28      bowersj2  143: 	    my $partsAttempted = 0;
1.5       bowersj2  144:             my $stack = $iterator->getStack();
                    145:             
                    146:             for my $part (@{$parts}) {
1.28      bowersj2  147: 		my $completionStatus = $curRes->getCompletionStatus($part);
                    148: 		my $dateStatus = $curRes->getDateStatus($part);
                    149: 		
                    150:                 if ($completionStatus == $curRes->EXCUSED()) {
1.21      matthew   151:                     next;
                    152:                 }
1.17      bowersj2  153: 		if ($showPoints) {
1.28      bowersj2  154: 		    my $score = 0;
                    155: 		    # If we're not telling status and the answer date isn't passed yet, 
                    156: 		    # it's an "attempted" point
1.42      raeburn   157: 		    if ((($curRes->problemstatus($part) eq 'no') ||
                    158:                         ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
1.28      bowersj2  159: 			($dateStatus != $curRes->ANSWER_OPEN)) {
1.31      albertel  160: 			my $status = $curRes->simpleStatus($part);
                    161: 			if ($status == $curRes->ATTEMPTED) {
                    162: 			    $partsAttempted += $curRes->weight($part);
                    163: 			    $totalAttempted += $partsAttempted;
                    164: 			}
1.28      bowersj2  165: 		    } else {
1.39      albertel  166: 			$score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
1.28      bowersj2  167: 		    }
1.17      bowersj2  168: 		    $partsRight += $score;
                    169: 		    $totalRight += $score;
1.19      bowersj2  170: 		    $partsCount += $curRes->weight($part);
1.18      bowersj2  171: 
1.17      bowersj2  172: 		    if ($curRes->opendate($part) < $now) {
1.20      bowersj2  173: 			$totalPossible += $curRes->weight($part);
1.17      bowersj2  174: 		    }
1.19      bowersj2  175: 		    $totalParts += $curRes->weight($part);
1.17      bowersj2  176: 		} else {
1.27      bowersj2  177: 		    my $status = $curRes->simpleStatus($part);
1.17      bowersj2  178: 		    my $thisright = 0;
                    179: 		    $partsCount++;
1.37      albertel  180: 		    if ($status == $curRes->CORRECT ||
                    181: 			$status == $curRes->PARTIALLY_CORRECT ) {
1.17      bowersj2  182: 			$partsRight++;
                    183: 			$totalRight++;
                    184: 			$thisright = 1;
                    185: 		    }
1.28      bowersj2  186: 
                    187: 		    if ($status == $curRes->ATTEMPTED) {
                    188: 			$partsAttempted++;
                    189: 			$totalAttempted++;
                    190: 		    }
1.17      bowersj2  191: 		    
                    192: 		    my $dateStatus = $curRes->getDateStatus($part);
1.19      bowersj2  193: 		    $totalParts++;
1.17      bowersj2  194: 		    if ($curRes->opendate($part) < $now) {
                    195: 			$totalPossible++;
                    196: 		    }
                    197: 		}
1.5       bowersj2  198:             }
1.15      bowersj2  199: 
                    200:             if ($depth == 1) { # in top-level only
1.19      bowersj2  201: 		$topLevelParts += $partsCount;
1.15      bowersj2  202: 		$topLevelRight += $partsRight;
1.28      bowersj2  203: 		$topLevelAttempted += $partsAttempted;
1.15      bowersj2  204: 	    }
                    205: 
1.5       bowersj2  206:             # Crawl down stack and record parts correct and total
                    207:             for my $res (@{$stack}) {
                    208:                 if (ref($res) && $res->is_map()) {
                    209:                     if (!defined($res->{DATA}->{CHILD_PARTS})) {
                    210:                         $res->{DATA}->{CHILD_PARTS} = 0;
                    211:                         $res->{DATA}->{CHILD_CORRECT} = 0;
1.28      bowersj2  212: 			$res->{DATA}->{CHILD_ATTEMPTED} = 0;
1.5       bowersj2  213:                     }
                    214:                     
1.17      bowersj2  215:                     $res->{DATA}->{CHILD_PARTS} += $partsCount;
1.5       bowersj2  216:                     $res->{DATA}->{CHILD_CORRECT} += $partsRight;
1.28      bowersj2  217: 		    $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
1.5       bowersj2  218:                 }
                    219:             }
                    220:         }
                    221:         $curRes = $iterator->next();
                    222:     }
1.51    ! www       223:     return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
        !           224: }
        !           225: 
        !           226: #
        !           227: # Outputting everything.
        !           228: #
        !           229: 
        !           230: sub outputTable {
1.5       bowersj2  231: 
1.51    ! www       232:     my ($r,$showPoints,$notshowTotals,
        !           233:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
1.5       bowersj2  234: 
1.7       bowersj2  235:     my @start = (255, 255, 192);
1.5       bowersj2  236:     my @end   = (0, 192, 0);
                    237: 
                    238:     my $indentString = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                    239: 
                    240:     # Second pass: Print the maps.
1.43      bisitz    241:     $r->print(&Apache::loncommon::start_data_table()
                    242:              .&Apache::loncommon::start_data_table_header_row()
                    243:              .'<th>'.&mt('Folder').'</th>');
1.51    ! www       244:     my $title = &mt($showPoints ? "Points Scored" : "Done");
1.28      bowersj2  245:     if ($totalAttempted) {
1.51    ! www       246:         $title .= " / " . &mt("Attempted");
1.28      bowersj2  247:     }
1.49      www       248:     $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
1.43      bisitz    249:              .&Apache::loncommon::end_data_table_header_row());
1.51    ! www       250: #
        !           251: # Output of folder scores
        !           252: #
        !           253: 
        !           254:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
        !           255:     my $depth = 1;
        !           256:     $iterator->next(); # ignore first BEGIN_MAP
        !           257:     my $curRes = $iterator->next();
        !           258: 
1.5       bowersj2  259:     while ($depth > 0) {
                    260:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    261:         if ($curRes == $iterator->END_MAP()) { $depth--; }
                    262: 
                    263:         if (ref($curRes) && $curRes->is_map()) {
                    264:             my $title = $curRes->compTitle();
                    265:             
                    266:             my $correct = $curRes->{DATA}->{CHILD_CORRECT};
                    267:             my $total = $curRes->{DATA}->{CHILD_PARTS};
1.28      bowersj2  268: 	    my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
1.5       bowersj2  269: 
1.6       bowersj2  270:             if ($total > 0) {
                    271:                 my $ratio;
                    272:                 $ratio = $correct / $total;
1.51    ! www       273:                 my $color = &mixColors(\@start, \@end, $ratio);
1.43      bisitz    274:                 $r->print(&Apache::loncommon::start_data_table_row()
                    275:                          .'<td style="background-color:'.$color.';">');
1.6       bowersj2  276:                 
1.15      bowersj2  277: 		my $thisIndent = '';
                    278:                 for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
1.6       bowersj2  279:                 
1.15      bowersj2  280:                 $r->print("$thisIndent$title</td>");
1.28      bowersj2  281: 		if ($totalAttempted) {
1.45      bisitz    282: 		    $r->print('<td valign="top">'
                    283:                              .$thisIndent
                    284:                              .'<span class="LC_nobreak">'
1.49      www       285:                              .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
1.45      bisitz    286:                              .'</span></td>'
                    287:                              .&Apache::loncommon::end_data_table_row()
                    288:                     );
1.28      bowersj2  289: 		} else {
1.45      bisitz    290: 		    $r->print('<td valign="top">'
                    291:                              .$thisIndent
                    292:                              .'<span class="LC_nobreak">'
1.49      www       293:                              .$correct.($notshowTotals?'':' / '.$total)
1.45      bisitz    294:                              .'</span></td>'
1.43      bisitz    295:                              .&Apache::loncommon::end_data_table_row());
1.28      bowersj2  296: 		}
1.6       bowersj2  297:             }
1.5       bowersj2  298:         }
1.4       bowersj2  299: 
1.5       bowersj2  300:         $curRes = $iterator->next();
                    301:     }
1.4       bowersj2  302: 
1.6       bowersj2  303:     # If there were any problems at the top level, print an extra "catchall"
1.15      bowersj2  304:     if ($topLevelParts > 0) {
                    305:         my $ratio = $topLevelRight / $topLevelParts;
1.6       bowersj2  306:         my $color = mixColors(\@start, \@end, $ratio);
1.43      bisitz    307:         $r->print(&Apache::loncommon::start_data_table_row()
                    308:                  .'<td style="background-color:'.$color.';">');
1.25      www       309:         $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
1.43      bisitz    310:         $r->print("$topLevelRight / $topLevelParts</td>"
                    311:                  .&Apache::loncommon::end_data_table_row());
1.6       bowersj2  312:     }
1.4       bowersj2  313: 
1.51    ! www       314: #
        !           315: # show totals (if applicable), close table
        !           316: #
1.35      albertel  317:     if ($showPoints) {
1.51    ! www       318:         my $maxHelpLink = Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
1.2       bowersj2  319: 
1.51    ! www       320:         $title = $showPoints ? "Points" : "Parts Done";
        !           321:         my $totaltitle = $showPoints ? &mt("Awarded Total Points") : &mt("Total Parts Done");
        !           322:         $r->print(&Apache::loncommon::start_data_table_row()
1.48      bisitz    323:                  .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
1.51    ! www       324:         $r->print(&mt('Max Possible To Date')." $maxHelpLink: <b>$totalPossible</b><br />");
        !           325:         $title = $showPoints ? "Points" : "Parts";
        !           326:         $r->print(&mt("Total $title In Course").': <b>'.$totalParts.'</b></td>'
1.43      bisitz    327:                  .&Apache::loncommon::end_data_table_row());
1.34      www       328:     }
1.1       bowersj2  329: 
1.43      bisitz    330:     $r->print(&Apache::loncommon::end_data_table()
                    331:              .&Apache::loncommon::end_page());
1.1       bowersj2  332: 
1.5       bowersj2  333: }
                    334: 
                    335: # Pass this two refs to arrays for the start and end color, and a number
                    336: # from 0 to 1 for how much of the latter you want to mix in. It will
                    337: # return a string ready to show ("#FFC309");
1.51    ! www       338: 
1.5       bowersj2  339: sub mixColors {
                    340:     my $start = shift;
                    341:     my $end = shift;
                    342:     my $ratio = shift;
                    343:     
1.9       matthew   344:     my ($a,$b);
1.5       bowersj2  345:     my $final = "";
1.9       matthew   346:     $a = $start->[0]; $b = $end->[0];
1.5       bowersj2  347:     my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew   348:     $a = $start->[1]; $b = $end->[1];
1.5       bowersj2  349:     my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew   350:     $a = $start->[2]; $b = $end->[2];
1.5       bowersj2  351:     my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
                    352: 
1.16      bowersj2  353:     $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
1.5       bowersj2  354:     return "#" . $final;
1.1       bowersj2  355: }
                    356: 
                    357: 1;

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