[LON-CAPA-cvs] cvs: loncom /homework grades.pm

raeburn raeburn at source.lon-capa.org
Wed May 20 18:02:58 EDT 2020


raeburn		Wed May 20 22:02:58 2020 EDT

  Modified files:              
    /loncom/homework	grades.pm 
  Log:
  - Bug 6215
    Bubblesheet grading by a role (instructor or custom) with specific section
  
  
-------------- next part --------------
Index: loncom/homework/grades.pm
diff -u loncom/homework/grades.pm:1.769 loncom/homework/grades.pm:1.770
--- loncom/homework/grades.pm:1.769	Fri May  8 15:12:34 2020
+++ loncom/homework/grades.pm	Wed May 20 22:02:57 2020
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.769 2020/05/08 15:12:34 raeburn Exp $
+# $Id: grades.pm,v 1.770 2020/05/20 22:02:57 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -5897,8 +5897,7 @@
 
     $ssi_error = 0;
 
-    if (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) ||
-        &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
+    if (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) || $perm{'usc'}) {
 
 	# Chunk of form to prompt for a scantron file upload.
 
@@ -5906,6 +5905,7 @@
     <br />');
     my $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
     my $cnum= $env{'course.'.$env{'request.course.id'}.'.num'};
+    my $csec= $env{'request.course.sec'};
     my $alertmsg = &mt('Please use the browse button to select a file from your local directory.');
     &js_escape(\$alertmsg);
     my ($formatoptions,$formattitle,$formatjs) = &scantron_upload_dataformat($cdom);
@@ -5921,6 +5921,7 @@
               <form enctype="multipart/form-data" action="/adm/grades" name="rules" method="post">
                 '.$default_form_data.'
                 <input name="courseid" type="hidden" value="'.$cnum.'" />
+                <input name="coursesec" type="hidden" value="'.$csec.'" />
                 <input name="domainid" type="hidden" value="'.$cdom.'" />
                 <input name="command" value="scantronupload_save" type="hidden" />
               '.&Apache::loncommon::start_data_table('LC_scantron_action').'
@@ -6978,7 +6979,7 @@
             '<tr><td><b>'.&mt('Hand-graded items: points from last bubble in row').'</b></td><td><tt>'.
             $env{'form.scantron_lastbubblepoints'}.'</tt></td></tr>';
     }
-    return ('
+    return '
 <p>
 <span class="LC_warning">
 '.&mt("Please double check the information below before clicking on '[_1]'",&mt($button_text)).'</span>
@@ -6990,9 +6991,7 @@
 </table>
 <p> '.&mt("If this information is correct, please click on '[_1]'.",&mt($button_text)).'<br />
 '.&mt('If something is incorrect, please return to [_1]Grade/Manage/Review Bubblesheets[_2] to start over.','<a href="/adm/grades?symb='.$symb.'&command=scantron_selectphase" class="LC_info">','</a>').'</p>
-
-<br />
-');
+';
 }
 
 =pod
