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

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

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