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

1.1       bowersj2    1: # The LearningOnline Network with CAPA
                      2: # Quick Student Grades Display
                      3: #
1.123   ! raeburn     4: # $Id: lonquickgrades.pm,v 1.122 2021/11/30 15:55:37 raeburn 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;
1.104     musolffc   32: use Apache::Constants qw(:common :http REDIRECT);
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.100     www        38: use Apache::loncoursedata;
1.101     www        39: use Apache::lonstudentassessment;
1.107     raeburn    40: use Apache::lonuserstate;
1.1       bowersj2   41: 
1.102     www        42: use Time::HiRes;
                     43: use Spreadsheet::WriteExcel;
                     44: use Spreadsheet::WriteExcel::Utility();
                     45: #
                     46: # Excel data
                     47: #
                     48: my $excel_sheet;
                     49: my $excel_workbook;
                     50: my $filename;
                     51: my $format;
                     52: my $request_aborted;
                     53: my $header_row;
                     54: my $cols_output;
                     55: my %prog_state;
                     56: 
                     57: 
1.1       bowersj2   58: sub handler {
                     59:     my $r = shift;
1.5       bowersj2   60:     return real_handler($r);
                     61: }
                     62: 
                     63: sub real_handler {
                     64:     my $r = shift;
1.1       bowersj2   65: 
                     66:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
                     67: 
                     68:     # Handle header-only request
1.40      albertel   69:     if ($env{'browser.mathml'}) {
                     70: 	&Apache::loncommon::content_type($r,'text/xml');
                     71:     } else {
                     72: 	&Apache::loncommon::content_type($r,'text/html');
                     73:     }
1.1       bowersj2   74:     if ($r->header_only) {
1.40      albertel   75: 	$r->send_http_header;
1.1       bowersj2   76:         return OK;
                     77:     }
                     78: 
1.106     raeburn    79:     my $cangrade=&Apache::lonnet::allowed('mgr');
1.111     raeburn    80:     my $showPoints =
                     81:         (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard')
                     82:       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories'));
                     83: 
                     84:     my $reinitresult;
1.106     raeburn    85: 
1.117     raeburn    86:     if ($env{'request.course.id'}) {
                     87:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                     88:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.122     raeburn    89:         my $clientip = &Apache::lonnet::get_requestor_ip($r);
1.117     raeburn    90:         my ($blocked,$blocktext) =
1.122     raeburn    91:             &Apache::loncommon::blocking_status('grades',$clientip,$cnum,$cdom);
1.117     raeburn    92:         if ($blocked) {
                     93:             my $checkrole = "cm./$cdom/$cnum";
                     94:             if ($env{'request.course.sec'} ne '') {
                     95:                 $checkrole .= "/$env{'request.course.sec'}";
                     96:             }
                     97:             unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
                     98:                     ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
                     99:                 &grades_blocked($r,$blocktext,$showPoints);
                    100:                 return OK;
                    101:             }
                    102:         }
                    103:     }
                    104: 
1.106     raeburn   105:     unless ($cangrade) {
                    106:         # Check for critical messages and redirect if present.
1.110     raeburn   107:         my ($redirect,$url) = &Apache::loncommon::critical_redirect(300,'grades');
1.106     raeburn   108:         if ($redirect) {
                    109:             &Apache::loncommon::content_type($r,'text/html');
                    110:             $r->header_out(Location => $url);
                    111:             return REDIRECT;
                    112:         }
                    113: 
                    114:         # Check if course needs to be re-initialized
                    115:         my $loncaparev = $r->dir_config('lonVersion');
1.111     raeburn   116:         ($reinitresult,my @reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
1.106     raeburn   117: 
1.111     raeburn   118:         if ($reinitresult eq 'switch') {
1.106     raeburn   119:             &Apache::loncommon::content_type($r,'text/html');
                    120:             $r->send_http_header;
                    121:             $r->print(&Apache::loncommon::check_release_result(@reinit));
                    122:             return OK;
1.123   ! raeburn   123:         }
        !           124:         my ($cid,$cnum,$cdom);
        !           125:         if ($reinitresult) {
        !           126:             $cid = $env{'request.course.id'};
        !           127:             $cnum = $env{'course.'.$cid.'.num'};
        !           128:             $cdom = $env{'course.'.$cid.'.domain'};
        !           129:         }
        !           130:         if (($reinitresult eq 'main') || ($reinitresult eq 'both')) {
1.111     raeburn   131:             &Apache::loncommon::content_type($r,'text/html');
                    132:             $r->send_http_header;
                    133:             &startpage($r,$showPoints);
                    134:             my $preamble = '<div id="LC_update_'.$cid.'" class="LC_info">'.
                    135:                            '<br />'.
                    136:                            &mt('Your course session is being updated because of recent changes by course personnel.').
1.120     raeburn   137:                            ' '.&mt('Please be patient').'.<br /></div>'.
1.111     raeburn   138:                            '<div style="padding:0;clear:both;margin:0;border:0"></div>';
                    139:             %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
                    140:             &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Updating course'));
                    141:             $r->rflush();
1.106     raeburn   142:             my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
1.120     raeburn   143:             &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished!')); 
1.111     raeburn   144:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    145:             my $closure = <<ENDCLOSE;
                    146: <script type="text/javascript">
                    147: // <![CDATA[
                    148: \$("#LC_update_$cid").hide('slow');
                    149: // ]]>
                    150: </script>
                    151: ENDCLOSE
1.106     raeburn   152:             if ($ferr) {
1.111     raeburn   153:                 $r->print($closure.&Apache::loncommon::end_page());
1.106     raeburn   154:                 my $requrl = $r->uri;
                    155:                 $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
                    156:                 $env{'user.reinit'} = 1;
                    157:                 return HTTP_NOT_ACCEPTABLE;
1.111     raeburn   158:             } else {
                    159:                $r->print($closure);
1.106     raeburn   160:             }
1.109     raeburn   161:         } elsif ((&Apache::loncommon::course_type() eq 'Placement') &&
                    162:                  (!$env{'request.role.adv'})) {
                    163:             my $furl = &Apache::lonpageflip::first_accessible_resource();
                    164:             &Apache::loncommon::content_type($r,'text/html');
                    165:             $r->header_out(Location => $furl);
                    166:             return REDIRECT;
1.106     raeburn   167:         }
1.123   ! raeburn   168:         if (($reinitresult eq 'supp') || ($reinitresult eq 'both')) {
        !           169:             my ($supplemental,$refs_updated) = &Apache::lonnet::get_supplemental($cnum,$cdom);
        !           170:             unless ($refs_updated) {
        !           171:                 &Apache::loncommon::set_supp_httprefs($cnum,$cdom,$supplemental);
        !           172:             }
        !           173:         }
1.105     raeburn   174:     }
                    175: 
1.123   ! raeburn   176:     unless (($reinitresult eq 'main') || ($reinitresult eq 'both')) {
1.111     raeburn   177:         # Send header, don't cache this page
                    178:         &Apache::loncommon::no_cache($r);
                    179:         $r->send_http_header;
                    180:         &startpage($r,$showPoints);
                    181:     }
                    182:     $r->rflush();
1.1       bowersj2  183: 
1.55      www       184:     &startGradeScreen($r,'quick');
1.17      bowersj2  185: 
1.74      www       186: #
                    187: # Pick student
                    188: #
1.54      www       189:     my $uname;
                    190:     my $udom;
1.74      www       191:     my $stdid;
                    192:     if ($cangrade) {
1.101     www       193:         $r->print("<h2>".&mt("Download Multiple")."</h2>".
                    194:                   '<table cellspacing="5">'."\n".
                    195:                   '<tr>'.
                    196:                   '<td align="center"><b>'.&mt('Sections').'</b>'.
                    197:                   &Apache::loncommon::help_open_topic("Chart_Sections").
                    198:                   '</td>'.
                    199:                   '<td align="center"><b>'.&mt('Groups').'</b>'.
                    200:                   '</td>'.
                    201:                   '<td align="center"><b>'.&mt('Student Data').'</b>'.
                    202:                   &Apache::loncommon::help_open_topic("Chart_Student_Data").
                    203:                   '</td>'.
                    204:                   '<td align="center"><b>'.&mt('Access Status').'</b>'.
                    205:                   &Apache::loncommon::help_open_topic("Chart_Enrollment_Status").
                    206:                   '</td>'.
                    207:                   '<td align="center"><b>'.&mt('Output Format').'</b>'.
                    208:                   &Apache::loncommon::help_open_topic("Chart_Output_Formats").
                    209:                   '</td><td>&nbsp;</td></tr>'."\n".
                    210:                   '<tr><td align="center">'."\n".
                    211:                   &Apache::lonstatistics::SectionSelect('Section','multiple',5).
                    212:                   '</td><td align="center">'.
                    213:                   &Apache::lonstatistics::GroupSelect('Group','multiple',5).
                    214:                   '</td><td align="center">'.
                    215:                   &Apache::lonstatistics::StudentDataSelect('StudentData','multiple',5,undef).
                    216:                   '</td><td>'."\n".
                    217:                   &Apache::lonhtmlcommon::StatusOptions(undef,undef,5).
                    218:                   '</td><td>'."\n".
                    219:                   &Apache::lonstudentassessment::CreateAndParseOutputSelector().
                    220:                   '</td><td>'.
                    221:                   '<input type="submit" name="download" value="'.&mt('Display/Download Multiple Students').'" />'.
                    222:                   '</td></tr>'."\n".
                    223:                   '</table>'."\n"
                    224:                  );
                    225:         $r->print("<hr /><h2>".&mt("Display Individual")."</h2>");
1.74      www       226:         if ($env{'form.uname'}) { $uname=$env{'form.uname'}; }
                    227:         if ($env{'form.udom'}) { $udom=$env{'form.udom'}; }
                    228:         if ($env{'form.id'}) { $stdid=$env{'form.id'}; }
                    229:         if (($stdid) && ($udom)) {
1.108     raeburn   230:             $uname=(&Apache::lonnet::idget($udom,[$stdid],'ids'))[1];
1.74      www       231:         }
1.75      www       232:         if (($stdid) && (!$uname)) {
                    233:             $r->print('<p><span class="LC_warning">'.&mt("Unknown Student/Employee ID: [_1]",$stdid).'</span></p>');
                    234:             $stdid='';
                    235:         }
1.118     raeburn   236:         if (($uname eq '') && ($udom eq '')) {
                    237:             $uname = $env{'user.name'};
                    238:             $udom = $env{'user.domain'};
                    239:         }
1.74      www       240:         $r->print('<form method="post" name="quickform" action="/adm/quickgrades">');
                    241:         my $chooseopt=&Apache::loncommon::select_dom_form($udom,'udom').' '.
                    242:            &Apache::loncommon::selectstudent_link('quickform','uname','udom');
                    243:         $r->print("<p>\n".&Apache::loncommon::studentbrowser_javascript()."\n");
                    244:         $r->print(&mt('For User [_1] or Student/Employee ID [_2] at Domain [_3]'
                    245:                  ,'<input type="text" value="'.$uname.'" size="12" name="uname" />'
                    246:                  ,'<input type="text" value="'.$stdid.'" size="12" name="id" /> '
1.101     www       247:                  ,$chooseopt).
                    248:                  '&nbsp;&nbsp;<input type="submit" name="display" value="'.&mt('Display Individual Student').'" /></p>');
1.75      www       249:         if (($uname) && ($udom)) {
                    250:             $r->print('<p>'.&mt('Full Name: [_1]',&Apache::loncommon::plainname($uname,$udom)).'</p>');
                    251:         }
1.74      www       252:     }
                    253:     $r->rflush();
1.53      www       254: 
1.111     raeburn   255:     my $notshowTotals=
                    256:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
                    257:     my $showCategories=
                    258:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
                    259: 
1.119     raeburn   260:     my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,
                    261:         $topLevelRight,$topLevelAttempted) = &getData($showPoints,$uname,$udom);
1.53      www       262: 
1.118     raeburn   263:     if (ref($navmap)) {
                    264:         if ($showCategories) {
1.119     raeburn   265:             &outputCategories($r,$showPoints,$notshowTotals,$navmap,$totalParts,$totalPossible,
                    266:                               $totalRight,$totalAttempted,$topLevelParts,$topLevelRight,
                    267:                               $topLevelAttempted);
1.118     raeburn   268:         } else {
1.119     raeburn   269:             &outputTable($r,$showPoints,$notshowTotals,$navmap,$totalParts,$totalPossible,
                    270:                          $totalRight,$totalAttempted,$topLevelParts,$topLevelRight,
                    271:                          $topLevelAttempted);
1.118     raeburn   272:         }
1.53      www       273:     } else {
1.118     raeburn   274:         if ($cangrade) { $r->print("\n</form>\n"); }
                    275:         my $requrl = $r->uri;
                    276:         $env{'user.error.msg'} = "$requrl:bre:0:0:Navmap initialization failed.";
                    277:         return HTTP_NOT_ACCEPTABLE;
1.53      www       278:     }
1.74      www       279:     if ($cangrade) { $r->print("\n</form>\n"); }
1.55      www       280:     &endGradeScreen($r);
1.51      www       281:     return OK;
                    282: }