@@ -7018,15 +7017,58 @@
 	} 
 	if ( $env{'form.scantron_selectfile'} eq '') {
 	    $r->print('<p><span class="LC_error">'.&mt("You have not selected a file that contains the student's response data.").'</span></p>');
-	} 
+	}
 	if ( $env{'form.scantron_format'} eq '') {
 	    $r->print('<p><span class="LC_error">'.&mt("You have not selected the format of the student's response data.").'</span></p>');
-	} 
+	}
     } else {
 	my $warning=&scantron_warning_screen('Grading: Validate Records',$symb);
+        my ($checksec, at possibles) = &gradable_sections();
+        my $gradesections;
+        if ($checksec) {
+            my $file=$env{'form.scantron_selectfile'};
+            if (&valid_file($file)) {
+                my %bysec = &scantron_get_sections();
+                my $table;
+                if ((keys(%bysec) > 1) || ((keys(%bysec) == 1) && ((keys(%bysec))[0] ne $checksec))) {
+                    $gradesections = &mt('Your current role is for section [_1].','<i>'.$checksec.'</i>').'<br />';
+                    $table = &Apache::loncommon::start_data_table()."\n".
+                             &Apache::loncommon::start_data_table_header_row().
+                             '<th>'.&mt('Section').'</th><th>'.&mt('Number of records').'</th>'.
+                              &Apache::loncommon::end_data_table_header_row()."\n";
+                    if ($bysec{'none'}) {
+                        $table .= &Apache::loncommon::start_data_table_row().
+                                  '<td>'.&mt('None').'</td><td>'.$bysec{'none'}.'</td>'.
+                                  &Apache::loncommon::end_data_table_row()."\n";
+                    }
+                    foreach my $sec (sort { $a <=> $b } keys(%bysec)) {
+                        next if ($sec eq 'none');
+                        $table .= &Apache::loncommon::start_data_table_row().
+                                  '<td>'.$sec.'</td><td>'.$bysec{$sec}.'</td>'.
+                                  &Apache::loncommon::end_data_table_row()."\n";
+                    }
+                    $table .= &Apache::loncommon::end_data_table()."\n";
+                    $gradesections .= &mt('Sections represented in the bubblesheet data file (based on bubbled student IDs) are as follows:').
+                                      '<p>'.$table.'</p>';
+                    if (@possibles) {
+                        $gradesections .= '<p>'.
+                                          &mt('You have role(s) in [quant,_1,other section,other sections] with privileges to manage grades.',
+                                              scalar(@possibles)).'<br />'.
+                                          &mt('Check which of those section(s), in addition to section [_1], you wish to grade using this bubblesheet file:',
+                                              '<i>'.$checksec.'</i>').' ';
+                        foreach my $sec (sort {$a <=> $b } @possibles) {
+                            $gradesections .= '<label><input type="checkbox" name="scantron_othersections" value="'.$sec.'" />'.$sec.'</label>'.(' 'x2);
+                        }
+                        $gradesections .= '</p>';
+                    }
+                }
+            } else {
+                $gradesections = '<p class="LC_error">'.&mt('The selected file is unavailable').'</p>';
+            }
+        }
         my $bubbledbyhand=&hand_bubble_option();
 	$r->print('
-'.$warning.$bubbledbyhand.'
+'.$warning.$gradesections.$bubbledbyhand.'
 <input type="submit" name="submit" value="'.&mt('Grading: Validate Records').'" />
 <input type="hidden" name="command" value="scantron_validate" />
 ');
@@ -7113,7 +7155,38 @@
     if ($env{'form.scantron_corrections'}) {
 	&scantron_process_corrections($r);
     }
-    $r->print('<p>'.&mt('Gathering necessary information.').'</p>');$r->rflush();
+
+    $r->print('<p>'.&mt('Gathering necessary information.').'</p>');
+    my ($checksec, at gradable);
+    if ($env{'request.course.sec'}) {
+        ($checksec,my @possibles) = &gradable_sections();
+        if ($checksec) {
+            if (@possibles) {
+                my @chosensecs = &Apache::loncommon::get_env_multiple('form.scantron_othersections');
+                if (@chosensecs) {
+                    foreach my $sec (@chosensecs) {
+                        if (grep(/^\Q$sec\E$/, at possibles)) {
+                            unless (grep(/^\Q$sec\E$/, at gradable)) {
+                                push(@gradable,$sec);
+                            }
+                        }
+                    }
+                }
+            }
+            $r->print('<p><table>');
+            if (@gradable) {
+                my @showsections = sort { $a <=> $b } (@gradable,$checksec);
+                $r->print(
+                    '<tr><td><b>'.&mt('Sections to be Graded:').'</b></td><td>'.join(', ', at showsections).'</td></tr>');
+            } else {
+                $r->print(
+                    '<tr><td><b>'.&mt('Section to be Graded:').'</b></td><td>'.$checksec.'</td></tr>');
+            }
+            $r->print('</table></p>');
+        }
+    }
+    $r->rflush();
+
     #get the student pick code ready
     $r->print(&Apache::loncommon::studentbrowser_javascript());
     my $nav_error;
@@ -7138,7 +7211,7 @@
 	$env{'form.validatepass'} = 0;
     }
     my $currentphase=$env{'form.validatepass'};
-
+    my %skipbysec=();
 
     my $stop=0;
     while (!$stop && $currentphase < scalar(@validate_phases)) {
@@ -7148,13 +7221,29 @@
 	my $which="scantron_validate_".$validate_phases[$currentphase];
 	{
 	    no strict 'refs';
-	    ($stop,$currentphase)=&$which($r,$currentphase);
+            my @extras=();
+            if ($validate_phases[$currentphase] eq 'ID') {
+                @extras = (\%skipbysec,$checksec, at gradable);
+            }
+	    ($stop,$currentphase)=&$which($r,$currentphase, at extras);
 	}
     }
     if (!$stop) {
 	my $warning=&scantron_warning_screen('Start Grading',$symb);
+        my $secinfo;
+        if (keys(%skipbysec) > 0) {
+            my $seclist = '<ul>';
+            foreach my $sec (sort { $a <=> $b } keys(%skipbysec)) {
+                $seclist .= '<li>'.&mt('section [_1]: [_2]',$sec,$skipbysec{$sec}).'</li>';
+            }
+            $seclist .= '</ul>';
+            $secinfo = '<p class="LC_info">'.
+                       &mt('Numbers of records for students in sections not being graded [_1]',
+                           $seclist).
+                       '</p>';
+        }
 	$r->print(&mt('Validation process complete.').'<br />'.
-                  $warning.
+                  $secinfo.$warning.
                   &mt('Perform verification for each student after storage of submissions?').
                   ' <span class="LC_nobreak"><label>'.
                   '<input type="radio" name="verifyrecord" value="1" />'.&mt('Yes').'</label>'.
@@ -7570,11 +7659,12 @@
 
 
 sub scantron_validate_ID {
-    my ($r,$currentphase) = @_;
+    my ($r,$currentphase,$skipbysec,$checksec, at gradable) = @_;
     
     #get student info
     my $classlist=&Apache::loncoursedata::get_classlist();
     my %idmap=&username_to_idmap($classlist);
+    my $secidx = &Apache::loncoursedata::CL_SECTION();
 
     #get scantron line setup
     my %scantron_config=&Apache::lonnet::get_scantron_config($env{'form.scantron_format'});
@@ -7588,6 +7678,7 @@
     }
 
     my %found=('ids'=>{},'usernames'=>{});
+    my $unsavedskips = 0;
     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 	if ($line=~/^[\s\cz]*$/) { next; }
@@ -7600,13 +7691,41 @@
 	}
 	if ($found) {
 	    my $username=$idmap{$found};
+            if ($checksec) {
+                if (ref($classlist->{$username}) eq 'ARRAY') {
+                    my $stusec = $classlist->{$username}->[$secidx];
+                    if ($stusec ne $checksec) {
+                        unless ((@gradable > 0) && (grep(/^\Q$stusec\E$/, at gradable))) {
+                            my $skip=1;
+                            &scantron_put_line($scanlines,$scan_data,$i,$line,$skip);
+                            if (ref($skipbysec) eq 'HASH') {
+                                if ($stusec eq '') {
+                                    $skipbysec->{'none'} ++;
+                                } else {
+                                    $skipbysec->{$stusec} ++;
+                                }
+                            }
+                            $unsavedskips ++;
+                            next;
+                        }
+                    }
+                }
+            }
 	    if ($found{'ids'}{$found}) {
 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 					 $line,'duplicateID',$found);
+                if ($unsavedskips) {
+                    &scantron_putfile($scanlines,$scan_data);
+                    $unsavedskips = 0;
+                }
 		return(1,$currentphase);
 	    } elsif ($found{'usernames'}{$username}) {
 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 					 $line,'duplicateID',$username);
+                if ($unsavedskips) {
+                    &scantron_putfile($scanlines,$scan_data);
+                    $unsavedskips = 0;
+                }
 		return(1,$currentphase);
 	    }
 	    #FIXME store away line we previously saw the ID on to use above
@@ -7615,29 +7734,95 @@
 	} else {
 	    if ($id =~ /^\s*$/) {
 		my $username=&scan_data($scan_data,"$i.user");
-		if (defined($username) && $found{'usernames'}{$username}) {
+                if (($checksec && $username ne '')) {
+                    if (ref($classlist->{$username}) eq 'ARRAY') {
+                        my $stusec = $classlist->{$username}->[$secidx];
+                        if ($stusec ne $checksec) {
+                            unless ((@gradable > 0) && (grep(/^\Q$stusec\E$/, at gradable))) {
+                                my $skip=1;
+                                &scantron_put_line($scanlines,$scan_data,$i,$line,$skip);
+                                if (ref($skipbysec) eq 'HASH') {
+                                    if ($stusec eq '') {
+                                        $skipbysec->{'none'} ++;
+                                    } else {
+                                        $skipbysec->{$stusec} ++;
+                                    }
+                                }
+                                $unsavedskips ++;
+                                next;
+                            }
+                        }
+                    }
+		} elsif (defined($username) && $found{'usernames'}{$username}) {
 		    &scantron_get_correction($r,$i,$scan_record,
 					     \%scantron_config,
 					     $line,'duplicateID',$username);
+                    if ($unsavedskips) {
+                        &scantron_putfile($scanlines,$scan_data);
+                        $unsavedskips = 0;
+                    }
 		    return(1,$currentphase);
 		} elsif (!defined($username)) {
 		    &scantron_get_correction($r,$i,$scan_record,
 					     \%scantron_config,
 					     $line,'incorrectID');
+                    if ($unsavedskips) {
+                        &scantron_putfile($scanlines,$scan_data);
+                        $unsavedskips = 0;
+                    }
 		    return(1,$currentphase);
 		}
 		$found{'usernames'}{$username}++;
 	    } else {
 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 					 $line,'incorrectID');
+                if ($unsavedskips) {
+                    &scantron_putfile($scanlines,$scan_data);
+                    $unsavedskips = 0;
+                }
 		return(1,$currentphase);
 	    }
 	}
     }
