--- loncom/homework/response.pm	2011/06/06 17:43:51	1.224
+++ loncom/homework/response.pm	2011/12/15 01:21:28	1.230
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # various response type definitons response definition
 #
-# $Id: response.pm,v 1.224 2011/06/06 17:43:51 www Exp $
+# $Id: response.pm,v 1.230 2011/12/15 01:21:28 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -53,6 +53,7 @@ package Apache::response;
 use strict;
 use Apache::lonlocal;
 use Apache::lonnet;
+use Apache::inputtags();
 use Apache::lonmaxima();
 use Apache::lonr();
 
@@ -322,7 +323,7 @@ sub check_for_previous {
                         next if (($questiontype eq 'randomizetry') &&
                              ($curr_rndseed ne $Apache::lonhomework::history{"$history:resource.$partid.rndseed"}));
                         push (@{$previous{'versionci'}},$history);
-                        $previous{'awardci'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
+                        $previous{'awardci'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                         $previous{'usedci'} = 1;
                     }
                 }
@@ -490,12 +491,12 @@ sub end_customresponse {
 	$result .= &Apache::response::answer_footer('customresponse');
     }
     if ($target eq 'web') {
-	&setup_prior_tries_hash(\&format_prior_response_math);
+	&setup_prior_tries_hash(\&format_prior_response_custom);
     }
     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
 	$target eq 'tex' || $target eq 'analyze') {
-	&Apache::lonxml::increment_counter(&Apache::response::repetition(),
-					   "$part.$id");
+        my $repetition = &repetition();
+	&Apache::lonxml::increment_counter($repetition,"$part.$id");
 	if ($target eq 'analyze') {
             $Apache::lonhomework::analyze{"$part.$id.type"} = 'customresponse';
 	    &Apache::lonhomework::set_bubble_lines();
@@ -511,6 +512,9 @@ sub end_customresponse {
 
 sub format_prior_response_custom {
     my ($mode,$answer) =@_;
+    if (ref($answer) eq 'ARRAY') {
+        $answer = '('.join(', ', @{ $answer }).')';
+    }
     return '<span class="LC_prior_custom">'.
 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
 }
@@ -1003,16 +1007,18 @@ sub getresponse {
 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
 	    $response;
 	if ($resulttype ne 'letter') {
-	    if ($resulttype eq 'A is 1') {
-		$response = $let_to_num{$response}+1;
-	    } else {
-		$response = $let_to_num{$response};
+            $response = $let_to_num{$response};
+            if ($resulttype eq 'A is 1') {
+                if ($response ne "") {
+                    $response = $response+1;
+                }
 	    }
 	    if ($response ne "") {
 		$response += $line * $bubbles_per_line;
 	    }
 	} else {
 	    if ($response ne "") {
+                my $raw = $response;
 		$response = chr(ord($response) + $line * $bubbles_per_line);
 	    }
 	}
@@ -1032,8 +1038,17 @@ sub getresponse {
 
 =item &repetition();
 
-Returns the number of lines that are required to encode the weight.
-(Currently expects that there are 10 bubbles per line)
+In scalar context:
+
+returns: the number of lines that are required to encode the weight.
+(Default is for 10 bubbles per bubblesheet item; other (integer) 
+values can be specified by using a custom Bubblesheet format file 
+with an eighteenth entry (BubblesPerRow) set to the integer 
+appropriate for the bubblesheets which will be used to assign weights.
+
+In array context:
+ 
+returns: number of lines required to encode weight, and bubbles/line.
 
 =cut
 
@@ -1041,9 +1056,25 @@ sub repetition {
     my $id = $Apache::inputtags::part;
     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
     if (!defined($weight) || ($weight eq '')) { $weight=1; }
-    my $repetition = int($weight/10);
-    if ($weight % 10 != 0) { $repetition++; } 
+    my $bubbles_per_row;
+    if (($env{'form.bubbles_per_row'} =~ /^\d+$/) && 
+        ($env{'form.bubbles_per_row'} > 0)) {
+        $bubbles_per_row = $env{'form.bubbles_per_row'};
+    } else {
+        $bubbles_per_row = 10;
+    }
+    my $denominator = $bubbles_per_row;
+    if (($env{'form.scantron_lastbubblepoints'} == 0) && 
+        ($bubbles_per_row > 1)) {
+        $denominator = $bubbles_per_row - 1;
+    } 
+    my $repetition = int($weight/$denominator);
+    if ($weight % $denominator != 0) { $repetition++; } 
+    if (wantarray) {
+        return ($repetition,$bubbles_per_row);
+    }
     return $repetition;
+
 }
 
 =pod
@@ -1073,11 +1104,24 @@ Arguments
 sub scored_response {
     my ($part,$id)=@_;
     my $repetition=&repetition();
+    my $bubbles_per_row;
+    if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
+        ($env{'form.bubbles_per_row'} > 0)) {
+        $bubbles_per_row = $env{'form.bubbles_per_row'};
+    } else {
+        $bubbles_per_row = 10;
+    }
     my $score=0;
     for (my $i=0;$i<$repetition;$i++) {
-	# A is 1, B is 2, etc. (get response return 0-9 and then we add 1)
+	# A is 1, B is 2, etc.
 	my $increase=&Apache::response::getresponse($i+1);
-	if ($increase ne '') { $score+=$increase+1; }
+        unless (($increase == $bubbles_per_row-1) &&
+                ($env{'form.scantron_lastbubblepoints'} == 0)) {
+            # (get response return 0-9 and then we add 1)
+            if ($increase ne '') {
+                $score+=$increase+1;
+            }
+        }
     }
     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
@@ -1141,11 +1185,17 @@ sub whichorder {
 
 sub show_answer {
     my $part   = $Apache::inputtags::part;
-    my $award  = $Apache::lonhomework::history{"resource.$part.awarded"};
+    my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
     my $status = $Apache::inputtags::status[-1];
-    return  ( ($award ==1
-	       && &Apache::lonhomework::show_problem_status())
-	      || $status eq "SHOW_ANSWER");
+    my $canshow = 0;
+    if ($award =~ /^correct/) {
+        if (($Apache::lonhomework::history{"resource.$part.awarded"} >= 1) ||
+            (&Apache::lonnet::EXT("resource.$part.retrypartial") !~/^1|on|yes$/)) {
+            $canshow = 1;
+        }   
+    }
+    return  (($canshow && &Apache::lonhomework::show_problem_status()) 
+	     || $status eq "SHOW_ANSWER");
 }
 
 sub analyze_store_foilgroup {