File:  [LON-CAPA] / loncom / interface / lonquickgrades.pm
Revision 1.74: download - view: text, annotated - select for diffs
Fri Mar 11 02:39:53 2011 UTC (13 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Pick particular user.

    1: # The LearningOnline Network with CAPA
    2: # Quick Student Grades Display
    3: #
    4: # $Id: lonquickgrades.pm,v 1.74 2011/03/11 02:39:53 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:         $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();
  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,
  117:                  $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
  118:     }
  119:     if ($cangrade) { $r->print("\n</form>\n"); }
  120:     &endGradeScreen($r);
  121:     return OK;
  122: 
  123: }
  124: 
  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) {
  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)));
  150:     }
  151: 
  152: 
  153:     $r->print("\n".'<ul class="LC_TabContentBigger" id="main">');
  154:     $r->print("\n".'<li'.($mode eq 'quick'?' class="active"':'').'><a href="/adm/quickgrades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
  155:                                           ($showPoints?&mt('Individual Points Overview'):($showCategories?&mt('Grades Overview'):&mt('Completion Overview'))).
  156:                                           '&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
  157: 
  158:     if (!($showPoints || $notshowSPRSlink) || ($allowed_to_view)) {
  159:        $r->print("\n".'<li'.($mode eq 'spreadsheet'?' class="active"':'').'><a href="/adm/'.($allowed_to_view?'classcalc':'studentcalc').'"><b>'.
  160:                                                                  &mt('Spreadsheet (Detailed)').'</b></a></li>');
  161:     }
  162:     if ($allowed_to_view) {
  163:        $r->print("\n".'<li'.($mode eq 'statistics'?' class="active"':'').'><a href="/adm/statistics"><b>'.
  164:                                                                  &mt('Statistics and Reports').'</b></a></li>');
  165: 
  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:     }
  170:     if ($allowed_to_edit) {
  171:        $r->print("\n".'<li'.($mode eq 'grading'?' class="active"':'').'><a href="/adm/grades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
  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:        }
  180:     }
  181:     $r->print("\n".'</ul>'."\n");
  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)=@_;
  187:    $r->print('</div></div></div>'.&Apache::loncommon::end_page());
  188: }
  189: 
  190: 
  191: sub getData {
  192: 
  193:     my ($showPoints,$uname,$udom)=@_;
  194: 
  195:     # Create the nav map
  196:     my $navmap = Apache::lonnavmaps::navmap->new($uname,$udom);
  197: 
  198:     my $res = $navmap->firstResource(); # temp resource to access constants
  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();
  204:     
  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.)
  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.
  216: 
  217:     my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
  218:     my $totalAttempted = 0;
  219:     my $now = time();
  220:     my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
  221: 
  222:     # Pre-run: Count parts correct
  223:     while ( $depth > 0 ) {
  224:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
  225:         if ($curRes == $iterator->END_MAP()) { $depth--; }
  226: 
  227:         if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
  228:         {
  229:             # Get number of correct, incorrect parts
  230:             my $parts = $curRes->parts();
  231:             my $partsRight = 0;
  232: 	    my $partsCount = 0;
  233: 	    my $partsAttempted = 0;
  234:             my $stack = $iterator->getStack();
  235:             
  236:             for my $part (@{$parts}) {
  237: 		my $completionStatus = $curRes->getCompletionStatus($part);
  238: 		my $dateStatus = $curRes->getDateStatus($part);
  239: 		
  240:                 if ($completionStatus == $curRes->EXCUSED()) {
  241:                     next;
  242:                 }
  243: 		if ($showPoints) {
  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
  247: 		    if ((($curRes->problemstatus($part) eq 'no') ||
  248:                         ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
  249: 			($dateStatus != $curRes->ANSWER_OPEN)) {
  250: 			my $status = $curRes->simpleStatus($part);
  251: 			if ($status == $curRes->ATTEMPTED) {
  252: 			    $partsAttempted += $curRes->weight($part);
  253: 			    $totalAttempted += $partsAttempted;
  254: 			}
  255: 		    } else {
  256: 			$score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
  257: 		    }
  258: 		    $partsRight += $score;
  259: 		    $totalRight += $score;
  260: 		    $partsCount += $curRes->weight($part);
  261: 
  262: 		    if ($curRes->opendate($part) < $now) {
  263: 			$totalPossible += $curRes->weight($part);
  264: 		    }
  265: 		    $totalParts += $curRes->weight($part);
  266: 		} else {
  267: 		    my $status = $curRes->simpleStatus($part);
  268: 		    my $thisright = 0;
  269: 		    $partsCount++;
  270: 		    if ($status == $curRes->CORRECT ||
  271: 			$status == $curRes->PARTIALLY_CORRECT ) {
  272: 			$partsRight++;
  273: 			$totalRight++;
  274: 			$thisright = 1;
  275: 		    }
  276: 
  277: 		    if ($status == $curRes->ATTEMPTED) {
  278: 			$partsAttempted++;
  279: 			$totalAttempted++;
  280: 		    }
  281: 		    
  282: 		    my $dateStatus = $curRes->getDateStatus($part);
  283: 		    $totalParts++;
  284: 		    if ($curRes->opendate($part) < $now) {
  285: 			$totalPossible++;
  286: 		    }
  287: 		}
  288:             }
  289: 
  290:             if ($depth == 1) { # in top-level only
  291: 		$topLevelParts += $partsCount;
  292: 		$topLevelRight += $partsRight;
  293: 		$topLevelAttempted += $partsAttempted;
  294: 	    }
  295: 
  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;
  302: 			$res->{DATA}->{CHILD_ATTEMPTED} = 0;
  303:                     }
  304:                     
  305:                     $res->{DATA}->{CHILD_PARTS} += $partsCount;
  306:                     $res->{DATA}->{CHILD_CORRECT} += $partsRight;
  307: 		    $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
  308:                 }
  309:             }
  310:         }
  311:         $curRes = $iterator->next();
  312:     }
  313:     return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
  314: }
  315: 
  316: #
  317: # Outputting everything.
  318: #
  319: 
  320: sub outputTable {
  321: 
  322:     my ($r,$showPoints,$notshowTotals,
  323:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
  324: 
  325:     my @start = (255, 255, 192);
  326:     my @end   = (0, 192, 0);
  327: 
  328:     my $indentString = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  329: 
  330:     # Second pass: Print the maps.
  331:     $r->print(&Apache::loncommon::start_data_table()
  332:              .&Apache::loncommon::start_data_table_header_row()
  333:              .'<th>'.&mt('Folder').'</th>');
  334:     my $title = &mt($showPoints ? "Points Scored" : "Done");
  335:     if ($totalAttempted) {
  336:         $title .= " / " . &mt("Attempted");
  337:     }
  338:     $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
  339:              .&Apache::loncommon::end_data_table_header_row());
  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: 
  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};
  358: 	    my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
  359: 
  360:             if ($total > 0) {
  361:                 my $ratio;
  362:                 $ratio = $correct / $total;
  363:                 my $color = &mixColors(\@start, \@end, $ratio);
  364:                 $r->print(&Apache::loncommon::start_data_table_row()
  365:                          .'<td style="background-color:'.$color.';">');
  366:                 
  367: 		my $thisIndent = '';
  368:                 for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
  369:                 
  370:                 $r->print("$thisIndent$title</td>");
  371: 		if ($totalAttempted) {
  372: 		    $r->print('<td valign="top">'
  373:                              .$thisIndent
  374:                              .'<span class="LC_nobreak">'
  375:                              .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
  376:                              .'</span></td>'
  377:                              .&Apache::loncommon::end_data_table_row()
  378:                     );
  379: 		} else {
  380: 		    $r->print('<td valign="top">'
  381:                              .$thisIndent
  382:                              .'<span class="LC_nobreak">'
  383:                              .$correct.($notshowTotals?'':' / '.$total)
  384:                              .'</span></td>'
  385:                              .&Apache::loncommon::end_data_table_row());
  386: 		}
  387:             }
  388:         }
  389: 
  390:         $curRes = $iterator->next();
  391:     }
  392: 
  393:     # If there were any problems at the top level, print an extra "catchall"
  394:     if ($topLevelParts > 0) {
  395:         my $ratio = $topLevelRight / $topLevelParts;
  396:         my $color = &mixColors(\@start, \@end, $ratio);
  397:         $r->print(&Apache::loncommon::start_data_table_row()
  398:                  .'<td style="background-color:'.$color.';">');
  399:         $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
  400:         $r->print("$topLevelRight / $topLevelParts</td>"
  401:                  .&Apache::loncommon::end_data_table_row());
  402:     }
  403: 
  404: #
  405: # show totals (if applicable), close table
  406: #
  407:     if ($showPoints) {
  408:         my $maxHelpLink = &Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
  409: 
  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()
  413:                  .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
  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>'
  417:                  .&Apache::loncommon::end_data_table_row());
  418:     }
  419: 
  420:     $r->print(&Apache::loncommon::end_data_table());
  421: }
  422: 
  423: #
  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
  429: #
  430: 
  431: sub outputCategories {
  432: 
  433:     my ($r,$showPoints,$notshowTotals,
  434:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
  435: # Take care of storing and retrieving categories
  436: 
  437:     my $cangrade=&Apache::lonnet::allowed('mgr');
  438: 
  439:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  440:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  441:     my %categories=();
  442: # Loading old categories
  443:     %categories=&Apache::lonnet::dump('grading_categories',$cdom,$cnum);
  444: # Storing
  445:     if (($cangrade) && (($env{'form.storechanges'}) || ($env{'form.storemove'} ne '') || ($env{'form.cmd'} ne ''))) {
  446: # Process the changes
  447:         %categories=&process_category_edits($r,$cangrade,%categories);
  448: # Actually store
  449:         &Apache::lonnet::put('grading_categories',\%categories,$cdom,$cnum);
  450:     }
  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="" />'.
  456:                   '<input type="hidden" name="cmd" value="" />'.
  457:                   '<input type="submit" name="storechanges" value="'.&mt("Save changes to grading categories").'" />'.
  458:                   '<script>function storecmd (cmd) { document.quickform.cmd.value=cmd; document.quickform.submit(); }</script>');
  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; }
  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
  478:     my $cmd=$env{'form.cmd'};
  479:     if ($cmd eq 'createnewcat') {
  480:         %categories=&make_new_category($r,$cangrade,undef,%categories);
  481:     } elsif ($cmd=~/^up\_(.+)$/) {
  482:         %categories=&move_up_category($1,$cangrade,%categories);
  483:     } elsif ($cmd=~/^down\_(.+)$/) {
  484:         %categories=&move_down_category($1,$cangrade,%categories);
  485:     } elsif ($cmd=~/^delcat\_(.+)$/) {
  486:         %categories=&del_category($1,$cangrade,%categories);
  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);
  492:     } 
  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;
  503:     my $total=0;
  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);
  513:         $sum+=$value*$weight;
  514:         $total+=$weight;
  515:     }
  516: #
  517:     &bottom_line_category($r,$cangrade,$sum,$total);
  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>'.
  532:               '<th>'.&mt('Total Points').'</th>'.
  533:               '<th>'.&mt('Relative Weight').'</th>');
  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">
  556:   <a href='javascript:storecmd("up_$id");'>
  557:     <img src="${iconpath}move_up.gif" alt='$lt{'up'}' class="LC_icon" />
  558:   </a>
  559: </div>
  560: <div class="LC_docs_entry_move">
  561:   <a href='javascript:storecmd("down_$id");'>
  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");
  576:         $r->print('<td><a href="javascript:storecmd(\'delcat_'.$id.'\');">'.&mt('Delete').'</a></td>');
  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>');
  581:     }
  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:     }
  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:     }
  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 {
  614:         $r->print('<td>'.$categories{$id.'_weight'}.'</td>');
  615:     }
  616: 
  617:     return ($value,$weight);
  618: }
  619: 
  620: #
  621: # Bottom line with grades
  622: #
  623: 
  624: sub bottom_line_category {
  625:     my ($r,$cangrade,$sum,$total)=@_;
  626:     $r->print(&Apache::loncommon::start_data_table_row());
  627:     if ($cangrade) {
  628:         $r->print('<td colspan="3"><a href="javascript:storecmd(\'createnewcat\');">'.&mt('Create New Category').'</a></td>');
  629:     }
  630:     $r->print('<td colspan="5">'.&mt('Current:').$sum.'<br />'.&mt('Total:').$total.'<br /></td>');
  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:     }
  652:     $categories{$id.'_weight'}=0;
  653:     $categories{$id.'_totaltype'}='default';
  654:     return %categories;
  655: }
  656: 
  657: #
  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: #
  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: #
  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 {
  748:     my ($cangrade,$id,$name,%categories)=@_;
  749:     unless ($cangrade) { return %categories; }
  750:     $categories{$id.'_name'}=$name;
  751:     return %categories;
  752: }
  753: 
  754: #
  755: # Set total of a category
  756: #
  757: sub set_category_total {
  758:     my ($cangrade,$id,$totaltype,$total,%categories)=@_;
  759:     unless ($cangrade) { return %categories; }
  760:     if (($categories{$id.'_total'} eq '') && ($total=~/\d/)) {
  761:         $totaltype='typein';
  762:     }
  763:     $categories{$id.'_totaltype'}=$totaltype;
  764:     if ($totaltype eq 'default') {
  765:         $categories{$id.'_total'}='';
  766:     } else {
  767:         $total=~s/\D//gs;
  768:         unless ($total) { $total=0; }
  769:         $categories{$id.'_total'}=$total;
  770:     }
  771:     return %categories;
  772: }
  773: 
  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: }
  782: 
  783: #
  784: # === end category-related
  785: #
  786: #
  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");
  790: 
  791: sub mixColors {
  792:     my $start = shift;
  793:     my $end = shift;
  794:     my $ratio = shift;
  795:     
  796:     my ($a,$b);
  797:     my $final = "";
  798:     $a = $start->[0]; $b = $end->[0];
  799:     my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  800:     $a = $start->[1]; $b = $end->[1];
  801:     my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  802:     $a = $start->[2]; $b = $end->[2];
  803:     my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
  804: 
  805:     $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
  806:     return "#" . $final;
  807: }
  808: 
  809: 1;

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