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

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

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