File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.50: download - view: text, annotated - select for diffs
Sat Nov 27 20:51:26 2010 UTC (13 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
New grading mode: categories

    1: # The LearningOnline Network with CAPA
    2: # Quick Student Grades Display
    3: #
    4: # $Id: lonquickgrades.pm,v 1.50 2010/11/27 20:51:26 www 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: 
   29: package Apache::lonquickgrades;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common :http);
   33: use POSIX;
   34: use Apache::loncommon;
   35: use Apache::lonlocal;
   36: use Apache::lonnet;
   37: use Apache::grades;
   38: 
   39: sub handler {
   40:     my $r = shift;
   41:     return real_handler($r);
   42: }
   43: 
   44: sub real_handler {
   45:     my $r = shift;
   46: 
   47:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
   48: 
   49:     # Handle header-only request
   50:     if ($env{'browser.mathml'}) {
   51: 	&Apache::loncommon::content_type($r,'text/xml');
   52:     } else {
   53: 	&Apache::loncommon::content_type($r,'text/html');
   54:     }
   55:     if ($r->header_only) {
   56: 	$r->send_http_header;
   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: 
   64:     my $showPoints = 
   65:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
   66:     my $notshowSPRSlink = 
   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';
   71:     my $showCategories=
   72:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
   73: 
   74: 
   75:     # Create the nav map
   76:     my $navmap = Apache::lonnavmaps::navmap->new();
   77: 
   78:     if (!defined($navmap)) {
   79:         my $requrl = $r->uri;
   80:         $env{'user.error.msg'} = "$requrl:bre:0:0:Navamp initialization failed.";
   81:         return HTTP_NOT_ACCEPTABLE;
   82:     }
   83: 
   84:     # Keep this hash in sync with %statusIconMap in lonnavmaps; they
   85:     # should match color/icon
   86:     my $res = $navmap->firstResource(); # temp resource to access constants
   87:  
   88:     # Header
   89:     my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
   90:     my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
   91:     $r->print(&Apache::loncommon::start_page($title,undef,
   92:                                             {'bread_crumbs' => $brcrum})
   93:              );
   94: 
   95:     if (!$showPoints && !$notshowSPRSlink ) {
   96:         $r->print('<p>'
   97:                  .&mt('This screen shows how many problems (or problem parts) you have completed'
   98:                      .', and how many you have not yet done.'
   99:                      .' You can also look at [_1]a detailed score sheet[_2].'
  100:                      ,'<a href="/adm/studentcalc">','</a>')
  101:                  .'</p>');
  102:     }
  103: 
  104:     $r->print('<p class="LC_info">'.&mt('This may take a few moments to display.').'</p>');
  105: 
  106:     $r->rflush();
  107: 
  108:     # End navmap using boilerplate
  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();
  114:     
  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.)
  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.
  126: 
  127:     my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
  128:     my $totalAttempted = 0;
  129:     my $now = time();
  130:     my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
  131: 
  132:     # Pre-run: Count parts correct
  133:     while ( $depth > 0 ) {
  134:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
  135:         if ($curRes == $iterator->END_MAP()) { $depth--; }
  136: 
  137:         if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
  138:         {
  139:             # Get number of correct, incorrect parts
  140:             my $parts = $curRes->parts();
  141:             my $partsRight = 0;
  142: 	    my $partsCount = 0;
  143: 	    my $partsAttempted = 0;
  144:             my $stack = $iterator->getStack();
  145:             
  146:             for my $part (@{$parts}) {
  147: 		my $completionStatus = $curRes->getCompletionStatus($part);
  148: 		my $dateStatus = $curRes->getDateStatus($part);
  149: 		
  150:                 if ($completionStatus == $curRes->EXCUSED()) {
  151:                     next;
  152:                 }
  153: 		if ($showPoints) {
  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
  157: 		    if ((($curRes->problemstatus($part) eq 'no') ||
  158:                         ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
  159: 			($dateStatus != $curRes->ANSWER_OPEN)) {
  160: 			my $status = $curRes->simpleStatus($part);
  161: 			if ($status == $curRes->ATTEMPTED) {
  162: 			    $partsAttempted += $curRes->weight($part);
  163: 			    $totalAttempted += $partsAttempted;
  164: 			}
  165: 		    } else {
  166: 			$score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
  167: 		    }
  168: 		    $partsRight += $score;
  169: 		    $totalRight += $score;
  170: 		    $partsCount += $curRes->weight($part);
  171: 
  172: 		    if ($curRes->opendate($part) < $now) {
  173: 			$totalPossible += $curRes->weight($part);
  174: 		    }
  175: 		    $totalParts += $curRes->weight($part);
  176: 		} else {
  177: 		    my $status = $curRes->simpleStatus($part);
  178: 		    my $thisright = 0;
  179: 		    $partsCount++;
  180: 		    if ($status == $curRes->CORRECT ||
  181: 			$status == $curRes->PARTIALLY_CORRECT ) {
  182: 			$partsRight++;
  183: 			$totalRight++;
  184: 			$thisright = 1;
  185: 		    }
  186: 
  187: 		    if ($status == $curRes->ATTEMPTED) {
  188: 			$partsAttempted++;
  189: 			$totalAttempted++;
  190: 		    }
  191: 		    
  192: 		    my $dateStatus = $curRes->getDateStatus($part);
  193: 		    $totalParts++;
  194: 		    if ($curRes->opendate($part) < $now) {
  195: 			$totalPossible++;
  196: 		    }
  197: 		}
  198:             }
  199: 
  200:             if ($depth == 1) { # in top-level only
  201: 		$topLevelParts += $partsCount;
  202: 		$topLevelRight += $partsRight;
  203: 		$topLevelAttempted += $partsAttempted;
  204: 	    }
  205: 
  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;
  212: 			$res->{DATA}->{CHILD_ATTEMPTED} = 0;
  213:                     }
  214:                     
  215:                     $res->{DATA}->{CHILD_PARTS} += $partsCount;
  216:                     $res->{DATA}->{CHILD_CORRECT} += $partsRight;
  217: 		    $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
  218:                 }
  219:             }
  220:         }
  221:         $curRes = $iterator->next();
  222:     }
  223: 
  224:     $iterator = $navmap->getIterator(undef, undef, undef, 1);
  225:     $depth = 1;
  226:     $iterator->next(); # ignore first BEGIN_MAP
  227:     $curRes = $iterator->next();
  228: 
  229:     my @start = (255, 255, 192);
  230:     my @end   = (0, 192, 0);
  231: 
  232:     my $indentString = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  233: 
  234:     # Second pass: Print the maps.
  235:     $r->print(&Apache::loncommon::start_data_table()
  236:              .&Apache::loncommon::start_data_table_header_row()
  237:              .'<th>'.&mt('Folder').'</th>');
  238:     $title = &mt($showPoints ? "Points Scored" : "Done");
  239:     if ($totalAttempted) {
  240: 	$title .= " / " . &mt("Attempted");
  241:     }
  242:     $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
  243:              .&Apache::loncommon::end_data_table_header_row());
  244:     while ($depth > 0) {
  245:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
  246:         if ($curRes == $iterator->END_MAP()) { $depth--; }
  247: 
  248:         if (ref($curRes) && $curRes->is_map()) {
  249:             my $title = $curRes->compTitle();
  250:             
  251:             my $correct = $curRes->{DATA}->{CHILD_CORRECT};
  252:             my $total = $curRes->{DATA}->{CHILD_PARTS};
  253: 	    my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
  254: 
  255:             if ($total > 0) {
  256:                 my $ratio;
  257:                 $ratio = $correct / $total;
  258:                 my $color = mixColors(\@start, \@end, $ratio);
  259:                 $r->print(&Apache::loncommon::start_data_table_row()
  260:                          .'<td style="background-color:'.$color.';">');
  261:                 
  262: 		my $thisIndent = '';
  263:                 for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
  264:                 
  265:                 $r->print("$thisIndent$title</td>");
  266: 		if ($totalAttempted) {
  267: 		    $r->print('<td valign="top">'
  268:                              .$thisIndent
  269:                              .'<span class="LC_nobreak">'
  270:                              .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
  271:                              .'</span></td>'
  272:                              .&Apache::loncommon::end_data_table_row()
  273:                     );
  274: 		} else {
  275: 		    $r->print('<td valign="top">'
  276:                              .$thisIndent
  277:                              .'<span class="LC_nobreak">'
  278:                              .$correct.($notshowTotals?'':' / '.$total)
  279:                              .'</span></td>'
  280:                              .&Apache::loncommon::end_data_table_row());
  281: 		}
  282:             }
  283:         }
  284: 
  285:         $curRes = $iterator->next();
  286:     }
  287: 
  288:     # If there were any problems at the top level, print an extra "catchall"
  289:     if ($topLevelParts > 0) {
  290:         my $ratio = $topLevelRight / $topLevelParts;
  291:         my $color = mixColors(\@start, \@end, $ratio);
  292:         $r->print(&Apache::loncommon::start_data_table_row()
  293:                  .'<td style="background-color:'.$color.';">');
  294:         $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
  295:         $r->print("$topLevelRight / $topLevelParts</td>"
  296:                  .&Apache::loncommon::end_data_table_row());
  297:     }
  298: 
  299:     if ($showPoints) {
  300: 	my $maxHelpLink = Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
  301: 
  302: 	$title = $showPoints ? "Points" : "Parts Done";
  303: 	my $totaltitle = $showPoints ? &mt("Awarded Total Points") : &mt("Total Parts Done");
  304: 	$r->print(&Apache::loncommon::start_data_table_row()
  305:                  .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
  306: 	$r->print(&mt('Max Possible To Date')." $maxHelpLink: <b>$totalPossible</b><br />");
  307: 	$title = $showPoints ? "Points" : "Parts";
  308: 	$r->print(&mt("Total $title In Course").': <b>'.$totalParts.'</b></td>'
  309:                  .&Apache::loncommon::end_data_table_row());
  310:     }
  311: 
  312:     $r->print(&Apache::loncommon::end_data_table()
  313:              .&Apache::loncommon::end_page());
  314: 
  315:     return OK;
  316: }
  317: 
  318: # Pass this two refs to arrays for the start and end color, and a number
  319: # from 0 to 1 for how much of the latter you want to mix in. It will
  320: # return a string ready to show ("#FFC309");
  321: sub mixColors {
  322:     my $start = shift;
  323:     my $end = shift;
  324:     my $ratio = shift;
  325:     
  326:     my ($a,$b);
  327:     my $final = "";
  328:     $a = $start->[0]; $b = $end->[0];
  329:     my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  330:     $a = $start->[1]; $b = $end->[1];
  331:     my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  332:     $a = $start->[2]; $b = $end->[2];
  333:     my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  334: 
  335:     $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
  336:     return "#" . $final;
  337: }
  338: 
  339: 1;

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