File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.61: download - view: text, annotated - select for diffs
Mon Jan 3 14:39:19 2011 UTC (13 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Coding style.
  - Separate content generation from presentation.

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

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