--- loncom/homework/grades.pm	2002/09/06 21:01:09	1.46.2.1
+++ loncom/homework/grades.pm	2002/10/02 21:24:35	1.53
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.46.2.1 2002/09/06 21:01:09 albertel Exp $
+# $Id: grades.pm,v 1.53 2002/10/02 21:24:35 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -111,6 +111,10 @@ sub getclasslist {
     my %classlist=&Apache::lonnet::dump('classlist',
 					$ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 					$ENV{'course.'.$ENV{'request.course.id'}.'.num'});
+    my ($tmp) = keys(%classlist);
+    # Bail out if we were unable to get the classlist
+    return if ($tmp =~ /^(con_lost|error|no_such_host)/i);
+
     # codes to check for fields in the classlist
     # should contain end:start:id:section:fullname
     for (keys %classlist) {
@@ -261,7 +265,7 @@ sub verifyreceipt {
     my ($string,$contents,$matches) = ('','',0);
     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist('all','0');
     
-    foreach (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
+    foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
 	my ($uname,$udom)=split(/\:/);
 	if ($receipt eq 
 	    &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
@@ -288,7 +292,7 @@ sub verifyreceipt {
 	    $contents.
 	    '</table></td></tr></table>'."\n";
     }
-    return $string.&show_grading_menu_form ($symb,$url);
+    return $string.&show_grading_menu_form($symb,$url);
 }
 
 #--- This is called by a number of programs.
@@ -297,6 +301,37 @@ sub verifyreceipt {
 #    on the problem page.
 sub listStudents {
     my ($request) = shift;
+
+    my ($symb,$url) = &get_symb_and_url();
+    my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
+    my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
+    my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
+    my $submitonly= $ENV{'form.submitonly'} eq '' ? 'all' : $ENV{'form.submitonly'};
+
+    my $result;
+    my ($partlist,$handgrade) = &response_type($url);
+    for (sort keys(%$handgrade)) {
+	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
+	$ENV{'form.handgrade'} = 'yes' if ($handgrade eq 'yes');
+	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
+	    '<td><b>Type: </b>'.$responsetype.'</td>'.
+	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
+    }
+    $result.='</table>';
+
+    my $viewgrade;
+    if ($ENV{'form.handgrade'} eq 'yes') {
+	$viewgrade = 'View/Grade';
+    } else {
+	$viewgrade = 'View';
+    }
+
+    $result='<h3><font color="#339933">&nbsp;'.
+	$viewgrade.
+	    ' Submissions for a Student or a Group of Students</font></h3>'.
+		'<table border="0"><tr><td colspan=3><font size=+1>'.
+		    '<b>Resource: </b>'.$url.'</font></td></tr>'.$result;
+
     $request->print(<<LISTJAVASCRIPT);
 <script type="text/javascript" language="javascript">
   function checkSelect(checkBox) {
@@ -316,7 +351,7 @@ sub listStudents {
        sense = "the student";
     }
     if (ctr == 0) {
-       alert("Please select "+sense+" before clicking on the View/Grade button.");
+       alert("Please select "+sense+" before clicking on the $viewgrade button.");
        return false;
     }
     document.gradesub.submit();
@@ -324,26 +359,6 @@ sub listStudents {
 </script>
 LISTJAVASCRIPT
 
-    my ($symb,$url) = &get_symb_and_url();
-    my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
-    my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
-    my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
-    my $submitonly= $ENV{'form.submitonly'} eq '' ? 'all' : $ENV{'form.submitonly'};
-
-    my $result='<h3><font color="#339933">&nbsp;'.
-	'View/Grade Submissions for a Student or a Group of Students</font></h3>';
-    $result.='<table border="0">';
-    $result.='<tr><td colspan=3><font size=+1>'.
-	'<b>Resource: </b>'.$url.'</font></td></tr>';
-    my ($partlist,$handgrade) = &response_type($url);
-    for (sort keys(%$handgrade)) {
-	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
-	$ENV{'form.handgrade'} = 'yes' if ($handgrade eq 'yes');
-	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
-	    '<td><b>Type: </b>'.$responsetype.'</td>'.
-	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
-    }
-    $result.='</table>';
     $request->print($result);
 
     my $checkhdgrade = $ENV{'form.handgrade'} eq 'yes' ? 'checked' : '';
@@ -352,9 +367,11 @@ LISTJAVASCRIPT
     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'."\n".
 	'&nbsp;<b>View Problem: </b><input type="radio" name="vProb" value="no" checked> no '."\n".
 	'<input type="radio" name="vProb" value="yes"> yes <br />'."\n".
-	'&nbsp;<b>Submissions: </b>'."\n".
-	'<input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> handgrade only'."\n".
-	'<input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> last sub only'."\n".
+	'&nbsp;<b>Submissions: </b>'."\n";
+    if ($ENV{'form.handgrade'} eq 'yes') {
+	$gradeTable.='<input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> handgrade only'."\n";
+    }
+    $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".
 	'<input type="radio" name="lastSub" value="all" /> all details'."\n".
 	'<input type="hidden" name="section"     value="'.$getsec.'" />'."\n".
@@ -364,13 +381,13 @@ LISTJAVASCRIPT
 	'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" /><br />'."\n".
 	'<input type="hidden" name="url"  value="'.$url.'" />'."\n".
 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
-	'To view/grade a submission, click on the check box next to the student\'s name. Then '."\n".
-	'click on the View/Grade button. To view the submissions for a group of students, click'."\n".
+	'To '.lc($viewgrade).' a submission, click on the check box next to the student\'s name. Then '."\n".
+	'click on the '.$viewgrade.' button. To view the submissions for a group of students, click'."\n".
 	' on the check boxes for the group of students.<br />'."\n".
 	'<input type="hidden" name="command" value="processGroup" />'."\n".
 	'<input type="button" '."\n".
 	'onClick="javascript:checkSelect(this.form.stuinfo);" '."\n".
-	'value="View/Grade" />'."\n";
+	'value="'.$viewgrade.'" />'."\n";
  
     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($getsec,'0');
     
@@ -384,7 +401,7 @@ LISTJAVASCRIPT
     $gradeTable.='</tr>'."\n";
 
     my $ctr = 0;
-    foreach my $student (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
+    foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
 	my ($uname,$udom) = split(/:/,$student);
 	my (%status) =&student_gradeStatus($url,$symb,$udom,$uname,$partlist);
 	my $statusflg = '';
@@ -419,14 +436,14 @@ LISTJAVASCRIPT
     $gradeTable.='</table></td></tr></table>'.
 	'<input type="button" '.
 	'onClick="javascript:checkSelect(this.form.stuinfo);" '.
-	'value="View/Grade" /><form />'."\n";
+	'value="'.$viewgrade.'" /></form>'."\n";
     if ($ctr == 0) {
 	$gradeTable='<br />&nbsp;<font color="red">'.
 	    'No submission found for this resource.</font><br />';
-	$gradeTable.=&show_grading_menu_form($symb,$url);
     } elsif ($ctr == 1) {
 	$gradeTable =~ s/type=checkbox/type=checkbox checked/;
     }
+    $gradeTable.=&show_grading_menu_form($symb,$url);
     $request->print($gradeTable);
     return '';
 }
@@ -1128,6 +1145,7 @@ KEYWORDS
 	$endform.='(Next and Previous do not save the scores.)'."\n" 
 	    if ($ENV{'form.handgrade'} eq 'yes');
 	$endform.='</td><tr></table></form>';
+	$endform.=&show_grading_menu_form($symb,$url);
 	$request->print($endform);
     }
     return '';
@@ -1282,7 +1300,7 @@ sub processHandGrade {
     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($ENV{'form.section'},'0');
     my (@parsedlist,@nextlist);
     my ($nextflg) = 0;
-    foreach (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
+    foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
 	if ($nextflg == 1 && $button =~ /Next$/) {
 	    push @parsedlist,$_;
 	}
@@ -1566,12 +1584,22 @@ sub viewgrades {
 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
 	'<input type="hidden" name="command" value="editgrades" />'."\n".
 	'<input type="hidden" name="section" value="'.$ENV{'form.section'}.'" />'."\n";
-    $result.='To assign the same score for all the students use the radio buttons or '.
-	'text box below. To assign scores individually fill in the score boxes for '.
-	'each student in the table below. <font color="red">A part that has already '.
-	'been graded does not get changed using the radio buttons or text box. '.
-	'If needed, it has to be changed individually.</font>';
-
+    $result.='<h3>Assign Common Grade To ';
+    if ($ENV{'form.section'} eq 'all') {
+	$result.='Class </h3>';
+    } elsif ($ENV{'form.section'} eq 'no') {
+	$result.='Students in no Section </h3>';
+    } else {
+	$result.='Students in Section '.$ENV{'form.section'}.'</h3>';
+    }
+    $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
+	'<table border=0><tr bgcolor="#ffffdd"><td>';
+#    $result.='To assign the same score for all the students use the radio buttons or '.
+#	'text box below. To assign scores individually fill in the score boxes for '.
+#	'each student in the table below. <font color="red">A part that has already '.
+#	'been graded does not get changed using the radio buttons or text box. '.
+#	'If needed, it has to be changed individually.</font>';
+#    $result.='</td></tr><tr><td>';
     #radio buttons/text box for assigning points for a section or class.
     #handles different parts of a problem
     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
@@ -1613,7 +1641,8 @@ sub viewgrades {
 	    '<option>excused</option></select></td></tr>'."\n";
 	$ctsparts++;
     }
-    $result.='</table><input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
+    $result.='</table>'.'</td></tr></table>'.'</td></tr></table>'."\n".
+	'<input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
     $result.='<input type="button" value="Reset" '.
 	'onClick="javascript:resetEntry('.$ctsparts.');" TARGET=_self> &nbsp; &nbsp;';
     $result.='<input type="button" value="Submit Changes" '.
@@ -1621,6 +1650,14 @@ sub viewgrades {
 
     #table listing all the students in a section/class
     #header of table
+    $result.= '<h3>Assign Grade to Specific Students in ';
+    if ($ENV{'form.section'} eq 'all') {
+	$result.='the Class </h3>';
+    } elsif ($ENV{'form.section'} eq 'no') {
+	$result.='no Section </h3>';
+    } else {
+	$result.='Section '.$ENV{'form.section'}.'</h3>';
+    }
     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
 	'<table border=0><tr bgcolor="#deffff">'.
 	'<td><b>Fullname</b></td><td><b>Username</b></td><td><b>Domain</b></td>'."\n";
@@ -1632,11 +1669,11 @@ sub viewgrades {
 	if ($display =~ /^Partial Credit Factor/) {
 	    $_ = $display;
 	    my ($partid) = /.*?(\d+).*/;
-	    $result.='<td><b>Score Part '.$partid.'<br>(weight = '.
+	    $result.='<td><b>Score Part '.$partid.'<br />(weight = '.
 		$weight{$partid}.')</b></td>'."\n";
 	    next;
 	}
-	$display =~ s/Problem Status/Grade Status<br>/;
+	$display =~ s|Problem Status|Grade Status<br />|;
 	$result.='<td><b>'.$display.'</b></td>'."\n";
     }
     $result.='</tr>';
@@ -1645,7 +1682,7 @@ sub viewgrades {
     #list all the students - with points and grade status
     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($ENV{'form.section'},'0');
     my $ctr = 0;
-    foreach (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
+    foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
 	my ($uname,$udom) = split(/:/);
 	$result.='<input type="hidden" name="ctr'.$ctr.'" value="'.$uname.'" />'."\n";
 	$result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},
@@ -2098,18 +2135,20 @@ sub view_edit_entire_class_form {
     my ($classlist,$sections) = &getclasslist('all','0');
     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
-    $result.='&nbsp;<b>View/Grade Entire Section/Class</b></td></tr>'."\n";
+    $result.='&nbsp;<b>Grade Entire Section or Class</b></td></tr>'."\n";
     $result.='<tr bgcolor=#ffffe6><td>'."\n";
     $result.='<form action="/adm/grades" method="post">'."\n".
 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
 	'<input type="hidden" name="command" value="viewgrades" />'."\n";
     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
-    foreach (sort (@$sections)) {
-	$result.= '<option>'.$_.'</option>'."\n";
+    if (ref($sections)) {
+	foreach (sort (@$sections)) {
+	    $result.= '<option>'.$_.'</option>'."\n";
+	}
     }
     $result.='<option selected="on">all</select>'."<br />\n";
-    $result.='&nbsp;<input type="button" onClick="submit();" value="View/Grade" /></form>'."\n";
+    $result.='&nbsp;<input type="button" onClick="submit();" value="Grade" /></form>'."\n";
     $result.='</td></tr></table>'."\n";
     $result.='</td></tr></table>'."\n";
     return $result;
@@ -2154,7 +2193,13 @@ sub viewGradeaStu_form {
     my ($classlist,$sections) = &getclasslist('all','0');
     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
-    $result.='&nbsp;<b>View/Grade an Individual Student\'s Submission</b></td></tr>'."\n";
+    $result.='&nbsp;<b>';
+    if ($handgrade eq 'yes') {
+	$result.="View/Grade ";
+    } else {
+	$result.="View ";
+    }
+    $result.='an Individual Student\'s Submission</b></td></tr>'."\n";
     $result.='<tr bgcolor=#ffffe6><td>'."\n";
     $result.='<form action="/adm/grades" method="post">'."\n".
 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
@@ -2165,18 +2210,26 @@ sub viewGradeaStu_form {
 	'<input type="hidden" name="command" value="submission" />'."\n";
 
     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
-    foreach (sort (@$sections)) {
-	$result.= '<option>'.$_.'</option>'."\n";
+    if (ref($sections)) {
+	foreach (sort (@$sections)) {$result.='<option>'.$_.'</option>'."\n";}
     }
     $result.= '<option selected="on">all</select>'."\n";
     $result.='&nbsp;&nbsp;<b>Display students who has: </b>'.
 	'<input type="radio" name="submitonly" value="yes" checked> submitted'.
 	'<input type="radio" name="submitonly" value="all"> everybody <br />';
-    $result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
-	if (grep /no/,@$sections);
-   
-    $result.='<br />&nbsp;<input type="button" onClick="submit();" value="View/Grade" />'."\n".
-	'</form>'."\n";
+    if (ref($sections)) {
+	$result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
+	    if (grep /no/,@$sections);
+    }
+
+
+    $result.='<br />&nbsp;<input type="button" onClick="submit();" value="';
+    if ($handgrade eq 'yes') {
+	$result.="View/Grade";
+    } else {
+	$result.="View";
+    }
+    $result.='" />'."\n".'</form>'."\n";
     $result.='</td></tr></table>'."\n";
     $result.='</td></tr></table>'."\n";
     return $result;
@@ -2316,7 +2369,7 @@ sub send_header {
 #remotewindow=open('','homeworkremote');
 #remotewindow.close();
 #</script>"); 
-    $request->print('<body bgcolor="#FFFFFF">');
+    $request->print(&Apache::loncommon::bodytag('Grading'));
 }
 
 sub send_footer {