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

1.1       bowersj2    1: # The LearningOnline Network with CAPA
                      2: # Quick Student Grades Display
                      3: #
1.82    ! www         4: # $Id: lonquickgrades.pm,v 1.81 2011/03/26 20:04:15 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: 
1.74      www        83:     my $cangrade=&Apache::lonnet::allowed('mgr');
                     84: #
                     85: # Pick student
                     86: #
1.54      www        87:     my $uname;
                     88:     my $udom;
1.74      www        89:     my $stdid;
                     90:     if ($cangrade) {
                     91:         if ($env{'form.uname'}) { $uname=$env{'form.uname'}; }
                     92:         if ($env{'form.udom'}) { $udom=$env{'form.udom'}; }
                     93:         if ($env{'form.id'}) { $stdid=$env{'form.id'}; }
                     94:         if (($stdid) && ($udom)) {
                     95:             $uname=(&Apache::lonnet::idget($udom,$stdid))[1];
                     96:         }
1.75      www        97:         if (($stdid) && (!$uname)) {
                     98:             $r->print('<p><span class="LC_warning">'.&mt("Unknown Student/Employee ID: [_1]",$stdid).'</span></p>');
                     99:             $stdid='';
                    100:         }
1.74      www       101:         $r->print('<form method="post" name="quickform" action="/adm/quickgrades">');
                    102:         my $chooseopt=&Apache::loncommon::select_dom_form($udom,'udom').' '.
                    103:            &Apache::loncommon::selectstudent_link('quickform','uname','udom');
                    104:         $r->print("<p>\n".&Apache::loncommon::studentbrowser_javascript()."\n");
                    105:         $r->print(&mt('For User [_1] or Student/Employee ID [_2] at Domain [_3]'
                    106:                  ,'<input type="text" value="'.$uname.'" size="12" name="uname" />'
                    107:                  ,'<input type="text" value="'.$stdid.'" size="12" name="id" /> '
                    108:                  ,$chooseopt).'<br />'.
                    109:                  '<input type="submit" name="display" value="'.&mt('Update Display').'" /></p>');
1.75      www       110:         if (($uname) && ($udom)) {
                    111:             $r->print('<p>'.&mt('Full Name: [_1]',&Apache::loncommon::plainname($uname,$udom)).'</p>');
                    112:         }
1.74      www       113:     }
                    114:     $r->rflush();