1.2       bowersj2  283: 
1.117     raeburn   284: sub grades_blocked {
                    285:     my ($r,$blocktext,$caller) = @_;
                    286:     my $title = 'Points Display';
                    287:     if ($caller eq 'spreadsheet') {
                    288:         $title = 'Spreadsheet';
                    289:     } elsif ($env{'course.'.$env{'request.course.id'}.'.grading'} ne 'standard') {
                    290:         $title = 'Completed Problems Display';
                    291:     }
                    292:     my $brcrum = [{href=>"/adm/quickgrades",text => $title}];
                    293:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    294:     &Apache::lonhtmlcommon::add_breadcrumb({href=>'/adm/quickgrades',
                    295:                                             text=> $title});
                    296:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs($title);
                    297:     &Apache::loncommon::content_type($r,'text/html');
                    298:     &Apache::loncommon::no_cache($r);
                    299:     $r->send_http_header;
                    300:     $r->print(&Apache::loncommon::start_page($title).
                    301:               $breadcrumbs.
                    302:               $blocktext.
                    303:               &Apache::loncommon::end_page());
                    304:     return;
                    305: }
                    306: 
1.99      www       307: sub getStudentCatGrade {
                    308:     my ($uname,$udom,%categories)=@_;
                    309:     my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=
                    310:        &getData(1,$uname,$udom);
                    311:     return &output_category_table(undef,0,$navmap,0,%categories);
                    312: }
                    313: 
                    314: sub getAllStudentData {
                    315:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    316:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.100     www       317: 
1.99      www       318:     my %categories=&Apache::lonnet::dump('grading_categories',$cdom,$cnum);
1.100     www       319: 
                    320:     my $classlist = &Apache::loncoursedata::get_classlist();
                    321: 
                    322:     my $statusidx   = &Apache::loncoursedata::CL_STATUS();
                    323:     my $usernameidx = &Apache::loncoursedata::CL_SNAME();
                    324:     my $domainidx   = &Apache::loncoursedata::CL_SDOM();
                    325:     my $fullnameidx = &Apache::loncoursedata::CL_FULLNAME();
                    326: 
                    327:     foreach my $key (keys(%{$classlist})) {
                    328:         my $student = $classlist->{$key};
                    329:         my $perc=&getStudentCatGrade($classlist->{$student}->[$usernameidx],
                    330:                                      $classlist->{$student}->[$domainidx],
                    331:                                      %categories);
                    332:     }
1.99      www       333: }
                    334: 
1.111     raeburn   335: sub startpage {
                    336:     my ($r,$showPoints) = @_;
                    337:     my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
                    338:     my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
                    339:     $r->print(&Apache::loncommon::start_page($title,undef,
                    340:                                             {'bread_crumbs' => $brcrum})
                    341:              );
                    342: }
