--- loncom/homework/grades.pm	2004/09/14 14:13:08	1.210
+++ loncom/homework/grades.pm	2004/11/13 09:42:38	1.226
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.210 2004/09/14 14:13:08 albertel Exp $
+# $Id: grades.pm,v 1.226 2004/11/13 09:42:38 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,16 +25,6 @@
 #
 # http://www.lon-capa.org/
 #
-# 2/9,2/13 Guy Albertelli
-# 6/8 Gerd Kortemeyer
-# 7/26 H.K. Ng
-# 8/20 Gerd Kortemeyer
-# Year 2002
-# June-August H.K. Ng
-# Year 2003
-# February, March H.K. Ng
-# July, H. K. Ng
-#
 
 package Apache::grades;
 use strict;
@@ -101,25 +91,6 @@ sub get_symb_and_url {
     return ($symb,$url);
 }
 
-# --- Retrieve the fullname for a user. Return lastname, first middle ---
-# --- Generation is attached next to the lastname if it exists. ---
-sub get_fullname {
-    my ($uname,$udom) = @_;
-    my %name=&Apache::lonnet::get('environment', ['lastname','generation',
-						  'firstname','middlename'],
-                                  $udom,$uname);
-    my $fullname;
-    my ($tmp) = keys(%name);
-    if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
-        $fullname = &Apache::loncoursedata::ProcessFullName
-            (@name{qw/lastname generation firstname middlename/});
-    } else {
-        &Apache::lonnet::logthis('grades.pm: no name data for '.$uname.
-                                 '@'.$udom.':'.$tmp);
-    }
-    return $fullname;
-}
-
 #--- Format fullname, username:domain if different for display
 #--- Use anywhere where the student names are listed
 sub nameUserString {
@@ -1001,6 +972,7 @@ sub sub_page_kw_js {
     my $request = shift;
     my $iconpath = $request->dir_config('lonIconsURL');
     &commonJSfunctions($request);
+    my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
     $request->print(<<SUBJAVASCRIPT);
 <script type="text/javascript" language="javascript">
 
@@ -1113,7 +1085,7 @@ sub sub_page_kw_js {
     pWin = window.open('', 'MessageCenter', 'resizable=yes,toolbar=no,location=no,scrollbars='+scrollbar+',screenx='+xpos+',screeny='+ypos+',width=600,height='+height);
     pWin.focus();
     pDoc = pWin.document;
-    pDoc.open('text/html','replace');
+    pDoc.$docopen;
     pDoc.write("<html><head>");
     pDoc.write("<title>Message Central</title>");
 
@@ -1244,7 +1216,7 @@ sub sub_page_kw_js {
     hwdWin = window.open('', 'KeywordHighlightCentral', 'resizeable=yes,toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx='+xpos+',screeny='+ypos);
     hwdWin.focus();
     var hDoc = hwdWin.document;
-    hDoc.open('text/html','replace');
+    hDoc.$docopen;
     hDoc.write("<html><head>");
     hDoc.write("<title>Highlight Central</title>");
 
@@ -1413,14 +1385,15 @@ sub submission {
     my ($uname,$udom)     = ($ENV{'form.student'},$ENV{'form.userdom'});
     $udom = ($udom eq '' ? $ENV{'user.domain'} : $udom); #has form.userdom changed for a student?
     my $usec = &Apache::lonnet::getsection($udom,$uname,$ENV{'request.course.id'});
-    $ENV{'form.fullname'} = &get_fullname ($uname,$udom) if $ENV{'form.fullname'} eq '';
+    $ENV{'form.fullname'} = &Apache::loncommon::plainname($uname,$udom,'lastname') if $ENV{'form.fullname'} eq '';
 
     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
 
     if (!&canview($usec)) {
 	$request->print('<font color="red">Unable to view requested student.('.
-			$uname.$udom.$usec.$ENV{'request.course.id'}.')</font>');
+			$uname.'@'.$udom.' in section '.$usec.' in course id '.
+			$ENV{'request.course.id'}.')</font>');
 	$request->print(&show_grading_menu_form($symb,$url));
 	return;
     }
@@ -3079,8 +3052,6 @@ sub getSymbMap {
 	    $minder++;
 	}
     }
-
-    $navmap->untieHashes();
     return \@titles,\%symbx;
 }
 
@@ -3213,8 +3184,6 @@ sub displayPage {
         $curRes = $iterator->next();
     }
 
-    $navmap->untieHashes();
-
     $studentTable.='</td></tr></table></td></tr></table>'."\n".
 	'<input type="button" value="Save" '.
 	'onClick="javascript:checkSubmitPage(this.form,'.$question.');" TARGET=_self />'.
@@ -3227,9 +3196,12 @@ sub displayPage {
 
 sub displaySubByDates {
     my ($symb,$record,$parts,$responseType,$checkIcon,$uname,$udom) = @_;
+    my $isCODE=0;
+    if (exists($record->{'resource.CODE'})) { $isCODE=1; }
     my $studentTable='<table border="0" width="100%"><tr><td bgcolor="#777777">'.
 	'<table border="0" width="100%"><tr bgcolor="#e6ffff">'.
 	'<td><b>Date/Time</b></td>'.
+	($isCODE?'<td><b>CODE</b></td>':'').
 	'<td><b>Submission</b></td>'.
 	'<td><b>Status&nbsp;</b></td></tr>';
     my ($version);
@@ -3242,6 +3214,9 @@ sub displaySubByDates {
     for ($version=1;$version<=$$record{'version'};$version++) {
 	my $timestamp = scalar(localtime($$record{$version.':timestamp'}));
 	$studentTable.='<tr bgcolor="#ffffff" valign="top"><td>'.$timestamp.'</td>';
+	if ($isCODE) {
+	    $studentTable.='<td>'.$record->{$version.':resource.CODE'}.'</td>';
+	}
 	my @versionKeys = split(/\:/,$$record{$version.':keys'});
 	my @displaySub = ();
 	foreach my $partid (@{$parts}) {
@@ -3410,8 +3385,6 @@ sub updateGradeByPage {
         $curRes = $iterator->next();
     }
 
-    $navmap->untieHashes();
-
     $studentTable.='</td></tr></table></td></tr></table>';
     $studentTable.=&show_grading_menu_form($ENV{'form.symb'},$ENV{'form.url'});
     my $grademsg=($changeflag == 0 ? 'No score was changed or updated.' :
@@ -3501,7 +3474,7 @@ sub scantron_CODElist {
     my $cnum = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
     my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum);
     my $namechoice='<option></option>';
-    foreach my $name (sort(@names)) {
+    foreach my $name (sort {uc($a) cmp uc($b)} @names) {
 	if ($name =~ /^error: 2 /) { next; }
 	$namechoice.='<option value="'.$name.'">'.$name.'</option>';
     }
@@ -3538,8 +3511,8 @@ sub scantron_selectphase {
     $result.= <<SCANTRONFORM;
     <table width="100%" border="0">
     <tr>
+     <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantron_process">
       <td bgcolor="#777777">
-       <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantron_process">
        <input type="hidden" name="command" value="scantron_warning" />
         $default_form_data
         <table width="100%" border="0">
@@ -3576,8 +3549,8 @@ sub scantron_selectphase {
             </td>
           </tr>
         </table>
-       </form>
-      </td>
+       </td>
+     </form>
     </tr>
 SCANTRONFORM
    
@@ -3633,8 +3606,8 @@ SCANTRONFORM
     }
     $r->print(<<SCANTRONFORM);
     <tr>
-      <td bgcolor="#777777">
-        <form action='/adm/grades' name='scantron_download'>
+      <form action='/adm/grades' name='scantron_download'>
+        <td bgcolor="#777777">
           <input type="hidden" name="command" value="scantron_download" />
           <table width="100%" border="0">
             <tr bgcolor="#e6ffff">
@@ -3651,14 +3624,13 @@ SCANTRONFORM
               </td>
             </tr>
           </table>
-        </form>
-      </td>
+        </td>
+      </form>
     </tr>
 SCANTRONFORM
 
     $r->print(<<SCANTRONFORM);
   </table>
-</form>
 $grading_menu_button
 SCANTRONFORM
 
@@ -3855,7 +3827,8 @@ sub scantron_find_student {
 
 sub scantron_filter {
     my ($curres)=@_;
-    if (ref($curres) && $curres->is_problem() && !$curres->randomout) {
+                        # randomout is dysfunctional at best for this purpose
+    if (ref($curres) && $curres->is_problem()) { #&& !$curres->randomout) {
 	return 1;
     }
     return 0;
@@ -4007,7 +3980,7 @@ sub scantron_form_start {
   <input type="hidden" name="selectpage" value="$ENV{'form.selectpage'}" />
   <input type="hidden" name="scantron_format" value="$ENV{'form.scantron_format'}" />
   <input type="hidden" name="scantron_selectfile" value="$ENV{'form.scantron_selectfile'}" />
-  <input type="hidden" name="scantron_maxbubble" value="$max_bubble'" />
+  <input type="hidden" name="scantron_maxbubble" value="$max_bubble" />
   <input type="hidden" name="scantron_CODElist" value="$ENV{'form.scantron_CODElist'}" />
   <input type="hidden" name="scantron_CODEunique" value="$ENV{'form.scantron_CODEunique'}" />
   <input type="hidden" name="scantron_options_redo" value="$ENV{'form.scantron_options_redo'}" />
@@ -4327,8 +4300,8 @@ sub scantron_get_correction {
 	} elsif ($error eq 'duplicateCODE') {
 	    $r->print("</p><p>The encoded CODE has also been used by a previous paper ".join(', ',@{$arg}).", and CODEs are supposed to be unique</p>\n");
 	}
-	$r->print("<p>The CODE on the form is  <tt>".
-		  $$scan_record{'scantron.CODE'}."</tt><br />\n");
+	$r->print("<p>The CODE on the form is  <tt>'".
+		  $$scan_record{'scantron.CODE'}."'</tt><br />\n");
 	$r->print("<p>The ID on the form is  <tt>".
 		  $$scan_record{'scantron.ID'}."</tt><br />\n");
 	$r->print("The name on the paper is ".
@@ -4474,13 +4447,21 @@ sub scantron_validate_CODE {
 						 $scan_data);
 	my $CODE=$$scan_record{'scantron.CODE'};
 	my $error=0;
-	if (!exists($allcodes{$CODE}) && !$$scan_record{'scantron.useCODE'}) {
+	if (!&Apache::lonnet::validCODE($CODE)) {
+	    &scantron_get_correction($r,$i,$scan_record,
+				     \%scantron_config,
+				     $line,'incorrectCODE',\%allcodes);
+	    return(1,$currentphase);
+	}
+	if (%allcodes && !exists($allcodes{$CODE}) 
+	    && !$$scan_record{'scantron.useCODE'}) {
 	    &scantron_get_correction($r,$i,$scan_record,
 				     \%scantron_config,
 				     $line,'incorrectCODE',\%allcodes);
 	    return(1,$currentphase);
 	}
-	if (exists($usedCODEs{$CODE}) && $ENV{'form.scantron_CODEunique'}
+	if (exists($usedCODEs{$CODE}) 
+	    && $ENV{'form.scantron_CODEunique'} eq 'yes'
 	    && !$$scan_record{'scantron.CODE_ignore_dup'}) {
 	    &scantron_get_correction($r,$i,$scan_record,
 				     \%scantron_config,
@@ -4645,11 +4626,14 @@ SCANTRONFORM
 	    if (exists($scan_record->{'scantron.CODE'}) &&
 		$scan_record->{'scantron.CODE'}) {
 		$form{'CODE'}=$scan_record->{'scantron.CODE'};
+	    } else {
+		$form{'CODE'}='';
 	    }
 	    my $result=&Apache::lonnet::ssi($resource->src(),%form);
-
+	    if (&Apache::loncommon::connection_aborted($r)) { last; }
 	}
 	$completedstudents{$uname}={'line'=>$line};
+	if (&Apache::loncommon::connection_aborted($r)) { last; }
     } continue {
 	&Apache::lonnet::delenv('form.counter');
 	&Apache::lonnet::delenv('scantron\.');
@@ -4658,7 +4642,6 @@ SCANTRONFORM
 #    my $lasttime = &Time::HiRes::time()-$start;
 #    $r->print("<p>took $lasttime</p>");
 
-    $navmap->untieHashes();
     $r->print("</form>");
     $r->print(&show_grading_menu_form($symb,$url));
     return '';
@@ -4719,7 +4702,8 @@ sub scantron_upload_scantron_data_save {
 	}
 	return '';
     }
-    $r->print("Doing upload to ".$ENV{'form.courseid'}." <br />");
+    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'};
@@ -5113,8 +5097,7 @@ sub send_header {
 
 sub send_footer {
     my ($request)= @_;
-    $request->print('</body>');
-    $request->print(&Apache::lontexconvert::footer());
+    $request->print('</body></html>');
 }
 
 1;