--- loncom/homework/grades.pm	2003/08/07 17:32:21	1.132
+++ loncom/homework/grades.pm	2003/10/28 23:20:03	1.145
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.132 2003/08/07 17:32:21 bowersj2 Exp $
+# $Id: grades.pm,v 1.145 2003/10/28 23:20:03 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -185,7 +185,7 @@ sub cleanRecord {
 	    $ENV{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
 	    $ENV{'form.'.$symb} = 1; # so that we don't have to read it from disk for multiple sub of the same prob.
 	}
-	return '<br /><br /><blockquote>'.&keywords_highlight($answer).'</blockquote>';
+	return '<br /><br /><blockquote><pre>'.&keywords_highlight($answer).'</pre></blockquote>';
     }
     return $answer;
 }
@@ -218,7 +218,8 @@ sub commonJSfunctions {
 		}
 	    }
 	} else {
-	    if (selectOne.selected) return selectOne.value;
+            // only one value it must be the selected one
+	    return selectOne.value;
 	}
     }
 </script>
@@ -506,9 +507,12 @@ LISTJAVASCRIPT
     my $checkhdgrade = ($ENV{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1 ) ? 'checked' : '';
     my $checklastsub = $checkhdgrade eq '' ? 'checked' : '';
     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'."\n".
-	'&nbsp;<b>View Problem Text: </b><input type="radio" name="vProb" value="no" checked /> no '."\n".
+	'&nbsp;<b>View Problem Text: </b><input type="radio" name="vProb" value="no" checked="on" /> no '."\n".
 	'<input type="radio" name="vProb" value="yes" /> one student '."\n".
 	'<input type="radio" name="vProb" value="all" /> all students <br />'."\n".
+	'&nbsp;<b>View Answer: </b><input type="radio" name="vAns" value="no"  /> no '."\n".
+	'<input type="radio" name="vAns" value="yes" /> one student '."\n".
+	'<input type="radio" name="vAns" value="all" checked="on" /> all students <br />'."\n".
 	'&nbsp;<b>Submissions: </b>'."\n";
     if ($ENV{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1) {
 	$gradeTable.='<input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> essay part only'."\n";
@@ -517,8 +521,8 @@ LISTJAVASCRIPT
     my $saveStatus = $ENV{'form.Status'} eq '' ? 'Active' : $ENV{'form.Status'};
     $ENV{'form.Status'} = $saveStatus;
 
-    $gradeTable.='<input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> last sub only'."\n".
-	'<input type="radio" name="lastSub" value="last" /> last sub & parts info'."\n".
+    $gradeTable.='<input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> last submission only'."\n".
+	'<input type="radio" name="lastSub" value="last" /> last submission & parts info'."\n".
 	'<input type="radio" name="lastSub" value="datesub" /> by dates and submissions'."\n".
 	'<input type="radio" name="lastSub" value="all" /> all details'."\n".
 	'<input type="hidden" name="section"     value="'.$getsec.'" />'."\n".
@@ -544,7 +548,7 @@ LISTJAVASCRIPT
     $gradeTable.='<input type="button" '."\n".
 	'onClick="javascript:checkSelect(this.form.stuinfo);" '."\n".
 	'value="Next->" />'."\n";
-
+    $gradeTable.='<input type="checkbox" name="checkPlag" checked="on">Check For Plagiarism</input>';
     my (undef, undef, $fullname) = &getclasslist($getsec,'1');  
     $gradeTable.='<table border="0"><tr><td bgcolor="#777777">'.
 	'<table border="0"><tr bgcolor="#e6ffff">';
@@ -568,18 +572,21 @@ LISTJAVASCRIPT
 	my %status = ();
 	if ($ENV{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
 	    (%status) =&student_gradeStatus($url,$symb,$udom,$uname,$partlist);
-	    my $statusflg = '';
+	    my $submitted = 0;
+	    my $graded = 1;
 	    foreach (keys(%status)) {
-		$statusflg = 1 if ($status{$_} ne 'nothing');
+		$submitted = 1 if ($status{$_} ne 'nothing');
+		$graded = 0 if ($status{$_} =~ /^correct/);
 		my ($foo,$partid,$foo1) = split(/\./,$_);
 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
-		    $statusflg = '';
+		    $submitted = 0;
 		    $gradeTable.='<input type="hidden" name="'.
 			$student.':submitted_by" value="'.
 			$status{'resource.'.$partid.'.submitted_by'}.'" />';
 		}
 	    }
-	    next if ($statusflg eq '' && $submitonly eq 'yes');
+	    next if (!$submitted && ($submitonly eq 'yes' || $submitonly eq 'graded'));
+	    next if (!$graded && $submitonly eq 'graded');
 	}
 
 	$ctr++;
@@ -621,7 +628,7 @@ LISTJAVASCRIPT
 	} else {
 	    $gradeTable='<br />&nbsp;<font color="red">'.
 		'No submissions found for this resource for any students. ('.$num_students.
-		' checked for submissions</font><br />';
+		' checked for submissions)</font><br />';
 	}
     } elsif ($ctr == 1) {
 	$gradeTable =~ s/type=checkbox/type=checkbox checked/;
@@ -1195,27 +1202,46 @@ sub gradeBox {
 }
 
 sub show_problem {
-    my ($request,$symb,$uname,$udom,$removeform,$viewon) = @_;
-    my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
-						      $ENV{'request.course.id'});
+    my ($request,$symb,$uname,$udom,$removeform,$viewon,$mode) = @_;
+    my $rendered;
+    if ($mode eq 'both' or $mode eq 'text') {
+	$rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
+					     $ENV{'request.course.id'});
+    }
     if ($removeform) {
 	$rendered=~s|<form(.*?)>||g;
 	$rendered=~s|</form>||g;
 	$rendered=~s|name="submit"|name="would_have_been_submit"|g;
     }
-    my $companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
-							   $ENV{'request.course.id'});
+    my $companswer;
+    if ($mode eq 'both' or $mode eq 'answer') {
+	$companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
+						    $ENV{'request.course.id'});
+    }
     if ($removeform) {
 	$companswer=~s|<form(.*?)>||g;
 	$companswer=~s|</form>||g;
-	$rendered=~s|name="submit"|name="would_have_been_submit"|g;
+	$companswer=~s|name="submit"|name="would_have_been_submit"|g;
     }
     my $result.='<table border="0" width="100%"><tr><td bgcolor="#777777">';
     $result.='<table border="0" width="100%">';
