Annotation of loncom/interface/lonquickgrades.pm, revision 1.73

1.1       bowersj2    1: # The LearningOnline Network with CAPA
                      2: # Quick Student Grades Display
                      3: #
1.73    ! www         4: # $Id: lonquickgrades.pm,v 1.72 2011/03/09 00:35:57 www Exp $
1.1       bowersj2    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);
1.5       bowersj2   33: use POSIX;
1.25      www        34: use Apache::loncommon;
                     35: use Apache::lonlocal;
1.36      albertel   36: use Apache::lonnet;
1.38      bowersj2   37: use Apache::grades;
1.1       bowersj2   38: 
                     39: sub handler {
                     40:     my $r = shift;
1.5       bowersj2   41:     return real_handler($r);
                     42: }
                     43: 
                     44: sub real_handler {
                     45:     my $r = shift;
1.1       bowersj2   46: 
                     47:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
                     48: 
                     49:     # Handle header-only request
1.40      albertel   50:     if ($env{'browser.mathml'}) {
                     51: 	&Apache::loncommon::content_type($r,'text/xml');
                     52:     } else {
                     53: 	&Apache::loncommon::content_type($r,'text/html');
                     54:     }
1.1       bowersj2   55:     if ($r->header_only) {
1.40      albertel   56: 	$r->send_http_header;
1.1       bowersj2   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: 
1.55      www        64:     my $showPoints =
1.36      albertel   65:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
1.55      www        66:     my $notshowSPRSlink =
1.49      www        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';
1.50      www        71:     my $showCategories=
                     72:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
                     73: 
1.17      bowersj2   74: 
1.47      schafran   75:     my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
1.46      raeburn    76:     my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
                     77:     $r->print(&Apache::loncommon::start_page($title,undef,
                     78:                                             {'bread_crumbs' => $brcrum})
                     79:              );
1.1       bowersj2   80: 
1.55      www        81:     &startGradeScreen($r,'quick');
1.17      bowersj2   82: 
                     83:     $r->rflush();
1.53      www        84: 
1.54      www        85: #    my $uname='korte';
                     86: #    my $udom='gerd';
                     87: 
                     88:     my $uname;
                     89:     my $udom;
1.53      www        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,
1.51      www        99:                  $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
1.53      www       100:     }
1.55      www       101:     &endGradeScreen($r);
1.51      www       102:     return OK;
                    103: 
                    104: }
1.2       bowersj2  105: 
1.55      www       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) {
1.61      raeburn   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)));
1.55      www       131:     }
                    132: 
                    133: 
