File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.49.6.5.2.1: download - view: text, annotated - select for diffs
Mon May 25 16:45:31 2020 UTC (3 years, 11 months ago) by raeburn
Branches: version_2_11_2_msu
- For 2.11.2 (modified)
  Include changes in 1.110

    1: # The LearningOnline Network with CAPA
    2: # Quick Student Grades Display
    3: #
    4: # $Id: lonquickgrades.pm,v 1.49.6.5.2.1 2020/05/25 16:45:31 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 REDIRECT);
   33: use POSIX;
   34: use Apache::loncommon;
   35: use Apache::lonlocal;
   36: use Apache::lonnet;
   37: use Apache::grades;
   38: use Apache::lonuserstate;
   39: 
   40: sub handler {
   41:     my $r = shift;
   42:     return real_handler($r);
   43: }
   44: 
   45: sub real_handler {
   46:     my $r = shift;
   47: 
   48:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
   49: 
   50:     # Handle header-only request
   51:     if ($env{'browser.mathml'}) {
   52: 	&Apache::loncommon::content_type($r,'text/xml');
   53:     } else {
   54: 	&Apache::loncommon::content_type($r,'text/html');
   55:     }
   56:     if ($r->header_only) {
   57: 	$r->send_http_header;
   58:         return OK;
   59:     }
   60: 
   61:     my $showPoints =
   62:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
   63: 
   64:     my $reinitresult;
   65: 
   66:     unless (&Apache::lonnet::allowed('mgr')) {
   67:         # Check for critical messages and redirect if present.
   68:         my ($redirect,$url) = &Apache::loncommon::critical_redirect(300,'grades');
   69:         if ($redirect) {
   70:             &Apache::loncommon::content_type($r,'text/html');
   71:             $r->header_out(Location => $url);
   72:             return REDIRECT;
   73:         }
   74: 
   75:         # Check if course needs to be re-initialized
   76:         my $loncaparev = $r->dir_config('lonVersion');
   77:         ($reinitresult,my @reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
   78: 
   79:         if ($reinitresult eq 'switch') {
   80:             &Apache::loncommon::content_type($r,'text/html');
   81:             $r->send_http_header;
   82:             $r->print(&Apache::loncommon::check_release_result(@reinit));
   83:             return OK;
   84:         } elsif ($reinitresult eq 'update') {
   85:             my $cid = $env{'request.course.id'};
   86:             my $cnum = $env{'course.'.$cid.'.num'};
   87:             my $cdom = $env{'course.'.$cid.'.domain'};
   88:             &Apache::loncommon::content_type($r,'text/html');
   89:             $r->send_http_header;
   90:             &startpage($r,$showPoints);
   91:             my $preamble = '<div id="LC_update_'.$cid.'" class="LC_info">'.
   92:                            '<br />'.
   93:                            &mt('Your course session is being updated because of recent changes by course personnel.').
   94:                            ' '.&mt('Please be patient.').'<br /></div>'.
   95:                            '<div style="padding:0;clear:both;margin:0;border:0"></div>';
   96:             my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
   97:             &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Updating course'));
   98:             $r->rflush();
   99:             my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
  100:             &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished'));
  101:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  102:             my $closure = <<ENDCLOSE;
  103: <script type="text/javascript">
  104: // <![CDATA[
  105: \$("#LC_update_$cid").hide('slow');
  106: // ]]>
  107: </script>
  108: ENDCLOSE
  109:             if ($ferr) {
  110:                 $r->print($closure.&Apache::loncommon::end_page());
  111:                 my $requrl = $r->uri;
  112:                 $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  113:                 $env{'user.reinit'} = 1;
  114:                 return HTTP_NOT_ACCEPTABLE;
  115:             } else {
  116:                $r->print($closure);
  117:             }
  118:         }
  119:     }
  120: 
  121:     unless ($reinitresult eq 'update') {
  122:         # Send header, don't cache this page
  123:         &Apache::loncommon::no_cache($r);
  124:         $r->send_http_header;
  125:         &startpage($r,$showPoints);
  126:     }
  127:     $r->rflush();
  128: 
  129:     my $notshowSPRSlink = 
  130:         (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
  131:       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals'));
  132:     my $notshowTotals=
  133:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
  134: 
  135:     # Create the nav map
  136:     my $navmap = Apache::lonnavmaps::navmap->new();
  137: 
  138:     if (!defined($navmap)) {
  139:         my $requrl = $r->uri;
  140:         $env{'user.error.msg'} = "$requrl:bre:0:0:Navamp initialization failed.";
  141:         return HTTP_NOT_ACCEPTABLE;
  142:     }
  143: 
  144:     # Keep this hash in sync with %statusIconMap in lonnavmaps; they
  145:     # should match color/icon
  146:     my $res = $navmap->firstResource(); # temp resource to access constants
  147:  
  148:     if (!$showPoints && !$notshowSPRSlink ) {
  149:         $r->print('<p>'
  150:                  .&mt('This screen shows how many problems (or problem parts) you have completed'
  151:                      .', and how many you have not yet done.'
  152:                      .' You can also look at [_1]a detailed score sheet[_2].'
  153:                      ,'<a href="/adm/studentcalc">','</a>')
  154:                  .'</p>');
  155:     }
  156: 
  157:     $r->print('<p class="LC_info">'.&mt('This may take a few moments to display.').'</p>');
  158: 
  159:     $r->rflush();
  160: 
  161:     # End navmap using boilerplate
  162: 
  163:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
  164:     my $depth = 1;
  165:     $iterator->next(); # ignore first BEGIN_MAP
  166:     my $curRes = $iterator->next();
  167:     
  168:     # General overview of the following: Walk along the course resources.
  169:     # For every problem in the resource, tell its parent maps how many
  170:     # parts and how many parts correct it has. After that, each map will
  171:     # have a count of the total parts underneath it, correct and otherwise.
  172:     # After that, we will walk through the course again and read off
  173:     # maps in order, with their data. 
  174:     # (If in the future people decide not to be cumulative, only add
  175:     #  the counts to the parent map.)
  176:     # For convenience, "totalParts" is also "totalPoints" when we're looking
  177:     #  at points; I can't come up with a variable name that makes sense
  178:     #  equally for both cases.
  179: 
  180:     my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
  181:     my $totalAttempted = 0;
  182:     my $now = time();
  183:     my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
  184: 
  185:     # Pre-run: Count parts correct
  186:     while ( $depth > 0 ) {
  187:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
  188:         if ($curRes == $iterator->END_MAP()) { $depth--; }
  189: 
  190:         if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
  191:         {
  192:             # Get number of correct, incorrect parts
  193:             my $parts = $curRes->parts();
  194:             my $partsRight = 0;
  195: 	    my $partsCount = 0;
  196: 	    my $partsAttempted = 0;
  197:             my $stack = $iterator->getStack();
  198:             
  199:             for my $part (@{$parts}) {
  200: 		my $completionStatus = $curRes->getCompletionStatus($part);
  201: 		my $dateStatus = $curRes->getDateStatus($part);
  202: 		
  203:                 if ($completionStatus == $curRes->EXCUSED()) {
  204:                     next;
  205:                 }
  206: 		if ($showPoints) {
  207: 		    my $score = 0;
  208: 		    # If we're not telling status and the answer date isn't passed yet, 
  209: 		    # it's an "attempted" point
  210: 		    if ((($curRes->problemstatus($part) eq 'no') ||
  211:                         ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
  212: 			($dateStatus != $curRes->ANSWER_OPEN)) {
  213: 			my $status = $curRes->simpleStatus($part);
  214: 			if ($status == $curRes->ATTEMPTED) {
  215: 			    $partsAttempted += $curRes->weight($part);
  216: 			    $totalAttempted += $partsAttempted;
  217: 			}
  218: 		    } else {
  219: 			$score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
  220: 		    }
  221: 		    $partsRight += $score;
  222: 		    $totalRight += $score;
  223: 		    $partsCount += $curRes->weight($part);
  224: 
  225: 		    if ($curRes->opendate($part) < $now) {
  226: 			$totalPossible += $curRes->weight($part);
  227: 		    }
  228: 		    $totalParts += $curRes->weight($part);
  229: 		} else {
  230: 		    my $status = $curRes->simpleStatus($part);
  231: 		    my $thisright = 0;
  232: 		    $partsCount++;
  233: 		    if ($status == $curRes->CORRECT ||
  234: 			$status == $curRes->PARTIALLY_CORRECT ) {
  235: 			$partsRight++;
  236: 			$totalRight++;
  237: 			$thisright = 1;
  238: 		    }
  239: 
  240: 		    if ($status == $curRes->ATTEMPTED) {
  241: 			$partsAttempted++;
  242: 			$totalAttempted++;
  243: 		    }
  244: 		    
  245: 		    my $dateStatus = $curRes->getDateStatus($part);
  246: 		    $totalParts++;
  247: 		    if ($curRes->opendate($part) < $now) {
  248: 			$totalPossible++;
  249: 		    }
  250: 		}
  251:             }
  252: 
  253:             if ($depth == 1) { # in top-level only
  254: 		$topLevelParts += $partsCount;
  255: 		$topLevelRight += $partsRight;
  256: 		$topLevelAttempted += $partsAttempted;
  257: 	    }
  258: 
  259:             # Crawl down stack and record parts correct and total
  260:             for my $res (@{$stack}) {
  261:                 if (ref($res) && $res->is_map()) {
  262:                     if (!defined($res->{DATA}->{CHILD_PARTS})) {
  263:                         $res->{DATA}->{CHILD_PARTS} = 0;
  264:                         $res->{DATA}->{CHILD_CORRECT} = 0;
  265: 			$res->{DATA}->{CHILD_ATTEMPTED} = 0;
  266:                     }
  267:                     
  268:                     $res->{DATA}->{CHILD_PARTS} += $partsCount;
  269:                     $res->{DATA}->{CHILD_CORRECT} += $partsRight;
  270: 		    $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
  271:                 }
  272:             }
  273:         }
  274:         $curRes = $iterator->next();
  275:     }
  276: 
  277:     $iterator = $navmap->getIterator(undef, undef, undef, 1);
  278:     $depth = 1;
  279:     $iterator->next(); # ignore first BEGIN_MAP
  280:     $curRes = $iterator->next();
  281: 
  282:     my @start = (255, 255, 192);
  283:     my @end   = (0, 192, 0);
  284: 
  285:     my $indentString = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  286: 
  287:     # Second pass: Print the maps.
  288:     $r->print(&Apache::loncommon::start_data_table()
  289:              .&Apache::loncommon::start_data_table_header_row()
  290:              .'<th>'.&mt('Folder').'</th>');
  291:     my $title = &mt($showPoints ? "Points Scored" : "Done");
  292:     if ($totalAttempted) {
  293: 	$title .= " / " . &mt("Attempted");
  294:     }
  295:     $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
  296:              .&Apache::loncommon::end_data_table_header_row());
  297:     while ($depth > 0) {
  298:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
  299:         if ($curRes == $iterator->END_MAP()) { $depth--; }
  300: 
  301:         if (ref($curRes) && $curRes->is_map()) {
  302:             my $title = $curRes->compTitle();
  303:             
  304:             my $correct = $curRes->{DATA}->{CHILD_CORRECT};
  305:             my $total = $curRes->{DATA}->{CHILD_PARTS};
  306: 	    my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
  307: 
  308:             if ($total > 0) {
  309:                 my $ratio;
  310:                 $ratio = $correct / $total;
  311:                 my $color = &mixColors(\@start, \@end, $ratio);
  312:                 $r->print(&Apache::loncommon::start_data_table_row()
  313:                          .'<td style="background-color:'.$color.';">');
  314:                 
  315: 		my $thisIndent = '';
  316:                 for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
  317:                 
  318:                 $r->print("$thisIndent$title</td>");
  319: 		if ($totalAttempted) {
  320: 		    $r->print('<td valign="top">'
  321:                              .$thisIndent
  322:                              .'<span class="LC_nobreak">'
  323:                              .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
  324:                              .'</span></td>'
  325:                              .&Apache::loncommon::end_data_table_row()
  326:                     );
  327: 		} else {
  328: 		    $r->print('<td valign="top">'
  329:                              .$thisIndent
  330:                              .'<span class="LC_nobreak">'
  331:                              .$correct.($notshowTotals?'':' / '.$total)
  332:                              .'</span></td>'
  333:                              .&Apache::loncommon::end_data_table_row());
  334: 		}
  335:             }
  336:         }
  337: 
  338:         $curRes = $iterator->next();
  339:     }
  340: 
  341:     # If there were any problems at the top level, print an extra "catchall"
  342:     if ($topLevelParts > 0) {
  343:         my $ratio = $topLevelRight / $topLevelParts;
  344:         my $color = &mixColors(\@start, \@end, $ratio);
  345:         $r->print(&Apache::loncommon::start_data_table_row()
  346:                  .'<td style="background-color:'.$color.';">');
  347:         $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
  348:         $r->print("$topLevelRight / $topLevelParts</td>"
  349:                  .&Apache::loncommon::end_data_table_row());
  350:     }
  351: 
  352:     if ($showPoints) {
  353: 	my $maxHelpLink = &Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
  354: 
  355: 	$title = $showPoints ? "Points" : "Parts Done";
  356: 	my $totaltitle = $showPoints ? &mt("Awarded Total Points") : &mt("Total Parts Done");
  357: 	$r->print(&Apache::loncommon::start_data_table_row()
  358:                  .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
  359: 	$r->print(&mt('Max Possible To Date')." $maxHelpLink: <b>$totalPossible</b><br />");
  360: 	$title = $showPoints ? "Points" : "Parts";
  361: 	$r->print(&mt("Total $title In Course").': <b>'.$totalParts.'</b></td>'
  362:                  .&Apache::loncommon::end_data_table_row());
  363:     }
  364: 
  365:     $r->print(&Apache::loncommon::end_data_table()
  366:              .&Apache::loncommon::end_page());
  367: 
  368:     return OK;
  369: }
  370: 
  371: sub startpage {
  372:     my ($r,$showPoints) = @_;
  373:     my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
  374:     my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
  375:     $r->print(&Apache::loncommon::start_page($title,undef,
  376:                                             {'bread_crumbs' => $brcrum})
  377:              );
  378: }
  379: 
  380: # Pass this two refs to arrays for the start and end color, and a number
  381: # from 0 to 1 for how much of the latter you want to mix in. It will
  382: # return a string ready to show ("#FFC309");
  383: sub mixColors {
  384:     my $start = shift;
  385:     my $end = shift;
  386:     my $ratio = shift;
  387:     
  388:     my ($a,$b);
  389:     my $final = "";
  390:     $a = $start->[0]; $b = $end->[0];
  391:     my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  392:     $a = $start->[1]; $b = $end->[1];
  393:     my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  394:     $a = $start->[2]; $b = $end->[2];
  395:     my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  396: 
  397:     $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
  398:     return "#" . $final;
  399: }
  400: 
  401: 1;

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