File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.52: download - view: text, annotated - select for diffs
Thu Dec 2 00:53:37 2010 UTC (13 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Directly jump to Course Preferences

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

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