1.99      www       343: 
1.55      www       344: sub startGradeScreen {
                    345:     my ($r,$mode)=@_;
                    346: 
                    347:     my $showPoints =
                    348:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
                    349:     my $notshowSPRSlink =
                    350:         (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
                    351:       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals')
                    352:       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories'));
1.119     raeburn   353:     my $notshowTotals =
1.55      www       354:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
1.118     raeburn   355:     my $showSPRSlink =
                    356:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'spreadsheet';
1.119     raeburn   357:     my $showCategories =
1.55      www       358:         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
1.115     raeburn   359:  
                    360:     my $allowed_to_view = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
                    361:     if ((!$allowed_to_view) && ($env{'request.course.sec'} ne '')) {
                    362:         $allowed_to_view = &Apache::lonnet::allowed('vgr',
                    363:                                "$env{'request.course.id'}/$env{'request.course.sec'}");
                    364:     }
1.55      www       365: 
1.115     raeburn   366:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1.116     raeburn   367:     if ((!$allowed_to_edit) && ($env{'request.course.sec'} ne '')) {
1.115     raeburn   368:         $allowed_to_edit = &Apache::lonnet::allowed('mgr',
                    369:                                "$env{'request.course.id'}/$env{'request.course.sec'}");
                    370:     }
1.55      www       371: 
                    372:     if ($allowed_to_view) {
1.61      raeburn   373:        my @notes;
                    374:        push(@notes,&mt('Students do not see total points.')) if ($notshowTotals);
                    375:        push(@notes,&mt('Students do not see link to spreadsheet.')) if ($notshowSPRSlink);
                    376:        push(@notes,&mt('Students will see points based on problem weights.')) if ($showPoints);
                    377:        push(@notes,&mt('Students will see points based on categories.')) if ($showCategories);
1.118     raeburn   378:        push(@notes,&mt('Students will see link to spreadsheet.')) if ($showSPRSlink);
1.61      raeburn   379:        push(@notes, &Apache::lonhtmlcommon::coursepreflink(&mt('Grade display settings'),'grading'));
                    380:        $r->print(&Apache::loncommon::head_subbox(join('&nbsp;&nbsp;',@notes)));
1.55      www       381:     }
                    382: 
                    383: 
1.56      www       384:     $r->print("\n".'<ul class="LC_TabContentBigger" id="main">');
1.57      www       385:     $r->print("\n".'<li'.($mode eq 'quick'?' class="active"':'').'><a href="/adm/quickgrades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
1.62      www       386:                                           ($showPoints?&mt('Individual Points Overview'):($showCategories?&mt('Grades Overview'):&mt('Completion Overview'))).
1.57      www       387:                                           '&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
1.55      www       388: 
                    389:     if (!($showPoints || $notshowSPRSlink) || ($allowed_to_view)) {
1.56      www       390:        $r->print("\n".'<li'.($mode eq 'spreadsheet'?' class="active"':'').'><a href="/adm/'.($allowed_to_view?'classcalc':'studentcalc').'"><b>'.
1.57      www       391:                                                                  &mt('Spreadsheet (Detailed)').'</b></a></li>');
1.55      www       392:     }
1.58      www       393:     if ($allowed_to_view) {
1.63      www       394:        $r->print("\n".'<li'.($mode eq 'statistics'?' class="active"':'').'><a href="/adm/statistics"><b>'.
                    395:                                                                  &mt('Statistics and Reports').'</b></a></li>');
                    396: 
1.58      www       397:        $r->print("\n".'<li'.($mode eq 'chart'?' class="active"':'').'><a href="/adm/statistics?reportSelected=student_assessment"><b>'.
1.113     raeburn   398:                                                                  &mt('Assessment Chart').'</b></a></li>');
1.58      www       399: 
                    400:     }
1.59      www       401:     if ($allowed_to_edit) {
                    402:        $r->print("\n".'<li'.($mode eq 'grading'?' class="active"':'').'><a href="/adm/grades"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
1.66      www       403:                                                                  &mt('Content Grading').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
                    404:        if ($env{'form.symb'}) {
                    405:           $r->print("\n".'<li'.($mode eq 'probgrading'?' class="active"':'').'><a href="/adm/grades?symb='.
                    406:                                               &Apache::lonhtmlcommon::entity_encode($env{'form.symb'}).
1.103     raeburn   407:                                               '&amp;command=gradingmenu"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
1.66      www       408:                                               &mt('Problem Grading').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
                    409: 
                    410:        }
1.59      www       411:     }
1.56      www       412:     $r->print("\n".'</ul>'."\n");
1.55      www       413:     $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;">');
                    414: }
                    415: 
                    416: sub endGradeScreen {
1.119     raeburn   417:     my ($r)=@_;
                    418:     $r->print('</div></div></div>'.&Apache::loncommon::end_page());
                    419:     return;
1.55      www       420: }
                    421: 
1.102     www       422: # -----------
                    423: 
                    424: 
                    425: sub excel_cleanup {
                    426:     undef ($excel_sheet);
                    427:     undef ($excel_workbook);
                    428:     undef ($filename);
                    429:     undef ($format);
                    430: }
                    431: 
                    432: 
                    433: sub excel_initialize {
                    434:     my ($r) = @_;
                    435: 
                    436:     &excel_cleanup();
                    437: 
                    438:     # Create sheet
                    439:     ($excel_workbook,$filename,$format)=
                    440:         &Apache::loncommon::create_workbook($r);
                    441:     return if (! defined($excel_workbook));
                    442:    #
                    443:    # Add a worksheet
                    444:     my $sheetname = $env{'course.'.$env{'request.course.id'}.'.description'};
                    445:     $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
                    446:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
                    447:    #
                    448:    # Put the course description in the header
                    449:     $excel_sheet->write($header_row,$cols_output++,
                    450:                    $env{'course.'.$env{'request.course.id'}.'.description'},
                    451:                         $format->{'h1'});
                    452: } 
                    453: 
                    454: sub excel_finish {
                    455:     my ($r) = @_;
                    456:     if ($request_aborted || ! defined($excel_sheet)) {
                    457:         &excel_cleanup();
                    458:         return;
                    459:     }
                    460:     #
                    461:     # Write the excel file
                    462:     $excel_workbook->close();
                    463:     #
                    464:     # Close the progress window
                    465:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    466:     #
                    467:     # Tell the user where to get their excel file
                    468:     $r->print('<br />'.
                    469:               '<a href="'.$filename.'">'.&mt('Your Excel spreadsheet').'</a>'."\n");
                    470:     $r->rflush();
                    471:     &excel_cleanup();
                    472:     return;
                    473: }
                    474: 
                    475: 
                    476: #
                    477: # CSV data
                    478: #
                    479: # -----------
                    480: 
                    481: #
                    482: # Go through the complete course and collect data
                    483: #
1.55      www       484: 
1.51      www       485: sub getData {
                    486: 
1.53      www       487:     my ($showPoints,$uname,$udom)=@_;
                    488: 
1.51      www       489:     # Create the nav map
1.53      www       490:     my $navmap = Apache::lonnavmaps::navmap->new($uname,$udom);
1.51      www       491: 
1.118     raeburn   492:     if (!defined($navmap)) {
                    493:         return ();
                    494:     }
                    495: 
1.51      www       496:     my $res = $navmap->firstResource(); # temp resource to access constants
1.1       bowersj2  497: 
1.121     raeburn   498:     my $deeplinkcond = 1;
                    499:     my $iterator = $navmap->getIterator(undef, undef, undef, 1, undef, undef, $deeplinkcond);
1.1       bowersj2  500:     my $depth = 1;
                    501:     $iterator->next(); # ignore first BEGIN_MAP
                    502:     my $curRes = $iterator->next();
1.119     raeburn   503: 
1.5       bowersj2  504:     # General overview of the following: Walk along the course resources.
                    505:     # For every problem in the resource, tell its parent maps how many
                    506:     # parts and how many parts correct it has. After that, each map will
                    507:     # have a count of the total parts underneath it, correct and otherwise.
                    508:     # After that, we will walk through the course again and read off
                    509:     # maps in order, with their data. 
                    510:     # (If in the future people decide not to be cumulative, only add
                    511:     #  the counts to the parent map.)
1.17      bowersj2  512:     # For convenience, "totalParts" is also "totalPoints" when we're looking
                    513:     #  at points; I can't come up with a variable name that makes sense
                    514:     #  equally for both cases.
1.5       bowersj2  515: 
                    516:     my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
1.28      bowersj2  517:     my $totalAttempted = 0;
1.14      bowersj2  518:     my $now = time();
1.28      bowersj2  519:     my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
1.5       bowersj2  520: 
                    521:     # Pre-run: Count parts correct
1.1       bowersj2  522:     while ( $depth > 0 ) {
                    523:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    524:         if ($curRes == $iterator->END_MAP()) { $depth--; }
1.121     raeburn   525:         my ($deeplink,$nodeeplinkcheck,$symb);
                    526:         $nodeeplinkcheck = 1;
                    527:         if (ref($curRes)) {
                    528:             $symb = $curRes->symb();
                    529:             $deeplink = $curRes->deeplink('quickgrades');
                    530:             if ($deeplink eq 'absent') {
                    531:                 $nodeeplinkcheck = 0;
                    532:             }
                    533:         }
                    534:         if (ref($curRes) && $curRes->is_gradable() && !$curRes->randomout &&
                    535:             ($nodeeplinkcheck))
1.5       bowersj2  536:         {
                    537:             # Get number of correct, incorrect parts
                    538:             my $parts = $curRes->parts();
                    539:             my $partsRight = 0;
1.17      bowersj2  540: 	    my $partsCount = 0;
1.28      bowersj2  541: 	    my $partsAttempted = 0;
1.5       bowersj2  542:             my $stack = $iterator->getStack();
                    543:             
                    544:             for my $part (@{$parts}) {
1.28      bowersj2  545: 		my $dateStatus = $curRes->getDateStatus($part);
1.97      www       546:                 my $weight = $curRes->weight($part);
                    547:                 my $problemstatus = $curRes->problemstatus($part);
                    548: 
1.98      www       549:                 if ($curRes->solved($part) eq 'excused') {
1.21      matthew   550:                     next;
                    551:                 }
1.17      bowersj2  552: 		if ($showPoints) {
1.28      bowersj2  553: 		    my $score = 0;
                    554: 		    # If we're not telling status and the answer date isn't passed yet, 
                    555: 		    # it's an "attempted" point
1.97      www       556: 		    if ((($problemstatus eq 'no') ||
                    557:                          ($problemstatus eq 'no_feedback_ever')) &&
1.28      bowersj2  558: 			($dateStatus != $curRes->ANSWER_OPEN)) {
1.31      albertel  559: 			my $status = $curRes->simpleStatus($part);
                    560: 			if ($status == $curRes->ATTEMPTED) {
1.97      www       561: 			    $partsAttempted += $weight;
1.31      albertel  562: 			    $totalAttempted += $partsAttempted;
                    563: 			}
1.28      bowersj2  564: 		    } else {
1.97      www       565: 			$score = &Apache::grades::compute_points($weight, $curRes->awarded($part));
1.28      bowersj2  566: 		    }
1.17      bowersj2  567: 		    $partsRight += $score;
                    568: 		    $totalRight += $score;
1.97      www       569: 		    $partsCount += $weight;
1.18      bowersj2  570: 
1.83      www       571:                     $curRes->{DATA}->{PROB_SCORE}  += $score;
1.97      www       572:                     $curRes->{DATA}->{PROB_WEIGHT} += $weight;
1.83      www       573: 
1.17      bowersj2  574: 		    if ($curRes->opendate($part) < $now) {
1.97      www       575: 			$totalPossible += $weight;
                    576:                         $curRes->{DATA}->{PROB_POSSIBLE} += $weight;
1.17      bowersj2  577: 		    }
1.97      www       578: 		    $totalParts += $weight;
1.17      bowersj2  579: 		} else {
1.27      bowersj2  580: 		    my $status = $curRes->simpleStatus($part);
1.17      bowersj2  581: 		    my $thisright = 0;
                    582: 		    $partsCount++;
1.37      albertel  583: 		    if ($status == $curRes->CORRECT ||
                    584: 			$status == $curRes->PARTIALLY_CORRECT ) {
1.17      bowersj2  585: 			$partsRight++;
                    586: 			$totalRight++;
                    587: 			$thisright = 1;
                    588: 		    }
1.28      bowersj2  589: 
                    590: 		    if ($status == $curRes->ATTEMPTED) {
                    591: 			$partsAttempted++;
                    592: 			$totalAttempted++;
                    593: 		    }
1.17      bowersj2  594: 		    
1.19      bowersj2  595: 		    $totalParts++;
1.17      bowersj2  596: 		    if ($curRes->opendate($part) < $now) {
                    597: 			$totalPossible++;
                    598: 		    }
                    599: 		}
1.5       bowersj2  600:             }
1.15      bowersj2  601: 
                    602:             if ($depth == 1) { # in top-level only
1.19      bowersj2  603: 		$topLevelParts += $partsCount;
1.15      bowersj2  604: 		$topLevelRight += $partsRight;
1.28      bowersj2  605: 		$topLevelAttempted += $partsAttempted;
1.15      bowersj2  606: 	    }
                    607: 
1.5       bowersj2  608:             # Crawl down stack and record parts correct and total
                    609:             for my $res (@{$stack}) {
                    610:                 if (ref($res) && $res->is_map()) {
                    611:                     if (!defined($res->{DATA}->{CHILD_PARTS})) {
                    612:                         $res->{DATA}->{CHILD_PARTS} = 0;
                    613:                         $res->{DATA}->{CHILD_CORRECT} = 0;
1.28      bowersj2  614: 			$res->{DATA}->{CHILD_ATTEMPTED} = 0;
1.5       bowersj2  615:                     }
                    616:                     
1.17      bowersj2  617:                     $res->{DATA}->{CHILD_PARTS} += $partsCount;
1.5       bowersj2  618:                     $res->{DATA}->{CHILD_CORRECT} += $partsRight;
1.28      bowersj2  619: 		    $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
1.5       bowersj2  620:                 }
                    621:             }
                    622:         }
                    623:         $curRes = $iterator->next();
                    624:     }
1.119     raeburn   625:     return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,
                    626:             $topLevelParts,$topLevelRight,$topLevelAttempted);
1.51      www       627: }
                    628: 
                    629: #
                    630: # Outputting everything.
                    631: #
                    632: 
                    633: sub outputTable {
1.5       bowersj2  634: 
1.119     raeburn   635:     my ($r,$showPoints,$notshowTotals,$navmap,$totalParts,$totalPossible,$totalRight,
                    636:         $totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
1.5       bowersj2  637: 
1.7       bowersj2  638:     my @start = (255, 255, 192);
1.5       bowersj2  639:     my @end   = (0, 192, 0);
                    640: 
                    641:     my $indentString = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                    642: 
                    643:     # Second pass: Print the maps.
1.43      bisitz    644:     $r->print(&Apache::loncommon::start_data_table()
                    645:              .&Apache::loncommon::start_data_table_header_row()
                    646:              .'<th>'.&mt('Folder').'</th>');
1.51      www       647:     my $title = &mt($showPoints ? "Points Scored" : "Done");
1.28      bowersj2  648:     if ($totalAttempted) {
1.51      www       649:         $title .= " / " . &mt("Attempted");
1.28      bowersj2  650:     }
1.49      www       651:     $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
1.43      bisitz    652:              .&Apache::loncommon::end_data_table_header_row());
1.51      www       653: #
                    654: # Output of folder scores
                    655: #
                    656: 
1.121     raeburn   657:     my $deeplinkcond = 1;
                    658:     my $iterator = $navmap->getIterator(undef, undef, undef, 1, undef, undef, $deeplinkcond);
1.51      www       659:     my $depth = 1;
                    660:     $iterator->next(); # ignore first BEGIN_MAP
                    661:     my $curRes = $iterator->next();
                    662: 
1.5       bowersj2  663:     while ($depth > 0) {
                    664:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    665:         if ($curRes == $iterator->END_MAP()) { $depth--; }
                    666: 
                    667:         if (ref($curRes) && $curRes->is_map()) {
                    668:             my $title = $curRes->compTitle();
                    669:             
                    670:             my $correct = $curRes->{DATA}->{CHILD_CORRECT};
                    671:             my $total = $curRes->{DATA}->{CHILD_PARTS};
1.28      bowersj2  672: 	    my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
1.5       bowersj2  673: 
1.6       bowersj2  674:             if ($total > 0) {
                    675:                 my $ratio;
                    676:                 $ratio = $correct / $total;
1.51      www       677:                 my $color = &mixColors(\@start, \@end, $ratio);
1.43      bisitz    678:                 $r->print(&Apache::loncommon::start_data_table_row()
                    679:                          .'<td style="background-color:'.$color.';">');
1.6       bowersj2  680:                 
1.15      bowersj2  681: 		my $thisIndent = '';
                    682:                 for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
1.6       bowersj2  683:                 
1.15      bowersj2  684:                 $r->print("$thisIndent$title</td>");
1.28      bowersj2  685: 		if ($totalAttempted) {
1.45      bisitz    686: 		    $r->print('<td valign="top">'
                    687:                              .$thisIndent
                    688:                              .'<span class="LC_nobreak">'
1.49      www       689:                              .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
1.45      bisitz    690:                              .'</span></td>'
                    691:                              .&Apache::loncommon::end_data_table_row()
                    692:                     );
1.28      bowersj2  693: 		} else {
1.45      bisitz    694: 		    $r->print('<td valign="top">'
                    695:                              .$thisIndent
                    696:                              .'<span class="LC_nobreak">'
1.49      www       697:                              .$correct.($notshowTotals?'':' / '.$total)
1.45      bisitz    698:                              .'</span></td>'
1.43      bisitz    699:                              .&Apache::loncommon::end_data_table_row());
1.28      bowersj2  700: 		}
1.6       bowersj2  701:             }
1.5       bowersj2  702:         }
1.4       bowersj2  703: 
1.5       bowersj2  704:         $curRes = $iterator->next();
                    705:     }
1.4       bowersj2  706: 
1.6       bowersj2  707:     # If there were any problems at the top level, print an extra "catchall"
1.15      bowersj2  708:     if ($topLevelParts > 0) {
                    709:         my $ratio = $topLevelRight / $topLevelParts;
1.68      www       710:         my $color = &mixColors(\@start, \@end, $ratio);
1.43      bisitz    711:         $r->print(&Apache::loncommon::start_data_table_row()
                    712:                  .'<td style="background-color:'.$color.';">');
1.25      www       713:         $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
1.43      bisitz    714:         $r->print("$topLevelRight / $topLevelParts</td>"
                    715:                  .&Apache::loncommon::end_data_table_row());
1.6       bowersj2  716:     }
1.4       bowersj2  717: 
1.51      www       718: #
                    719: # show totals (if applicable), close table
                    720: #
1.35      albertel  721:     if ($showPoints) {
1.68      www       722:         my $maxHelpLink = &Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
1.2       bowersj2  723: 
1.51      www       724:         $title = $showPoints ? "Points" : "Parts Done";
                    725:         my $totaltitle = $showPoints ? &mt("Awarded Total Points") : &mt("Total Parts Done");
                    726:         $r->print(&Apache::loncommon::start_data_table_row()
1.48      bisitz    727:                  .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
1.51      www       728:         $r->print(&mt('Max Possible To Date')." $maxHelpLink: <b>$totalPossible</b><br />");
                    729:         $title = $showPoints ? "Points" : "Parts";
                    730:         $r->print(&mt("Total $title In Course").': <b>'.$totalParts.'</b></td>'
1.43      bisitz    731:                  .&Apache::loncommon::end_data_table_row());
1.34      www       732:     }
1.1       bowersj2  733: 
1.72      www       734:     $r->print(&Apache::loncommon::end_data_table());
1.118     raeburn   735:     return;
1.5       bowersj2  736: }
                    737: 
1.53      www       738: #
1.65      www       739: # === Outputting category-based grades.
                    740: #
                    741: # $category{'order'}: output order of categories by id
                    742: # $category{'all'}: complete list of all categories 
                    743: # $category{$id.'_name'}: display-name of category
1.53      www       744: #
                    745: 
                    746: sub outputCategories {
                    747: 
                    748:     my ($r,$showPoints,$notshowTotals,
                    749:            $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
1.62      www       750: # Take care of storing and retrieving categories
                    751: 
1.64      www       752:     my $cangrade=&Apache::lonnet::allowed('mgr');
                    753: 
1.62      www       754:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    755:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.64      www       756:     my %categories=();
1.65      www       757: # Loading old categories
                    758:     %categories=&Apache::lonnet::dump('grading_categories',$cdom,$cnum);
1.64      www       759: # Storing
1.72      www       760:     if (($cangrade) && (($env{'form.storechanges'}) || ($env{'form.storemove'} ne '') || ($env{'form.cmd'} ne ''))) {
1.65      www       761: # Process the changes
                    762:         %categories=&process_category_edits($r,$cangrade,%categories);
1.64      www       763: # Actually store
                    764:         &Apache::lonnet::put('grading_categories',\%categories,$cdom,$cnum);
                    765:     }
1.65      www       766: # new categories loaded now
1.99      www       767:     &output_category_table($r,$cangrade,$navmap,1,%categories);
1.65      www       768: #
                    769:     if ($cangrade) {
1.84      www       770:         $r->print(&Apache::loncommon::resourcebrowser_javascript().
                    771:                   '<input type="hidden" name="storemove" value="" />'.
1.72      www       772:                   '<input type="hidden" name="cmd" value="" />'.
1.86      www       773:                   '<input type="hidden" name="resourcesymb" value="" />'.
1.72      www       774:                   '<input type="submit" name="storechanges" value="'.&mt("Save changes to grading categories").'" />'.
1.74      www       775:                   '<script>function storecmd (cmd) { document.quickform.cmd.value=cmd; document.quickform.submit(); }</script>');
1.65      www       776:     }
1.82      www       777: }
                    778: 
                    779: #
                    780: # Get data for all symbs
                    781: #
                    782: 
                    783: sub dumpdata {
                    784:     my ($navmap)=@_;
                    785:     my %returndata=();
                    786: 
                    787: # Run through the map and get all data
                    788: 
1.121     raeburn   789:     my $deeplinkcond = 1;
                    790:     my $iterator = $navmap->getIterator(undef, undef, undef, 1, undef, undef, $deeplinkcond);
1.82      www       791:     my $depth = 1;
                    792:     $iterator->next(); # ignore first BEGIN_MAP
                    793:     my $curRes = $iterator->next();
                    794: 
                    795:     while ($depth > 0) {
                    796:         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                    797:         if ($curRes == $iterator->END_MAP()) { $depth--; }
1.83      www       798:         if (ref($curRes)) {
                    799:             if ($curRes->is_map()) {
                    800:                 $returndata{$curRes->symb()}='folder:'.$curRes->{DATA}->{CHILD_PARTS}.':'.$curRes->{DATA}->{CHILD_ATTEMPTED}.':'.$curRes->{DATA}->{CHILD_CORRECT};
                    801:             } else {
                    802:                 $returndata{$curRes->symb()}='res:'.$curRes->{DATA}->{PROB_WEIGHT}.':'.$curRes->{DATA}->{PROB_POSSIBLE}.':'.$curRes->{DATA}->{PROB_SCORE};
                    803:             } 
1.82      www       804:         }
                    805:         $curRes = $iterator->next();
                    806:     }
                    807:     return %returndata;
1.65      www       808: }
                    809: 
                    810: #
                    811: # Process editing commands, update category hash
                    812: #
                    813: 
                    814: sub process_category_edits {
                    815:     my ($r,$cangrade,%categories)=@_;
                    816:     unless ($cangrade) { return %categories; }
1.72      www       817: # First store everything
                    818:     foreach my $id (split(/\,/,$categories{'order'})) {
1.80      www       819: # Set names, types, and weight (there is only one of each per category)
1.72      www       820:         %categories=&set_category_name($cangrade,$id,$env{'form.name_'.$id},%categories);
                    821:         %categories=&set_category_total($cangrade,$id,$env{'form.totaltype_'.$id},$env{'form.total_'.$id},%categories);
                    822:         %categories=&set_category_weight($cangrade,$id,$env{'form.weight_'.$id},%categories);
1.81      www       823:         %categories=&set_category_displayachieved($cangrade,$id,$env{'form.displayachieved_'.$id},%categories);
1.80      www       824: # Set values for category rules (before names may change)
                    825:         %categories=&set_category_rules($cangrade,$id,%categories);
1.72      www       826:     }
                    827: 
                    828: # Now deal with commands
1.67      www       829:     my $cmd=$env{'form.cmd'};
                    830:     if ($cmd eq 'createnewcat') {
1.69      www       831:         %categories=&make_new_category($r,$cangrade,undef,%categories);
1.72      www       832:     } elsif ($cmd=~/^up\_(.+)$/) {
                    833:         %categories=&move_up_category($1,$cangrade,%categories);
                    834:     } elsif ($cmd=~/^down\_(.+)$/) {
                    835:         %categories=&move_down_category($1,$cangrade,%categories);
1.69      www       836:     } elsif ($cmd=~/^delcat\_(.+)$/) {
                    837:         %categories=&del_category($1,$cangrade,%categories);
1.75      www       838:     } elsif ($cmd=~/^addcont\_(.+)$/) {
1.87      www       839:         %categories=&add_category_content($1,$cangrade,$env{'form.resourcesymb'},%categories);
1.75      www       840:     } elsif ($cmd=~/^delcont\_(.+)\_\_\_\_\_\_(.+)$/) {
                    841:         %categories=&del_category_content($1,$cangrade,$2,%categories);
1.77      www       842:     } elsif ($cmd=~/^newrule\_(.+)$/) {
                    843:         %categories=&add_calculation_rule($1,$cangrade,':',%categories);
1.79      www       844:     } elsif ($cmd=~/^delrule\_(.+)\_\_\_\_\_\_(.*)$/) {
                    845:         %categories=&del_calculation_rule($1,$cangrade,$2,%categories);
1.73      www       846:     }
                    847: # Move to a new position
                    848:     my $moveid=$env{'form.storemove'};
                    849:     if ($moveid) {
                    850:         %categories=&move_category($moveid,$cangrade,$env{'form.newpos_'.$moveid},%categories);
1.72      www       851:     } 
1.65      www       852:     return %categories;
                    853: }
                    854: 
                    855: #
                    856: # Output the table
                    857: #
                    858: 
                    859: sub output_category_table {
1.99      www       860:     my ($r,$cangrade,$navmaps,$output,%categories)=@_;
1.96      www       861:     
                    862:     my $totalweight=0;
                    863:     my $totalpoints=0;
                    864: 
1.99      www       865:     if ($output) { 
                    866:        $r->print(&Apache::loncommon::start_data_table());
1.65      www       867: #
1.99      www       868:        &output_category_table_header($r,$cangrade);
                    869:     }
1.65      www       870: #
                    871:     my @order=split(/\,/,$categories{'order'});
                    872: #
1.88      www       873:     my %performance=&dumpdata($navmaps);
1.65      www       874:     my $maxpos=$#order;
                    875:     for (my $i=0;$i<=$maxpos;$i++) {
1.99      www       876:         my ($correct,$possible,$type,$weight)=&output_and_calc_category($r,$cangrade,$navmaps,$order[$i],$i,$maxpos,\%performance,$output,%categories);
1.96      www       877:         unless ($possible) { next; }
                    878:         $totalpoints+=$weight*$correct/$possible;
                    879:         $totalweight+=$weight;
1.65      www       880:     }
                    881: #
1.96      www       882:     my $perc=0;
                    883:     if ($totalweight) { $perc=100.*$totalpoints/$totalweight; }
                    884: 
1.99      www       885:     if ($output) { 
                    886:         &bottom_line_category($r,$cangrade,$perc); 
                    887:         $r->print(&Apache::loncommon::end_data_table());
                    888:     }
1.96      www       889:     return $perc;
1.65      www       890: }
                    891: 
                    892: sub output_category_table_header {
                    893:     my ($r,$cangrade)=@_;
                    894:     $r->print(&Apache::loncommon::start_data_table_header_row());
                    895:     if ($cangrade) {
                    896:         $r->print('<th colspan="2">'.&mt("Move").'</th><th>'.&mt('Action').'</th>');
                    897:     }
                    898:     $r->print('<th>'.&mt('Category').'</th>'.
                    899:               '<th>'.&mt('Contents').'</th>'.
1.81      www       900:               '<th>'.&mt('Total Points').'</th>'.
1.65      www       901:               '<th>'.&mt('Calculation').'</th>'.
1.81      www       902:               '<th>'.&mt('Relative Weight').'</th>'.
                    903:               '<th>'.&mt('Achieved').'</th>');
1.65      www       904:     $r->print(&Apache::loncommon::end_data_table_header_row());
                    905: }
                    906: 
                    907: 
                    908: #
                    909: # Output one category to table
                    910: #
                    911: 
                    912: sub output_and_calc_category {
1.89      www       913:     my ($r,$cangrade,$navmaps,$id,$currentpos,$maxpos,$performance,$output,%categories)=@_;
1.99      www       914:     
                    915:     if ($output) { $r->print("\n".&Apache::loncommon::start_data_table_row()); }
1.95      www       916: 
1.99      www       917:     if ($output && $cangrade) {
                    918:         my $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL') . "/");
                    919:         my %lt=&Apache::lonlocal::texthash(
1.65      www       920:            'up' => 'Move Up',
                    921:            'dw' => 'Move Down');
                    922: 
                    923:         $r->print(<<ENDMOVE);
                    924: <td>
                    925: <div class="LC_docs_entry_move">
1.72      www       926:   <a href='javascript:storecmd("up_$id");'>
1.65      www       927:     <img src="${iconpath}move_up.gif" alt='$lt{'up'}' class="LC_icon" />
                    928:   </a>
                    929: </div>
                    930: <div class="LC_docs_entry_move">
1.72      www       931:   <a href='javascript:storecmd("down_$id");'>
1.65      www       932:     <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' class="LC_icon" />
                    933:   </a>
                    934: </div>
                    935: </td>
                    936: ENDMOVE
                    937:         $r->print("\n<td>\n<select name='newpos_$id' onchange='this.form.storemove.value=\"$id\";this.form.submit()'>");
                    938:         for (my $i=0;$i<=$maxpos;$i++) {
                    939:             if ($i==$currentpos) {
                    940:                 $r->print('<option value="" selected="selected">('.$i.')</option>');
                    941:             } else {
                    942:                 $r->print('<option value="'.$i.'">'.$i.'</option>');
                    943:             }
                    944:         }
                    945:         $r->print("\n</select>\n</td>\n");
1.72      www       946:         $r->print('<td><a href="javascript:storecmd(\'delcat_'.$id.'\');">'.&mt('Delete').'</a></td>');
1.69      www       947:         $r->print('<td><input type="text" name="name_'.$id.
                    948:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_name'}).'" /></td>');
1.89      www       949:     } elsif ($output) {
1.69      www       950:         $r->print('<td>'.$categories{$id.'_name'}.'</td>');
1.65      www       951:     }
1.89      www       952: # Content display and summing up of points
                    953:     my $totalpossible=0;
                    954:     my $totalcorrect=0;
1.91      www       955:     my @individual=();
1.89      www       956:     if ($output) { $r->print('<td><ul>'); }
1.75      www       957:     foreach my $contentid (split(/\,/,$categories{$id.'_content'})) {
1.89      www       958:         my ($type,$possible,$attempted,$correct)=split(/\:/,$$performance{$contentid});
                    959:         $totalpossible+=$possible;
                    960:         $totalcorrect+=$correct;
1.91      www       961:         if ($possible>0) { push(@individual,"$possible:$correct"); }
1.89      www       962:         if ($output) {
                    963:            $r->print('<li>');
1.96      www       964:            $r->print(&Apache::lonnet::gettitle($contentid).' ('.&numberout($correct).'/'.&numberout($possible).')');
1.89      www       965:            if ($cangrade) {
                    966:               $r->print(' <a href="javascript:storecmd(\'delcont_'.$id.'______'.$contentid.'\');">'.&mt('Delete').'</a>');
                    967:            }
                    968:            $r->print('</li>');
1.75      www       969:         }
                    970:     }
1.89      www       971:     if ($output) {
                    972:        $r->print('</ul>');
                    973:        if ($cangrade) {
                    974:            $r->print('<br />'.&Apache::loncommon::selectresource_link('quickform','addcont_'.$id,&mt('Add Problem or Folder')).'<br />');
                    975:        }
1.96      www       976:        $r->print('<p><b>'.&mt('Total raw points: [_1]/[_2]',&numberout($totalcorrect),&numberout($totalpossible)).'</b></p>');
1.89      www       977:        $r->print('</td>'); 
1.70      www       978:     }
1.81      www       979: # Total
1.90      www       980:     if ($output) { $r->print('<td>'); }
1.81      www       981:     if ($cangrade) {
1.89      www       982:        if ($output) { 
1.90      www       983:           $r->print(
1.81      www       984:                   '<select name="totaltype_'.$id.'">'.
                    985:                   '<option value="default"'.($categories{$id.'_totaltype'} eq 'default'?' selected="selected"':'').'>'.&mt('default').'</option>'.
                    986:                   '<option value="typein"'.($categories{$id.'_totaltype'} eq 'typein'?' selected="selected"':'').'>'.&mt('Type-in value').'</option>'.
                    987:                   '</select>'.
                    988:                   '<input type="text" size="4" name="total_'.$id.
1.90      www       989:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_total'}).'" />'); 
1.89      www       990:        }
1.81      www       991:     } else {
1.89      www       992:        if ($output) {
1.90      www       993:           $r->print('<td>'.($categories{$id.'_totaltype'} eq 'default'?&mt('default'):$categories{$id.'_total'}));
1.89      www       994:        }
1.81      www       995:     }
1.90      www       996: # Adjust total points
                    997:     if ($categories{$id.'_totaltype'} eq 'typein') {
                    998:        $totalpossible=1.*$categories{$id.'_total'};
                    999:     }
                   1000:     if ($output) {
1.96      www      1001:        $r->print('<p><b>'.&mt('Adjusted raw points: [_1]/[_2]',&numberout($totalcorrect),&numberout($totalpossible)).'</b></p>');
1.90      www      1002:     }
1.81      www      1003: 
                   1004: 
1.70      www      1005: # Calculation
1.89      www      1006:     if ($output) { $r->print('<td><ul>'); }
1.76      www      1007:     foreach my $calcrule (split(/\,/,$categories{$id.'_calculations'})) {
1.89      www      1008:         if ($output) { $r->print('<li>'); }
1.78      www      1009:         my ($code,$value)=split(/\:/,$calcrule);
1.89      www      1010:         if ($output) { $r->print(&pretty_prt_rule($cangrade,$id,$code,$value)); }
1.76      www      1011:         if ($cangrade) {
1.89      www      1012:            if ($output) { $r->print(' <a href="javascript:storecmd(\'delrule_'.$id.'______'.$code.'\');">'.&mt('Delete').'</a>'); }
1.76      www      1013:         }
1.91      www      1014:         if ($code eq 'capabove') {
                   1015:             if ($totalpossible>0) {
1.92      www      1016:                 if ($totalcorrect/$totalpossible>$value/100.) {
                   1017:                     $totalcorrect=$totalpossible*$value/100.;
1.91      www      1018:                 }
                   1019:             }
                   1020:         } elsif ($code eq 'capbelow') {
                   1021:             if ($totalpossible>0) {
1.92      www      1022:                 if ($totalcorrect/$totalpossible<$value/100.) {
                   1023:                     $totalcorrect=$totalpossible*$value/100.;
1.91      www      1024:                 }
                   1025:             }
1.92      www      1026:         } elsif ($code eq 'droplow') {
1.94      www      1027:             ($totalpossible,$totalcorrect,@individual)=&drop(0,0,$value,@individual);
1.92      www      1028:         } elsif ($code eq 'drophigh') {
1.94      www      1029:             ($totalpossible,$totalcorrect,@individual)=&drop(1,0,$value,@individual);
1.92      www      1030:         } elsif ($code eq 'droplowperc') {
1.94      www      1031:             ($totalpossible,$totalcorrect,@individual)=&drop(0,1,$value,@individual);
1.92      www      1032:         } elsif ($code eq 'drophighperc') {
1.94      www      1033:             ($totalpossible,$totalcorrect,@individual)=&drop(1,1,$value,@individual);
1.91      www      1034:         }
1.89      www      1035:         if ($output) { $r->print('</li>'); }
1.76      www      1036:     }
1.94      www      1037: # Re-adjust total points if force total
                   1038:     if ($categories{$id.'_totaltype'} eq 'typein') {
                   1039:        $totalpossible=1.*$categories{$id.'_total'};
                   1040:     }
                   1041: 
1.91      www      1042:     if ($output) { 
1.92      www      1043:         $r->print('</ul>'); 
                   1044:         if ($cangrade) { $r->print('<br />'.&new_calc_rule_form($id)); }
1.96      www      1045:         $r->print('<p><b>'.&mt('Calculated points: [_1]/[_2]',&numberout($totalcorrect),&numberout($totalpossible)).'</b></p>');
1.92      www      1046:         $r->print('</td>'); 
1.91      www      1047:     }
1.95      www      1048: #
                   1049: # Prepare for export
                   1050: #
1.70      www      1051: # Weight
1.95      www      1052:     my $weight=$categories{$id.'_weight'};
                   1053:     unless (1.*$weight>0) { $weight=0; }
1.70      www      1054:     if ($cangrade) {
1.89      www      1055:        if ($output) { 
                   1056:           $r->print('<td>'.
1.70      www      1057:                   '<input type="text" size="4" name="weight_'.$id.
1.95      www      1058:                   '" value="'.&Apache::lonhtmlcommon::entity_encode($weight).'" /></td>');
1.89      www      1059:        }
1.70      www      1060:     } else {
1.89      www      1061:        if ($output) {
1.95      www      1062:           $r->print('<td>'.$weight.'</td>');
1.89      www      1063:        }
1.70      www      1064:     }
1.81      www      1065: # Achieved
1.95      www      1066:     my $type=$categories{$id.'_displayachieved'};
                   1067:     unless (($type eq 'percent') || ($type eq 'points')) { $type='points'; }
1.89      www      1068:     if ($output) { $r->print('<td>'); }
1.81      www      1069:     if ($cangrade) {
1.89      www      1070:         if ($output) {
                   1071:            $r->print('<select name="displayachieved_'.$id.'">'.
1.95      www      1072:                   '<option value="percent"'.($type eq 'percent'?' selected="selected"':'').'>'.&mt('percent').'</option>'.
                   1073:                   '<option value="points"'.($type eq 'points'?' selected="selected"':'').'>'.&mt('points').'</option>'.
1.81      www      1074:                   '</select>');
1.89      www      1075:         }
1.95      www      1076:     }
                   1077:     if ($output) {
1.96      www      1078:         $r->print('<p><b>');
1.95      www      1079:         if ($type eq 'percent') {
                   1080:             my $perc='---';
                   1081:             if ($totalpossible) {
                   1082:                 $perc=100.*$totalcorrect/$totalpossible;
1.89      www      1083:             }
1.96      www      1084:             $r->print(&mt('[_1] percent',&numberout($perc)));
1.95      www      1085:         } else {
1.96      www      1086:             $r->print(&mt('[_1]/[_2] points',&numberout($totalcorrect),&numberout($totalpossible)));
1.81      www      1087:         }
1.96      www      1088:         $r->print('</b></p>');
1.81      www      1089:     }
1.89      www      1090:     if ($output) { $r->print('</td>'); }
1.70      www      1091: 
1.95      www      1092:     return ($totalcorrect,$totalpossible,$type,$weight);
1.65      www      1093: }
                   1094: 
                   1095: #
1.92      www      1096: # Drop folders and problems
                   1097: #
                   1098: 
                   1099: sub drop {
1.93      www      1100:     my ($high,$percent,$n,@individual)=@_;
1.94      www      1101: # Sort assignments by points or percent
1.92      www      1102:     my @newindividual=sort {
                   1103:         my ($pa,$ca)=split(/\:/,$a);
                   1104:         my ($pb,$cb)=split(/\:/,$b);
                   1105:         if ($percent) {
                   1106:             my $perca=0;
                   1107:             if ($pa>0) { $perca=$ca/$pa; }
                   1108:             my $percb=0;
                   1109:             if ($pb>0) { $percb=$cb/$pb; }
                   1110:             $perca<=>$percb;
                   1111:         } else {
                   1112:             $ca<=>$cb;
                   1113:         }
                   1114:     } @individual;
1.94      www      1115: # Drop the ones we don't want
1.93      www      1116:     if ($#newindividual>=$n) {
                   1117:         if ($high) {
                   1118:            splice(@newindividual,$#newindividual+1-$n,$n);
                   1119:         } else {
                   1120:            splice(@newindividual,0,$n);
                   1121:         }
                   1122:     } else {
                   1123:         @newindividual=();
                   1124:     }
1.94      www      1125: # Re-calculate how many points possible and achieved
                   1126:     my $newpossible=0;
1.92      www      1127:     my $newcorrect=0;
1.93      www      1128:     for my $score (@newindividual) {
1.94      www      1129:         my ($thispossible,$thiscorrect)=(split(/\:/,$score));
                   1130:         $newpossible+=$thispossible;
                   1131:         $newcorrect+=$thiscorrect;
1.93      www      1132:     }
1.94      www      1133:     return ($newpossible,$newcorrect,@newindividual);
1.92      www      1134: } 
                   1135: #
1.65      www      1136: # Bottom line with grades
                   1137: #
                   1138: 
                   1139: sub bottom_line_category {
1.96      www      1140:     my ($r,$cangrade,$perc)=@_;
1.67      www      1141:     $r->print(&Apache::loncommon::start_data_table_row());
                   1142:     if ($cangrade) {
1.72      www      1143:         $r->print('<td colspan="3"><a href="javascript:storecmd(\'createnewcat\');">'.&mt('Create New Category').'</a></td>');
1.67      www      1144:     }
1.96      www      1145:     $r->print('<td colspan="6"><b>'.&mt('Total: [_1] percent',&numberout($perc)).'</b></td>');
1.65      www      1146: }
                   1147: 
1.96      www      1148: sub numberout {
                   1149:     my ($number)=@_;
                   1150:     my $printout=sprintf("%.3f", $number);
                   1151:     $printout=~s/0+$//;
                   1152:     $printout=~s/\.$//;
                   1153:     return $printout;
                   1154: }
1.65      www      1155: #
                   1156: # Make one new category
                   1157: #
                   1158: 
                   1159: sub make_new_category {
                   1160:     my ($r,$cangrade,$ordernum,%categories)=@_;
                   1161:     unless ($cangrade) { return %categories; }
                   1162: # Generate new ID
                   1163:     my $id=time.'_'.$$.'_'.rand(10000);
                   1164: # Add new ID to list of all IDs ever created in this course
                   1165:     $categories{'all'}.=','.$id;
                   1166:     $categories{'all'}=~s/^\,//;
                   1167: # Add new ID to ordered list of displayed and evaluated categories
                   1168:     $categories{'order'}.=','.$id;
                   1169:     $categories{'order'}=~s/^\,//;
                   1170: # Move it into desired space
                   1171:     if (defined($ordernum)) {
                   1172:         %categories=&move_category($id,$cangrade,$ordernum,%categories);
                   1173:     }
1.71      www      1174:     $categories{$id.'_weight'}=0;
                   1175:     $categories{$id.'_totaltype'}='default';
1.81      www      1176:     $categories{$id.'_displayachieved'}='percent';
1.65      www      1177:     return %categories;
1.53      www      1178: }
                   1179: 
1.76      www      1180: 
                   1181: # === Calculation Rule Editing
                   1182: 
1.80      www      1183: sub category_rule_codes {
                   1184:     return &Apache::lonlocal::texthash(
1.91      www      1185:                 'droplowperc'  => 'Drop N lowest grade percentage problems/folders',
                   1186:                 'drophighperc' => 'Drop N highest grade percentage problems/folderss',
                   1187:                 'droplow'  => 'Drop N lowest point problems/folders',
                   1188:                 'drophigh' => 'Drop N highest point problems/folders',
1.78      www      1189:                 'capabove' => 'Cap percentage above N percent',
                   1190:                 'capbelow' => 'Cap percentage below N percent');
1.80      www      1191: }
                   1192: 
                   1193: sub pretty_prt_rule {
                   1194:     my ($cangrade,$id,$code,$value)=@_;
                   1195:     my $cid=$id.'_'.$code;
                   1196:     my %lt=&category_rule_codes();
1.78      www      1197:     my $ret='<span class="LC_nobreak">';
                   1198:     if ($cangrade) {
                   1199:         $ret.='<select name="sel_'.$cid.'">';
                   1200:         foreach my $calc (''=>'',sort(keys(%lt))) {
                   1201:             $ret.='<option value="'.$calc.'"'.($calc eq $code?' selected="selected"':'').' />'.$lt{$calc}.'</input>';
                   1202:         }
1.80      www      1203:         $ret.='</select> N=<input type="text" size="5" name="val_'.$cid.'" value="'.$value.'" /></span>';
1.78      www      1204:     } else {
                   1205:         $ret.=$lt{$code}.'; N='.$value;
                   1206:     }
                   1207:     $ret.='</span>';
                   1208:     return $ret;
1.76      www      1209: }
                   1210: 
                   1211: sub new_calc_rule_form {
1.77      www      1212:     my ($id)=@_;
                   1213:     return '<a href="javascript:storecmd(\'newrule_'.$id.'\');">'.&mt('New Calculation Rule').'</a>';
1.76      www      1214: }
                   1215: 
                   1216: #
                   1217: # Add a calculation rule
                   1218: #
                   1219: 
                   1220: sub add_calculation_rule {
                   1221:     my ($id,$cangrade,$newcontent,%categories)=@_;
                   1222:     unless ($cangrade) { return %categories; }
                   1223:     my %newcontent=($newcontent => 1);
                   1224:     foreach my $current (split(/\,/,$categories{$id.'_calculations'})) {
                   1225:         $newcontent{$current}=1;
                   1226:     }
                   1227:     $categories{$id.'_calculations'}=join(',',sort(keys(%newcontent)));
                   1228:     return %categories;
                   1229: }
                   1230: 
                   1231: #
                   1232: # Delete a calculation rule
                   1233: #
                   1234: 
                   1235: sub del_calculation_rule {
                   1236:     my ($id,$cangrade,$delcontent,%categories)=@_;
                   1237:     unless ($cangrade) { return %categories; }
                   1238:     my @newcontent=();
                   1239:     foreach my $current (split(/\,/,$categories{$id.'_calculations'})) {
1.78      www      1240:         unless ($current=~/^\Q$delcontent\E\:/) {
1.76      www      1241:             push(@newcontent,$current);
                   1242:         }
                   1243:     }
                   1244:     $categories{$id.'_calculations'}=join(',',@newcontent);
                   1245:     return %categories;
                   1246: }
                   1247: 
1.80      www      1248: sub set_category_rules {
                   1249:     my ($cangrade,$id,%categories)=@_;
                   1250:     unless ($cangrade) { return %categories; }
                   1251:     my %lt=&category_rule_codes();
                   1252:     my @newrules=();
                   1253:     foreach my $code ('',(keys(%lt))) {
                   1254:         if ($env{'form.sel_'.$id.'_'.$code}) {
                   1255:             push(@newrules,$env{'form.sel_'.$id.'_'.$code}.':'.$env{'form.val_'.$id.'_'.$code});
                   1256:         }
                   1257:     }
                   1258:     $categories{$id.'_calculations'}=join(',',sort(@newrules));
                   1259:     return %categories;
                   1260: }
                   1261: 
                   1262: 
1.76      www      1263: # === Category Editing
                   1264: 
1.65      www      1265: #
1.75      www      1266: # Add to category content
                   1267: #
                   1268: 
                   1269: sub add_category_content {
                   1270:     my ($id,$cangrade,$newcontent,%categories)=@_;
                   1271:     unless ($cangrade) { return %categories; }
1.87      www      1272:     &Apache::lonnet::logthis("In here $newcontent");
1.75      www      1273:     my %newcontent=($newcontent => 1);
                   1274:     foreach my $current (split(/\,/,$categories{$id.'_content'})) {
                   1275:         $newcontent{$current}=1;
                   1276:     }
                   1277:     $categories{$id.'_content'}=join(',',sort(keys(%newcontent)));
                   1278:     return %categories;
                   1279: }
                   1280: 
                   1281: #
                   1282: # Delete from category content
                   1283: #
                   1284: 
                   1285: sub del_category_content {
                   1286:     my ($id,$cangrade,$delcontent,%categories)=@_;
                   1287:     unless ($cangrade) { return %categories; }
                   1288:     my @newcontent=();
                   1289:     foreach my $current (split(/\,/,$categories{$id.'_content'})) {
                   1290:         unless ($current eq $delcontent) {
                   1291:             push(@newcontent,$current);
                   1292:         }
                   1293:     }
                   1294:     $categories{$id.'_content'}=join(',',@newcontent);
                   1295:     return %categories;
                   1296: }
                   1297: 
                   1298: #
1.68      www      1299: # Delete category
                   1300: #
                   1301: 
                   1302: sub del_category {
1.75      www      1303:     my ($id,$cangrade,%categories)=@_;
                   1304:     unless ($cangrade) { return %categories; }
                   1305:     my @neworder=();
                   1306:     foreach my $currentid (split(/\,/,$categories{'order'})) {
                   1307:         unless ($currentid eq $id) {
                   1308:             push(@neworder,$currentid);
                   1309:         }
                   1310:     }
                   1311:     $categories{'order'}=join(',',@neworder);
                   1312:     return %categories;
1.68      www      1313: }
                   1314: 
                   1315: #
1.72      www      1316: # Move category up
                   1317: #
                   1318: 
                   1319: sub move_up_category {
                   1320:     my ($id,$cangrade,%categories)=@_;
                   1321:     my $currentpos=&current_pos_category($id,%categories);
                   1322:     if ($currentpos<1) { return %categories; }
                   1323:     return &move_category($id,$cangrade,$currentpos-1,%categories);
                   1324: }
                   1325: 
                   1326: #
                   1327: # Move category down
                   1328: #
                   1329: 
                   1330: sub move_down_category {
                   1331:     my ($id,$cangrade,%categories)=@_;
                   1332:     my $currentpos=&current_pos_category($id,%categories);
                   1333:     my @order=split(/\,/,$categories{'order'});
                   1334:     if ($currentpos>=$#order) { return %categories; }
                   1335:     return &move_category($id,$cangrade,$currentpos+1,%categories);
                   1336: }
                   1337: 
                   1338: #
1.65      www      1339: # Move a category to a desired position n the display order
                   1340: #
                   1341: 
                   1342: sub move_category {
                   1343:     my ($id,$cangrade,$ordernum,%categories)=@_;
                   1344:     unless ($cangrade) { return %categories; }
                   1345:     my @order=split(/\,/,$categories{'order'});
                   1346: # Where is the index currently?
                   1347:     my $currentpos=&current_pos_category($id,%categories);
                   1348:     if (defined($currentpos)) {
                   1349:         if ($currentpos<$ordernum) {
                   1350: # This is moving to a higher index
                   1351: # ....X1234....
                   1352: # ....1234X....
                   1353:             for (my $i=$currentpos;$i<$ordernum;$i++) {
                   1354:                 $order[$i]=$order[$i+1];
                   1355:             }
                   1356:             $order[$ordernum]=$id;
                   1357:         }
                   1358:         if ($currentpos>$ordernum) {
                   1359: # This is moving to a lower index
                   1360: # ....1234X....
                   1361: # ....X1234....
                   1362:             for (my $i=$currentpos;$i>$ordernum;$i--) {
                   1363:                 $order[$i]=$order[$i-1];
                   1364:             }
                   1365:             $order[$ordernum]=$id;
                   1366:         }
                   1367:     }
                   1368:     $categories{'order'}=join(',',@order);
                   1369:     return %categories;
                   1370: }
                   1371: 
                   1372: #
                   1373: #  Find current postion of a category in the order
                   1374: #
                   1375: 
                   1376: sub current_pos_category {
                   1377:     my ($id,%categories)=@_;
                   1378:     my @order=split(/\,/,$categories{'order'});
                   1379:     for (my $i=0;$i<=$#order;$i++) {
                   1380:         if ($order[$i] eq $id) { return $i; }
                   1381:     }
                   1382: # not found
                   1383:     return undef;
                   1384: }
                   1385: 
                   1386: #
                   1387: # Set name of a category
                   1388: #
                   1389: sub set_category_name {
1.69      www      1390:     my ($cangrade,$id,$name,%categories)=@_;
                   1391:     unless ($cangrade) { return %categories; }
1.65      www      1392:     $categories{$id.'_name'}=$name;
                   1393:     return %categories;
                   1394: }
                   1395: 
                   1396: #
1.71      www      1397: # Set total of a category
1.70      www      1398: #
1.71      www      1399: sub set_category_total {
                   1400:     my ($cangrade,$id,$totaltype,$total,%categories)=@_;
1.70      www      1401:     unless ($cangrade) { return %categories; }
1.71      www      1402:     if (($categories{$id.'_total'} eq '') && ($total=~/\d/)) {
                   1403:         $totaltype='typein';
1.70      www      1404:     }
1.71      www      1405:     $categories{$id.'_totaltype'}=$totaltype;
                   1406:     if ($totaltype eq 'default') {
                   1407:         $categories{$id.'_total'}='';
1.70      www      1408:     } else {
1.71      www      1409:         $total=~s/\D//gs;
                   1410:         unless ($total) { $total=0; }
                   1411:         $categories{$id.'_total'}=$total;
1.70      www      1412:     }
                   1413:     return %categories;
                   1414: }
                   1415: 
1.71      www      1416: sub set_category_weight {
                   1417:     my ($cangrade,$id,$weight,%categories)=@_;
                   1418:     unless ($cangrade) { return %categories; }
                   1419:     $weight=~s/\D//gs;
                   1420:     unless ($weight) { $weight=0; }
                   1421:     $categories{$id.'_weight'}=$weight;
                   1422:     return %categories;
                   1423: }
1.70      www      1424: 
1.81      www      1425: sub set_category_displayachieved {
                   1426:     my ($cangrade,$id,$value,%categories)=@_;
                   1427:     unless ($cangrade) { return %categories; }
                   1428:     unless (($value eq 'percent') || ($value eq 'points')) { $value='percent'; }
                   1429:     $categories{$id.'_displayachieved'}=$value;
                   1430:     return %categories;
                   1431: }
                   1432: 
                   1433: 
1.70      www      1434: #
1.65      www      1435: # === end category-related
                   1436: #
                   1437: #
1.5       bowersj2 1438: # Pass this two refs to arrays for the start and end color, and a number
                   1439: # from 0 to 1 for how much of the latter you want to mix in. It will
                   1440: # return a string ready to show ("#FFC309");
1.51      www      1441: 
1.5       bowersj2 1442: sub mixColors {
                   1443:     my $start = shift;
                   1444:     my $end = shift;
                   1445:     my $ratio = shift;
                   1446:     
1.9       matthew  1447:     my ($a,$b);
1.5       bowersj2 1448:     my $final = "";
1.9       matthew  1449:     $a = $start->[0]; $b = $end->[0];
1.5       bowersj2 1450:     my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew  1451:     $a = $start->[1]; $b = $end->[1];
1.5       bowersj2 1452:     my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
1.9       matthew  1453:     $a = $start->[2]; $b = $end->[2];
1.5       bowersj2 1454:     my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
                   1455: 
1.16      bowersj2 1456:     $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
1.5       bowersj2 1457:     return "#" . $final;
1.1       bowersj2 1458: }
                   1459: 
                   1460: 1;

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