--- loncom/homework/grades.pm	2020/09/09 23:01:12	1.776
+++ loncom/homework/grades.pm	2021/06/04 20:09:28	1.785
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.776 2020/09/09 23:01:12 raeburn Exp $
+# $Id: grades.pm,v 1.785 2021/06/04 20:09:28 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1166,8 +1166,33 @@ LISTJAVASCRIPT
         $nocompmsg = '';
     }
     $gradeTable .= &Apache::lonhtmlcommon::row_title($viewtitle)
-                  .$submission_options
-                  .&Apache::lonhtmlcommon::row_closure()
+                  .$submission_options;
+# Check if any gradable
+    my $showmore;
+    if ($perm{'mgr'}) {
+        my @sections;
+        if ($env{'request.course.sec'} ne '') {
+            @sections = ($env{'request.course.sec'});
+        } elsif ($env{'form.section'} eq '') {
+            @sections = ('all');
+        } else {
+            @sections = &Apache::loncommon::get_env_multiple('form.section');
+        }
+        if (grep(/^all$/,@sections)) {
+            $showmore = 1;
+        } else {
+            foreach my $sec (@sections) {
+                if (&canmodify($sec)) {
+                    $showmore = 1;
+                    last;
+                }
+            }
+        }
+    }
+
+    if ($showmore) {
+        $gradeTable .=
+                   &Apache::lonhtmlcommon::row_closure()
                   .&Apache::lonhtmlcommon::row_title(&mt('Send Messages'))
                   .'<span class="LC_nobreak">'
                   .'<label><input type="radio" name="compmsg" value="0"'.$nocompmsg.' />'
@@ -1176,20 +1201,22 @@ LISTJAVASCRIPT
                   .&mt('Yes').('&nbsp;'x2).'</label>'
                   .&Apache::lonhtmlcommon::row_closure();
 
-    $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Grading Increments'))
+        $gradeTable .= 
+                   &Apache::lonhtmlcommon::row_title(&mt('Grading Increments'))
                   .'<select name="increment">'
                   .'<option value="1">'.&mt('Whole Points').'</option>'
                   .'<option value=".5">'.&mt('Half Points').'</option>'
                   .'<option value=".25">'.&mt('Quarter Points').'</option>'
                   .'<option value=".1">'.&mt('Tenths of a Point').'</option>'
                   .'</select>';
+    }
     $gradeTable .= 
         &build_section_inputs().
 	'<input type="hidden" name="submitonly"  value="'.$submitonly.'" />'."\n".
 	'<input type="hidden" name="symb" value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 	'<input type="hidden" name="saveStatusOld" value="'.$saveStatus.'" />'."\n";
     if (exists($env{'form.Status'})) {
-	$gradeTable .= '<input type="hidden" name="Status" value="'.$stu_status.'" />'."\n";
+	$gradeTable .= '<input type="hidden" name="Status" value="'.$env{'form.Status'}.'" />'."\n";
     } else {
         $gradeTable .= &Apache::lonhtmlcommon::row_closure()
                       .&Apache::lonhtmlcommon::row_title(&mt('Student Status'))
@@ -3977,7 +4004,26 @@ sub viewgrades {
     }
 
     my ($common_header,$specific_header,@sections,$section_display);
-    @sections = &Apache::loncommon::get_env_multiple('form.section');
+    if ($env{'request.course.sec'} ne '') {
+        @sections = ($env{'request.course.sec'});
+    } else {
+        @sections = &Apache::loncommon::get_env_multiple('form.section');
+    }
+
+# Check if Save button should be usable
+    my $disabled = ' disabled="disabled"';
+    if ($perm{'mgr'}) {
+        if (grep(/^all$/,@sections)) {
+            undef($disabled);
+        } else {
+            foreach my $sec (@sections) {
+                if (&canmodify($sec)) {
+                    undef($disabled);
+                    last;
+                }
+            }
+        }
+    }
     if (grep(/^all$/,@sections)) {
         @sections = ('all');
         if ($group_display) {
@@ -4169,7 +4215,7 @@ sub viewgrades {
     }
     $result.=&Apache::loncommon::end_data_table();
     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
-    $result.='<input type="button" value="'.&mt('Save').'" '.
+    $result.='<input type="button" value="'.&mt('Save').'"'.$disabled.' '.
 	'onclick="javascript:submit();" target="_self" /></form>'."\n";
     if ($ctr == 0) {
         my $stu_status = join(' or ',&Apache::loncommon::get_env_multiple('form.Status'));
@@ -5409,10 +5455,14 @@ sub displayPage {
 	}
         $curRes = $iterator->next();
     }
+    my $disabled;
+    unless (&canmodify($usec)) {
+        $disabled = ' disabled="disabled"';
+    }
 
     $studentTable.=
         '</table>'."\n".
-        '<input type="button" value="'.&mt('Save').'" '.
+        '<input type="button" value="'.&mt('Save').'"'.$disabled.' '.
         'onclick="javascript:checkSubmitPage(this.form,'.$question.');" />'.
         '</form>'."\n";
     $request->print($studentTable);
@@ -10382,37 +10432,47 @@ sub grading_menu {
 
     $fields{'command'} = 'initialverifyreceipt';
     my $url5 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
-    
+
+    my %permissions;
+    if ($perm{'mgr'}) {
+        $permissions{'either'} = 'F';
+        $permissions{'mgr'} = 'F';
+    }
+    if ($perm{'vgr'}) {
+        $permissions{'either'} = 'F';
+        $permissions{'vgr'} = 'F';
+    }
+
     my @menu = ({	categorytitle=>'Hand Grading',
             items =>[
                         {	linktext => 'Select individual students to grade',
                     		url => $url1a,
-                    		permission => 'F',
+                    		permission => $permissions{'either'},
                     		icon => 'grade_students.png',
                     		linktitle => 'Grade current resource for a selection of students.'
                         }, 
                         {       linktext => 'Grade ungraded submissions',
                                 url => $url1b,
-                                permission => 'F',
+                                permission => $permissions{'either'},
                                 icon => 'ungrade_sub.png',
                                 linktitle => 'Grade all submissions that have not been graded yet.'
                         },
 
                         {       linktext => 'Grading table',
                                 url => $url1c,
-                                permission => 'F',
+                                permission => $permissions{'either'},
                                 icon => 'grading_table.png',
                                 linktitle => 'Grade current resource for all students.'
                         },
                         {       linktext => 'Grade page/folder for one student',
                                 url => $url1d,
-                                permission => 'F',
+                                permission => $permissions{'either'},
                                 icon => 'grade_PageFolder.png',
                                 linktitle => 'Grade all resources in current page/sequence/folder for one student.'
                         },
                         {       linktext => 'Download submissions',
                                 url => $url1e,
-                                permission => 'F',
+                                permission => $permissions{'either'},
                                 icon => 'download_sub.png',
                                 linktitle => 'Download all students submissions.'
                         }]},
@@ -10421,25 +10481,25 @@ sub grading_menu {
 
                 	    {	linktext => 'Upload Scores',
                     		url => $url2,
-                    		permission => 'F',
+                    		permission => $permissions{'mgr'},
                     		icon => 'uploadscores.png',
                     		linktitle => 'Specify a file containing the class scores for current resource.'
                 	    },
                 	    {	linktext => 'Process Clicker',
                     		url => $url3,
-                    		permission => 'F',
+                    		permission => $permissions{'mgr'},
                     		icon => 'addClickerInfoFile.png',
                     		linktitle => 'Specify a file containing the clicker information for this resource.'
                 	    },
                 	    {	linktext => 'Grade/Manage/Review Bubblesheets',
                     		url => $url4,
-                    		permission => 'F',
+                    		permission => $permissions{'mgr'},
                     		icon => 'bubblesheet.png',
                     		linktitle => 'Grade bubblesheet exams, upload/download bubblesheet data files, and review previously graded bubblesheet exams.'
                 	    },
                             {   linktext => 'Verify Receipt Number',
                                 url => $url5,
-                                permission => 'F',
+                                permission => $permissions{'either'},
                                 icon => 'receipt_number.png',
                                 linktitle => 'Verify a system-generated receipt number for correct problem solution.'
                             }
@@ -10574,6 +10634,14 @@ sub selectfield {
            (&substatus_options,
             'select_form_order' => ['yes','queued','graded','incorrect','all']);
    }
+
+  #
+  # PrepareClasslist() needs to be called to avoid getting a sections list
+  # for a different course from the @Sections global in lonstatistics.pm, 
+  # populated by an earlier request.
+  #
+   &Apache::lonstatistics::PrepareClasslist();
+
    my $result='<div class="LC_columnSection">
   
     <fieldset>
@@ -11305,7 +11373,7 @@ sub navmap_errormsg {
 }
 
 sub startpage {
-    my ($r,$symb,$crumbs,$onlyfolderflag,$nodisplayflag,$stuvcurrent,$stuvdisp,$nomenu,$js,$onload,$divforres) = @_;
+    my ($r,$symb,$crumbs,$onlyfolderflag,$nodisplayflag,$stuvcurrent,$stuvdisp,$nomenu,$head_extra,$onload,$divforres) = @_;
     my %args;
     if ($onload) {
          my %loaditems = (
@@ -11315,11 +11383,13 @@ sub startpage {
     }
     if ($nomenu) {
         $args{'only_body'} = 1; 
-        $r->print(&Apache::loncommon::start_page("Student's Version",$js,\%args));
+        $r->print(&Apache::loncommon::start_page("Student's Version",$head_extra,\%args));
     } else {
-        unshift(@$crumbs,{href=>&href_symb_cmd($symb,'gradingmenu'),text=>"Grading"});
+        if ($env{'request.course.id'}) { 
+            unshift(@$crumbs,{href=>&href_symb_cmd($symb,'gradingmenu'),text=>"Grading"});
+        }
         $args{'bread_crumbs'} = $crumbs;
-        $r->print(&Apache::loncommon::start_page('Grading',$js,\%args));
+        $r->print(&Apache::loncommon::start_page('Grading',$head_extra,\%args));
         if ($env{'request.course.id'}) {
             &Apache::lonquickgrades::startGradeScreen($r,($env{'form.symb'}?'probgrading':'grading'));
         }
@@ -11402,7 +11472,15 @@ sub handler {
             } else {
                 $divforres = 1;
             }
-            &startpage($request,$symb,[{href=>"", text=>"Student Submissions"}],undef,undef,$stuvcurrent,$stuvdisp,undef,$js,$onload,$divforres);
+            my $head_extra = $js;
+            unless ($env{'form.vProb'} eq 'no') {
+                my $csslinks = &Apache::loncommon::css_links($symb);
+                if ($csslinks) {
+                    $head_extra .= "\n$csslinks";
+                }
+            }
+            &startpage($request,$symb,[{href=>"", text=>"Student Submissions"}],undef,undef,
+                       $stuvcurrent,$stuvdisp,undef,$head_extra,$onload,$divforres);
             if ($versionform) {
                 if ($divforres) {
                     $request->print('<div style="padding:0;clear:both;margin:0;border:0"></div>');
@@ -11415,7 +11493,15 @@ sub handler {
                 &choose_task_version_form($symb,$env{'form.student'},
                                           $env{'form.userdom'},
                                           $env{'form.inhibitmenu'});
-            &startpage($request,$symb,[{href=>"", text=>"Previous Student Version"}],undef,undef,$stuvcurrent,$stuvdisp,$env{'form.inhibitmenu'},$js);
+            my $head_extra = $js;
+            unless ($env{'form.vProb'} eq 'no') {
+                my $csslinks = &Apache::loncommon::css_links($symb);
+                if ($csslinks) {
+                    $head_extra .= "\n$csslinks";
+                }
+            }
+            &startpage($request,$symb,[{href=>"", text=>"Previous Student Version"}],undef,undef,
+                       $stuvcurrent,$stuvdisp,$env{'form.inhibitmenu'},$head_extra);
             if ($versionform) {
                 $request->print($versionform);
             }
@@ -11426,10 +11512,14 @@ sub handler {
                                        {href=>'',text=>'Select student'}],1,1);
 	    &pickStudentPage($request,$symb);
 	} elsif ($command eq 'displayPage' && $perm{'vgr'}) {
+            my $csslinks;
+            unless ($env{'form.vProb'} eq 'no') {
+                $csslinks = &Apache::loncommon::css_links($symb,'map');
+            }
             &startpage($request,$symb,
                                       [{href=>&href_symb_cmd($symb,'all_for_one'),text=>'Grade page/folder for one student'},
                                        {href=>'',text=>'Select student'},
-                                       {href=>'',text=>'Grade student'}],1,1);
+                                       {href=>'',text=>'Grade student'}],1,1,undef,undef,undef,$csslinks);
 	    &displayPage($request,$symb);
 	} elsif ($command eq 'gradeByPage' && $perm{'mgr'}) {
             &startpage($request,$symb,[{href=>&href_symb_cmd($symb,'all_for_one'),text=>'Grade page/folder for one student'},
@@ -11438,8 +11528,12 @@ sub handler {
                                        {href=>'',text=>'Store grades'}],1,1);
 	    &updateGradeByPage($request,$symb);
 	} elsif ($command eq 'processGroup' && $perm{'vgr'}) {
+            my $csslinks;
+            unless ($env{'form.vProb'} eq 'no') {
+                $csslinks = &Apache::loncommon::css_links($symb);
+            }
             &startpage($request,$symb,[{href=>'',text=>'...'},
-                                       {href=>'',text=>'Modify grades'}],undef,undef,undef,undef,undef,undef,undef,1);
+                                       {href=>'',text=>'Modify grades'}],undef,undef,undef,undef,undef,$csslinks,undef,1);
 	    &processGroup($request,$symb);
 	} elsif ($command eq 'gradingmenu' && $perm{'vgr'}) {
             &startpage($request,$symb);