[LON-CAPA-cvs] cvs: loncom /homework grades.pm

ng lon-capa-cvs@mail.lon-capa.org
Wed, 26 Jun 2002 21:25:31 -0000


This is a MIME encoded message

--ng1025126731
Content-Type: text/plain

ng		Wed Jun 26 17:25:31 2002 EDT

  Modified files:              
    /loncom/homework	grades.pm 
  Log:
  javascript to update radiobuttons/text box
  routine to save score for one student only.
  
  
  
--ng1025126731
Content-Type: text/plain
Content-Disposition: attachment; filename="ng-20020626172531.txt"

Index: loncom/homework/grades.pm
diff -u loncom/homework/grades.pm:1.31 loncom/homework/grades.pm:1.32
--- loncom/homework/grades.pm:1.31	Tue Jun 25 17:07:56 2002
+++ loncom/homework/grades.pm	Wed Jun 26 17:25:31 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.31 2002/06/25 21:07:56 ng Exp $
+# $Id: grades.pm,v 1.32 2002/06/26 21:25:31 ng Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -116,6 +116,21 @@
     return '';
 }
 
+sub student_gradeStatus {
+  my ($url,$udom,$uname) = @_;
+  my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
+  my %record= &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
+  foreach my $part (&getpartlist($url)) {
+    my ($temp,$part,$type)=split(/_/,$part);
+    if ($type eq 'solved') {
+      my ($status,$foo)=split(/_/,$record{"resource.$part.$type"},2);
+      $status = 'nothing' if ($status eq '');
+      return $type,$status;
+    }
+  }
+  return '';
+}
+
 sub listStudents {
   my ($request) = shift;
   my $cdom=$ENV{"course.$ENV{'request.course.id'}.domain"};
@@ -124,11 +139,12 @@
   $request->print(<<ENDTABLEST);
 <h2><font color="#339933">Show Student Submissions on Assessment</font></h2>
 
-<table border="0"><tr><td bgcolor="#000000">
+<table border="0"><tr><td bgcolor="#777777">
 <table border="0">
-<tr bgcolor="#e6ffff"><td colspan="6"><b>Resource: </b> $ENV{'form.url'}</td></tr>
+<tr bgcolor="#e6ffff"><td colspan="7"><b>Resource: </b> $ENV{'form.url'}</td></tr>
 <tr bgcolor="#e6ffff"><td><b>Username</b></td><td><b>Name</b></td><td><b>Domain</b></td>
-<td><b>View Problem</b></td><td><b>Submissions</b></td><td><b>Action</b></td></tr>
+<td><b>View Problem</b></td><td><b>Submissions</b></td>
+<td><b>Grade Status</b></td><td><b>Action</b></td></tr>
 ENDTABLEST
   my (%classlist) = &getclasslist($cdom,$cnum,'0');
   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
@@ -145,7 +161,7 @@
 	$fullname.=$name{'firstname'}.' '.$name{'middlename'};
       }
       if ( $Apache::grades::viewgrades eq 'F' ) {
-	  $request->print("\n".'<tr bgcolor=#ffffe6>'."<td>$sname</td><td>$fullname</td><td>$sdom</td>".
+	  $request->print("\n".'<tr bgcolor=#ffffe6>'."<td>$sname</td><td>$fullname</td><td align=\"middle\">$sdom</td>".
 			  '<form action="/adm/grades" method="post">');
 	  if ($ENV{'form.url'}) {
 	    $request->print(
@@ -169,6 +185,9 @@
 	  $request->print('<td>'.
 			  '<input type="radio" name="submission" value="last" checked> last '.
 			  '<input type="radio" name="submission" value="all"> all </td>');
+	  my ($type,$status) = &student_gradeStatus($ENV{'form.url'},$cdom,$sname);
+	  $request->print(
+			  '<td align="middle">'.$status.'&nbsp;</td>');
 	  $request->print(
                          '<td><input type="submit" name="submit" value="View/Grade" />');
 	  $request->print('</td></tr></form>');
@@ -237,6 +256,7 @@
   my (@requests) = ('lastname','firstname','middlename','generation');
   my (%name) = &Apache::lonnet::get('environment',\@requests,$domain,$username);
   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$username);
+
   my $fullname=$name{'lastname'}.$name{'generation'};
   if ($fullname =~ /[^\s]+/) { $fullname.=', '; }
   $fullname.=$name{'firstname'}.' '.$name{'middlename'};
@@ -277,6 +297,7 @@
     my ($temp,$part,$type)=split(/_/,$part);
     my $oldscore=$record{"resource.$part.$type"};
     my $newscore=$ENV{"form.GRADE.$student.$part.$type"};
+    print "old=$oldscore:new=$newscore:<br>";
     if ($type eq 'solved') {
       my $update=0;
       if ($newscore eq 'nothing' ) {
@@ -305,7 +326,11 @@
     }
   }
   if ( scalar(keys(%newrecord)) > 0 ) {
-    $newrecord{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
+    $newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
+    print "grader=$newrecord{'resource.regrader'}:<br>records<br>";
+    while (my ($k,$v) = each %newrecord) {
+	print "k=$k:v=$v:<br>\n";
+    }
     &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
 
     $result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
@@ -313,14 +338,40 @@
   return $result;
 }
 
-# -- show submissions of a student, option to grade
+# --------------------------- show submissions of a student, option to grade --------
 #
 sub submission {
   my ($request) = @_;
   $request->print(<<JAVASCRIPT);
-<script type="text/javascript">
-    function changeRadio(title,url) {
+<script type="text/javascript" language="javascript">
+    function updateRadio() {
+	var pts = document.SCORE.GRADE_BOX.value;
+	var radioButton = document.SCORE.radval;
+	var checked  =true;
+	var unchecked=false;
+	var resetbox =false;
+	if (isNaN(pts) || pts < 0) {
+	    alert("A number equal or greater than 0 is expected. Entered value = "+pts);
+	    for (var i=0; i<radioButton.length; i++) {
+		if (radioButton[i].checked) {
+		    document.SCORE.GRADE_BOX.value = i;
+		    resetbox = true;
+		}
+	    }
+	    if (!resetbox) {
+		document.SCORE.GRADE_BOX.value = "";
+	    }
+	    return;
+	}
+
+	for (var i=0; i<radioButton.length; i++) {
+	    radioButton[i].checked=unchecked;
+	    if (pts == i) {
+		radioButton[i].checked=checked;
+	    }
+	}
     }
+
 </script>
 JAVASCRIPT
   my $url=$ENV{'form.url'};
@@ -328,12 +379,8 @@
   if ($ENV{'form.student'} eq '') { &moreinfo($request,"Need student login id"); return ''; }
   my ($uname,$udom) = &finduser($ENV{'form.student'});
   if ($uname eq '') { &moreinfo($request,"Unable to find student"); return ''; }
-  my $symb;
-  if ($ENV{'form.symb'}) {
-    $symb=$ENV{'form.symb'};
-  } else {
-    $symb=&Apache::lonnet::symbread($url);
-  }
+
+  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 ''; }
 #
 # header info
@@ -347,7 +394,7 @@
 						   $ENV{'request.course.id'});
       my $companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
 							 $ENV{'request.course.id'});
-      $result.='<table border="0"><tr><td bgcolor="#000000">';
+      $result.='<table border="0"><tr><td bgcolor="#777777">';
       $result.='<table border="0"><tr><td bgcolor="#e6ffff">';
       $result.='<b>Student\'s view of the problem</b></td></tr><tr><td bgcolor="#ffffff">'.$rendered.'<br />';
       $result.='<b>Correct answer:</b><br />'.$companswer;
@@ -360,54 +407,88 @@
 						      $ENV{'request.course.id'},$last);
   $result.=$answer;
 
-  my $maxpt = &Apache::lonnet::EXT('resource.partid.weight',$symb,$udom,$uname);
+  my $wgt   = &Apache::lonnet::EXT('resource.partid.weight',$symb,$udom,$uname);
   my %record= &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
-  my $score = $record{'resource.0.awarded'}*$maxpt;
+  my $score = $record{'resource.0.awarded'}*$wgt;
 
   $result.= '<form action="/adm/grades" method="post" name="SCORE">'."\n".
     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
-	'<input type="hidden" name="command" value="editgrades" />'."\n".
-         '<table border="0"><tr><td><b>Points</b></td><td>';
+	'<input type="hidden" name="command" value="handgrade" />'."\n";
+
+  $result.='<table border="0"><tr><td><b>Points</b></td><td>';
   my $ctr = 0;
-  while ($ctr<=$maxpt) {
+  while ($ctr<=$wgt) {
       $result.= '<input type="radio" name="radval" '.
-	  'onclick="javascript:SCORE.GRADE_'.$ENV{'form.student'}.'.value='.$ctr.'" value="'.$ctr.'" '.
+#	  'onclick="javascript:writeBox(\'GRADE_'.$ENV{'form.student'}.'\',\''.$ctr.'\')" '.
+	  'onclick="javascript:SCORE.GRADE_BOX.value='.$ctr.'" '.
 	  ($score == $ctr? 'checked':'').' /> '.$ctr."\n";
       $ctr++;
   }
   $result.='</td><td>&nbsp;<b>or</b>&nbsp;</td>';
-  $result.='<td><input type="text" name="GRADE_'.$ENV{'form.student'}.'"'.
-      ($score ne ''? ' value = "'.$score.'"':'').' size="4" /></td>'."\n";  
-  $result.='<td>/'.$maxpt.' (max pts)</td><td>';
+  $result.='<td><input type="text" name="GRADE_BOX"'.
+      ($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
+      'onChange="javascript:updateRadio()" /></td>'."\n";  
+  $result.='<td>/'.$wgt.' (problem weight)</td></tr>';
 
-  foreach my $part (&getpartlist($url)) {
-    my ($temp,$part,$type)=split(/_/,$part);
-    if ($type eq 'solved') {
-      my ($status,$foo)=split(/_/,$record{"resource.$part.$type"},2);
-      $result.="<select name=\"GRADE.$uname:$udom.$part.$type\">\n";
-      my $optsel = '<option>correct</option><option>incorrect</option>'.
-	  '<option>excused</option><option>ungraded</option>'.
-	      '<option>handgraded</option><option>nothing</option>'."\n";
-      $status = 'nothing' if ($status eq '');
-      $optsel =~ s/<option>$status/<option selected="on">$status/;
-      $result.=$optsel;
-      $result.="</select></td></tr>\n";
-    }
-  }
-  $result.='<tr><td colspan="5">The buttons do not work yet.<input type="submit" name="gradeOpt" value="Save N Next" />&nbsp;';
+  $result.='<input type="hidden" name="GRADE_'.$uname.':'.$udom.'" value="handgraded" />'."\n";
+  $result.='<tr><td colspan="5"><input type="submit" name="gradeOpt" value="Save & Next" />&nbsp;';
   $result.='<input type="submit" name="gradeOpt" value="Next" />&nbsp';
   $result.='<input type="submit" name="gradeOpt" value="Previous" />&nbsp';
   $result.='</td><tr></table></form>';
   return $result;
 }
 
+sub processHandGrade {
+  my ($url,$symb,$courseid,$student,@parts) = @_;
+
+  my ($stuname,$domain) = split(/:/,$student);
+  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
+  my %newrecord;
+
+  foreach my $part (@parts) {
+    my ($temp,$part,$type)=split(/_/,$part);
+    my $oldscore=$record{"resource.$part.$type"};
+    my $newscore=($ENV{'form.GRADE_BOX'} ne '' ? $ENV{"form.GRADE_BOX"} : $ENV{'form.radval'});
+    if ($type eq 'solved') {
+      my $update=0;
+      if ($newscore eq 'nothing' ) {
+	if ($oldscore ne '') {
+	  $update=1;
+	  $newscore = '';
+	}
+      } elsif ($oldscore !~ m/^$newscore/) {
+	$update=1;
+	if ($newscore eq 'correct')   { $newscore = 'correct_by_override'; }
+	if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
+	if ($newscore eq 'excused')   { $newscore = 'excused'; }
+	if ($newscore eq 'ungraded')  { $newscore = 'ungraded_attempted'; }
+	if ($newscore eq 'handgraded') { $newscore = 'handgraded_by_grader'; }
+      }
+      if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
+    } else {
+      if ($oldscore ne $newscore) {
+	$newrecord{"resource.$part.$type"}=$newscore;
+    }
+  }
+  if ( scalar(keys(%newrecord)) > 0 ) {
+    $newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
+    print "grader=$newrecord{'resource.regrader'}:<br>records<br>";
+    while (my ($k,$v) = each %newrecord) {
+	print "k=$k:v=$v:<br>\n";
+    }
+    &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
+  }
+  return '';
+}
+
 sub get_symb_and_url {
  my ($request) = @_;
   my $url=$ENV{'form.url'};
   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
-  my $symb=$ENV{'form.symb'};
-  if (!$symb) { $symb=&Apache::lonnet::symbread($url); }
+#  my $symb=$ENV{'form.symb'};
+#  if (!$symb) { $symb=&Apache::lonnet::symbread($url); }
+  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 ''; }
  return ($symb,$url);
 }
@@ -440,7 +521,7 @@
   if (!$symb) {return '';}
 
   my $result='<h2>&nbsp;<font color="#339933">Select a Grading Method</font></h2><br />';
-  $result.='<table width=100% border=0><tr><td bgcolor=#000000>'."\n";
+  $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
   $result.='<table width=100% border=0><tr><td bgcolor=#e6ffff>'."\n";
   $result.='&nbsp;<b>Resource :</b> '.$url.'</td></tr>'."\n";
   $result.='<tr bgcolor=#ffffe6><td>'."\n";
@@ -493,7 +574,7 @@
       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
 	'<input type="hidden" name="command" value="editgrades" />'."\n".
 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
-	    '<table border=0><tr><td bgcolor="#000000">'."\n".
+	    '<table border=0><tr><td bgcolor="#777777">'."\n".
 	     '<table border=0>'."\n".
 	      '<tr bgcolor='.$headerclr.'><td><b>Username</b></td><td><b>Name</b></td><td><b>Domain</b></td>'."\n";
   foreach my $part (@parts) {

--ng1025126731--