-
+    if ($unsavedskips) {
+        &scantron_putfile($scanlines,$scan_data);
+        $unsavedskips = 0;
+    }
     return (0,$currentphase+1);
 }
 
+sub scantron_get_sections {
+    my %bysec;
+    if ($env{'form.scantron_format'} ne '') {
+        my %scantron_config=&Apache::lonnet::get_scantron_config($env{'form.scantron_format'});
+        my ($scanlines,$scan_data)=&scantron_getfile();
+        my $classlist=&Apache::loncoursedata::get_classlist();
+        my %idmap=&username_to_idmap($classlist);
+        foreach my $key (keys(%idmap)) {
+            my $lckey = lc($key);
+            $idmap{$lckey} = $idmap{$key};
+        }
+        my $secidx = &Apache::loncoursedata::CL_SECTION();
+        for (my $i=0;$i<=$scanlines->{'count'};$i++) {
+            my $line=&scantron_get_line($scanlines,$scan_data,$i);
+            if ($line=~/^[\s\cz]*$/) { next; }
+            my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
+                                                     $scan_data);
+            my $id=lc($$scan_record{'scantron.ID'});
+            if (exists($idmap{$id})) {
+                if (ref($classlist->{$idmap{$id}}) eq 'ARRAY') {
+                    my $stusec = $classlist->{$idmap{$id}}->[$secidx];
+                    if ($stusec eq '') {
+                        $bysec{'none'} ++;
+                    } else {
+                        $bysec{$stusec} ++;
+                    }
+                }
+            }
+        }
+    }
+    return %bysec;
+}
 
 sub scantron_get_correction {
     my ($r,$i,$scan_record,$scan_config,$line,$error,$arg,
@@ -8655,9 +8840,10 @@
 SCANTRONFORM
     $r->print($result);
 
+    my ($checksec, at possibles)=&gradable_sections();
     my @delayqueue;
     my (%completedstudents,%scandata);
-    
+
     my $lock=&Apache::lonnet::set_lock(&mt('Grading bubblesheet exam'));
     my $count=&get_todo_count($scanlines,$scan_data);
     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,$count);
@@ -8717,6 +8903,13 @@
  	    next;
  	}
         my $usec = $classlist->{$uname}->[&Apache::loncoursedata::CL_SECTION];
