--- loncom/homework/grades.pm	2005/06/04 02:56:44	1.270
+++ loncom/homework/grades.pm	2005/07/12 14:57:55	1.276
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.270 2005/06/04 02:56:44 albertel Exp $
+# $Id: grades.pm,v 1.276 2005/07/12 14:57:55 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -743,7 +743,7 @@ LISTJAVASCRIPT
 	    if ($env{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
 		foreach (sort keys(%status)) {
 		    next if (/^resource.*?submitted_by$/);
-		    $gradeTable.='<td align="middle">&nbsp;'.$status{$_}.'&nbsp;</td>'."\n";
+		    $gradeTable.='<td align="center">&nbsp;'.$status{$_}.'&nbsp;</td>'."\n";
 		}
 	    }
 #	    $gradeTable.='<td></td>' if ($ctr%2 ==1);
@@ -2273,18 +2273,14 @@ sub get_last_resets {
     my %last_resets;
     my $cdom = $env{'course.'.$courseid.'.domain'};
     my $cname = $env{'course.'.$courseid.'.num'};
-    my %resethash = &Apache::lonnet::restore($symb,'nohist_resourcetracker',
-					     $cdom,$cname);
-    if ($resethash{'version'}) {
-        foreach my $part (@{$partids}) {
-            $last_resets{$part} = '';
-            for (my $version=$resethash{'version'};$version>=1;$version--) {
-                if (exists($resethash{$version.':'.$part."\0".'prev_attempts'})) {
-                    $last_resets{$part} = $resethash{$version.':timestamp'};
-                    last;
-                }
-            }
-        }
+    my @keys;
+    foreach my $part (@{$partids}) {
+	push(@keys,"$symb\0$part\0resettime");
+    }
+    my %results=&Apache::lonnet::get('nohist_resourcetracker',\@keys,
+				     $cdom,$cname);
+    foreach my $part (@{$partids}) {
+	$last_resets{$part}=$results{"$symb\0$part\0resettime"};
     }
     return %last_resets;
 }
@@ -2325,12 +2321,11 @@ sub version_portfiles {
                     }
                 }
                 $version++;
-                my $home_server = &Apache::lonnet::homeserver($stuname,$domain,undef);
                 $env{'form.copy'} = &Apache::lonnet::getfile("/uploaded/$domain/$stuname/portfolio$directory$answer_file");
                 if($env{'form.copy'} eq '-1') {
                     &Apache::lonnet::logthis('problem getting file '.$directory.$answer_file);
                 } else {
-                   my $copy_result = &Apache::lonnet::finishuserfileupload($stuname,$domain,$home_server,'copy',
+                   my $copy_result = &Apache::lonnet::finishuserfileupload($stuname,$domain,'copy',
                                     '/portfolio'.$directory.$answer_file_parts[0].'.'.$version.'.'.$answer_file_parts[-1]);
                     push(@v_portfiles, $answer_file_parts[0].'.'.$version.'.'.$answer_file_parts[-1]);
                     &Apache::lonnet::mark_as_readonly($domain,$stuname,
@@ -2672,7 +2667,7 @@ sub viewstudentgrade {
     foreach my $apart (@$parts) {
 	my ($part,$type) = &split_part_type($apart);
 	my $score=$record{"resource.$part.$type"};
-        $result.='<td align="middle">';
+        $result.='<td align="center">';
         my ($aggtries,$totaltries);
         unless (exists($aggregates{$part})) {
 	    $totaltries = $record{'resource.'.$part.'.tries'};
@@ -3877,12 +3872,12 @@ sub scantron_CODElist {
 
 sub scantron_CODEunique {
     my $result='<nobr>
-                 <input type="radio" name="scantron_CODEunique"
-                        value="Yes" checked="on" /> Yes
+                 <label><input type="radio" name="scantron_CODEunique"
+                        value="Yes" checked="on" /> Yes </label>
                 </nobr>
                 <nobr>
-                 <input type="radio" name="scantron_CODEunique"
-                        value="No" /> No
+                 <label><input type="radio" name="scantron_CODEunique"
+                        value="No" /> No </label>
                 </nobr>';
     return $result;
 }
@@ -3932,8 +3927,8 @@ sub scantron_selectphase {
           <tr bgcolor="#ffffe6">
 	    <td> Options: </td>
             <td>
-                <input type="checkbox" name="scantron_options_redo" value="redo_skipped"/> Do only previously skipped records <br />
-                <input type="checkbox" name="scantron_options_ignore" value="ignore_corrections"/> Remove all exisiting corrections
+	       <label><input type="checkbox" name="scantron_options_redo" value="redo_skipped"/> Do only previously skipped records</label> <br />
+               <label><input type="checkbox" name="scantron_options_ignore" value="ignore_corrections"/> Remove all exisiting corrections</label>
 	    </td>
           </tr>
           <tr bgcolor="#ffffe6">
@@ -4112,7 +4107,14 @@ sub scantron_fixup_scanline {
 	    &scan_data($scan_data,
 		       "$whichline.no_bubble.".$args->{'question'},'1');
 	} else {
-	    substr($answer,$args->{'response'},1)=$on;
+	    if ($on eq 'letter') {
+		my @alphabet=('A'..'Z');
+		$answer=$alphabet[$args->{'response'}];
+	    } elsif ($on eq 'number') {
+		$answer=$args->{'response'}+1;
+	    } else {
+		substr($answer,$args->{'response'},1)=$on;
+	    }
 	    &scan_data($scan_data,
 		       "$whichline.no_bubble.".$args->{'question'},undef,'1');
 	}
@@ -4173,8 +4175,12 @@ sub scantron_parse_scanline {
 	substr($questions,0,$$scantron_config{'Qlength'})='';
 	if (length($currentquest) < $$scantron_config{'Qlength'}) { next; }
 	if ($$scantron_config{'Qon'} eq 'letter') {
-	    if (!$currentquest || $currentquest eq $$scantron_config{'Qoff'} ||
-		$currentquest !~ /^[A-Z]$/) {
+	    if ($currentquest eq '?') {
+		push(@{$record{'scantron.doubleerror'}},$questnum);
+		$record{"scantron.$questnum.answer"}='';
+	    } elsif (!$currentquest 
+		     || $currentquest eq $$scantron_config{'Qoff'}
+		     || $currentquest !~ /^[A-Z]$/) {
 		$record{"scantron.$questnum.answer"}='';
 		if (!&scan_data($scan_data,"$whichline.no_bubble.$questnum")) {
 		    push(@{$record{"scantron.missingerror"}},$questnum);
@@ -4183,8 +4189,12 @@ sub scantron_parse_scanline {
 		$record{"scantron.$questnum.answer"}=$currentquest;
 	    }
 	} elsif ($$scantron_config{'Qon'} eq 'number') {
-	    if (!$currentquest || $currentquest eq $$scantron_config{'Qoff'} ||
-		$currentquest !~ /^\d$/) {
+	    if ($currentquest eq '?') {
+		push(@{$record{'scantron.doubleerror'}},$questnum);
+		$record{"scantron.$questnum.answer"}='';
+		} elsif (!$currentquest 
+			 || $currentquest eq $$scantron_config{'Qoff'} 
+			 || $currentquest !~ /^\d$/) {
 		$record{"scantron.$questnum.answer"}='';
 		if (!&scan_data($scan_data,"$whichline.no_bubble.$questnum")) {
 		    push(@{$record{"scantron.missingerror"}},$questnum);
@@ -4569,9 +4579,8 @@ sub lonnet_putfile {
     my ($contents,$filename)=@_;
     my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
     my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
-    my $docuhome=$env{'course.'.$env{'request.course.id'}.'.home'};
     $env{'form.sillywaytopassafilearound'}=$contents;
-    &Apache::lonnet::finishuserfileupload($docuname,$docudom,$docuhome,'sillywaytopassafilearound',$filename);
+    &Apache::lonnet::finishuserfileupload($docuname,$docudom,'sillywaytopassafilearound',$filename);
 
 }
 
@@ -4739,19 +4748,22 @@ sub scantron_get_correction {
 	$r->print("<p>How should I handle this? <br /> \n");
 	$r->print("\n<br /> ");
 	my $i=0;
-	if ($error eq 'incorrectCODE') {
+	if ($error eq 'incorrectCODE' 
+	    && $$scan_record{'scantron.CODE'}=~/\S/ ) {
 	    my ($max,$closest)=&scantron_get_closely_matching_CODEs($arg,$$scan_record{'scantron.CODE'});
 	    foreach my $testcode (@{$closest}) {
 		my $checked='';
 		if (!$i) { $checked=' checked="on" '; }
-		$r->print("<input type='radio' name='scantron_CODE_resolution' value='use_closest_$i' $checked /> Use the similar CODE <b><tt>".$testcode."</tt></b> instead.<input type='hidden' name='scantron_CODE_closest_$i' value='$testcode' />");
+		$r->print("<label><input type='radio' name='scantron_CODE_resolution' value='use_closest_$i' $checked /> Use the similar CODE <b><tt>".$testcode."</tt></b> instead.</label><input type='hidden' name='scantron_CODE_closest_$i' value='$testcode' />");
 		$r->print("\n<br />");
 		$i++;
 	    }
 	}
-	my $checked; if (!$i) { $checked=' checked="on" '; }
-	$r->print("<input type='radio' name='scantron_CODE_resolution' value='use_unfound' $checked /> Use the CODE <b><tt>".$$scan_record{'scantron.CODE'}."</tt></b> that is was on the paper, ignoring the error.");
-	$r->print("\n<br />");
+	if ($$scan_record{'scantron.CODE'}=~/\S/ ) {
+	    my $checked; if (!$i) { $checked=' checked="on" '; }
+	    $r->print("<label><input type='radio' name='scantron_CODE_resolution' value='use_unfound' $checked /> Use the CODE <b><tt>".$$scan_record{'scantron.CODE'}."</tt></b> that is was on the paper, ignoring the error.</label>");
+	    $r->print("\n<br />");
+	}
 
 	$r->print(<<ENDSCRIPT);
 <script type="text/javascript">
@@ -4770,9 +4782,9 @@ ENDSCRIPT
 	   "&scantron_CODElist=".&Apache::lonnet::escape($env{'form.scantron_CODElist'}).
 	   "&curCODE=".&Apache::lonnet::escape($$scan_record{'scantron.CODE'}).
 	   "&scantron_selectfile=".&Apache::lonnet::escape($env{'form.scantron_selectfile'});
-	$r->print("<input type='radio' name='scantron_CODE_resolution' value='use_found' /> <a target='_blank' href='$href'>Select</a> a CODE from the list of all CODEs and use it. Selected CODE is <input readonly='true' type='text' size='8' name='scantron_CODE_selectedvalue' onfocus=\"javascript:change_radio('use_found')\" onchange=\"javascript:change_radio('use_found')\" />");
+	$r->print("<label><input type='radio' name='scantron_CODE_resolution' value='use_found' /> <a target='_blank' href='$href'>Select</a> a CODE from the list of all CODEs and use it.</label> Selected CODE is <input readonly='true' type='text' size='8' name='scantron_CODE_selectedvalue' onfocus=\"javascript:change_radio('use_found')\" onchange=\"javascript:change_radio('use_found')\" />");
 	$r->print("\n<br />");
-	$r->print("<input type='radio' name='scantron_CODE_resolution' value='use_typed' /> Use <input type='text' size='8' name='scantron_CODE_newvalue' onfocus=\"javascript:change_radio('use_typed')\" onkeypress=\"javascript:change_radio('use_typed')\" /> as the CODE.");
+	$r->print("<label><input type='radio' name='scantron_CODE_resolution' value='use_typed' /> Use </label><input type='text' size='8' name='scantron_CODE_newvalue' onfocus=\"javascript:change_radio('use_typed')\" onkeypress=\"javascript:change_radio('use_typed')\" /> as the CODE.");
 	$r->print("\n<br /><br />");
     } elsif ($error eq 'doublebubble') {
 	$r->print("<p>There have been multiple bubbles scanned for a some question(s)</p>\n");
@@ -4805,21 +4817,26 @@ ENDSCRIPT
 sub scantron_bubble_selector {
     my ($r,$scan_config,$quest,@selected)=@_;
     my $max=$$scan_config{'Qlength'};
+
+    my $scmode=$$scan_config{'Qon'};
+    if ($scmode eq 'number' || $scmode eq 'letter') { $max=10; }	     
+
     my @alphabet=('A'..'Z');
     $r->print("<table border='1'><tr><td rowspan='2'>$quest</td>");
     for (my $i=0;$i<$max+1;$i++) {
-	$r->print('<td align="center">');
+	$r->print("\n".'<td align="center">');
 	if ($selected[0] eq $alphabet[$i]) { $r->print('X'); shift(@selected) }
 	else { $r->print('&nbsp;'); }
 	$r->print('</td>');
     }
-    $r->print('<td></td></tr><tr>');
+    $r->print('</tr><tr>');
     for (my $i=0;$i<$max;$i++) {
-	$r->print('<td><input type="radio" name="scantron_correct_Q_'.$quest.
-		  '" value="'.$i.'" />'.$alphabet[$i]."</td>");
+	$r->print("\n".
+		  '<td><label><input type="radio" name="scantron_correct_Q_'.
+		  $quest.'" value="'.$i.'" />'.$alphabet[$i]."</label></td>");
     }
-    $r->print('<td><input type="radio" name="scantron_correct_Q_'.$quest.
-	      '" value="none" /> No bubble </td>');
+    $r->print('<td><label><input type="radio" name="scantron_correct_Q_'.
+	      $quest.'" value="none" /> No bubble </label></td>');
     $r->print('</tr></table>');
 }
 
@@ -5139,8 +5156,6 @@ sub scantron_upload_scantron_data_save {
     }
     my %coursedata=&Apache::lonnet::coursedescription($env{'form.domainid'}.'_'.$env{'form.courseid'});
     $r->print("Doing upload to ".$coursedata{'description'}." <br />");
-    my $home=&Apache::lonnet::homeserver($env{'form.courseid'},
-					 $env{'form.domainid'});
     my $fname=$env{'form.upfile.filename'};
     #FIXME
     #copied from lonnet::userfileupload()
@@ -5160,7 +5175,7 @@ sub scantron_upload_scantron_data_save {
     if (length($env{'form.upfile'}) < 2) {
 	$r->print("<font color='red'>Error:</font> The file you attempted to upload, <tt>".&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"')."</tt>, contained no information. Please check that you entered the correct filename.");
     } else {
-	my $result=&Apache::lonnet::finishuserfileupload($env{'form.courseid'},$env{'form.domainid'},$home,'upfile',$fname);
+	my $result=&Apache::lonnet::finishuserfileupload($env{'form.courseid'},$env{'form.domainid'},'upfile',$fname);
 	if ($result =~ m|^/uploaded/|) {
 	    $r->print("<font color='green'>Success:</font> Successfully uploaded ".(length($env{'form.upfile'})-1)." bytes of data into location <tt>".$result."</tt>");
 	} else {