--- loncom/homework/grades.pm 2019/02/06 05:46:51 1.758
+++ 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.758 2019/02/06 05:46:51 raeburn Exp $
+# $Id: grades.pm,v 1.785 2021/06/04 20:09:28 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -48,6 +48,8 @@ use Apache::lonquickgrades;
use Apache::bridgetask();
use Apache::lontexconvert();
use String::Similarity;
+use HTML::Parser();
+use File::MMagic;
use LONCAPA;
use POSIX qw(floor);
@@ -145,7 +147,7 @@ sub nameUserString {
}
#--- Get the partlist and the response type for a given problem. ---
-#--- Indicate if a response type is coded handgraded or not. ---
+#--- Count responseIDs, essayresponse items, and dropbox items ---
#--- Sets response_error pointer to "1" if navmaps object broken ---
sub response_type {
my ($symb,$response_error) = @_;
@@ -163,6 +165,7 @@ sub response_type {
return;
}
my $partlist = $res->parts();
+ my ($numresp,$numessay,$numdropbox) = (0,0,0);
my %vPart =
map { $_ => 1 } (&Apache::loncommon::get_env_multiple('form.vPart'));
my (%response_types,%handgrade);
@@ -172,13 +175,20 @@ sub response_type {
my @types = $res->responseType($part);
my @ids = $res->responseIds($part);
for (my $i=0; $i < scalar(@ids); $i++) {
+ $numresp ++;
$response_types{$part}{$ids[$i]} = $types[$i];
+ if ($types[$i] eq 'essay') {
+ $numessay ++;
+ if (&Apache::lonnet::EXT("resource.$part".'_'.$ids[$i].".uploadedfiletypes",$symb)) {
+ $numdropbox ++;
+ }
+ }
$handgrade{$part.'_'.$ids[$i]} =
&Apache::lonnet::EXT('resource.'.$part.'_'.$ids[$i].
'.handgrade',$symb);
}
}
- return ($partlist,\%handgrade,\%response_types);
+ return ($partlist,\%handgrade,\%response_types,$numresp,$numessay,$numdropbox);
}
sub flatten_responseType {
@@ -205,6 +215,129 @@ sub get_display_part {
return $display;
}
+#--- Show parts and response type
+sub showResourceInfo {
+ my ($symb,$partlist,$responseType,$formname,$checkboxes,$uploads) = @_;
+ unless ((ref($partlist) eq 'ARRAY') && (ref($responseType) eq 'HASH')) {
+ return '
';
+ }
+ my $coltitle = &mt('Problem Part Shown');
+ if ($checkboxes) {
+ $coltitle = &mt('Problem Part');
+ } else {
+ my $checkedparts = 0;
+ foreach my $partid (&Apache::loncommon::get_env_multiple('form.vPart')) {
+ if (grep(/^\Q$partid\E$/,@{$partlist})) {
+ $checkedparts ++;
+ }
+ }
+ if ($checkedparts == scalar(@{$partlist})) {
+ return '
';
+ }
+ if ($uploads) {
+ $coltitle = &mt('Problem Part Selected');
+ }
+ }
+ my $result = '
'. + &mt('No dropbox items or essayresponse items with uploadedfiletypes set.'). + '
'; + } else { + return ''.&keywords_highlight($answer).''; - } elsif ( $response eq 'organic') { my $result=&mt('Smile representation: [_1]', '"'.&HTML::Entities::encode($answer, '"<>&').'"'); @@ -537,7 +669,7 @@ sub getclasslist { $cdom = $env{"course.$env{'request.course.id'}.domain"}; $cnum = $env{"course.$env{'request.course.id'}.num"}; my $res_error; - ($partlist,my $handgrade,my $responseType) = &response_type($symb,\$res_error); + ($partlist) = &response_type($symb,\$res_error); } foreach my $student (keys(%$classlist)) { my $end = @@ -653,7 +785,7 @@ sub canmodify { #can modify the requested section return 1; } else { - # can't modify the request section + # can't modify the requested section return 0; } } @@ -666,19 +798,19 @@ sub canview { my ($sec)=@_; if ($perm{'vgr'}) { if (!defined($perm{'vgr_section'})) { - # can modify whole class + # can view whole class return 1; } else { if ($sec eq $perm{'vgr_section'}) { - #can modify the requested section + #can view the requested section return 1; } else { - # can't modify the request section + # can't view the requested section return 0; } } } - #can't modify + #can't view return 0; } @@ -819,14 +951,14 @@ sub initialverifyreceipt { #--- Check whether a receipt number is valid.--- sub verifyreceipt { - my ($request,$symb) = @_; + my ($request,$symb) = @_; my $courseid = $env{'request.course.id'}; my $receipt = &Apache::lonnet::recprefix($courseid).'-'. $env{'form.receipt'}; $receipt =~ s/[^\-\d]//g; - my $title.= + my $title = '
'.&mt("Please double check the information below before clicking on '[_1]'",&mt($button_text)).' @@ -6990,9 +7209,7 @@ sub scantron_warning_screen {
'.&mt("If this information is correct, please click on '[_1]'.",&mt($button_text)).'
'.&mt('If something is incorrect, please return to [_1]Grade/Manage/Review Bubblesheets[_2] to start over.','','').'
'.&mt("You have not selected a file that contains the student's response data.").'
'); - } + } if ( $env{'form.scantron_format'} eq '') { $r->print(''.&mt("You have not selected the format of the student's response data.").'
'); - } + } } else { my $warning=&scantron_warning_screen('Grading: Validate Records',$symb); + my ($checksec,@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].',''.$checksec.'').''.$table.'
'; + if (@possibles) { + $gradesections .= ''.
+ &mt('You have role(s) in [quant,_1,other section,other sections] with privileges to manage grades.',
+ scalar(@possibles)).'
'.
+ &mt('Check which of those section(s), in addition to section [_1], you wish to grade using this bubblesheet file:',
+ ''.$checksec.'').' ';
+ foreach my $sec (sort {$a <=> $b } @possibles) {
+ $gradesections .= ''.(' 'x2);
+ }
+ $gradesections .= '
'.&mt('The selected file is unavailable').'
'; + } + } my $bubbledbyhand=&hand_bubble_option(); $r->print(' -'.$warning.$bubbledbyhand.' +'.$warning.$gradesections.$bubbledbyhand.' '); @@ -7113,7 +7373,38 @@ sub scantron_validate_file { if ($env{'form.scantron_corrections'}) { &scantron_process_corrections($r); } - $r->print(''.&mt('Gathering necessary information.').'
');$r->rflush(); + + $r->print(''.&mt('Gathering necessary information.').'
'); + my ($checksec,@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$/,@possibles)) { + unless (grep(/^\Q$sec\E$/,@gradable)) { + push(@gradable,$sec); + } + } + } + } + } + $r->print(''.&mt('Sections to be Graded:').' | '.join(', ',@showsections).' |
'.&mt('Section to be Graded:').' | '.$checksec.' |
'. + &mt('Numbers of records for students in sections not being graded [_1]', + $seclist). + '
'; + } $r->print(&mt('Validation process complete.').''.&mt('You do not have permission to upload bubblesheet data').'
'; + } + return $output; + } + ($checksec,@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 +9726,8 @@ sub validate_uploaded_scantron_file { %{$counts{$key}} = ( 'found' => 0, 'total' => 0, + 'totalanysec' => 0, + 'othersec' => 0, ); foreach my $line (@lines) { next if ($line =~ /^#/); @@ -9290,6 +9735,23 @@ sub validate_uploaded_scantron_file { 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$/,@possibles)) { + $counts{$key}{'othersec'} ++; + next; + } + } else { + $counts{$key}{'othersec'} ++; + next; + } + } + } + } $counts{$key}{'found'} ++; } $counts{$key}{'total'} ++; @@ -9304,7 +9766,7 @@ sub validate_uploaded_scantron_file { } } } - 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 +9811,179 @@ sub validate_uploaded_scantron_file { ''.
+ &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',
+ '',$counts{$max_match_format}{'othersec'},'').
+ '
'.
+ &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.',''.$showpct.'').
+ '
'. + &mt('If you prefer to delete the file now, use: [_1]'). + '
'.&mt('Uploaded file contained no data').'
'; } return $output; } +sub gradable_sections { + my $checksec = $env{'request.course.sec'}; + my @oksecs; + if ($checksec) { + my %availablesecs = §ions_grade_privs(); + if (ref($availablesecs{'mgr'}) eq 'ARRAY') { + foreach my $sec (@{$availablesecs{'mgr'}}) { + unless (grep(/^\Q$sec\E$/,@oksecs)) { + push(@oksecs,$sec); + } + } + if (grep(/^all$/,@oksecs)) { + undef($checksec); + } + } + } + return($checksec,@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).''. + &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). + '
'); + 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 +10065,7 @@ sub checkscantron_results { 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(); @@ -9736,7 +10387,6 @@ sub verify_scantron_grading { return ($counter,$record); } - #-------- end of section for handling grading scantron forms ------- # #------------------------------------------------------------------- @@ -9782,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.', + { 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.' }]}, @@ -9821,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.' } @@ -9857,7 +10517,6 @@ sub grading_menu { return $Str; } - sub ungraded { my ($request)=@_; &submit_options($request); @@ -9905,11 +10564,30 @@ sub submit_options_download { my ($request,$symb) = @_; if (!$symb) {return '';} + my $res_error; + my ($partlist,$handgrade,$responseType,$numresp,$numessay,$numdropbox) = + &response_type($symb,\$res_error); + if ($res_error) { + $request->print(&mt('An error occurred retrieving response types')); + return; + } + unless ($numessay) { + $request->print(&mt('No essayresponse items found')); + return; + } + my $table; + if (ref($partlist) eq 'ARRAY') { + if (scalar(@$partlist) > 1 ) { + $table = &showResourceInfo($symb,$partlist,$responseType,'gradingMenu',1,1); + } + } + my $is_tool = ($symb =~ /ext\.tool$/); &commonJSfunctions($request); my $result='