+        if (($checksec ne '') && ($checksec ne $usec)) {
+            unless (grep(/^\Q$usec\E$/, at possibles)) {
+                &scantron_add_delay(\@delayqueue,$line,
+                                    "No role with manage grades privilege in student's section ($usec)",3);
+                next;
+            }
+        }
         my $user = $uname.':'.$usec;
   	($uname,$udom)=split(/:/,$uname);
 
@@ -9172,7 +9365,9 @@
 	'</form>'."\n";
     if (!&Apache::lonnet::allowed('usc',$env{'form.domainid'}) &&
 	!&Apache::lonnet::allowed('usc',
-			    $env{'form.domainid'}.'_'.$env{'form.courseid'})) {
+			    $env{'form.domainid'}.'_'.$env{'form.courseid'}) &&
+        !&Apache::lonnet::allowed('usc',
+                            $env{'form.domainid'}.'_'.$env{'form.courseid'}.'/'.$env{'form.coursesec'})) {
 	$r->print(&mt("You are not allowed to upload bubblesheet data to the requested course.")."<br />");
 	unless ($symb) {
 	    $r->print($doanotherupload);
@@ -9228,8 +9423,17 @@
                         (length($env{'form.upfile'})-1),
                         '<span class="LC_filename">'.$result.'</span>'));
             ($uploadedfile) = ($result =~ m{/([^/]+)$});
