--- loncom/interface/lonquickgrades.pm	2011/05/24 23:44:33	1.91
+++ loncom/interface/lonquickgrades.pm	2020/09/01 03:39:38	1.119
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Quick Student Grades Display
 #
-# $Id: lonquickgrades.pm,v 1.91 2011/05/24 23:44:33 www Exp $
+# $Id: lonquickgrades.pm,v 1.119 2020/09/01 03:39:38 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -29,12 +29,31 @@
 package Apache::lonquickgrades;
 
 use strict;
-use Apache::Constants qw(:common :http);
+use Apache::Constants qw(:common :http REDIRECT);
 use POSIX;
 use Apache::loncommon;
 use Apache::lonlocal;
 use Apache::lonnet;
 use Apache::grades;
+use Apache::loncoursedata;
+use Apache::lonstudentassessment;
+use Apache::lonuserstate;
+
+use Time::HiRes;
+use Spreadsheet::WriteExcel;
+use Spreadsheet::WriteExcel::Utility();
+#
+# Excel data
+#
+my $excel_sheet;
+my $excel_workbook;
+my $filename;
+my $format;
+my $request_aborted;
+my $header_row;
+my $cols_output;
+my %prog_state;
+
 
 sub handler {
     my $r = shift;
@@ -57,31 +76,102 @@ sub real_handler {
         return OK;
     }
 
-    # Send header, don't cache this page
-    &Apache::loncommon::no_cache($r);
-    $r->send_http_header;
-
+    my $cangrade=&Apache::lonnet::allowed('mgr');
     my $showPoints =
         (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard')
       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories'));
-    my $notshowSPRSlink =
-        (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
-      || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals'));
-    my $notshowTotals=
-        $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
-    my $showCategories=
-        $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
 
+    my $reinitresult;
 
-    my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
-    my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
-    $r->print(&Apache::loncommon::start_page($title,undef,
-                                            {'bread_crumbs' => $brcrum})
-             );
+    if ($env{'request.course.id'}) {
+        my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+        my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+        my ($blocked,$blocktext) =
+            &Apache::loncommon::blocking_status('grades',$cnum,$cdom);
+        if ($blocked) {
+            my $checkrole = "cm./$cdom/$cnum";
+            if ($env{'request.course.sec'} ne '') {
+                $checkrole .= "/$env{'request.course.sec'}";
+            }
+            unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
+                    ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
+                &grades_blocked($r,$blocktext,$showPoints);
+                return OK;
+            }
+        }
+    }
+
+    unless ($cangrade) {
+        # Check for critical messages and redirect if present.
+        my ($redirect,$url) = &Apache::loncommon::critical_redirect(300,'grades');
+        if ($redirect) {
+            &Apache::loncommon::content_type($r,'text/html');
+            $r->header_out(Location => $url);
+            return REDIRECT;
+        }
+
+        # Check if course needs to be re-initialized
+        my $loncaparev = $r->dir_config('lonVersion');
+        ($reinitresult,my @reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
+
+        if ($reinitresult eq 'switch') {
+            &Apache::loncommon::content_type($r,'text/html');
+            $r->send_http_header;
+            $r->print(&Apache::loncommon::check_release_result(@reinit));
+            return OK;
+        } elsif ($reinitresult eq 'update') {
+            my $cid = $env{'request.course.id'};
+            my $cnum = $env{'course.'.$cid.'.num'};
+            my $cdom = $env{'course.'.$cid.'.domain'};
+            &Apache::loncommon::content_type($r,'text/html');
+            $r->send_http_header;
+            &startpage($r,$showPoints);
+            my $preamble = '<div id="LC_update_'.$cid.'" class="LC_info">'.
+                           '<br />'.
+                           &mt('Your course session is being updated because of recent changes by course personnel.').
+                           ' '.&mt('Please be patient.').'<br /></div>'.
+                           '<div style="padding:0;clear:both;margin:0;border:0"></div>';
+            %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
+            &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Updating course'));
+            $r->rflush();
+            my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
+            &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished')); 
+            &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
+            my $closure = <<ENDCLOSE;
+<script type="text/javascript">
+// <![CDATA[
+\$("#LC_update_$cid").hide('slow');
+// ]]>
+</script>
+ENDCLOSE
+            if ($ferr) {
+                $r->print($closure.&Apache::loncommon::end_page());
+                my $requrl = $r->uri;
+                $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
+                $env{'user.reinit'} = 1;
+                return HTTP_NOT_ACCEPTABLE;
+            } else {
+               $r->print($closure);
+            }
+        } elsif ((&Apache::loncommon::course_type() eq 'Placement') &&
+                 (!$env{'request.role.adv'})) {
+            my $furl = &Apache::lonpageflip::first_accessible_resource();
+            &Apache::loncommon::content_type($r,'text/html');
+            $r->header_out(Location => $furl);
+            return REDIRECT;
+        }
+    }
+
+    unless ($reinitresult eq 'update') {
+        # Send header, don't cache this page
+        &Apache::loncommon::no_cache($r);
+        $r->send_http_header;
+        &startpage($r,$showPoints);
+    }
+    $r->rflush();
 
     &startGradeScreen($r,'quick');
 
-    my $cangrade=&Apache::lonnet::allowed('mgr');
 #
 # Pick student
 #
@@ -89,16 +179,53 @@ sub real_handler {
     my $udom;
     my $stdid;
     if ($cangrade) {
+        $r->print("<h2>".&mt("Download Multiple")."</h2>".
+                  '<table cellspacing="5">'."\n".
+                  '<tr>'.
+                  '<td align="center"><b>'.&mt('Sections').'</b>'.
+                  &Apache::loncommon::help_open_topic("Chart_Sections").
+                  '</td>'.
+                  '<td align="center"><b>'.&mt('Groups').'</b>'.
+                  '</td>'.
+                  '<td align="center"><b>'.&mt('Student Data').'</b>'.
+                  &Apache::loncommon::help_open_topic("Chart_Student_Data").
+                  '</td>'.
+                  '<td align="center"><b>'.&mt('Access Status').'</b>'.
+                  &Apache::loncommon::help_open_topic("Chart_Enrollment_Status").
+                  '</td>'.
+                  '<td align="center"><b>'.&mt('Output Format').'</b>'.
+                  &Apache::loncommon::help_open_topic("Chart_Output_Formats").
+                  '</td><td>&nbsp;</td></tr>'."\n".
+                  '<tr><td align="center">'."\n".
+                  &Apache::lonstatistics::SectionSelect('Section','multiple',5).
+                  '</td><td align="center">'.
+                  &Apache::lonstatistics::GroupSelect('Group','multiple',5).
+                  '</td><td align="center">'.
+                  &Apache::lonstatistics::StudentDataSelect('StudentData','multiple',5,undef).
+                  '</td><td>'."\n".
+                  &Apache::lonhtmlcommon::StatusOptions(undef,undef,5).
+                  '</td><td>'."\n".
+                  &Apache::lonstudentassessment::CreateAndParseOutputSelector().
+                  '</td><td>'.
+                  '<input type="submit" name="download" value="'.&mt('Display/Download Multiple Students').'" />'.
+                  '</td></tr>'."\n".
+                  '</table>'."\n"
+                 );
+        $r->print("<hr /><h2>".&mt("Display Individual")."</h2>");
         if ($env{'form.uname'}) { $uname=$env{'form.uname'}; }
         if ($env{'form.udom'}) { $udom=$env{'form.udom'}; }
         if ($env{'form.id'}) { $stdid=$env{'form.id'}; }
         if (($stdid) && ($udom)) {
-            $uname=(&Apache::lonnet::idget($udom,$stdid))[1];
+            $uname=(&Apache::lonnet::idget($udom,[$stdid],'ids'))[1];
         }
         if (($stdid) && (!$uname)) {
             $r->print('<p><span class="LC_warning">'.&mt("Unknown Student/Employee ID: [_1]",$stdid).'</span></p>');
             $stdid='';
         }
+        if (($uname eq '') && ($udom eq '')) {
+            $uname = $env{'user.name'};
+            $udom = $env{'user.domain'};
+        }
         $r->print('<form method="post" name="quickform" action="/adm/quickgrades">');
         my $chooseopt=&Apache::loncommon::select_dom_form($udom,'udom').' '.
            &Apache::loncommon::selectstudent_link('quickform','uname','udom');
@@ -106,28 +233,101 @@ sub real_handler {
         $r->print(&mt('For User [_1] or Student/Employee ID [_2] at Domain [_3]'
                  ,'<input type="text" value="'.$uname.'" size="12" name="uname" />'
                  ,'<input type="text" value="'.$stdid.'" size="12" name="id" /> '
-                 ,$chooseopt).'<br />'.
-                 '<input type="submit" name="display" value="'.&mt('Update Display').'" /></p>');
+                 ,$chooseopt).
+                 '&nbsp;&nbsp;<input type="submit" name="display" value="'.&mt('Display Individual Student').'" /></p>');
         if (($uname) && ($udom)) {
             $r->print('<p>'.&mt('Full Name: [_1]',&Apache::loncommon::plainname($uname,$udom)).'</p>');
         }
     }
     $r->rflush();
 
-    my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=
-       &getData($showPoints,$uname,$udom);
+    my $notshowTotals=
+        $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
+    my $showCategories=
+        $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
+
+    my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,
+        $topLevelRight,$topLevelAttempted) = &getData($showPoints,$uname,$udom);
 
-    if ($showCategories) {
-       &outputCategories($r,$showPoints,$notshowTotals,
-                 $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
+    if (ref($navmap)) {
+        if ($showCategories) {
+            &outputCategories($r,$showPoints,$notshowTotals,$navmap,$totalParts,$totalPossible,
+                              $totalRight,$totalAttempted,$topLevelParts,$topLevelRight,
+                              $topLevelAttempted);
+        } else {
+            &outputTable($r,$showPoints,$notshowTotals,$navmap,$totalParts,$totalPossible,
+                         $totalRight,$totalAttempted,$topLevelParts,$topLevelRight,
+                         $topLevelAttempted);
+        }
     } else {
-       &outputTable($r,$showPoints,$notshowTotals,
-                 $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
+        if ($cangrade) { $r->print("\n</form>\n"); }
+        my $requrl = $r->uri;
+        $env{'user.error.msg'} = "$requrl:bre:0:0:Navmap initialization failed.";
+        return HTTP_NOT_ACCEPTABLE;
     }
     if ($cangrade) { $r->print("\n</form>\n"); }
     &endGradeScreen($r);
     return OK;
+}
+
+sub grades_blocked {
+    my ($r,$blocktext,$caller) = @_;
+    my $title = 'Points Display';
+    if ($caller eq 'spreadsheet') {
+        $title = 'Spreadsheet';
+    } elsif ($env{'course.'.$env{'request.course.id'}.'.grading'} ne 'standard') {
+        $title = 'Completed Problems Display';
+    }
+    my $brcrum = [{href=>"/adm/quickgrades",text => $title}];
+    &Apache::lonhtmlcommon::clear_breadcrumbs();
+    &Apache::lonhtmlcommon::add_breadcrumb({href=>'/adm/quickgrades',
+                                            text=> $title});
+    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs($title);
+    &Apache::loncommon::content_type($r,'text/html');
+    &Apache::loncommon::no_cache($r);
+    $r->send_http_header;
+    $r->print(&Apache::loncommon::start_page($title).
+              $breadcrumbs.
+              $blocktext.
+              &Apache::loncommon::end_page());
+    return;
+}
+
+sub getStudentCatGrade {
+    my ($uname,$udom,%categories)=@_;
+    my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=
+       &getData(1,$uname,$udom);
+    return &output_category_table(undef,0,$navmap,0,%categories);
+}
+
+sub getAllStudentData {
+    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+
+    my %categories=&Apache::lonnet::dump('grading_categories',$cdom,$cnum);
+
+    my $classlist = &Apache::loncoursedata::get_classlist();
 
+    my $statusidx   = &Apache::loncoursedata::CL_STATUS();
+    my $usernameidx = &Apache::loncoursedata::CL_SNAME();
+    my $domainidx   = &Apache::loncoursedata::CL_SDOM();
+    my $fullnameidx = &Apache::loncoursedata::CL_FULLNAME();
+
+    foreach my $key (keys(%{$classlist})) {
+        my $student = $classlist->{$key};
+        my $perc=&getStudentCatGrade($classlist->{$student}->[$usernameidx],
+                                     $classlist->{$student}->[$domainidx],
+                                     %categories);
+    }
+}
+
+sub startpage {
+    my ($r,$showPoints) = @_;
+    my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
+    my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
+    $r->print(&Apache::loncommon::start_page($title,undef,
+                                            {'bread_crumbs' => $brcrum})
+             );
 }
 
 sub startGradeScreen {
@@ -139,13 +339,24 @@ sub startGradeScreen {
         (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals')
       || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories'));
-    my $notshowTotals=
+    my $notshowTotals =
         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
-    my $showCategories=
+    my $showSPRSlink =
+        $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'spreadsheet';
+    my $showCategories =
         $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
+ 
+    my $allowed_to_view = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
+    if ((!$allowed_to_view) && ($env{'request.course.sec'} ne '')) {
+        $allowed_to_view = &Apache::lonnet::allowed('vgr',
+                               "$env{'request.course.id'}/$env{'request.course.sec'}");
+    }
 
-    my $allowed_to_view =  &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
-    my $allowed_to_edit =  &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
+    my $allowed_to_edit = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
+    if ((!$allowed_to_edit) && ($env{'request.course.sec'} ne '')) {
+        $allowed_to_edit = &Apache::lonnet::allowed('mgr',
+                               "$env{'request.course.id'}/$env{'request.course.sec'}");
+    }
 
     if ($allowed_to_view) {
        my @notes;
@@ -153,6 +364,7 @@ sub startGradeScreen {
        push(@notes,&mt('Students do not see link to spreadsheet.')) if ($notshowSPRSlink);
        push(@notes,&mt('Students will see points based on problem weights.')) if ($showPoints);
        push(@notes,&mt('Students will see points based on categories.')) if ($showCategories);
+       push(@notes,&mt('Students will see link to spreadsheet.')) if ($showSPRSlink);
        push(@notes, &Apache::lonhtmlcommon::coursepreflink(&mt('Grade display settings'),'grading'));
        $r->print(&Apache::loncommon::head_subbox(join('&nbsp;&nbsp;',@notes)));
     }
@@ -172,7 +384,7 @@ sub startGradeScreen {
                                                                  &mt('Statistics and Reports').'</b></a></li>');
 
        $r->print("\n".'<li'.($mode eq 'chart'?' class="active"':'').'><a href="/adm/statistics?reportSelected=student_assessment"><b>'.
-                                                                 &mt('Assessment Overview Chart').'</b></a></li>');
+                                                                 &mt('Assessment Chart').'</b></a></li>');
 
     }
     if ($allowed_to_edit) {
@@ -181,7 +393,7 @@ sub startGradeScreen {
        if ($env{'form.symb'}) {
           $r->print("\n".'<li'.($mode eq 'probgrading'?' class="active"':'').'><a href="/adm/grades?symb='.
                                               &Apache::lonhtmlcommon::entity_encode($env{'form.symb'}).
-                                              '&command=gradingmenu"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
+                                              '&amp;command=gradingmenu"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.
                                               &mt('Problem Grading').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>');
 
        }
@@ -191,10 +403,73 @@ sub startGradeScreen {
 }
 
 sub endGradeScreen {
-   my ($r)=@_;
-   $r->print('</div></div></div>'.&Apache::loncommon::end_page());
+    my ($r)=@_;
+    $r->print('</div></div></div>'.&Apache::loncommon::end_page());
+    return;
 }
 
+# -----------
+
+
+sub excel_cleanup {
+    undef ($excel_sheet);
+    undef ($excel_workbook);
+    undef ($filename);
+    undef ($format);
+}
+
+
+sub excel_initialize {
+    my ($r) = @_;
+
+    &excel_cleanup();
+
+    # Create sheet
+    ($excel_workbook,$filename,$format)=
+        &Apache::loncommon::create_workbook($r);
+    return if (! defined($excel_workbook));
+   #
+   # Add a worksheet
+    my $sheetname = $env{'course.'.$env{'request.course.id'}.'.description'};
+    $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
+    $excel_sheet = $excel_workbook->addworksheet($sheetname);
+   #
+   # Put the course description in the header
+    $excel_sheet->write($header_row,$cols_output++,
+                   $env{'course.'.$env{'request.course.id'}.'.description'},
+                        $format->{'h1'});
+} 
+
+sub excel_finish {
+    my ($r) = @_;
+    if ($request_aborted || ! defined($excel_sheet)) {
+        &excel_cleanup();
+        return;
+    }
+    #
+    # Write the excel file
+    $excel_workbook->close();
+    #
+    # Close the progress window
+    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
+    #
+    # Tell the user where to get their excel file
+    $r->print('<br />'.
+              '<a href="'.$filename.'">'.&mt('Your Excel spreadsheet').'</a>'."\n");
+    $r->rflush();
+    &excel_cleanup();
+    return;
+}
+
+
+#
+# CSV data
+#
+# -----------
+
+#
+# Go through the complete course and collect data
+#
 
 sub getData {
 
@@ -203,13 +478,17 @@ sub getData {
     # Create the nav map
     my $navmap = Apache::lonnavmaps::navmap->new($uname,$udom);
 
+    if (!defined($navmap)) {
+        return ();
+    }
+
     my $res = $navmap->firstResource(); # temp resource to access constants
 
     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
     my $depth = 1;
     $iterator->next(); # ignore first BEGIN_MAP
     my $curRes = $iterator->next();
-    
+
     # General overview of the following: Walk along the course resources.
     # For every problem in the resource, tell its parent maps how many
     # parts and how many parts correct it has. After that, each map will
@@ -232,7 +511,8 @@ sub getData {
         if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
         if ($curRes == $iterator->END_MAP()) { $depth--; }
 
-        if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
+        if (ref($curRes) && $curRes->is_gradable() && !$curRes->randomout && 
+            ($curRes->deeplink ne 'absent'))
         {
             # Get number of correct, incorrect parts
             my $parts = $curRes->parts();
@@ -242,39 +522,40 @@ sub getData {
             my $stack = $iterator->getStack();
             
             for my $part (@{$parts}) {
-		my $completionStatus = $curRes->getCompletionStatus($part);
 		my $dateStatus = $curRes->getDateStatus($part);
-		
-                if ($completionStatus == $curRes->EXCUSED()) {
+                my $weight = $curRes->weight($part);
+                my $problemstatus = $curRes->problemstatus($part);
+
+                if ($curRes->solved($part) eq 'excused') {
                     next;
                 }
 		if ($showPoints) {
 		    my $score = 0;
 		    # If we're not telling status and the answer date isn't passed yet, 
 		    # it's an "attempted" point
-		    if ((($curRes->problemstatus($part) eq 'no') ||
-                        ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
+		    if ((($problemstatus eq 'no') ||
+                         ($problemstatus eq 'no_feedback_ever')) &&
 			($dateStatus != $curRes->ANSWER_OPEN)) {
 			my $status = $curRes->simpleStatus($part);
 			if ($status == $curRes->ATTEMPTED) {
-			    $partsAttempted += $curRes->weight($part);
+			    $partsAttempted += $weight;
 			    $totalAttempted += $partsAttempted;
 			}
 		    } else {
-			$score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
+			$score = &Apache::grades::compute_points($weight, $curRes->awarded($part));
 		    }
 		    $partsRight += $score;
 		    $totalRight += $score;
-		    $partsCount += $curRes->weight($part);
+		    $partsCount += $weight;
 
                     $curRes->{DATA}->{PROB_SCORE}  += $score;
-                    $curRes->{DATA}->{PROB_WEIGHT} += $curRes->weight($part);
+                    $curRes->{DATA}->{PROB_WEIGHT} += $weight;
 
 		    if ($curRes->opendate($part) < $now) {
-			$totalPossible += $curRes->weight($part);
-                        $curRes->{DATA}->{PROB_POSSIBLE} += $curRes->weight($part);
+			$totalPossible += $weight;
+                        $curRes->{DATA}->{PROB_POSSIBLE} += $weight;
 		    }
-		    $totalParts += $curRes->weight($part);
+		    $totalParts += $weight;
 		} else {
 		    my $status = $curRes->simpleStatus($part);
 		    my $thisright = 0;
@@ -291,7 +572,6 @@ sub getData {
 			$totalAttempted++;
 		    }
 		    
-		    my $dateStatus = $curRes->getDateStatus($part);
 		    $totalParts++;
 		    if ($curRes->opendate($part) < $now) {
 			$totalPossible++;
@@ -322,7 +602,8 @@ sub getData {
         }
         $curRes = $iterator->next();
     }
-    return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
+    return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,
+            $topLevelParts,$topLevelRight,$topLevelAttempted);
 }
 
 #
@@ -331,8 +612,8 @@ sub getData {
 
 sub outputTable {
 
-    my ($r,$showPoints,$notshowTotals,
-           $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
+    my ($r,$showPoints,$notshowTotals,$navmap,$totalParts,$totalPossible,$totalRight,
+        $totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
 
     my @start = (255, 255, 192);
     my @end   = (0, 192, 0);
@@ -430,6 +711,7 @@ sub outputTable {
     }
 
     $r->print(&Apache::loncommon::end_data_table());
+    return;
 }
 
 #
@@ -461,7 +743,7 @@ sub outputCategories {
         &Apache::lonnet::put('grading_categories',\%categories,$cdom,$cnum);
     }
 # new categories loaded now
-    &output_category_table($r,$cangrade,$navmap,%categories);
+    &output_category_table($r,$cangrade,$navmap,1,%categories);
 #
     if ($cangrade) {
         $r->print(&Apache::loncommon::resourcebrowser_javascript().
@@ -471,13 +753,6 @@ sub outputCategories {
                   '<input type="submit" name="storechanges" value="'.&mt("Save changes to grading categories").'" />'.
                   '<script>function storecmd (cmd) { document.quickform.cmd.value=cmd; document.quickform.submit(); }</script>');
     }
-#
-# Debug
-#
-#    my %data=&dumpdata($navmap);
-#    foreach (keys(%data)) {
-#        $r->print("\n<br />".$_.'='.$data{$_});
-#    }
 }
 
 #
@@ -560,27 +835,36 @@ sub process_category_edits {
 #
 
 sub output_category_table {
-    my ($r,$cangrade,$navmaps,%categories)=@_;
-    my $sum=0;
-    my $total=0;
-    $r->print(&Apache::loncommon::start_data_table());
+    my ($r,$cangrade,$navmaps,$output,%categories)=@_;
+    
+    my $totalweight=0;
+    my $totalpoints=0;
+
+    if ($output) { 
+       $r->print(&Apache::loncommon::start_data_table());
 #
-    &output_category_table_header($r,$cangrade);
+       &output_category_table_header($r,$cangrade);
+    }
 #
     my @order=split(/\,/,$categories{'order'});
 #
     my %performance=&dumpdata($navmaps);
     my $maxpos=$#order;
     for (my $i=0;$i<=$maxpos;$i++) {
-        my ($value,$weight)=&output_and_calc_category($r,$cangrade,$navmaps,$order[$i],$i,$maxpos,\%performance,1,%categories);
-        $sum+=$value*$weight;
-        $total+=$weight;
+        my ($correct,$possible,$type,$weight)=&output_and_calc_category($r,$cangrade,$navmaps,$order[$i],$i,$maxpos,\%performance,$output,%categories);
+        unless ($possible) { next; }
+        $totalpoints+=$weight*$correct/$possible;
+        $totalweight+=$weight;
     }
 #
-    &bottom_line_category($r,$cangrade,$sum,$total);
-#
-    $r->print(&Apache::loncommon::end_data_table());
-    return $sum;
+    my $perc=0;
+    if ($totalweight) { $perc=100.*$totalpoints/$totalweight; }
+
+    if ($output) { 
+        &bottom_line_category($r,$cangrade,$perc); 
+        $r->print(&Apache::loncommon::end_data_table());
+    }
+    return $perc;
 }
 
 sub output_category_table_header {
@@ -605,15 +889,15 @@ sub output_category_table_header {
 
 sub output_and_calc_category {
     my ($r,$cangrade,$navmaps,$id,$currentpos,$maxpos,$performance,$output,%categories)=@_;
-    my $value=0;
-    my $weight=0;
-    my $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL') . "/");
-    my %lt=&Apache::lonlocal::texthash(
-           'up' => 'Move Up',
-           'dw' => 'Move Down');
+    
     if ($output) { $r->print("\n".&Apache::loncommon::start_data_table_row()); }
 
     if ($output && $cangrade) {
+        my $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL') . "/");
+        my %lt=&Apache::lonlocal::texthash(
+           'up' => 'Move Up',
+           'dw' => 'Move Down');
+
         $r->print(<<ENDMOVE);
 <td>
 <div class="LC_docs_entry_move">
@@ -655,7 +939,7 @@ ENDMOVE
         if ($possible>0) { push(@individual,"$possible:$correct"); }
         if ($output) {
            $r->print('<li>');
-           $r->print(&Apache::lonnet::gettitle($contentid).' ('.$correct.'/'.$possible.')');
+           $r->print(&Apache::lonnet::gettitle($contentid).' ('.&numberout($correct).'/'.&numberout($possible).')');
            if ($cangrade) {
               $r->print(' <a href="javascript:storecmd(\'delcont_'.$id.'______'.$contentid.'\');">'.&mt('Delete').'</a>');
            }
@@ -664,10 +948,10 @@ ENDMOVE
     }
     if ($output) {
        $r->print('</ul>');
-       $r->print('<p>'.&mt('Total raw points: [_1]/[_2]',$totalcorrect,$totalpossible).'</p>'); 
        if ($cangrade) {
            $r->print('<br />'.&Apache::loncommon::selectresource_link('quickform','addcont_'.$id,&mt('Add Problem or Folder')).'<br />');
        }
+       $r->print('<p><b>'.&mt('Total raw points: [_1]/[_2]',&numberout($totalcorrect),&numberout($totalpossible)).'</b></p>');
        $r->print('</td>'); 
     }
 # Total
@@ -692,12 +976,11 @@ ENDMOVE
        $totalpossible=1.*$categories{$id.'_total'};
     }
     if ($output) {
-       $r->print('<p>'.&mt('Adjusted raw points: [_1]/[_2]',$totalcorrect,$totalpossible).'</p>');
+       $r->print('<p><b>'.&mt('Adjusted raw points: [_1]/[_2]',&numberout($totalcorrect),&numberout($totalpossible)).'</b></p>');
     }
 
 
 # Calculation
-    my $actualtotal=$totalcorrect;
     if ($output) { $r->print('<td><ul>'); }
     foreach my $calcrule (split(/\,/,$categories{$id.'_calculations'})) {
         if ($output) { $r->print('<li>'); }
@@ -708,77 +991,145 @@ ENDMOVE
         }
         if ($code eq 'capabove') {
             if ($totalpossible>0) {
-                if ($actualtotal/$totalpossible>$value/100.) {
-                    $actualtotal=$totalpossible*$value/100.;
+                if ($totalcorrect/$totalpossible>$value/100.) {
+                    $totalcorrect=$totalpossible*$value/100.;
                 }
             }
         } elsif ($code eq 'capbelow') {
             if ($totalpossible>0) {
-                if ($actualtotal/$totalpossible<$value/100.) {
-                    $actualtotal=$totalpossible*$value/100.;
+                if ($totalcorrect/$totalpossible<$value/100.) {
+                    $totalcorrect=$totalpossible*$value/100.;
                 }
             }
+        } elsif ($code eq 'droplow') {
+            ($totalpossible,$totalcorrect,@individual)=&drop(0,0,$value,@individual);
+        } elsif ($code eq 'drophigh') {
+            ($totalpossible,$totalcorrect,@individual)=&drop(1,0,$value,@individual);
+        } elsif ($code eq 'droplowperc') {
+            ($totalpossible,$totalcorrect,@individual)=&drop(0,1,$value,@individual);
+        } elsif ($code eq 'drophighperc') {
+            ($totalpossible,$totalcorrect,@individual)=&drop(1,1,$value,@individual);
         }
         if ($output) { $r->print('</li>'); }
     }
-    if ($output) { 
-       $r->print('</ul>'); 
-       $r->print('<p>'.&mt('Calculated points: [_1]/[_2]',$actualtotal,$totalpossible).'</p>');
+# Re-adjust total points if force total
+    if ($categories{$id.'_totaltype'} eq 'typein') {
+       $totalpossible=1.*$categories{$id.'_total'};
     }
 
-    if ($cangrade) {
-        if ($output) { $r->print('<br />'.&new_calc_rule_form($id)); }
+    if ($output) { 
+        $r->print('</ul>'); 
+        if ($cangrade) { $r->print('<br />'.&new_calc_rule_form($id)); }
+        $r->print('<p><b>'.&mt('Calculated points: [_1]/[_2]',&numberout($totalcorrect),&numberout($totalpossible)).'</b></p>');
+        $r->print('</td>'); 
     }
-    if ($output) { $r->print('</td>'); }
-
+#
+# Prepare for export
+#
 # Weight
+    my $weight=$categories{$id.'_weight'};
+    unless (1.*$weight>0) { $weight=0; }
     if ($cangrade) {
        if ($output) { 
           $r->print('<td>'.
                   '<input type="text" size="4" name="weight_'.$id.
-                  '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_weight'}).'" /></td>');
+                  '" value="'.&Apache::lonhtmlcommon::entity_encode($weight).'" /></td>');
        }
     } else {
        if ($output) {
-          $r->print('<td>'.$categories{$id.'_weight'}.'</td>');
+          $r->print('<td>'.$weight.'</td>');
        }
     }
 # Achieved
+    my $type=$categories{$id.'_displayachieved'};
+    unless (($type eq 'percent') || ($type eq 'points')) { $type='points'; }
     if ($output) { $r->print('<td>'); }
     if ($cangrade) {
         if ($output) {
            $r->print('<select name="displayachieved_'.$id.'">'.
-                  '<option value="percent"'.($categories{$id.'_displayachieved'} eq 'percent'?' selected="selected"':'').'>'.&mt('percent').'</option>'.
-                  '<option value="points"'.($categories{$id.'_displayachieved'} eq 'points'?' selected="selected"':'').'>'.&mt('points').'</option>'.
+                  '<option value="percent"'.($type eq 'percent'?' selected="selected"':'').'>'.&mt('percent').'</option>'.
+                  '<option value="points"'.($type eq 'points'?' selected="selected"':'').'>'.&mt('points').'</option>'.
                   '</select>');
         }
-    } else {
-        if ($output) {
-            if ($categories{$id.'_displayachieved'} eq 'percent') {
-                $r->print(&mt('percent'));
-            } else {
-                $r->print(&mt('points'));
+    }
+    if ($output) {
+        $r->print('<p><b>');
+        if ($type eq 'percent') {
+            my $perc='---';
+            if ($totalpossible) {
+                $perc=100.*$totalcorrect/$totalpossible;
             }
+            $r->print(&mt('[_1] percent',&numberout($perc)));
+        } else {
+            $r->print(&mt('[_1]/[_2] points',&numberout($totalcorrect),&numberout($totalpossible)));
         }
+        $r->print('</b></p>');
     }
     if ($output) { $r->print('</td>'); }
 
-    return ($value,$weight);
+    return ($totalcorrect,$totalpossible,$type,$weight);
 }
 
 #
+# Drop folders and problems
+#
+
+sub drop {
+    my ($high,$percent,$n,@individual)=@_;
+# Sort assignments by points or percent
+    my @newindividual=sort {
+        my ($pa,$ca)=split(/\:/,$a);
+        my ($pb,$cb)=split(/\:/,$b);
+        if ($percent) {
+            my $perca=0;
+            if ($pa>0) { $perca=$ca/$pa; }
+            my $percb=0;
+            if ($pb>0) { $percb=$cb/$pb; }
+            $perca<=>$percb;
+        } else {
+            $ca<=>$cb;
+        }
+    } @individual;
+# Drop the ones we don't want
+    if ($#newindividual>=$n) {
+        if ($high) {
+           splice(@newindividual,$#newindividual+1-$n,$n);
+        } else {
+           splice(@newindividual,0,$n);
+        }
+    } else {
+        @newindividual=();
+    }
+# Re-calculate how many points possible and achieved
+    my $newpossible=0;
+    my $newcorrect=0;
+    for my $score (@newindividual) {
+        my ($thispossible,$thiscorrect)=(split(/\:/,$score));
+        $newpossible+=$thispossible;
+        $newcorrect+=$thiscorrect;
+    }
+    return ($newpossible,$newcorrect,@newindividual);
+} 
+#
 # Bottom line with grades
 #
 
 sub bottom_line_category {
-    my ($r,$cangrade,$sum,$total)=@_;
+    my ($r,$cangrade,$perc)=@_;
     $r->print(&Apache::loncommon::start_data_table_row());
     if ($cangrade) {
         $r->print('<td colspan="3"><a href="javascript:storecmd(\'createnewcat\');">'.&mt('Create New Category').'</a></td>');
     }
-    $r->print('<td colspan="6">'.&mt('Current:').$sum.'<br />'.&mt('Total:').$total.'<br /></td>');
+    $r->print('<td colspan="6"><b>'.&mt('Total: [_1] percent',&numberout($perc)).'</b></td>');
 }
 
+sub numberout {
+    my ($number)=@_;
+    my $printout=sprintf("%.3f", $number);
+    $printout=~s/0+$//;
+    $printout=~s/\.$//;
+    return $printout;
+}
 #
 # Make one new category
 #