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

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

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