File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.81: download - view: text, annotated - select for diffs
Sat Mar 26 20:04:15 2011 UTC (13 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Achieved points/percent
- this finishes the editor, now need to display the calculations

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

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