1.53      www       115: 
                    116:     my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=
                    117:        &getData($showPoints,$uname,$udom);
                    118: 
                    119:     if ($showCategories) {
                    120:        &outputCategories($r,$showPoints,$notshowTotals,
                    121:                  $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
                    122:     } else {
                    123:        &outputTable($r,$showPoints,$notshowTotals,
1.51      www       124:                  $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
1.53      www       125:     }
1.74      www       126:     if ($cangrade) { $r->print("\n</form>\n"); }
1.55      www       127:     &endGradeScreen($r);
1.51      www       128:     return OK;
                    129: 
                    130: }
1.2       bowersj2  131: 
1.55      www       132: sub startGradeScreen {
                    133:     my ($r,$mode)=@_;
                    134: 
                    135:     my $showPoints =
                    136:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
                    137:     my $notshowSPRSlink =
                    138:         (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
                    139:       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals')
                    140:       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories'));
                    141:     my $notshowTotals=
                    142:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
                    143:     my $showCategories=
                    144:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
                    145: 
                    146:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
                    147:     my $allowed_to_edit =  &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
                    148: 
                    149:     if ($allowed_to_view) {
1.61      raeburn   150:        my @notes;
                    151:        push(@notes,&mt('Students do not see total points.')) if ($notshowTotals);
                    152:        push(@notes,&mt('Students do not see link to spreadsheet.')) if ($notshowSPRSlink);
                    153:        push(@notes,&mt('Students will see points based on problem weights.')) if ($showPoints);
                    154:        push(@notes,&mt('Students will see points based on categories.')) if ($showCategories);
                    155:        push(@notes, &Apache::lonhtmlcommon::coursepreflink(&mt('Grade display settings'),'grading'));
                    156:        $r->print(&Apache::loncommon::head_subbox(join('&nbsp;&nbsp;',@notes)));
1.55      www       157:     }
                    158: 
                    159: 
1.56      www       160:     $r->print("\n".'<ul class="LC_TabContentBigger" id="main">');
1.57      www       161:     $r->print("\n".'<li'.($mode eq 'quick'?' class="active"':'').'><a href="/adm/quickgrades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
1.62      www       162:                                           ($showPoints?&mt('Individual Points Overview'):($showCategories?&mt('Grades Overview'):&mt('Completion Overview'))).
1.57      www       163:                                           '&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
1.55      www       164: 
                    165:     if (!($showPoints || $notshowSPRSlink) || ($allowed_to_view)) {
1.56      www       166:        $r->print("\n".'<li'.($mode eq 'spreadsheet'?' class="active"':'').'><a href="/adm/'.($allowed_to_view?'classcalc':'studentcalc').'"><b>'.
1.57      www       167:                                                                  &mt('Spreadsheet (Detailed)').'</b></a></li>');
1.55      www       168:     }
1.58      www       169:     if ($allowed_to_view) {
1.63      www       170:        $r->print("\n".'<li'.($mode eq 'statistics'?' class="active"':'').'><a href="/adm/statistics"><b>'.
                    171:                                                                  &mt('Statistics and Reports').'</b></a></li>');
                    172: 
1.58      www       173:        $r->print("\n".'<li'.($mode eq 'chart'?' class="active"':'').'><a href="/adm/statistics?reportSelected=student_assessment"><b>'.
                    174:                                                                  &mt('Assessment Overview Chart').'</b></a></li>');
                    175: 
                    176:     }
1.59      www       177:     if ($allowed_to_edit) {
                    178:        $r->print("\n".'<li'.($mode eq 'grading'?' class="active"':'').'><a href="/adm/grades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
1.66      www       179:                                                                  &mt('Content Grading').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
                    180:        if ($env{'form.symb'}) {
                    181:           $r->print("\n".'<li'.($mode eq 'probgrading'?' class="active"':'').'><a href="/adm/grades?symb='.
                    182:                                               &Apache::lonhtmlcommon::entity_encode($env{'form.symb'}).
                    183:                                               '&command=gradingmenu"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
                    184:                                               &mt('Problem Grading').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
                    185: 
                    186:        }
1.59      www       187:     }
1.56      www       188:     $r->print("\n".'</ul>'."\n");
1.55      www       189:     $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;">');
                    190: }
                    191: 
                    192: sub endGradeScreen {
                    193:    my ($r)=@_;
1.72      www       194:    $r->print('</div></div></div>'.&Apache::loncommon::end_page());
1.55      www       195: }
                    196: 
                    197: 
1.51      www       198: sub getData {
                    199: 
1.53      www       200:     my ($showPoints,$uname,$udom)=@_;
                    201: 
1.51      www       202:     # Create the nav map
1.53      www       203:     my $navmap = Apache::lonnavmaps::navmap->new($uname,$udom);
1.51      www       204: 
                    205:     my $res = $navmap->firstResource(); # temp resource to access constants
1.1       bowersj2  206: 
                    207:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
                    208:     my $depth = 1;
                    209:     $iterator->next(); # ignore first BEGIN_MAP
                    210:     my $curRes = $iterator->next();
1.4       bowersj2  211:     
1.5       bowersj2  212:     # General overview of the following: Walk along the course resources.
                    213:     # For every problem in the resource, tell its parent maps how many
                    214:     # parts and how many parts correct it has. After that, each map will
                    215:     # have a count of the total parts underneath it, correct and otherwise.
                    216:     # After that, we will walk through the course again and read off
                    217:     # maps in order, with their data. 
                    218:     # (If in the future people decide not to be cumulative, only add
                    219:     #  the counts to the parent map.)
1.17      bowersj2  220:     # For convenience, "totalParts" is also "totalPoints" when we're looking
                    221:     #  at points; I can't come up with a variable name that makes sense
                    222:     #  equally for both cases.
1.5       bowersj2  223: 
                    224:     my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
1.28      bowersj2  225:     my $totalAttempted = 0;
1.14      bowersj2  226:     my $now = time();
1.28      bowersj2  227:     my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
1.5       bowersj2  228: 
                    229:     # Pre-run: Count parts correct
1.1       bowersj2  230:     while ( $depth > 0 ) {
                    231:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    232:         if ($curRes == $iterator->END_MAP()) { $depth--; }
                    233: 
1.7       bowersj2  234:         if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
1.5       bowersj2  235:         {
                    236:             # Get number of correct, incorrect parts
                    237:             my $parts = $curRes->parts();
                    238:             my $partsRight = 0;
1.17      bowersj2  239: 	    my $partsCount = 0;
1.28      bowersj2  240: 	    my $partsAttempted = 0;
1.5       bowersj2  241:             my $stack = $iterator->getStack();
                    242:             
                    243:             for my $part (@{$parts}) {
1.28      bowersj2  244: 		my $completionStatus = $curRes->getCompletionStatus($part);
                    245: 		my $dateStatus = $curRes->getDateStatus($part);
                    246: 		
                    247:                 if ($completionStatus == $curRes->EXCUSED()) {
1.21      matthew   248:                     next;
                    249:                 }
1.17      bowersj2  250: 		if ($showPoints) {
1.28      bowersj2  251: 		    my $score = 0;
                    252: 		    # If we're not telling status and the answer date isn't passed yet, 
                    253: 		    # it's an "attempted" point
1.42      raeburn   254: 		    if ((($curRes->problemstatus($part) eq 'no') ||
                    255:                         ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
1.28      bowersj2  256: 			($dateStatus != $curRes->ANSWER_OPEN)) {
1.31      albertel  257: 			my $status = $curRes->simpleStatus($part);
                    258: 			if ($status == $curRes->ATTEMPTED) {
                    259: 			    $partsAttempted += $curRes->weight($part);
                    260: 			    $totalAttempted += $partsAttempted;
                    261: 			}
1.28      bowersj2  262: 		    } else {
1.39      albertel  263: 			$score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
1.28      bowersj2  264: 		    }
1.17      bowersj2  265: 		    $partsRight += $score;
                    266: 		    $totalRight += $score;
1.19      bowersj2  267: 		    $partsCount += $curRes->weight($part);
1.18      bowersj2  268: 
1.17      bowersj2  269: 		    if ($curRes->opendate($part) < $now) {
1.20      bowersj2  270: 			$totalPossible += $curRes->weight($part);
1.17      bowersj2  271: 		    }
1.19      bowersj2  272: 		    $totalParts += $curRes->weight($part);
1.17      bowersj2  273: 		} else {
1.27      bowersj2  274: 		    my $status = $curRes->simpleStatus($part);
1.17      bowersj2  275: 		    my $thisright = 0;
                    276: 		    $partsCount++;
1.37      albertel  277: 		    if ($status == $curRes->CORRECT ||
                    278: 			$status == $curRes->PARTIALLY_CORRECT ) {
1.17      bowersj2  279: 			$partsRight++;
                    280: 			$totalRight++;
                    281: 			$thisright = 1;
                    282: 		    }
1.28      bowersj2  283: 
                    284: 		    if ($status == $curRes->ATTEMPTED) {
                    285: 			$partsAttempted++;
                    286: 			$totalAttempted++;
                    287: 		    }
1.17      bowersj2  288: 		    
                    289: 		    my $dateStatus = $curRes->getDateStatus($part);
1.19      bowersj2  290: 		    $totalParts++;
1.17      bowersj2  291: 		    if ($curRes->opendate($part) < $now) {
                    292: 			$totalPossible++;
                    293: 		    }
                    294: 		}
1.5       bowersj2  295:             }
1.15      bowersj2  296: 
                    297:             if ($depth == 1) { # in top-level only
1.19      bowersj2  298: 		$topLevelParts += $partsCount;
1.15      bowersj2  299: 		$topLevelRight += $partsRight;
1.28      bowersj2  300: 		$topLevelAttempted += $partsAttempted;
1.15      bowersj2  301: 	    }
                    302: 
1.5       bowersj2  303:             # Crawl down stack and record parts correct and total
                    304:             for my $res (@{$stack}) {
                    305:                 if (ref($res) && $res->is_map()) {
                    306:                     if (!defined($res->{DATA}->{CHILD_PARTS})) {
                    307:                         $res->{DATA}->{CHILD_PARTS} = 0;
                    308:                         $res->{DATA}->{CHILD_CORRECT} = 0;
1.28      bowersj2  309: 			$res->{DATA}->{CHILD_ATTEMPTED} = 0;
1.5       bowersj2  310:                     }
                    311:                     
1.17      bowersj2  312:                     $res->{DATA}->{CHILD_PARTS} += $partsCount;
1.5       bowersj2  313:                     $res->{DATA}->{CHILD_CORRECT} += $partsRight;
1.28      bowersj2  314: 		    $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
1.5       bowersj2  315:                 }
                    316:             }
                    317:         }
                    318:         $curRes = $iterator->next();
                    319:     }
1.51      www       320:     return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
                    321: }
                    322: 
                    323: #
                    324: # Outputting everything.
                    325: #
                    326: 
                    327: sub outputTable {
1.5       bowersj2  328: 
1.51      www       329:     my ($r,$showPoints,$notshowTotals,
                    330:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
1.5       bowersj2  331: 
1.7       bowersj2  332:     my @start = (255, 255, 192);
1.5       bowersj2  333:     my @end   = (0, 192, 0);
                    334: 
                    335:     my $indentString = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                    336: 
                    337:     # Second pass: Print the maps.
1.43      bisitz    338:     $r->print(&Apache::loncommon::start_data_table()
                    339:              .&Apache::loncommon::start_data_table_header_row()
                    340:              .'<th>'.&mt('Folder').'</th>');
1.51      www       341:     my $title = &mt($showPoints ? "Points Scored" : "Done");
1.28      bowersj2  342:     if ($totalAttempted) {
1.51      www       343:         $title .= " / " . &mt("Attempted");
1.28      bowersj2  344:     }
1.49      www       345:     $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
1.43      bisitz    346:              .&Apache::loncommon::end_data_table_header_row());
1.51      www       347: #
                    348: # Output of folder scores
                    349: #
                    350: 
                    351:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
                    352:     my $depth = 1;
                    353:     $iterator->next(); # ignore first BEGIN_MAP
                    354:     my $curRes = $iterator->next();
                    355: 
1.5       bowersj2  356:     while ($depth > 0) {
                    357:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    358:         if ($curRes == $iterator->END_MAP()) { $depth--; }
                    359: 
                    360:         if (ref($curRes) && $curRes->is_map()) {
                    361:             my $title = $curRes->compTitle();
                    362:             
                    363:             my $correct = $curRes->{DATA}->{CHILD_CORRECT};
                    364:             my $total = $curRes->{DATA}->{CHILD_PARTS};
1.28      bowersj2  365: 	    my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
1.5       bowersj2  366: 
1.6       bowersj2  367:             if ($total > 0) {
                    368:                 my $ratio;
                    369:                 $ratio = $correct / $total;
1.51      www       370:                 my $color = &mixColors(\@start, \@end, $ratio);
1.43      bisitz    371:                 $r->print(&Apache::loncommon::start_data_table_row()
                    372:                          .'<td style="background-color:'.$color.';">');
1.6       bowersj2  373:                 
1.15      bowersj2  374: 		my $thisIndent = '';
                    375:                 for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
1.6       bowersj2  376:                 
1.15      bowersj2  377:                 $r->print("$thisIndent$title</td>");
1.28      bowersj2  378: 		if ($totalAttempted) {
1.45      bisitz    379: 		    $r->print('<td valign="top">'
                    380:                              .$thisIndent
                    381:                              .'<span class="LC_nobreak">'
1.49      www       382:                              .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
1.45      bisitz    383:                              .'</span></td>'
                    384:                              .&Apache::loncommon::end_data_table_row()
                    385:                     );
1.28      bowersj2  386: 		} else {
1.45      bisitz    387: 		    $r->print('<td valign="top">'
                    388:                              .$thisIndent
                    389:                              .'<span class="LC_nobreak">'
1.49      www       390:                              .$correct.($notshowTotals?'':' / '.$total)
1.45      bisitz    391:                              .'</span></td>'
1.43      bisitz    392:                              .&Apache::loncommon::end_data_table_row());
1.28      bowersj2  393: 		}
1.6       bowersj2  394:             }
1.5       bowersj2  395:         }
1.4       bowersj2  396: 
1.5       bowersj2  397:         $curRes = $iterator->next();
                    398:     }
1.4       bowersj2  399: 
1.6       bowersj2  400:     # If there were any problems at the top level, print an extra "catchall"
1.15      bowersj2  401:     if ($topLevelParts > 0) {
                    402:         my $ratio = $topLevelRight / $topLevelParts;
1.68      www       403:         my $color = &mixColors(\@start, \@end, $ratio);
1.43      bisitz    404:         $r->print(&Apache::loncommon::start_data_table_row()
                    405:                  .'<td style="background-color:'.$color.';">');
1.25      www       406:         $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
1.43      bisitz    407:         $r->print("$topLevelRight / $topLevelParts</td>"
                    408:                  .&Apache::loncommon::end_data_table_row());
1.6       bowersj2  409:     }
1.4       bowersj2  410: 
1.51      www       411: #
                    412: # show totals (if applicable), close table
                    413: #
1.35      albertel  414:     if ($showPoints) {
1.68      www       415:         my $maxHelpLink = &Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
1.2       bowersj2  416: 
1.51      www       417:         $title = $showPoints ? "Points" : "Parts Done";
                    418:         my $totaltitle = $showPoints ? &mt("Awarded Total Points") : &mt("Total Parts Done");
                    419:         $r->print(&Apache::loncommon::start_data_table_row()
1.48      bisitz    420:                  .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
1.51      www       421:         $r->print(&mt('Max Possible To Date')." $maxHelpLink: <b>$totalPossible</b><br />");
                    422:         $title = $showPoints ? "Points" : "Parts";
                    423:         $r->print(&mt("Total $title In Course").': <b>'.$totalParts.'</b></td>'
1.43      bisitz    424:                  .&Apache::loncommon::end_data_table_row());
1.34      www       425:     }
1.1       bowersj2  426: 
1.72      www       427:     $r->print(&Apache::loncommon::end_data_table());
1.5       bowersj2  428: }
                    429: 
1.53      www       430: #
1.65      www       431: # === Outputting category-based grades.
                    432: #
                    433: # $category{'order'}: output order of categories by id
                    434: # $category{'all'}: complete list of all categories 
                    435: # $category{$id.'_name'}: display-name of category
1.53      www       436: #
                    437: 
                    438: sub outputCategories {
                    439: 
                    440:     my ($r,$showPoints,$notshowTotals,
                    441:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
1.62      www       442: # Take care of storing and retrieving categories
                    443: 
1.64      www       444:     my $cangrade=&Apache::lonnet::allowed('mgr');
                    445: 
1.62      www       446:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    447:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.64      www       448:     my %categories=();
1.65      www       449: # Loading old categories
                    450:     %categories=&Apache::lonnet::dump('grading_categories',$cdom,$cnum);
1.64      www       451: # Storing
1.72      www       452:     if (($cangrade) && (($env{'form.storechanges'}) || ($env{'form.storemove'} ne '') || ($env{'form.cmd'} ne ''))) {
1.65      www       453: # Process the changes
                    454:         %categories=&process_category_edits($r,$cangrade,%categories);
1.64      www       455: # Actually store
                    456:         &Apache::lonnet::put('grading_categories',\%categories,$cdom,$cnum);
                    457:     }
1.65      www       458: # new categories loaded now
                    459:     &output_category_table($r,$cangrade,$navmap,%categories);
                    460: #
                    461:     if ($cangrade) {
                    462:         $r->print('<input type="hidden" name="storemove" value="" />'.
1.72      www       463:                   '<input type="hidden" name="cmd" value="" />'.
                    464:                   '<input type="submit" name="storechanges" value="'.&mt("Save changes to grading categories").'" />'.
1.74      www       465:                   '<script>function storecmd (cmd) { document.quickform.cmd.value=cmd; document.quickform.submit(); }</script>');
1.65      www       466:     }
1.82    ! www       467: #
        !           468: # Debug
        !           469: #
        !           470: #    my %data=&dumpdata($navmap);
        !           471: #    foreach (keys(%data)) {
        !           472: #        $r->print("\n<br />".$_.'='.$data{$_});
        !           473: #     }
        !           474: }
        !           475: 
        !           476: #
        !           477: # Get data for all symbs
        !           478: #
        !           479: 
        !           480: sub dumpdata {
        !           481:     my ($navmap)=@_;
        !           482:     my %returndata=();
        !           483: 
        !           484: # Run through the map and get all data
        !           485: 
        !           486:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
        !           487:     my $depth = 1;
        !           488:     $iterator->next(); # ignore first BEGIN_MAP
        !           489:     my $curRes = $iterator->next();
        !           490: 
        !           491:     while ($depth > 0) {
        !           492:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
        !           493:         if ($curRes == $iterator->END_MAP()) { $depth--; }
        !           494:         if (ref($curRes) && $curRes->is_map()) {
        !           495:             $returndata{$curRes->symb()}=$curRes->{DATA}->{CHILD_PARTS}.':'.$curRes->{DATA}->{CHILD_ATTEMPTED}.':'.$curRes->{DATA}->{CHILD_CORRECT};
        !           496:         }
        !           497:         $curRes = $iterator->next();
        !           498:     }
        !           499:     return %returndata;
1.65      www       500: }
                    501: 
                    502: #
                    503: # Process editing commands, update category hash
                    504: #
                    505: 
                    506: sub process_category_edits {
                    507:     my ($r,$cangrade,%categories)=@_;
                    508:     unless ($cangrade) { return %categories; }
1.72      www       509: # First store everything
                    510:     foreach my $id (split(/\,/,$categories{'order'})) {
1.80      www       511: # Set names, types, and weight (there is only one of each per category)
1.72      www       512:         %categories=&set_category_name($cangrade,$id,$env{'form.name_'.$id},%categories);
                    513:         %categories=&set_category_total($cangrade,$id,$env{'form.totaltype_'.$id},$env{'form.total_'.$id},%categories);
                    514:         %categories=&set_category_weight($cangrade,$id,$env{'form.weight_'.$id},%categories);
1.81      www       515:         %categories=&set_category_displayachieved($cangrade,$id,$env{'form.displayachieved_'.$id},%categories);
1.80      www       516: # Set values for category rules (before names may change)
                    517:         %categories=&set_category_rules($cangrade,$id,%categories);
1.72      www       518:     }
                    519: 
                    520: # Now deal with commands
1.67      www       521:     my $cmd=$env{'form.cmd'};
                    522:     if ($cmd eq 'createnewcat') {
1.69      www       523:         %categories=&make_new_category($r,$cangrade,undef,%categories);
1.72      www       524:     } elsif ($cmd=~/^up\_(.+)$/) {
                    525:         %categories=&move_up_category($1,$cangrade,%categories);
                    526:     } elsif ($cmd=~/^down\_(.+)$/) {
                    527:         %categories=&move_down_category($1,$cangrade,%categories);
1.69      www       528:     } elsif ($cmd=~/^delcat\_(.+)$/) {
                    529:         %categories=&del_category($1,$cangrade,%categories);
1.75      www       530:     } elsif ($cmd=~/^addcont\_(.+)$/) {
                    531:         %categories=&add_category_content($1,$cangrade,$env{'form.addcont_'.$1.'_symb'},%categories);
                    532:     } elsif ($cmd=~/^delcont\_(.+)\_\_\_\_\_\_(.+)$/) {
                    533:         %categories=&del_category_content($1,$cangrade,$2,%categories);
1.77      www       534:     } elsif ($cmd=~/^newrule\_(.+)$/) {
                    535:         %categories=&add_calculation_rule($1,$cangrade,':',%categories);
1.79      www       536:     } elsif ($cmd=~/^delrule\_(.+)\_\_\_\_\_\_(.*)$/) {
                    537:         %categories=&del_calculation_rule($1,$cangrade,$2,%categories);
1.73      www       538:     }
                    539: # Move to a new position
                    540:     my $moveid=$env{'form.storemove'};
                    541:     if ($moveid) {
                    542:         %categories=&move_category($moveid,$cangrade,$env{'form.newpos_'.$moveid},%categories);
1.72      www       543:     } 
1.65      www       544:     return %categories;
                    545: }
                    546: 
                    547: #
                    548: # Output the table
                    549: #
                    550: 
                    551: sub output_category_table {
                    552:     my ($r,$cangrade,$navmaps,%categories)=@_;
                    553:     my $sum=0;
1.67      www       554:     my $total=0;
1.65      www       555:     $r->print(&Apache::loncommon::start_data_table());
                    556: #
                    557:     &output_category_table_header($r,$cangrade);
                    558: #
                    559:     my @order=split(/\,/,$categories{'order'});
                    560: #
                    561:     my $maxpos=$#order;
                    562:     for (my $i=0;$i<=$maxpos;$i++) {
                    563:         my ($value,$weight)=&output_and_calc_category($r,$cangrade,$navmaps,$order[$i],$i,$maxpos,%categories);
1.67      www       564:         $sum+=$value*$weight;
                    565:         $total+=$weight;
1.65      www       566:     }
                    567: #
1.67      www       568:     &bottom_line_category($r,$cangrade,$sum,$total);
1.65      www       569: #
                    570:     $r->print(&Apache::loncommon::end_data_table());
                    571:     return $sum;
                    572: }
                    573: 
                    574: sub output_category_table_header {
                    575:     my ($r,$cangrade)=@_;
                    576:     $r->print(&Apache::loncommon::start_data_table_header_row());
                    577:     if ($cangrade) {
                    578:         $r->print('<th colspan="2">'.&mt("Move").'</th><th>'.&mt('Action').'</th>');
                    579:     }
                    580:     $r->print('<th>'.&mt('Category').'</th>'.
                    581:               '<th>'.&mt('Contents').'</th>'.
1.81      www       582:               '<th>'.&mt('Total Points').'</th>'.
1.65      www       583:               '<th>'.&mt('Calculation').'</th>'.
1.81      www       584:               '<th>'.&mt('Relative Weight').'</th>'.
                    585:               '<th>'.&mt('Achieved').'</th>');
1.65      www       586:     $r->print(&Apache::loncommon::end_data_table_header_row());
                    587: }
                    588: 
                    589: 
                    590: #
                    591: # Output one category to table
                    592: #
                    593: 
                    594: sub output_and_calc_category {
                    595:     my ($r,$cangrade,$navmaps,$id,$currentpos,$maxpos,%categories)=@_;
                    596:     my $value=0;
                    597:     my $weight=0;
                    598:     my $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL') . "/");
                    599:     my %lt=&Apache::lonlocal::texthash(
                    600:            'up' => 'Move Up',
                    601:            'dw' => 'Move Down');
                    602:     $r->print("\n".&Apache::loncommon::start_data_table_row());
                    603: 
                    604:     if ($cangrade) {
                    605:         $r->print(<<ENDMOVE);
                    606: <td>
                    607: <div class="LC_docs_entry_move">
1.72      www       608:   <a href='javascript:storecmd("up_$id");'>
1.65      www       609:     <img src="${iconpath}move_up.gif" alt='$lt{'up'}' class="LC_icon" />
                    610:   </a>
                    611: </div>
                    612: <div class="LC_docs_entry_move">
1.72      www       613:   <a href='javascript:storecmd("down_$id");'>
1.65      www       614:     <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' class="LC_icon" />
                    615:   </a>
                    616: </div>
                    617: </td>
                    618: ENDMOVE
                    619:         $r->print("\n<td>\n<select name='newpos_$id' onchange='this.form.storemove.value=\"$id\";this.form.submit()'>");
                    620:         for (my $i=0;$i<=$maxpos;$i++) {
                    621:             if ($i==$currentpos) {
                    622:                 $r->print('<option value="" selected="selected">('.$i.')</option>');
                    623:             } else {
                    624:                 $r->print('<option value="'.$i.'">'.$i.'</option>');
                    625:             }
                    626:         }
                    627:         $r->print("\n</select>\n</td>\n");
1.72      www       628:         $r->print('<td><a href="javascript:storecmd(\'delcat_'.$id.'\');">'.&mt('Delete').'</a></td>');
1.69      www       629:         $r->print('<td><input type="text" name="name_'.$id.
                    630:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_name'}).'" /></td>');
                    631:     } else {
                    632:         $r->print('<td>'.$categories{$id.'_name'}.'</td>');
1.65      www       633:     }
1.70      www       634: # Content
1.75      www       635:     $r->print('<td><ul>');
                    636:     foreach my $contentid (split(/\,/,$categories{$id.'_content'})) {
                    637:         $r->print('<li>');
                    638:         $r->print(&Apache::lonnet::gettitle($contentid));
                    639:         if ($cangrade) {
                    640:            $r->print(' <a href="javascript:storecmd(\'delcont_'.$id.'______'.$contentid.'\');">'.&mt('Delete').'</a>');
                    641:         }
                    642:         $r->print('</li>');
                    643:     }
                    644:     $r->print('</ul>');
1.70      www       645:     if ($cangrade) {
1.82    ! www       646:         $r->print('<br />'.&mt('Add Problem or Folder').'<br />'.
1.75      www       647:                   &Apache::lonstathelpers::problem_selector('.',undef,1,1,'addcont_'.$id.'_',1,'this.form.cmd.value="addcont_'.$id.'";this.form.submit();'));
1.70      www       648:     }
1.75      www       649:     $r->print('</td>'); 
1.81      www       650: 
                    651: # Total
                    652:     if ($cangrade) {
                    653:        $r->print('<td>'.
                    654:                   '<select name="totaltype_'.$id.'">'.
                    655:                   '<option value="default"'.($categories{$id.'_totaltype'} eq 'default'?' selected="selected"':'').'>'.&mt('default').'</option>'.
                    656:                   '<option value="typein"'.($categories{$id.'_totaltype'} eq 'typein'?' selected="selected"':'').'>'.&mt('Type-in value').'</option>'.
                    657:                   '</select>'.
                    658:                   '<input type="text" size="4" name="total_'.$id.
                    659:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_total'}).'" /></td>');
                    660:     } else {
                    661:         $r->print('<td>'.($categories{$id.'_totaltype'} eq 'default'?&mt('default'):$categories{$id.'_total'}).'</td>');
                    662:     }
                    663: 
                    664: 
1.70      www       665: # Calculation
1.76      www       666:     $r->print('<td><ul>');
                    667:     foreach my $calcrule (split(/\,/,$categories{$id.'_calculations'})) {
                    668:         $r->print('<li>');
1.78      www       669:         my ($code,$value)=split(/\:/,$calcrule);
                    670:         $r->print(&pretty_prt_rule($cangrade,$id,$code,$value));
1.76      www       671:         if ($cangrade) {
1.79      www       672:            $r->print(' <a href="javascript:storecmd(\'delrule_'.$id.'______'.$code.'\');">'.&mt('Delete').'</a>');
1.76      www       673:         }
                    674:         $r->print('</li>');
                    675:     }
                    676:     $r->print('</ul>');
1.70      www       677:     if ($cangrade) {
1.77      www       678:         $r->print('<br />'.&new_calc_rule_form($id));
1.70      www       679:     }
1.76      www       680:     $r->print('</td>');
                    681: 
1.70      www       682: # Weight
                    683:     if ($cangrade) {
                    684:        $r->print('<td>'.
                    685:                   '<input type="text" size="4" name="weight_'.$id.
                    686:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_weight'}).'" /></td>');
                    687:     } else {
1.71      www       688:         $r->print('<td>'.$categories{$id.'_weight'}.'</td>');
1.70      www       689:     }
1.81      www       690: # Achieved
                    691:     $r->print('<td>');
                    692:     if ($cangrade) {
                    693:         $r->print('<select name="displayachieved_'.$id.'">'.
                    694:                   '<option value="percent"'.($categories{$id.'_displayachieved'} eq 'percent'?' selected="selected"':'').'>'.&mt('percent').'</option>'.
                    695:                   '<option value="points"'.($categories{$id.'_displayachieved'} eq 'points'?' selected="selected"':'').'>'.&mt('points').'</option>'.
                    696:                   '</select>');
                    697:     } else {
                    698:         if ($categories{$id.'_displayachieved'} eq 'percent') {
                    699:             $r->print(&mt('percent'));
                    700:         } else {
                    701:             $r->print(&mt('points'));
                    702:         }
                    703:     }
                    704:     $r->print('</td>');
1.70      www       705: 
1.65      www       706:     return ($value,$weight);
                    707: }
                    708: 
                    709: #
                    710: # Bottom line with grades
                    711: #
                    712: 
                    713: sub bottom_line_category {
1.67      www       714:     my ($r,$cangrade,$sum,$total)=@_;
                    715:     $r->print(&Apache::loncommon::start_data_table_row());
                    716:     if ($cangrade) {
1.72      www       717:         $r->print('<td colspan="3"><a href="javascript:storecmd(\'createnewcat\');">'.&mt('Create New Category').'</a></td>');
1.67      www       718:     }
1.81      www       719:     $r->print('<td colspan="6">'.&mt('Current:').$sum.'<br />'.&mt('Total:').$total.'<br /></td>');
1.65      www       720: }
                    721: 
                    722: #
                    723: # Make one new category
                    724: #
                    725: 
                    726: sub make_new_category {
                    727:     my ($r,$cangrade,$ordernum,%categories)=@_;
                    728:     unless ($cangrade) { return %categories; }
                    729: # Generate new ID
                    730:     my $id=time.'_'.$$.'_'.rand(10000);
                    731: # Add new ID to list of all IDs ever created in this course
                    732:     $categories{'all'}.=','.$id;
                    733:     $categories{'all'}=~s/^\,//;
                    734: # Add new ID to ordered list of displayed and evaluated categories
                    735:     $categories{'order'}.=','.$id;
                    736:     $categories{'order'}=~s/^\,//;
                    737: # Move it into desired space
                    738:     if (defined($ordernum)) {
                    739:         %categories=&move_category($id,$cangrade,$ordernum,%categories);
                    740:     }
1.71      www       741:     $categories{$id.'_weight'}=0;
                    742:     $categories{$id.'_totaltype'}='default';
1.81      www       743:     $categories{$id.'_displayachieved'}='percent';
1.65      www       744:     return %categories;
1.53      www       745: }
                    746: 
1.76      www       747: 
                    748: # === Calculation Rule Editing
                    749: 
1.80      www       750: sub category_rule_codes {
                    751:     return &Apache::lonlocal::texthash(
1.78      www       752:                 'droplow'  => 'Drop N lowest grade assignments',
                    753:                 'drophigh' => 'Drop N highest grade assignments',
                    754:                 'capabove' => 'Cap percentage above N percent',
                    755:                 'capbelow' => 'Cap percentage below N percent');
1.80      www       756: }
                    757: 
                    758: sub pretty_prt_rule {
                    759:     my ($cangrade,$id,$code,$value)=@_;
                    760:     my $cid=$id.'_'.$code;
                    761:     my %lt=&category_rule_codes();
1.78      www       762:     my $ret='<span class="LC_nobreak">';
                    763:     if ($cangrade) {
                    764:         $ret.='<select name="sel_'.$cid.'">';
                    765:         foreach my $calc (''=>'',sort(keys(%lt))) {
                    766:             $ret.='<option value="'.$calc.'"'.($calc eq $code?' selected="selected"':'').' />'.$lt{$calc}.'</input>';
                    767:         }
1.80      www       768:         $ret.='</select> N=<input type="text" size="5" name="val_'.$cid.'" value="'.$value.'" /></span>';
1.78      www       769:     } else {
                    770:         $ret.=$lt{$code}.'; N='.$value;
                    771:     }
                    772:     $ret.='</span>';
                    773:     return $ret;
1.76      www       774: }
                    775: 
                    776: sub new_calc_rule_form {
1.77      www       777:     my ($id)=@_;
                    778:     return '<a href="javascript:storecmd(\'newrule_'.$id.'\');">'.&mt('New Calculation Rule').'</a>';
1.76      www       779: }
                    780: 
                    781: #
                    782: # Add a calculation rule
                    783: #
                    784: 
                    785: sub add_calculation_rule {
                    786:     my ($id,$cangrade,$newcontent,%categories)=@_;
                    787:     unless ($cangrade) { return %categories; }
                    788:     my %newcontent=($newcontent => 1);
                    789:     foreach my $current (split(/\,/,$categories{$id.'_calculations'})) {
                    790:         $newcontent{$current}=1;
                    791:     }
                    792:     $categories{$id.'_calculations'}=join(',',sort(keys(%newcontent)));
                    793:     return %categories;
                    794: }
                    795: 
                    796: #
                    797: # Delete a calculation rule
                    798: #
                    799: 
                    800: sub del_calculation_rule {
                    801:     my ($id,$cangrade,$delcontent,%categories)=@_;
                    802:     unless ($cangrade) { return %categories; }
                    803:     my @newcontent=();
                    804:     foreach my $current (split(/\,/,$categories{$id.'_calculations'})) {
1.78      www       805:         unless ($current=~/^\Q$delcontent\E\:/) {
1.76      www       806:             push(@newcontent,$current);
                    807:         }
                    808:     }
                    809:     $categories{$id.'_calculations'}=join(',',@newcontent);
                    810:     return %categories;
                    811: }
                    812: 
1.80      www       813: sub set_category_rules {
                    814:     my ($cangrade,$id,%categories)=@_;
                    815:     unless ($cangrade) { return %categories; }
                    816:     my %lt=&category_rule_codes();
                    817:     my @newrules=();
                    818:     foreach my $code ('',(keys(%lt))) {
                    819:         if ($env{'form.sel_'.$id.'_'.$code}) {
                    820:             push(@newrules,$env{'form.sel_'.$id.'_'.$code}.':'.$env{'form.val_'.$id.'_'.$code});
                    821:         }
                    822:     }
                    823:     $categories{$id.'_calculations'}=join(',',sort(@newrules));
                    824:     return %categories;
                    825: }
                    826: 
                    827: 
1.76      www       828: # === Category Editing
                    829: 
1.65      www       830: #
1.75      www       831: # Add to category content
                    832: #
                    833: 
                    834: sub add_category_content {
                    835:     my ($id,$cangrade,$newcontent,%categories)=@_;
                    836:     unless ($cangrade) { return %categories; }
                    837:     my %newcontent=($newcontent => 1);
                    838:     foreach my $current (split(/\,/,$categories{$id.'_content'})) {
                    839:         $newcontent{$current}=1;
                    840:     }
                    841:     $categories{$id.'_content'}=join(',',sort(keys(%newcontent)));
                    842:     return %categories;
                    843: }
                    844: 
                    845: #
                    846: # Delete from category content
                    847: #
                    848: 
                    849: sub del_category_content {
                    850:     my ($id,$cangrade,$delcontent,%categories)=@_;
                    851:     unless ($cangrade) { return %categories; }
                    852:     my @newcontent=();
                    853:     foreach my $current (split(/\,/,$categories{$id.'_content'})) {
                    854:         unless ($current eq $delcontent) {
                    855:             push(@newcontent,$current);
                    856:         }
                    857:     }
                    858:     $categories{$id.'_content'}=join(',',@newcontent);
                    859:     return %categories;
                    860: }
                    861: 
                    862: #
1.68      www       863: # Delete category
                    864: #
                    865: 
                    866: sub del_category {
1.75      www       867:     my ($id,$cangrade,%categories)=@_;
                    868:     unless ($cangrade) { return %categories; }
                    869:     my @neworder=();
                    870:     foreach my $currentid (split(/\,/,$categories{'order'})) {
                    871:         unless ($currentid eq $id) {
                    872:             push(@neworder,$currentid);
                    873:         }
                    874:     }
                    875:     $categories{'order'}=join(',',@neworder);
                    876:     return %categories;
1.68      www       877: }
                    878: 
                    879: #
1.72      www       880: # Move category up
                    881: #
                    882: 
                    883: sub move_up_category {
                    884:     my ($id,$cangrade,%categories)=@_;
                    885:     my $currentpos=&current_pos_category($id,%categories);
                    886:     if ($currentpos<1) { return %categories; }
                    887:     return &move_category($id,$cangrade,$currentpos-1,%categories);
                    888: }
                    889: 
                    890: #
                    891: # Move category down
                    892: #
                    893: 
                    894: sub move_down_category {
                    895:     my ($id,$cangrade,%categories)=@_;
                    896:     my $currentpos=&current_pos_category($id,%categories);
                    897:     my @order=split(/\,/,$categories{'order'});
                    898:     if ($currentpos>=$#order) { return %categories; }
                    899:     return &move_category($id,$cangrade,$currentpos+1,%categories);
                    900: }
                    901: 
                    902: #
1.65      www       903: # Move a category to a desired position n the display order
                    904: #
                    905: 
                    906: sub move_category {
                    907:     my ($id,$cangrade,$ordernum,%categories)=@_;
                    908:     unless ($cangrade) { return %categories; }
                    909:     my @order=split(/\,/,$categories{'order'});
                    910: # Where is the index currently?
                    911:     my $currentpos=&current_pos_category($id,%categories);
                    912:     if (defined($currentpos)) {
                    913:         if ($currentpos<$ordernum) {
                    914: # This is moving to a higher index
                    915: # ....X1234....
                    916: # ....1234X....
                    917:             for (my $i=$currentpos;$i<$ordernum;$i++) {
                    918:                 $order[$i]=$order[$i+1];
                    919:             }
                    920:             $order[$ordernum]=$id;
                    921:         }
                    922:         if ($currentpos>$ordernum) {
                    923: # This is moving to a lower index
                    924: # ....1234X....
                    925: # ....X1234....
                    926:             for (my $i=$currentpos;$i>$ordernum;$i--) {
                    927:                 $order[$i]=$order[$i-1];
                    928:             }
                    929:             $order[$ordernum]=$id;
                    930:         }
                    931:     }
                    932:     $categories{'order'}=join(',',@order);
                    933:     return %categories;
                    934: }
                    935: 
                    936: #
                    937: #  Find current postion of a category in the order
                    938: #
                    939: 
                    940: sub current_pos_category {
                    941:     my ($id,%categories)=@_;
                    942:     my @order=split(/\,/,$categories{'order'});
                    943:     for (my $i=0;$i<=$#order;$i++) {
                    944:         if ($order[$i] eq $id) { return $i; }
                    945:     }
                    946: # not found
                    947:     return undef;
                    948: }
                    949: 
                    950: #
                    951: # Set name of a category
                    952: #
                    953: sub set_category_name {
1.69      www       954:     my ($cangrade,$id,$name,%categories)=@_;
                    955:     unless ($cangrade) { return %categories; }
1.65      www       956:     $categories{$id.'_name'}=$name;
                    957:     return %categories;
                    958: }
                    959: 
                    960: #
1.71      www       961: # Set total of a category
1.70      www       962: #
1.71      www       963: sub set_category_total {
                    964:     my ($cangrade,$id,$totaltype,$total,%categories)=@_;
1.70      www       965:     unless ($cangrade) { return %categories; }
1.71      www       966:     if (($categories{$id.'_total'} eq '') && ($total=~/\d/)) {
                    967:         $totaltype='typein';
1.70      www       968:     }
1.71      www       969:     $categories{$id.'_totaltype'}=$totaltype;
                    970:     if ($totaltype eq 'default') {
                    971:         $categories{$id.'_total'}='';
1.70      www       972:     } else {
1.71      www       973:         $total=~s/\D//gs;
                    974:         unless ($total) { $total=0; }
                    975:         $categories{$id.'_total'}=$total;
1.70      www       976:     }
                    977:     return %categories;
                    978: }
                    979: 
1.71      www       980: sub set_category_weight {
                    981:     my ($cangrade,$id,$weight,%categories)=@_;
                    982:     unless ($cangrade) { return %categories; }
                    983:     $weight=~s/\D//gs;
                    984:     unless ($weight) { $weight=0; }
                    985:     $categories{$id.'_weight'}=$weight;
                    986:     return %categories;
                    987: }
1.70      www       988: 
1.81      www       989: sub set_category_displayachieved {
                    990:     my ($cangrade,$id,$value,%categories)=@_;
                    991:     unless ($cangrade) { return %categories; }
                    992:     unless (($value eq 'percent') || ($value eq 'points')) { $value='percent'; }
                    993:     $categories{$id.'_displayachieved'}=$value;
                    994:     return %categories;
                    995: }
                    996: 
                    997: 
1.70      www       998: #
1.65      www       999: # === end category-related
                   1000: #
                   1001: #
1.5       bowersj2 1002: # Pass this two refs to arrays for the start and end color, and a number
                   1003: # from 0 to 1 for how much of the latter you want to mix in. It will
                   1004: # return a string ready to show ("#FFC309");
1.51      www      1005: 
1.5       bowersj2 1006: sub mixColors {
                   1007:     my $start = shift;
                   1008:     my $end = shift;
                   1009:     my $ratio = shift;
                   1010:     
1.9       matthew  1011:     my ($a,$b);
1.5       bowersj2 1012:     my $final = "";
1.9       matthew  1013:     $a = $start->[0]; $b = $end->[0];
1.5       bowersj2 1014:     my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew  1015:     $a = $start->[1]; $b = $end->[1];
1.5       bowersj2 1016:     my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew  1017:     $a = $start->[2]; $b = $end->[2];
1.5       bowersj2 1018:     my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
                   1019: 
1.16      bowersj2 1020:     $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
1.5       bowersj2 1021:     return "#" . $final;
1.1       bowersj2 1022: }
                   1023: 
                   1024: 1;

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