-    $result.='<tr><td bgcolor="#e6ffff"><b> View of the problem - '.$ENV{'form.fullname'}.
-	'</b></td></tr>' if ($viewon);
-    $result.='<tr><td bgcolor="#ffffff">'.$rendered.'<br />';
-    $result.='<b>Correct answer:</b><br />'.$companswer;
+    if ($viewon) {
+	$result.='<tr><td bgcolor="#e6ffff"><b> ';
+	if ($mode eq 'both' or $mode eq 'text') {
+	    $result.='View of the problem - ';
+	} else {
+	    $result.='Correct answer: ';
+	}
+	$result.=$ENV{'form.fullname'}.'</b></td></tr>';
+    }
+    if ($mode eq 'both') {
+	$result.='<tr><td bgcolor="#ffffff">'.$rendered.'<br />';
+	$result.='<b>Correct answer:</b><br />'.$companswer;
+    } elsif ($mode eq 'text') {
+	$result.='<tr><td bgcolor="#ffffff">'.$rendered;
+    } elsif ($mode eq 'answer') {
+	$result.='<tr><td bgcolor="#ffffff">'.$companswer;
+    }
     $result.='</td></tr></table>';
     $result.='</td></tr></table><br />';
     return $result;
@@ -1264,8 +1290,16 @@ sub submission {
 
 	# option to display problem, only once else it cause problems 
         # with the form later since the problem has a form.
-	if ($ENV{'form.vProb'} eq 'yes' or !$ENV{'form.vProb'}) {
-	    $request->print(&show_problem($request,$symb,$uname,$udom,0,1));
+	if ($ENV{'form.vProb'} eq 'yes' or $ENV{'form.vAns'} eq 'yes') {
+	    my $mode;
+	    if ($ENV{'form.vProb'} eq 'yes' && $ENV{'form.vAns'} eq 'yes') {
+		$mode='both';
+	    } elsif ($ENV{'form.vProb'} eq 'yes') {
+		$mode='text';
+	    } elsif ($ENV{'form.vAns'} eq 'yes') {
+		$mode='answer';
+	    }
+	    $request->print(&show_problem($request,$symb,$uname,$udom,0,1,$mode));
 	}
 	
 	# kwclr is the only variable that is guaranteed to be non blank 
@@ -1300,6 +1334,7 @@ sub submission {
 			'<input type="hidden" name="url"        value="'.$url.'" />'."\n".
 			'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" />'."\n".
 			'<input type="hidden" name="vProb"      value="'.$ENV{'form.vProb'}.'" />'."\n".
+			'<input type="hidden" name="vAns"       value="'.$ENV{'form.vAns'}.'" />'."\n".
 			'<input type="hidden" name="lastSub"    value="'.$ENV{'form.lastSub'}.'" />'."\n".
 			'<input type="hidden" name="section"    value="'.$ENV{'form.section'}.'">'."\n".
 			'<input type="hidden" name="submitonly" value="'.$ENV{'form.submitonly'}.'">'."\n".
@@ -1350,10 +1385,19 @@ KEYWORDS
         }
     }
 
-    if ($ENV{'form.vProb'} eq 'all') {
+    if ($ENV{'form.vProb'} eq 'all' or $ENV{'form.vAns'} eq 'all') {
 	$request->print('<br /><br /><br />') if ($counter > 0);
-	$request->print(&show_problem($request,$symb,$uname,$udom,1,1));
+	my $mode;
+	if ($ENV{'form.vProb'} eq 'all' && $ENV{'form.vAns'} eq 'all') {
+	    $mode='both';
+	} elsif ($ENV{'form.vProb'} eq 'all' ) {
+	    $mode='text';
+	} elsif ($ENV{'form.vAns'} eq 'all') {
+	    $mode='answer';
+	}
+	$request->print(&show_problem($request,$symb,$uname,$udom,1,1,$mode));
     }
+
     my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
 
     my ($partlist,$handgrade) = &response_type($url,$symb);
@@ -1461,13 +1505,20 @@ KEYWORDS
 				my ($ressub,$subval) = split(/:/,$_,2);
                             # Similarity check
 				my $similar='';
-				my ($oname,$odom,$ocrsid,$oessay,$osim)=&most_similar($uname,$udom,$subval);
-				if ($osim) {
-				    $osim=int($osim*100.0);
-				    $similar='<hr /><h3><font color="#FF0000">Essay is '.$osim.
-					'% similar to an essay by '.&Apache::loncommon::plainname($oname,$odom).
-					'</font></h3><blockquote><i>'.
-					&keywords_highlight($oessay).'</i></blockquote><hr />';
+				my $oname;
+				my $odom;
+				my $ocrsid;
+				my $oessay;
+				my $osim;
+				if($ENV{'form.checkPlag'}){
+				    ($oname,$odom,$ocrsid,$oessay,$osim)=&most_similar($uname,$udom,$subval);
+				    if ($osim) {
+					$osim=int($osim*100.0);
+					$similar='<hr /><h3><font color="#FF0000">Essay is '.$osim.
+					    '% similar to an essay by '.&Apache::loncommon::plainname($oname,$odom).
+					    '</font></h3><blockquote><i>'.
+					    &keywords_highlight($oessay).'</i></blockquote><hr />';
+				    }
 				}
 				$lastsubonly.='<tr><td bgcolor="#ffffe6"><b>Part '.
 				    $partid.'</b> <font color="#999999">( ID '.$respid.
@@ -1480,7 +1531,7 @@ KEYWORDS
 				     'this file may contain virusses</font><br />':'').
 				     '<b>Submitted Answer: </b>'.
 				     &cleanRecord($subval,$responsetype,$symb).
-				      '<br /><br />'.$similar."\n"
+				     '<br /><br />'.$similar."\n"
 				     if ($ENV{'form.lastSub'} eq 'lastonly' || 
 					 ($ENV{'form.lastSub'} eq 'hdgrade' && 
 					  $$handgrade{$part} =~ /:yes$/));
@@ -1782,15 +1833,25 @@ sub processHandGrade {
     }
     $ctr = 0;
     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
+    my ($partlist) = &response_type($url);
     foreach my $student (@parsedlist) {
+	my $submitonly=$ENV{'form.submitonly'};
 	my ($uname,$udom) = split(/:/,$student);
-	if ($ENV{'form.submitonly'} eq 'yes') {
-	    my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
-	    my $statusflg = '';
-	    foreach (split(/:/,$ENV{'form.gradePartRespid'})){
-		$statusflg = 1 if (exists ($record{'resource.'.$_.'.submission'}));
+	if ($submitonly =~ /^(yes|graded)$/) {
+#	    my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
+	    my %status=&student_gradeStatus($url,$symb,$udom,$uname,$partlist);
+	    my $submitted = 0;
+	    my $graded = 1;
+	    foreach (keys(%status)) {
+		$submitted = 1 if ($status{$_} ne 'nothing');
+		$graded = 0 if ($status{$_} =~ /^correct/);
+		my ($foo,$partid,$foo1) = split(/\./,$_);
+		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
+		    $submitted = 0;
+		}
 	    }
-	    next if ($statusflg eq '');
+	    next if (!$submitted && ($submitonly eq 'yes' || $submitonly eq 'graded'));
+	    next if (!$graded && $submitonly eq 'graded');
 	}
 	push @nextlist,$student if ($ctr < $ntstu);
 	last if ($ctr == $ntstu);
@@ -2327,18 +2388,17 @@ sub editgrades {
 		$newrecord{'resource.'.$_.'.awarded'} = 0;
 		$newrecord{'resource.'.$_.'.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
 		$updateflag = 1;
+	    } elsif (!($old_part eq $partial && $old_score eq $score)) {
+		$updateflag = 1;
+		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
+		$newrecord{'resource.'.$_.'.solved'}   = $score;
+		$rec_update++;
 	    }
 
 	    $line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
 		'<td align="center">'.$awarded.
 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
 
-	    if (!($old_part eq $partial && $old_score eq $score)) {
-		$updateflag = 1;
-		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
-		$newrecord{'resource.'.$_.'.solved'}   = $score;
-		$rec_update++;
-	    }
 
 	    my $partid=$_;
 	    foreach my $stores (@parts) {
@@ -2510,7 +2570,6 @@ to this page if the data selected is ins
 $javascript
 </script>
 ENDPICK
-    $request->print(&show_grading_menu_form($symb,$url));
     return '';
 
 }
@@ -2727,7 +2786,9 @@ LISTJAVASCRIPT
     $result.='<form action="/adm/grades" method="post" name="displayPage">'."\n";
     $result.='&nbsp;<b>Problems from:</b> <select name="selectpage">'."\n";
     my ($titles,$symbx) = &getSymbMap($request);
-    my ($curpage,$type,$mapId) = ($symb =~ /(.*?\.(page|sequence))___(\d+)___/); 
+    my ($curpage) =&Apache::lonnet::decode_symb($symb); 
+#    my ($curpage,$mapId) =&Apache::lonnet::decode_symb($symb); 
+#    my $type=($curpage =~ /\.(page|sequence)/);
     my $ctr=0;
     foreach (@$titles) {
 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
@@ -2747,7 +2808,7 @@ LISTJAVASCRIPT
     $result.='<input type="hidden" name="page" />'."\n".
 	'<input type="hidden" name="title" />'."\n";
 
-    $result.='&nbsp;<b>View Problems Text: </b><input type="radio" name="vProb" value="no" checked /> no '."\n".
+    $result.='&nbsp;<b>View Problems Text: </b><input type="radio" name="vProb" value="no" checked="on" /> no '."\n".
 	'<input type="radio" name="vProb" value="yes" /> yes '."<br>\n";
 
     $result.='&nbsp;<b>Submission Details: </b>'.
@@ -2845,7 +2906,7 @@ sub displayPage {
     $request->print($result);
 
     my $navmap = Apache::lonnavmaps::navmap->new();
-    my ($mapUrl, $id, $resUrl) = split(/___/, $ENV{'form.page'});
+    my ($mapUrl, $id, $resUrl)=&Apache::lonnet::decode_symb($ENV{'form.page'});
     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
 
     my $iterator = $navmap->getIterator($map->map_start(),
@@ -2886,8 +2947,9 @@ sub displayPage {
 	    $studentTable.='<tr bgcolor="#ffffe6"><td align="center" valign="top" >'.$question.
 		(scalar(@{$parts}) == 1 ? '' : '<br>('.scalar(@{$parts}).'&nbsp;parts)').'</td>';
 	    $studentTable.='<td valign="top">';
-	    if ($ENV{'form.vProb'} eq 'yes') {
-		$studentTable.=&show_problem($request,$symbx,$uname,$udom,1);
+	    if ($ENV{'form.vProb'} eq 'yes' ) {
+		$studentTable.=&show_problem($request,$symbx,$uname,$udom,1,
+					     undef,'both');
 	    } else {
 		my $companswer = &Apache::loncommon::get_student_answers($symbx,$uname,$udom,$ENV{'request.course.id'});
 		$companswer =~ s|<form(.*?)>||g;
@@ -3007,7 +3069,7 @@ sub updateGradeByPage {
     $request->print($result);
 
     my $navmap = Apache::lonnavmaps::navmap->new();
-    my ($mapUrl, $id, $resUrl) = split(/___/, $ENV{'form.page'});
+    my ($mapUrl, $id, $resUrl) = &Apache::lonnet::decode_symb( $ENV{'form.page'});
     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
 
     my $iterator = $navmap->getIterator($map->map_start(),
@@ -3129,7 +3191,7 @@ sub getSequenceDropDown {
     my ($request,$symb)=@_;
     my $result='<select name="selectpage">'."\n";
     my ($titles,$symbx) = &getSymbMap($request);
-    my ($curpage,$type,$mapId) = ($symb =~ /(.*?\.(page|sequence))___(\d+)___/); 
+    my ($curpage)=&Apache::lonnet::decode_symb($symb); 
     my $ctr=0;
     foreach (@$titles) {
 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
@@ -3181,7 +3243,7 @@ sub scantron_selectphase {
     my $result;
     $result.= <<SCANTRONFORM;
 <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantro_process">
-  <input type="hidden" name="command" value="scantron_process" />
+  <input type="hidden" name="command" value="scantron_validate" />
   $default_form_data
   <table width="100%" border="0">
     <tr>
@@ -3211,7 +3273,7 @@ sub scantron_selectphase {
       </td>
     </tr>
   </table>
-  <input type="submit" value="Submit" />
+  <input type="submit" value="Validate Scantron Records" />
 </form>
 $grading_menu_button
 SCANTRONFORM
@@ -3293,14 +3355,23 @@ sub scantron_parse_scanline {
 }
 
 sub scantron_add_delay {
+    my ($delayqueue,$scanline,$errormessage,$errorcode)=@_;
+    Apache->request->print('add_delay_error '.$_[2] );
+    push(@$delayqueue,
+	 {'line' => $scanline, 'emsg' => $errormessage,
+	  'ecode' => $errorcode }
+	 );
 }
 
 sub scantron_find_student {
     my ($scantron_record,$idmap)=@_;
     my $scanID=$$scantron_record{'scantron.ID'};
     foreach my $id (keys(%$idmap)) {
-	Apache->request->print('<pre>checking studnet -'.$id.'- againt -'.$scanID.'- </pre>');
-	if (lc($id) eq lc($scanID)) { Apache->request->print('success');return $$idmap{$id}; }
+	#Apache->request->print('<pre>checking studnet -'.$id.'- againt -'.$scanID.'- </pre>');
+	if (lc($id) eq lc($scanID)) {
+	    #Apache->request->print('success');
+	    return $$idmap{$id};
+	}
     }
     return undef;
 }
@@ -3313,9 +3384,21 @@ sub scantron_filter {
     return 0;
 }
 
+#FIXME I think I am doing this in the wrong order, I think it would be
+#better to make a several passes analyzing all of the lines in the
+#file for common errors wrong/invalid PID/username duplicated
+#PID/username, missing bubbles, double bubbles, missing/invalid CODE
+#and then get the instructor to fix all of these errors, then grade
+#the corrected one, I'll still need to catch error conditions, but
+#maybe most will taken care even before we start
+
+sub scantron_validate_file {
+    my ($r) = @_;
+}
+
 sub scantron_process_students {
     my ($r) = @_;
-    my (undef,undef,$sequence)=split(/___/,$ENV{'form.selectpage'});
+    my (undef,undef,$sequence)=&Apache::lonnet::decode_symb($ENV{'form.selectpage'});
     my ($symb,$url)=&get_symb_and_url($r);
     if (!$symb) {return '';}
     my $default_form_data=&defaultFormData($symb,$url);
@@ -3328,7 +3411,7 @@ sub scantron_process_students {
     my $navmap=Apache::lonnavmaps::navmap->new();
     my $map=$navmap->getResourceByUrl($sequence);
     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
-    $r->print("geto ".scalar(@resources)."<br />");
+#    $r->print("geto ".scalar(@resources)."<br />");
     my $result= <<SCANTRONFORM;
 <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
   <input type="hidden" name="command" value="scantron_configphase" />
@@ -3337,29 +3420,36 @@ SCANTRONFORM
     $r->print($result);
 
     my @delayqueue;
-    my $totalcorrect;
-    my $totalincorrect;
-
+    my %completedstudents;
+    
     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,
 	           'Scantron Status','Scantron Progress',scalar(@scanlines));
+    &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
+					  'Processing first student');
+    my $start=&Time::HiRes::time();
     foreach my $line (@scanlines) {
-	my $studentcorrect;
-	my $studentincorrect;
+	$r->print('<pre>line is'.$line.'</pre>');
 
 	chomp($line);
 	my $scan_record=&scantron_parse_scanline($line,\%scantron_config);
 	my ($uname,$udom);
-	if ($uname=&scantron_find_student($scan_record,\%idmap)) {
+	unless ($uname=&scantron_find_student($scan_record,\%idmap)) {
 	    &scantron_add_delay(\@delayqueue,$line,
-				'Unable to find a student that matches');
+				'Unable to find a student that matches',1);
+	    next;
+	}
+	if (exists $completedstudents{$uname}) {
+	    &scantron_add_delay(\@delayqueue,$line,
+				'Student '.$uname.' has multiple sheets',2);
+	    next;
 	}
 	$r->print('<pre>doing studnet'.$uname.'</pre>');
 	($uname,$udom)=split(/:/,$uname);
 	&Apache::lonnet::delenv('form.counter');
 	&Apache::lonnet::appenv(%$scan_record);
 #    &Apache::lonhomework::showhash(%ENV);
-    $Apache::lonxml::debug=1;
-	&Apache::lonxml::debug("line is $line");
+#    $Apache::lonxml::debug=1;
+#	&Apache::lonxml::debug("line is $line");
 	
 	    my $i=0;
 	foreach my $resource (@resources) {
@@ -3371,31 +3461,31 @@ SCANTRONFORM
 				  'grade_domain'  =>$udom,
 				  'grade_courseid'=>$ENV{'request.course.id'},
 				  'grade_symb'    =>$resource->symb()));
-	    my %score=&Apache::lonnet::restore($resource->symb(),
-					       $ENV{'request.course.id'},
-					       $udom,$uname);
-	    foreach my $part ($resource->{PARTS}) {
-		if ($score{'resource.'.$part.'.solved'} =~ /^correct/) {
-		    $studentcorrect++;
-		    $totalcorrect++;
-		} else {
-		    $studentincorrect++;
-		    $totalincorrect++;
-		}
-	    }
-	    $r->print('<pre>'.
-		      $resource->symb().'-'.
-		      $resource->src().'-'.'</pre>result is'.$result);
-	    &Apache::lonhomework::showhash(%score);
+#	    my %score=&Apache::lonnet::restore($resource->symb(),
+#					       $ENV{'request.course.id'},
+#					       $udom,$uname);
+#	    foreach my $part ($resource->{PARTS}) {
+#		if ($score{'resource.'.$part.'.solved'} =~ /^correct/) {
+#		    $studentcorrect++;
+#		    $totalcorrect++;
+#		} else {
+#		    $studentincorrect++;
+#		    $totalincorrect++;
+#		}
+#	    }
+#	    $r->print('<pre>'.
+#		      $resource->symb().'-'.
+#		      $resource->src().'-'.'</pre>result is'.$result);
+#	    &Apache::lonhomework::showhash(%score);
 	#    if ($i eq 3) {last;}
 	}
+	$completedstudents{$uname}={'line'=>$line};
+    } continue {
 	&Apache::lonnet::delenv('form.counter');
 	&Apache::lonnet::delenv('scantron\.');
 	&Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
-             'last student Who got a '.$studentcorrect.' correct and '.
-	     $studentincorrect.' incorrect. The class has gotten '.
-             $totalcorrect.' correct and '.$totalincorrect.' incorrect');
-	last;
+						 'last student');
+	#last;
 	#FIXME
 	#get iterator for $sequence
 	#foreach question 'submit' the students answer to the server
@@ -3403,7 +3493,11 @@ SCANTRONFORM
 	#   generate data to pass back that includes grade recevied
 	#}
     }
-    $Apache::lonxml::debug=0;
+    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
+    my $lasttime = &Time::HiRes::time()-$start;
+    $r->print("<p>took $lasttime</p>");
+
+    #$Apache::lonxml::debug=0;
     foreach my $delay (@delayqueue) {
 	#FIXME
 	#print out each delayed student with interface to select how
@@ -3470,7 +3564,7 @@ sub gradingmenu {
 	}
 	formname.command.value = cmd;
 	formname.saveState.value = "saveCmd="+cmdsave+":saveSec="+pullDownSelection(formname.section)+
-	    ":saveSub="+radioSelection(formname.submitonly)+":saveStatus="+pullDownSelection(formname.Status);
+	    ":saveSub="+pullDownSelection(formname.submitonly)+":saveStatus="+pullDownSelection(formname.Status);
 	if (val < 5) formname.submit();
 	if (val == 5) {
 	    if (!checkReceiptNo(formname,'notOK')) { return false;}
@@ -3538,12 +3632,14 @@ GRADINGMENUJS
 
     $result.='<tr bgcolor="#ffffe6"valign="top"><td>'.
 	'<input type="radio" name="radioChoice" value="submission" '.
-	($saveCmd eq 'submission' ? 'checked' : '').'> '.'<b>Current Resource:</b> For one or more students'.
-	'<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;-->For students with '.
-	'<input type="radio" name="submitonly" value="yes" '.
-	($saveSub eq 'yes' ? 'checked' : '').' /> submissions or '.
-	'<input type="radio" name="submitonly" value="all" '.
-	($saveSub eq 'all' ? 'checked' : '').' /> for all</td></tr>'."\n";
+	($saveCmd eq 'submission' ? 'checked' : '').'> '.'<b>Current Resource:</b> For one or more students '.
+	'<select name="submitonly">'.
+	'<option value="yes" '.
+	($saveSub eq 'yes' ? 'selected="on"' : '').'>with submissions</option>'.
+	'<option value="graded" '.
+	($saveSub eq 'graded' ? 'selected="on"' : '').'>with ungraded submissions</option>'.
+	'<option value="all" '.
+	($saveSub eq 'all' ? 'selected="on"' : '').'>with any status</option></select></td></tr>'."\n";
 
     $result.='<tr bgcolor="#ffffe6"valign="top"><td>'.
 	'<input type="radio" name="radioChoice" value="viewgrades" '.
@@ -3589,9 +3685,9 @@ sub handler {
 
     undef(%perm);
     if ($ENV{'browser.mathml'}) {
-	$request->content_type('text/xml');
+	&Apache::loncommon::content_type($request,'text/xml');
     } else {
-	$request->content_type('text/html');
+	&Apache::loncommon::content_type($request,'text/html');
     }
     $request->send_http_header;
     return '' if $request->header_only;
@@ -3601,7 +3697,7 @@ sub handler {
     my $command=$ENV{'form.command'};
     if (!$url) {
 	my ($temp1,$temp2);
-	($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
+	($temp1,$temp2,$ENV{'form.url'})=&Apache::lonnet::decode_symb($symb);
 	$url = $ENV{'form.url'};
     }
     &send_header($request);
@@ -3614,7 +3710,7 @@ sub handler {
 		my ($tsymb,$tuname,$tudom,$tcrsid)=
 		    &Apache::lonnet::checkin($token);
 		if ($tsymb) {
-		    my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
+		    my ($map,$id,$url)=&Apache::lonnet::decode_symb($tsymb);
 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
 			$request->print(&Apache::lonnet::ssi_body('/res/'.$url,
 					  ('grade_username' => $tuname,
@@ -3686,6 +3782,8 @@ sub handler {
 	    }
 	} elsif ($command eq 'scantron_selectphase' && $perm{'mgr'}) {
 	    $request->print(&scantron_selectphase($request));
+	} elsif ($command eq 'scantron_validate' && $perm{'mgr'}) {
+	    $request->print(&scantron_validate_file($request));
 	} elsif ($command eq 'scantron_process' && $perm{'mgr'}) {
 	    $request->print(&scantron_process_students($request));
 	} elsif ($command) {