File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.54: download - view: text, annotated - select for diffs
Fri Dec 3 15:21:35 2010 UTC (13 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Oops, remove debug code

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

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