1.56      www       134:     $r->print("\n".'<ul class="LC_TabContentBigger" id="main">');
1.57      www       135:     $r->print("\n".'<li'.($mode eq 'quick'?' class="active"':'').'><a href="/adm/quickgrades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
1.62      www       136:                                           ($showPoints?&mt('Individual Points Overview'):($showCategories?&mt('Grades Overview'):&mt('Completion Overview'))).
1.57      www       137:                                           '&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
1.55      www       138: 
                    139:     if (!($showPoints || $notshowSPRSlink) || ($allowed_to_view)) {
1.56      www       140:        $r->print("\n".'<li'.($mode eq 'spreadsheet'?' class="active"':'').'><a href="/adm/'.($allowed_to_view?'classcalc':'studentcalc').'"><b>'.
1.57      www       141:                                                                  &mt('Spreadsheet (Detailed)').'</b></a></li>');
1.55      www       142:     }
1.58      www       143:     if ($allowed_to_view) {
1.63      www       144:        $r->print("\n".'<li'.($mode eq 'statistics'?' class="active"':'').'><a href="/adm/statistics"><b>'.
                    145:                                                                  &mt('Statistics and Reports').'</b></a></li>');
                    146: 
1.58      www       147:        $r->print("\n".'<li'.($mode eq 'chart'?' class="active"':'').'><a href="/adm/statistics?reportSelected=student_assessment"><b>'.
                    148:                                                                  &mt('Assessment Overview Chart').'</b></a></li>');
                    149: 
                    150:     }
1.59      www       151:     if ($allowed_to_edit) {
                    152:        $r->print("\n".'<li'.($mode eq 'grading'?' class="active"':'').'><a href="/adm/grades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
1.66      www       153:                                                                  &mt('Content Grading').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
                    154:        if ($env{'form.symb'}) {
                    155:           $r->print("\n".'<li'.($mode eq 'probgrading'?' class="active"':'').'><a href="/adm/grades?symb='.
                    156:                                               &Apache::lonhtmlcommon::entity_encode($env{'form.symb'}).
                    157:                                               '&command=gradingmenu"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
                    158:                                               &mt('Problem Grading').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
                    159: 
                    160:        }
1.59      www       161:     }
1.56      www       162:     $r->print("\n".'</ul>'."\n");
1.55      www       163:     $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;">');
                    164: }
                    165: 
                    166: sub endGradeScreen {
                    167:    my ($r)=@_;
1.72      www       168:    $r->print('</div></div></div>'.&Apache::loncommon::end_page());
1.55      www       169: }
                    170: 
                    171: 
1.51      www       172: sub getData {
                    173: 
1.53      www       174:     my ($showPoints,$uname,$udom)=@_;
                    175: 
1.51      www       176:     # Create the nav map
1.53      www       177:     my $navmap = Apache::lonnavmaps::navmap->new($uname,$udom);
1.51      www       178: 
                    179:     my $res = $navmap->firstResource(); # temp resource to access constants
1.1       bowersj2  180: 
                    181:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
                    182:     my $depth = 1;
                    183:     $iterator->next(); # ignore first BEGIN_MAP
                    184:     my $curRes = $iterator->next();
1.4       bowersj2  185:     
1.5       bowersj2  186:     # General overview of the following: Walk along the course resources.
                    187:     # For every problem in the resource, tell its parent maps how many
                    188:     # parts and how many parts correct it has. After that, each map will
                    189:     # have a count of the total parts underneath it, correct and otherwise.
                    190:     # After that, we will walk through the course again and read off
                    191:     # maps in order, with their data. 
                    192:     # (If in the future people decide not to be cumulative, only add
                    193:     #  the counts to the parent map.)
1.17      bowersj2  194:     # For convenience, "totalParts" is also "totalPoints" when we're looking
                    195:     #  at points; I can't come up with a variable name that makes sense
                    196:     #  equally for both cases.
1.5       bowersj2  197: 
                    198:     my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
1.28      bowersj2  199:     my $totalAttempted = 0;
1.14      bowersj2  200:     my $now = time();
1.28      bowersj2  201:     my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
1.5       bowersj2  202: 
                    203:     # Pre-run: Count parts correct
1.1       bowersj2  204:     while ( $depth > 0 ) {
                    205:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    206:         if ($curRes == $iterator->END_MAP()) { $depth--; }
                    207: 
1.7       bowersj2  208:         if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
1.5       bowersj2  209:         {
                    210:             # Get number of correct, incorrect parts
                    211:             my $parts = $curRes->parts();
                    212:             my $partsRight = 0;
1.17      bowersj2  213: 	    my $partsCount = 0;
1.28      bowersj2  214: 	    my $partsAttempted = 0;
1.5       bowersj2  215:             my $stack = $iterator->getStack();
                    216:             
                    217:             for my $part (@{$parts}) {
1.28      bowersj2  218: 		my $completionStatus = $curRes->getCompletionStatus($part);
                    219: 		my $dateStatus = $curRes->getDateStatus($part);
                    220: 		
                    221:                 if ($completionStatus == $curRes->EXCUSED()) {
1.21      matthew   222:                     next;
                    223:                 }
1.17      bowersj2  224: 		if ($showPoints) {
1.28      bowersj2  225: 		    my $score = 0;
                    226: 		    # If we're not telling status and the answer date isn't passed yet, 
                    227: 		    # it's an "attempted" point
1.42      raeburn   228: 		    if ((($curRes->problemstatus($part) eq 'no') ||
                    229:                         ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
1.28      bowersj2  230: 			($dateStatus != $curRes->ANSWER_OPEN)) {
1.31      albertel  231: 			my $status = $curRes->simpleStatus($part);
                    232: 			if ($status == $curRes->ATTEMPTED) {
                    233: 			    $partsAttempted += $curRes->weight($part);
                    234: 			    $totalAttempted += $partsAttempted;
                    235: 			}
1.28      bowersj2  236: 		    } else {
1.39      albertel  237: 			$score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
1.28      bowersj2  238: 		    }
1.17      bowersj2  239: 		    $partsRight += $score;
                    240: 		    $totalRight += $score;
1.19      bowersj2  241: 		    $partsCount += $curRes->weight($part);
1.18      bowersj2  242: 
1.17      bowersj2  243: 		    if ($curRes->opendate($part) < $now) {
1.20      bowersj2  244: 			$totalPossible += $curRes->weight($part);
1.17      bowersj2  245: 		    }
1.19      bowersj2  246: 		    $totalParts += $curRes->weight($part);
1.17      bowersj2  247: 		} else {
1.27      bowersj2  248: 		    my $status = $curRes->simpleStatus($part);
1.17      bowersj2  249: 		    my $thisright = 0;
                    250: 		    $partsCount++;
1.37      albertel  251: 		    if ($status == $curRes->CORRECT ||
                    252: 			$status == $curRes->PARTIALLY_CORRECT ) {
1.17      bowersj2  253: 			$partsRight++;
                    254: 			$totalRight++;
                    255: 			$thisright = 1;
                    256: 		    }
1.28      bowersj2  257: 
                    258: 		    if ($status == $curRes->ATTEMPTED) {
                    259: 			$partsAttempted++;
                    260: 			$totalAttempted++;
                    261: 		    }
1.17      bowersj2  262: 		    
                    263: 		    my $dateStatus = $curRes->getDateStatus($part);
1.19      bowersj2  264: 		    $totalParts++;
1.17      bowersj2  265: 		    if ($curRes->opendate($part) < $now) {
                    266: 			$totalPossible++;
                    267: 		    }
                    268: 		}
1.5       bowersj2  269:             }
1.15      bowersj2  270: 
                    271:             if ($depth == 1) { # in top-level only
1.19      bowersj2  272: 		$topLevelParts += $partsCount;
1.15      bowersj2  273: 		$topLevelRight += $partsRight;
1.28      bowersj2  274: 		$topLevelAttempted += $partsAttempted;
1.15      bowersj2  275: 	    }
                    276: 
1.5       bowersj2  277:             # Crawl down stack and record parts correct and total
                    278:             for my $res (@{$stack}) {
                    279:                 if (ref($res) && $res->is_map()) {
                    280:                     if (!defined($res->{DATA}->{CHILD_PARTS})) {
                    281:                         $res->{DATA}->{CHILD_PARTS} = 0;
                    282:                         $res->{DATA}->{CHILD_CORRECT} = 0;
1.28      bowersj2  283: 			$res->{DATA}->{CHILD_ATTEMPTED} = 0;
1.5       bowersj2  284:                     }
                    285:                     
1.17      bowersj2  286:                     $res->{DATA}->{CHILD_PARTS} += $partsCount;
1.5       bowersj2  287:                     $res->{DATA}->{CHILD_CORRECT} += $partsRight;
1.28      bowersj2  288: 		    $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
1.5       bowersj2  289:                 }
                    290:             }
                    291:         }
                    292:         $curRes = $iterator->next();
                    293:     }
1.51      www       294:     return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
                    295: }
                    296: 
                    297: #
                    298: # Outputting everything.
                    299: #
                    300: 
                    301: sub outputTable {
1.5       bowersj2  302: 
1.51      www       303:     my ($r,$showPoints,$notshowTotals,
                    304:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
1.5       bowersj2  305: 
1.7       bowersj2  306:     my @start = (255, 255, 192);
1.5       bowersj2  307:     my @end   = (0, 192, 0);
                    308: 
                    309:     my $indentString = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                    310: 
                    311:     # Second pass: Print the maps.
1.43      bisitz    312:     $r->print(&Apache::loncommon::start_data_table()
                    313:              .&Apache::loncommon::start_data_table_header_row()
                    314:              .'<th>'.&mt('Folder').'</th>');
1.51      www       315:     my $title = &mt($showPoints ? "Points Scored" : "Done");
1.28      bowersj2  316:     if ($totalAttempted) {
1.51      www       317:         $title .= " / " . &mt("Attempted");
1.28      bowersj2  318:     }
1.49      www       319:     $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
1.43      bisitz    320:              .&Apache::loncommon::end_data_table_header_row());
1.51      www       321: #
                    322: # Output of folder scores
                    323: #
                    324: 
                    325:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
                    326:     my $depth = 1;
                    327:     $iterator->next(); # ignore first BEGIN_MAP
                    328:     my $curRes = $iterator->next();
                    329: 
1.5       bowersj2  330:     while ($depth > 0) {
                    331:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    332:         if ($curRes == $iterator->END_MAP()) { $depth--; }
                    333: 
                    334:         if (ref($curRes) && $curRes->is_map()) {
                    335:             my $title = $curRes->compTitle();
                    336:             
                    337:             my $correct = $curRes->{DATA}->{CHILD_CORRECT};
                    338:             my $total = $curRes->{DATA}->{CHILD_PARTS};
1.28      bowersj2  339: 	    my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
1.5       bowersj2  340: 
1.6       bowersj2  341:             if ($total > 0) {
                    342:                 my $ratio;
                    343:                 $ratio = $correct / $total;
1.51      www       344:                 my $color = &mixColors(\@start, \@end, $ratio);
1.43      bisitz    345:                 $r->print(&Apache::loncommon::start_data_table_row()
                    346:                          .'<td style="background-color:'.$color.';">');
1.6       bowersj2  347:                 
1.15      bowersj2  348: 		my $thisIndent = '';
                    349:                 for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
1.6       bowersj2  350:                 
1.15      bowersj2  351:                 $r->print("$thisIndent$title</td>");
1.28      bowersj2  352: 		if ($totalAttempted) {
1.45      bisitz    353: 		    $r->print('<td valign="top">'
                    354:                              .$thisIndent
                    355:                              .'<span class="LC_nobreak">'
1.49      www       356:                              .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
1.45      bisitz    357:                              .'</span></td>'
                    358:                              .&Apache::loncommon::end_data_table_row()
                    359:                     );
1.28      bowersj2  360: 		} else {
1.45      bisitz    361: 		    $r->print('<td valign="top">'
                    362:                              .$thisIndent
                    363:                              .'<span class="LC_nobreak">'
1.49      www       364:                              .$correct.($notshowTotals?'':' / '.$total)
1.45      bisitz    365:                              .'</span></td>'
1.43      bisitz    366:                              .&Apache::loncommon::end_data_table_row());
1.28      bowersj2  367: 		}
1.6       bowersj2  368:             }
1.5       bowersj2  369:         }
1.4       bowersj2  370: 
1.5       bowersj2  371:         $curRes = $iterator->next();
                    372:     }
1.4       bowersj2  373: 
1.6       bowersj2  374:     # If there were any problems at the top level, print an extra "catchall"
1.15      bowersj2  375:     if ($topLevelParts > 0) {
                    376:         my $ratio = $topLevelRight / $topLevelParts;
1.68      www       377:         my $color = &mixColors(\@start, \@end, $ratio);
1.43      bisitz    378:         $r->print(&Apache::loncommon::start_data_table_row()
                    379:                  .'<td style="background-color:'.$color.';">');
1.25      www       380:         $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
1.43      bisitz    381:         $r->print("$topLevelRight / $topLevelParts</td>"
                    382:                  .&Apache::loncommon::end_data_table_row());
1.6       bowersj2  383:     }
1.4       bowersj2  384: 
1.51      www       385: #
                    386: # show totals (if applicable), close table
                    387: #
1.35      albertel  388:     if ($showPoints) {
1.68      www       389:         my $maxHelpLink = &Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
1.2       bowersj2  390: 
1.51      www       391:         $title = $showPoints ? "Points" : "Parts Done";
                    392:         my $totaltitle = $showPoints ? &mt("Awarded Total Points") : &mt("Total Parts Done");
                    393:         $r->print(&Apache::loncommon::start_data_table_row()
1.48      bisitz    394:                  .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
1.51      www       395:         $r->print(&mt('Max Possible To Date')." $maxHelpLink: <b>$totalPossible</b><br />");
                    396:         $title = $showPoints ? "Points" : "Parts";
                    397:         $r->print(&mt("Total $title In Course").': <b>'.$totalParts.'</b></td>'
1.43      bisitz    398:                  .&Apache::loncommon::end_data_table_row());
1.34      www       399:     }
1.1       bowersj2  400: 
1.72      www       401:     $r->print(&Apache::loncommon::end_data_table());
1.5       bowersj2  402: }
                    403: 
1.53      www       404: #
1.65      www       405: # === Outputting category-based grades.
                    406: #
                    407: # $category{'order'}: output order of categories by id
                    408: # $category{'all'}: complete list of all categories 
                    409: # $category{$id.'_name'}: display-name of category
1.53      www       410: #
                    411: 
                    412: sub outputCategories {
                    413: 
                    414:     my ($r,$showPoints,$notshowTotals,
                    415:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
1.62      www       416: # Take care of storing and retrieving categories
                    417: 
1.64      www       418:     my $cangrade=&Apache::lonnet::allowed('mgr');
                    419: 
1.62      www       420:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    421:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.64      www       422:     my %categories=();
1.65      www       423: # Loading old categories
                    424:     %categories=&Apache::lonnet::dump('grading_categories',$cdom,$cnum);
1.64      www       425: # Storing
1.72      www       426:     if (($cangrade) && (($env{'form.storechanges'}) || ($env{'form.storemove'} ne '') || ($env{'form.cmd'} ne ''))) {
1.65      www       427: # Process the changes
                    428:         %categories=&process_category_edits($r,$cangrade,%categories);
1.64      www       429: # Actually store
                    430:         &Apache::lonnet::put('grading_categories',\%categories,$cdom,$cnum);
                    431:     }
1.65      www       432: # new categories loaded now
1.64      www       433: # Form only generated if user can change the grading categories
1.65      www       434:     if ($cangrade) {
1.72      www       435:         $r->print('<form method="post" name="quickform" action="/adm/quickgrades">');
1.65      www       436:     }
                    437: #
                    438:     &output_category_table($r,$cangrade,$navmap,%categories);
                    439: #
                    440:     if ($cangrade) {
                    441:         $r->print('<input type="hidden" name="storemove" value="" />'.
1.72      www       442:                   '<input type="hidden" name="cmd" value="" />'.
                    443:                   '<input type="submit" name="storechanges" value="'.&mt("Save changes to grading categories").'" />'.
                    444:                   '<script>function storecmd (cmd) { document.quickform.cmd.value=cmd; document.quickform.submit(); }</script>'.
                    445:                   '</form>');
1.65      www       446:     }
                    447: }
                    448: 
                    449: #
                    450: # Process editing commands, update category hash
                    451: #
                    452: 
                    453: sub process_category_edits {
                    454:     my ($r,$cangrade,%categories)=@_;
                    455:     unless ($cangrade) { return %categories; }
1.72      www       456: # First store everything
                    457:     foreach my $id (split(/\,/,$categories{'order'})) {
                    458:         %categories=&set_category_name($cangrade,$id,$env{'form.name_'.$id},%categories);
                    459:         %categories=&set_category_total($cangrade,$id,$env{'form.totaltype_'.$id},$env{'form.total_'.$id},%categories);
                    460:         %categories=&set_category_weight($cangrade,$id,$env{'form.weight_'.$id},%categories);
                    461: # More changes here
                    462:     }
                    463: 
                    464: # Now deal with commands
1.67      www       465:     my $cmd=$env{'form.cmd'};
                    466:     if ($cmd eq 'createnewcat') {
1.69      www       467:         %categories=&make_new_category($r,$cangrade,undef,%categories);
1.72      www       468:     } elsif ($cmd=~/^up\_(.+)$/) {
                    469:         %categories=&move_up_category($1,$cangrade,%categories);
                    470:     } elsif ($cmd=~/^down\_(.+)$/) {
                    471:         %categories=&move_down_category($1,$cangrade,%categories);
1.69      www       472:     } elsif ($cmd=~/^delcat\_(.+)$/) {
                    473:         %categories=&del_category($1,$cangrade,%categories);
1.73    ! www       474:     }
        !           475: # Move to a new position
        !           476:     my $moveid=$env{'form.storemove'};
        !           477:     if ($moveid) {
        !           478:         %categories=&move_category($moveid,$cangrade,$env{'form.newpos_'.$moveid},%categories);
1.72      www       479:     } 
1.65      www       480:     return %categories;
                    481: }
                    482: 
                    483: #
                    484: # Output the table
                    485: #
                    486: 
                    487: sub output_category_table {
                    488:     my ($r,$cangrade,$navmaps,%categories)=@_;
                    489:     my $sum=0;
1.67      www       490:     my $total=0;
1.65      www       491:     $r->print(&Apache::loncommon::start_data_table());
                    492: #
                    493:     &output_category_table_header($r,$cangrade);
                    494: #
                    495:     my @order=split(/\,/,$categories{'order'});
                    496: #
                    497:     my $maxpos=$#order;
                    498:     for (my $i=0;$i<=$maxpos;$i++) {
                    499:         my ($value,$weight)=&output_and_calc_category($r,$cangrade,$navmaps,$order[$i],$i,$maxpos,%categories);
1.67      www       500:         $sum+=$value*$weight;
                    501:         $total+=$weight;
1.65      www       502:     }
                    503: #
1.67      www       504:     &bottom_line_category($r,$cangrade,$sum,$total);
1.65      www       505: #
                    506:     $r->print(&Apache::loncommon::end_data_table());
                    507:     return $sum;
                    508: }
                    509: 
                    510: sub output_category_table_header {
                    511:     my ($r,$cangrade)=@_;
                    512:     $r->print(&Apache::loncommon::start_data_table_header_row());
                    513:     if ($cangrade) {
                    514:         $r->print('<th colspan="2">'.&mt("Move").'</th><th>'.&mt('Action').'</th>');
                    515:     }
                    516:     $r->print('<th>'.&mt('Category').'</th>'.
                    517:               '<th>'.&mt('Contents').'</th>'.
                    518:               '<th>'.&mt('Calculation').'</th>'.
1.71      www       519:               '<th>'.&mt('Total Points').'</th>'.
                    520:               '<th>'.&mt('Relative Weight').'</th>');
1.65      www       521:     $r->print(&Apache::loncommon::end_data_table_header_row());
                    522: }
                    523: 
                    524: 
                    525: #
                    526: # Output one category to table
                    527: #
                    528: 
                    529: sub output_and_calc_category {
                    530:     my ($r,$cangrade,$navmaps,$id,$currentpos,$maxpos,%categories)=@_;
                    531:     my $value=0;
                    532:     my $weight=0;
                    533:     my $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL') . "/");
                    534:     my %lt=&Apache::lonlocal::texthash(
                    535:            'up' => 'Move Up',
                    536:            'dw' => 'Move Down');
                    537:     $r->print("\n".&Apache::loncommon::start_data_table_row());
                    538: 
                    539:     if ($cangrade) {
                    540:         $r->print(<<ENDMOVE);
                    541: <td>
                    542: <div class="LC_docs_entry_move">
1.72      www       543:   <a href='javascript:storecmd("up_$id");'>
1.65      www       544:     <img src="${iconpath}move_up.gif" alt='$lt{'up'}' class="LC_icon" />
                    545:   </a>
                    546: </div>
                    547: <div class="LC_docs_entry_move">
1.72      www       548:   <a href='javascript:storecmd("down_$id");'>
1.65      www       549:     <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' class="LC_icon" />
                    550:   </a>
                    551: </div>
                    552: </td>
                    553: ENDMOVE
                    554:         $r->print("\n<td>\n<select name='newpos_$id' onchange='this.form.storemove.value=\"$id\";this.form.submit()'>");
                    555:         for (my $i=0;$i<=$maxpos;$i++) {
                    556:             if ($i==$currentpos) {
                    557:                 $r->print('<option value="" selected="selected">('.$i.')</option>');
                    558:             } else {
                    559:                 $r->print('<option value="'.$i.'">'.$i.'</option>');
                    560:             }
                    561:         }
                    562:         $r->print("\n</select>\n</td>\n");
1.72      www       563:         $r->print('<td><a href="javascript:storecmd(\'delcat_'.$id.'\');">'.&mt('Delete').'</a></td>');
1.69      www       564:         $r->print('<td><input type="text" name="name_'.$id.
                    565:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_name'}).'" /></td>');
                    566:     } else {
                    567:         $r->print('<td>'.$categories{$id.'_name'}.'</td>');
1.65      www       568:     }
1.70      www       569: # Content
                    570: # FIXME: just placeholders
                    571:     if ($cangrade) {
                    572:        $r->print("<td>Content Edit</td>");
                    573:     } else {
                    574:        $r->print("<td>Content</td>");
                    575:     }
                    576: # Calculation
                    577: # FIXME: just placeholders
                    578:     if ($cangrade) {
                    579:        $r->print("<td>Calculation Edit</td>");
                    580:     } else {
                    581:        $r->print("<td>Calculation</td>");
                    582:     }
1.71      www       583: # Total
                    584:     if ($cangrade) {
                    585:        $r->print('<td>'.
                    586:                   '<select name="totaltype_'.$id.'">'.
                    587:                   '<option value="default"'.($categories{$id.'_totaltype'} eq 'default'?' selected="selected"':'').'>'.&mt('default').'</option>'.
                    588:                   '<option value="typein"'.($categories{$id.'_totaltype'} eq 'typein'?' selected="selected"':'').'>'.&mt('Type-in value').'</option>'.
                    589:                   '</select>'.
                    590:                   '<input type="text" size="4" name="total_'.$id.
                    591:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_total'}).'" /></td>');
                    592:     } else {
                    593:         $r->print('<td>'.($categories{$id.'_totaltype'} eq 'default'?&mt('default'):$categories{$id.'_total'}).'</td>');
                    594:     }
1.70      www       595: # Weight
                    596:     if ($cangrade) {
                    597:        $r->print('<td>'.
                    598:                   '<input type="text" size="4" name="weight_'.$id.
                    599:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_weight'}).'" /></td>');
                    600:     } else {
1.71      www       601:         $r->print('<td>'.$categories{$id.'_weight'}.'</td>');
1.70      www       602:     }
                    603: 
1.65      www       604:     return ($value,$weight);
                    605: }
                    606: 
                    607: #
                    608: # Bottom line with grades
                    609: #
                    610: 
                    611: sub bottom_line_category {
1.67      www       612:     my ($r,$cangrade,$sum,$total)=@_;
                    613:     $r->print(&Apache::loncommon::start_data_table_row());
                    614:     if ($cangrade) {
1.72      www       615:         $r->print('<td colspan="3"><a href="javascript:storecmd(\'createnewcat\');">'.&mt('Create New Category').'</a></td>');
1.67      www       616:     }
                    617:     $r->print('<td colspan="5">'.&mt('Current:').$sum.'<br />'.&mt('Total:').$total.'<br /></td>');
1.65      www       618: }
                    619: 
                    620: #
                    621: # Make one new category
                    622: #
                    623: 
                    624: sub make_new_category {
                    625:     my ($r,$cangrade,$ordernum,%categories)=@_;
                    626:     unless ($cangrade) { return %categories; }
                    627: # Generate new ID
                    628:     my $id=time.'_'.$$.'_'.rand(10000);
                    629: # Add new ID to list of all IDs ever created in this course
                    630:     $categories{'all'}.=','.$id;
                    631:     $categories{'all'}=~s/^\,//;
                    632: # Add new ID to ordered list of displayed and evaluated categories
                    633:     $categories{'order'}.=','.$id;
                    634:     $categories{'order'}=~s/^\,//;
                    635: # Move it into desired space
                    636:     if (defined($ordernum)) {
                    637:         %categories=&move_category($id,$cangrade,$ordernum,%categories);
                    638:     }
1.71      www       639:     $categories{$id.'_weight'}=0;
                    640:     $categories{$id.'_totaltype'}='default';
1.65      www       641:     return %categories;
1.53      www       642: }
                    643: 
1.65      www       644: #
1.68      www       645: # Delete category
                    646: #
                    647: 
                    648: sub del_category {
                    649:    my ($id,$cangrade,%categories)=@_; 
                    650:    my @neworder=();
                    651:    foreach my $currentid (split(/\,/,$categories{'order'})) {
                    652:       unless ($currentid eq $id) {
                    653:          push(@neworder,$currentid);
                    654:       }
                    655:    }
                    656:    $categories{'order'}=join(',',@neworder);
                    657:    return %categories;
                    658: }
                    659: 
                    660: #
1.72      www       661: # Move category up
                    662: #
                    663: 
                    664: sub move_up_category {
                    665:     my ($id,$cangrade,%categories)=@_;
                    666:     my $currentpos=&current_pos_category($id,%categories);
                    667:     if ($currentpos<1) { return %categories; }
                    668:     return &move_category($id,$cangrade,$currentpos-1,%categories);
                    669: }
                    670: 
                    671: #
                    672: # Move category down
                    673: #
                    674: 
                    675: sub move_down_category {
                    676:     my ($id,$cangrade,%categories)=@_;
                    677:     my $currentpos=&current_pos_category($id,%categories);
                    678:     my @order=split(/\,/,$categories{'order'});
                    679:     if ($currentpos>=$#order) { return %categories; }
                    680:     return &move_category($id,$cangrade,$currentpos+1,%categories);
                    681: }
                    682: 
                    683: #
1.65      www       684: # Move a category to a desired position n the display order
                    685: #
                    686: 
                    687: sub move_category {
                    688:     my ($id,$cangrade,$ordernum,%categories)=@_;
                    689:     unless ($cangrade) { return %categories; }
                    690:     my @order=split(/\,/,$categories{'order'});
                    691: # Where is the index currently?
                    692:     my $currentpos=&current_pos_category($id,%categories);
                    693:     if (defined($currentpos)) {
                    694:         if ($currentpos<$ordernum) {
                    695: # This is moving to a higher index
                    696: # ....X1234....
                    697: # ....1234X....
                    698:             for (my $i=$currentpos;$i<$ordernum;$i++) {
                    699:                 $order[$i]=$order[$i+1];
                    700:             }
                    701:             $order[$ordernum]=$id;
                    702:         }
                    703:         if ($currentpos>$ordernum) {
                    704: # This is moving to a lower index
                    705: # ....1234X....
                    706: # ....X1234....
                    707:             for (my $i=$currentpos;$i>$ordernum;$i--) {
                    708:                 $order[$i]=$order[$i-1];
                    709:             }
                    710:             $order[$ordernum]=$id;
                    711:         }
                    712:     }
                    713:     $categories{'order'}=join(',',@order);
                    714:     return %categories;
                    715: }
                    716: 
                    717: #
                    718: #  Find current postion of a category in the order
                    719: #
                    720: 
                    721: sub current_pos_category {
                    722:     my ($id,%categories)=@_;
                    723:     my @order=split(/\,/,$categories{'order'});
                    724:     for (my $i=0;$i<=$#order;$i++) {
                    725:         if ($order[$i] eq $id) { return $i; }
                    726:     }
                    727: # not found
                    728:     return undef;
                    729: }
                    730: 
                    731: #
                    732: # Set name of a category
                    733: #
                    734: sub set_category_name {
1.69      www       735:     my ($cangrade,$id,$name,%categories)=@_;
                    736:     unless ($cangrade) { return %categories; }
1.65      www       737:     $categories{$id.'_name'}=$name;
                    738:     return %categories;
                    739: }
                    740: 
                    741: #
1.71      www       742: # Set total of a category
1.70      www       743: #
1.71      www       744: sub set_category_total {
                    745:     my ($cangrade,$id,$totaltype,$total,%categories)=@_;
1.70      www       746:     unless ($cangrade) { return %categories; }
1.71      www       747:     if (($categories{$id.'_total'} eq '') && ($total=~/\d/)) {
                    748:         $totaltype='typein';
1.70      www       749:     }
1.71      www       750:     $categories{$id.'_totaltype'}=$totaltype;
                    751:     if ($totaltype eq 'default') {
                    752:         $categories{$id.'_total'}='';
1.70      www       753:     } else {
1.71      www       754:         $total=~s/\D//gs;
                    755:         unless ($total) { $total=0; }
                    756:         $categories{$id.'_total'}=$total;
1.70      www       757:     }
                    758:     return %categories;
                    759: }
                    760: 
1.71      www       761: sub set_category_weight {
                    762:     my ($cangrade,$id,$weight,%categories)=@_;
                    763:     unless ($cangrade) { return %categories; }
                    764:     $weight=~s/\D//gs;
                    765:     unless ($weight) { $weight=0; }
                    766:     $categories{$id.'_weight'}=$weight;
                    767:     return %categories;
                    768: }
1.70      www       769: 
                    770: #
1.65      www       771: # === end category-related
                    772: #
                    773: #
1.5       bowersj2  774: # Pass this two refs to arrays for the start and end color, and a number
                    775: # from 0 to 1 for how much of the latter you want to mix in. It will
                    776: # return a string ready to show ("#FFC309");
1.51      www       777: 
1.5       bowersj2  778: sub mixColors {
                    779:     my $start = shift;
                    780:     my $end = shift;
                    781:     my $ratio = shift;
                    782:     
1.9       matthew   783:     my ($a,$b);
1.5       bowersj2  784:     my $final = "";
1.9       matthew   785:     $a = $start->[0]; $b = $end->[0];
1.5       bowersj2  786:     my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew   787:     $a = $start->[1]; $b = $end->[1];
1.5       bowersj2  788:     my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew   789:     $a = $start->[2]; $b = $end->[2];
1.5       bowersj2  790:     my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
                    791: 
1.16      bowersj2  792:     $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
1.5       bowersj2  793:     return "#" . $final;
1.1       bowersj2  794: }
                    795: 
                    796: 1;

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