+            if ($uploadedfile =~ /^scantron_orig_/) {
+                my $logname = $uploadedfile;
+                $logname =~ s/^scantron_orig_//;
+                if ($logname ne '') {
+                    my $now = time;
+                    my %info = ($logname => { $now => $env{'user.name'}.':'.$env{'user.domain'} });  
+                    &Apache::lonnet::put('scantronupload',\%info,$env{'form.domainid'},$env{'form.courseid'});
+                }
+            }
             $r->print(&validate_uploaded_scantron_file($env{'form.domainid'},
-                                                       $env{'form.courseid'},$uploadedfile));
+                                                       $env{'form.courseid'},$symb,$uploadedfile));
         } else {
             $r->print(
                 &Apache::lonhtmlcommon::confirm_success(&mt('Upload failed'),1).'<br />'.
@@ -9247,13 +9451,34 @@
 }
 
 sub validate_uploaded_scantron_file {
-    my ($cdom,$cname,$fname) = @_;
+    my ($cdom,$cname,$symb,$fname,$context,$countsref) = @_;
+
     my $scanlines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.$fname);
     my @lines;
     if ($scanlines ne '-1') {
         @lines=split("\n",$scanlines,-1);
     }
-    my $output;
+    my ($output,$secidx,$checksec,$priv,%crsroleshash, at possibles);
+    $secidx = &Apache::loncoursedata::CL_SECTION();
+    if ($context eq 'download') {
+        $priv = 'mgr';
+    } else {
+        $priv = 'usc';
+    }
+    unless ((&Apache::lonnet::allowed($priv,$env{'request.role.domain'})) ||
+            (($env{'request.course.id'}) &&
+             (&Apache::lonnet::allowed($priv,$env{'request.course.id'})))) {
+        if ($env{'request.course.sec'} ne '') {
+            unless (&Apache::lonnet::allowed($priv,
+                                         "$env{'request.course.id'}/$env{'request.course.sec'}")) {
+                unless ($context eq 'download') {
+                    $output = '<p class="LC_warning">'.&mt('You do not have permission to upload bubblesheet data').'</p>';
+                }
+                return $output;
+            }
+            ($checksec, at possibles)=&gradable_sections();
+        }
+    }
     if (@lines) {
         my (%counts,$max_match_format);
         my ($found_match_count,$max_match_count,$max_match_pct) = (0,0,0);
@@ -9283,6 +9508,8 @@
             %{$counts{$key}} = (
                                'found'   => 0,
                                'total'   => 0,
+                               'totalanysec' => 0,
+                               'othersec' => 0,
                               );
             foreach my $line (@lines) {
                 next if ($line =~ /^#/);
@@ -9290,6 +9517,23 @@
                 my $id = substr($line,$idstart-1,$idlength);
                 $id = lc($id);
                 if (exists($idmap{$id})) {
+                    if ($checksec ne '') {
+                        $counts{$key}{'totalanysec'} ++;
+                        if (ref($classlist->{$idmap{$id}}) eq 'ARRAY') {
+                            my $stusec = $classlist->{$idmap{$id}}->[$secidx];
+                            if ($stusec ne $checksec) {
+                                if (@possibles) {
+                                    unless (grep(/^\Q$stusec\E$/, at possibles)) {
+                                        $counts{$key}{'othersec'} ++;
+                                        next;
+                                    }
+                                } else {
+                                    $counts{$key}{'othersec'} ++;
+                                    next;
+                                }
+                            }
+                        }
+                    }
                     $counts{$key}{'found'} ++;
                 }
                 $counts{$key}{'total'} ++;
@@ -9304,7 +9548,7 @@
                 }
             }
         }
-        if (ref($unique_formats{$max_match_format}) eq 'ARRAY') {
+        if ((ref($unique_formats{$max_match_format}) eq 'ARRAY') && ($context ne 'download')) {
             my $format_descs;
             my $numwithformat = @{$unique_formats{$max_match_format}};
             for (my $i=0; $i<$numwithformat; $i++) {
@@ -9349,13 +9593,179 @@
                     '<li>'.&mt('The course roster is not up to date.').'</li>'.
                     '</ul>';
             }
+            if (($checksec ne '') && (ref($counts{$max_match_format}) eq 'HASH')) {
+                if ($counts{$max_match_format}{'othersec'}) {
+                    my $percent_nongrade = (100*$counts{$max_match_format}{'othersec'})/($counts{$max_match_format}{'totalanysec'});
+                    my $showpct = sprintf("%.0f",$percent_nongrade).'%';
+                    my $confirmdel = &mt('Are you sure you want to permanently delete this file?');
+                    &js_escape(\$confirmdel);
+                    $output .= '<p class="LC_warning">'.
+                               &mt('Comparison of student IDs in the uploaded file with the course roster found [_1][quant,_2,match,matches][_3] for students in section(s) for which none of your role(s) have privileges to modify grades',
+                                   '<b>',$counts{$max_match_format}{'othersec'},'</b>').
+                               '<br />'.
+                               &mt('Unless you are assigned role(s) which allow modification of grades in additional sections, [_1] of the records in this file will be automatically excluded when you perform bubblesheet grading.','<b>'.$showpct.'</b>').
+                               '</p><p>'.
+                               &mt('If you prefer to delete the file now, use: [_1]').
+                               '<form method="post" name="delupload" action="/adm/grades">'.
+                               '<input type="hidden" name="symb" value="'.$symb.'" />'.
+                               '<input type="hidden" name="domainid" value="'.$cdom.'" />'.
+                               '<input type="hidden" name="courseid" value="'.$cname.'" />'.
+                               '<input type="hidden" name="coursesec" value="'.$env{'request.course.sec'}.'" />'. 
+                               '<input type="hidden" name="uploadedfile" value="'.$fname.'" />'. 
+                               '<input type="hidden" name="command" value="scantronupload_delete" />'.
+                               '<input type="button" name="delbutton" value="'.&mt('Delete Uploaded File').'" onclick="javascript:if (confirm('."'$confirmdel'".')) { document.delupload.submit(); }" />'.
+                               '</form></p>';
+                }
+            }
         }
-    } else {
+        if (($context eq 'download') && ($checksec ne '')) {
+            if ((ref($countsref) eq 'HASH') && (ref($counts{$max_match_format}) eq 'HASH')) {
+                $countsref->{'totalanysec'} = $counts{$max_match_format}{'totalanysec'};
+                $countsref->{'othersec'} = $counts{$max_match_format}{'othersec'};
+            }
+        } 
+    } elsif ($context ne 'download') {
         $output = '<p class="LC_warning">'.&mt('Uploaded file contained no data').'</p>';
     }
     return $output;
 }
 
