--- loncom/homework/optionresponse.pm	2016/09/14 18:30:06	1.195.2.2
+++ loncom/homework/optionresponse.pm	2015/04/28 13:12:52	1.200
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: optionresponse.pm,v 1.195.2.2 2016/09/14 18:30:06 raeburn Exp $
+# $Id: optionresponse.pm,v 1.200 2015/04/28 13:12:52 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -300,6 +300,10 @@ sub end_foilgroup {
 	my $wrong=0;
 	my $ignored=0;
         my $checked=0;
+        my $corrchkd=0;
+        my $corrunchkd=0;
+        my $incorrchkd=0;
+        my $incorrunchkd=0;
         my ($numrows,$bubbles_per_row);
         if ($Apache::lonhomework::scantronmode) {
             my $numitems = scalar(@opt);
@@ -329,6 +333,10 @@ sub end_foilgroup {
                 }
             }
         }
+        my $part=$Apache::inputtags::part;
+        my $id = $Apache::inputtags::response['-1'];
+        my @relweights;
+        my $nonlenient=&grading_is_nonlenient($part,$id,\@relweights);
 	foreach $name (@whichopt) {
 	  my $response;
           if ($env{'form.submitted'} eq 'scantron') {
@@ -369,15 +377,23 @@ sub end_foilgroup {
             if ($checkboxopt) {
                 if ($response eq $checkboxopt) {
                     $checked++;
+                    if ($grade{$name}) {
+                        $corrchkd ++;
+                    } else {
+                        $incorrchkd ++;
+                    }
+                } else {
+                    if ($grade{$name}) {
+                        $corrunchkd ++;
+                    } else {
+                        $incorrunchkd ++;
+                    }
                 }
-            } 
+            }
 	  } else {
 	    $ignored++;
 	  }
 	}
-	my $part=$Apache::inputtags::part;
-        my $id = $Apache::inputtags::response['-1'];
-        my $nonlenient=&grading_is_nonlenient($part,$id);
 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
 	my %previous=&Apache::response::check_for_previous($responsestr,
@@ -453,8 +469,22 @@ sub end_foilgroup {
 		    $ad='ASSIGNED_SCORE';
 	        }
 	        $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
+                my $awarded;
+                my $totalfoils = $#{ $Apache::response::foilgroup{'names'} }+1;
+                if (($checkboxopt) && ($max >= $totalfoils) && (@relweights > 0)) {
+                    if ($right == scalar(@whichopt)) {
+                        $awarded = 1;
+                    } else {
+                        $awarded = ($corrchkd*$relweights[0] + $corrunchkd*$relweights[1] +
+                                    $incorrchkd*$relweights[2] + $incorrunchkd*$relweights[3])/(scalar(@whichopt));
+                        $awarded = 1 if ($awarded > 1);
+                        $awarded = 0 if ($awarded < 0);
+                    }
+                } else {
+                    $awarded = $right/(scalar(@whichopt));
+                }
 	        $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
-		    $right/(scalar(@whichopt));
+		    $awarded;
 	        $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
 		    scalar(@whichopt);
 	     }
@@ -492,7 +522,7 @@ sub end_foilgroup {
 }
 
 sub grading_is_nonlenient {
-    my ($part,$id) = @_;
+    my ($part,$id,$relweights) = @_;
 # Web mode: we are non-lenient unless told otherwise
     my $defaultparm = 'off';
     my $nonlenient = 1;
@@ -511,9 +541,14 @@ sub grading_is_nonlenient {
     if ($lenientparm eq 'default') {
         $lenientparm = $defaultparm;
     }
-    if ($lenientparm=~/^0|off|no$/i) {
+    if ($lenientparm=~/^(?:0|off|no)$/i) {
         $nonlenient = 1;
-    } elsif ($lenientparm=~/^1|on|yes$/i) {
+    } elsif ($lenientparm=~/^(?:1|on|yes)$/i) {
+        $nonlenient = 0;
+    } elsif ($lenientparm =~ /^\-?[.\d]+,\-?[.\d]+,\-?[.\d]+,\-?[.\d]+$/) {
+        if (ref($relweights) eq 'ARRAY') {
+            @{$relweights} = split(/,/,$lenientparm);
+        }
         $nonlenient = 0;
     }
     if (!$nonlenient) {