File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.60: download - view: text, annotated - select for diffs
Mon Jan 3 14:19:39 2011 UTC (13 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- xhtml
  - Although LC_head_subbox was originally intended to occur only once
    on a page, it may occur multiple times (e.g., CSTR).
  - Converting from id to class to allow multiple instances.

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

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