+sub gradable_sections {
+    my $checksec = $env{'request.course.sec'};
+    my @oksecs;
+    if ($checksec) {
+        my %availablesecs = &sections_grade_privs();
+        if (ref($availablesecs{'mgr'}) eq 'ARRAY') {
+            foreach my $sec (@{$availablesecs{'mgr'}}) {
+                unless (grep(/^\Q$sec\E$/, at oksecs)) {
+                    push(@oksecs,$sec);
+                }
+            }
+            if (grep(/^all$/, at oksecs)) {
+                undef($checksec);
+            }
+        }
+    }
+    return($checksec, at oksecs);
+}
+
+sub sections_grade_privs {
+    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    my %availablesecs = (
+                          mgr => [],
+                          vgr => [],
+                          usc => [],
+                        );
+    my $ccrole = 'cc';
+    if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Community') {
+        $ccrole = 'co';
+    }
+    my %crsroleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},
+                                                     'userroles',['active'],
+                                                     [$ccrole,'in','cr'],$cdom,1);
+    my $crsid = $cnum.':'.$cdom;
+    foreach my $item (keys(%crsroleshash)) {
+        next unless ($item =~ /^$crsid\:/);
+        my ($crsnum,$crsdom,$role,$sec) = split(/\:/,$item);
+        my $suffix = "/$cdom/$cnum./$cdom/$cnum";
+        if ($sec ne '') {
+            $suffix = "/$cdom/$cnum/$sec./$cdom/$cnum/$sec";
+        }
+        if (($role eq $ccrole) || ($role eq 'in')) {
+            foreach my $priv ('mgr','vgr','usc') { 
+                unless (grep(/^all$/,@{$availablesecs{$priv}})) {
+                    if ($sec eq '') {
+                        $availablesecs{$priv} = ['all'];
+                    } elsif ($sec ne $env{'request.course.sec'}) {
+                        unless (grep(/^\Q$sec\E$/,@{$availablesecs{$priv}})) {
+                            push(@{$availablesecs{$priv}},$sec);
+                        }
+                    }
+                }
+            }
+        } elsif ($role =~ m{^cr/}) {
+            foreach my $priv ('mgr','vgr','usc') {
+                unless (grep(/^all$/,@{$availablesecs{$priv}})) {
+                    if ($env{"user.priv.$role.$suffix"} =~ /:$priv&/) {
+                        if ($sec eq '') {
+                            $availablesecs{$priv} = ['all'];
+                        } elsif ($sec ne $env{'request.course.sec'}) {
+                            unless (grep(/^\Q$sec\E$/,@{$availablesecs{$priv}})) {
+                                push(@{$availablesecs{$priv}},$sec);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return %availablesecs;
+}
+
+sub scantron_upload_delete {
+    my ($r,$symb) = @_;
+    my $filename = $env{'form.uploadedfile'};
+    if ($filename =~ /^scantron_orig_/) {
+        if (&Apache::lonnet::allowed('usc',$env{'form.domainid'}) ||
+            &Apache::lonnet::allowed('usc',
+                                     $env{'form.domainid'}.'_'.$env{'form.courseid'}) ||
+            &Apache::lonnet::allowed('usc',
+                                     $env{'form.domainid'}.'_'.$env{'form.courseid'}.'/'.$env{'form.coursesec'})) {
+            my $uploadurl = '/uploaded/'.$env{'form.domainid'}.'/'.$env{'form.courseid'}.'/'.$env{'form.uploadedfile'};
+            my $retrieval = &Apache::lonnet::getfile($uploadurl);
+            if ($retrieval eq '-1') {
+                $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('File deletion failed'),1).'<br />'.
+                          &mt('File requested for deletion not found.'));
+            } else {
+                $filename =~ s/^scantron_orig_//;
+                if ($filename ne '') {
+                    my ($is_valid,$numleft);
+                    my %info = &Apache::lonnet::get('scantronupload',[$filename],$env{'form.domainid'},$env{'form.courseid'});
+                    if (keys(%info)) {
+                        if (ref($info{$filename}) eq 'HASH') {
+                            foreach my $timestamp (sort(keys(%{$info{$filename}}))) {
+                                if ($info{$filename}{$timestamp} eq $env{'user.name'}.':'.$env{'user.domain'}) {
+                                    $is_valid = 1;
+                                    delete($info{$filename}{$timestamp}); 
+                                }
+                            }
+                            $numleft = scalar(keys(%{$info{$filename}}));
+                        }
+                    }
+                    if ($is_valid) {
+                        my $result = &Apache::lonnet::removeuploadedurl($uploadurl);
+                        if ($result eq 'ok') {
+                            $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('File deletion successful')).'<br />');
+                            if ($numleft) {
+                                &Apache::lonnet::put('scantronupload',\%info,$env{'form.domainid'},$env{'form.courseid'});
+                            } else {
+                                &Apache::lonnet::del('scantronupload',[$filename],$env{'form.domainid'},$env{'form.courseid'});
+                            }
+                        } else {
+                            $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('File deletion failed'),1).'<br />'.
+                                      &mt('Result was [_1]',$result));
+                        }
+                    } else {
+                        $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('File deletion failed'),1).'<br />'.
+                                  &mt('File requested for deletion was uploaded by a different user.'));
+                    }
+                } else {
+                    $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('File deletion failed'),1).'<br />'.
+                              &mt('Filename of bubblesheet data file requested for deletion is invalid.'));
+                }
+            }
+        } else {
+            $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('File deletion failed'),1).'<br />'. 
+                      &mt('You are not permitted to delete bubblesheet data files from the requested course.'));
+        }
+    } else {
+        $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('File deletion failed'),1).'<br />'.
+                          &mt('Filename of bubblesheet data file requested for deletion is invalid.'));
+    }
+    return;
+}
+
 sub valid_file {
     my ($requested_file)=@_;
     foreach my $filename (sort(&scantron_filenames())) {
@@ -9378,6 +9788,29 @@
 ');
 	return;
     }
+    my (%uploader,$is_owner,%counts,$percent);
+    my %uploader = &Apache::lonnet::get('scantronupload',[$file],$cdom,$cname);
+    if (ref($uploader{$file}) eq 'HASH') {
+        foreach my $timestamp (sort { $a <=> $b } keys(%{$uploader{$file}})) {
+            if ($uploader{$file}{$timestamp} eq $env{'user.name'}.':'.$env{'user.domain'}) {
+                $is_owner = 1;
+                last;
+            }
+        }
+    }
+    unless ($is_owner) {
+        &validate_uploaded_scantron_file($cdom,$cname,$symb,'scantron_orig_'.$file,'download',\%counts);
+        if ($counts{'totalanysec'}) {
+            my $percent_othersec = (100*$counts{'othersec'})/($counts{'totalanysec'});
+            if ($percent_othersec >= 10) {
+                my $showpct = sprintf("%.0f",$percent_othersec).'%';
+                $r->print('<p class="LC_warning">'.
+                          &mt('The original uploaded file includes [_1] or more of records for students for which none of your roles have rights to modify grades, so files are unavailable for download.',$showpct).
+                          '</p>');
+                return;
+            }
+        }
+    }
     my $orig='/uploaded/'.$cdom.'/'.$cname.'/scantron_orig_'.$file;
     my $corrected='/uploaded/'.$cdom.'/'.$cname.'/scantron_corrected_'.$file;
     my $skipped='/uploaded/'.$cdom.'/'.$cname.'/scantron_skipped_'.$file;
@@ -9414,7 +9847,7 @@
     my %scantron_config =
         &Apache::lonnet::get_scantron_config($env{'form.scantron_format'});
     my $bubbles_per_row = &bubblesheet_bubbles_per_row(\%scantron_config);
-    my ($scanlines,$scan_data)=&Apache::grades::scantron_getfile();
+    my ($scanlines,$scan_data)=&scantron_getfile();
     my $classlist=&Apache::loncoursedata::get_classlist();
     my %idmap=&Apache::grades::username_to_idmap($classlist);
     my $navmap=Apache::lonnavmaps::navmap->new();
@@ -10017,7 +10450,7 @@
 
 sub init_perm {
     &reset_perm();
-    foreach my $test_perm ('vgr','mgr','opa') {
+    foreach my $test_perm ('vgr','mgr','opa','usc') {
 
 	my $scope = $env{'request.course.id'};
 	if (!($perm{$test_perm}=&Apache::lonnet::allowed($test_perm,$scope))) {
@@ -10898,20 +11331,21 @@
             &startpage($request,$symb,[{href=>'', text=>'Grade/Manage/Review Bubblesheets'}],1,1);
 	    $request->print(&scantron_process_students($request,$symb));
  	} elsif ($command eq 'scantronupload' && 
- 		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
-		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
+ 		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) || $perm{'usc'})) {
             &startpage($request,$symb,[{href=>'', text=>'Grade/Manage/Review Bubblesheets'}],1,1,
                        undef,undef,undef,undef,'toggleScantab(document.rules);');
  	    $request->print(&scantron_upload_scantron_data($request,$symb)); 
  	} elsif ($command eq 'scantronupload_save' &&
- 		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
-		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
+ 		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) || $perm{'usc'})) {
             &startpage($request,$symb,[{href=>'', text=>'Grade/Manage/Review Bubblesheets'}],1,1);
  	    $request->print(&scantron_upload_scantron_data_save($request,$symb));
- 	} elsif ($command eq 'scantron_download' &&
-		 &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
+ 	} elsif ($command eq 'scantron_download' && ($perm{'usc'} || $perm{'mgr'})) {
             &startpage($request,$symb,[{href=>'', text=>'Grade/Manage/Review Bubblesheets'}],1,1);
  	    $request->print(&scantron_download_scantron_data($request,$symb));
+        } elsif ($command eq 'scantronupload_delete' &&
+                 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) || $perm{'usc'})) {
+            &startpage($request,$symb,[{href=>'', text=>'Grade/Manage/Review Bubblesheets'}],1,1);
+            &scantron_upload_delete($request,$symb);
         } elsif ($command eq 'checksubmissions' && $perm{'vgr'}) {
             &startpage($request,$symb,[{href=>'', text=>'Grade/Manage/Review Bubblesheets'}],1,1);
             $request->print(&checkscantron_results($request,$symb));
@@ -11171,7 +11605,12 @@
 =item  scantron_upload_scantron_data_save() : 
 
    Adds a provided bubble information data file to the course if user
-   has the correct privileges to do so. 
+   has the correct privileges to do so.
+
+= item scantron_upload_delete() :
+
+   Deletes a previously uploaded bubble information data file, if user
+   was the one who uploaded the file, and has the privileges to do so.
 
 =item  valid_file() :
 


More information about the LON-CAPA-cvs mailing list