--- loncom/homework/radiobuttonresponse.pm	2011/06/07 17:27:37	1.149
+++ loncom/homework/radiobuttonresponse.pm	2011/09/13 21:42:58	1.151
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # mutliple choice style responses
 #
-# $Id: radiobuttonresponse.pm,v 1.149 2011/06/07 17:27:37 www Exp $
+# $Id: radiobuttonresponse.pm,v 1.151 2011/09/13 21:42:58 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -138,14 +138,10 @@ sub end_radiobuttonresponse {
 %Apache::response::foilgroup=();
 sub start_foilgroup {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-    my $result;
     %Apache::response::foilgroup=();
     $Apache::radiobuttonresponse::conceptgroup=0;
     &Apache::response::pushrandomnumber(undef,$target);
-    if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
-	$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
-    }
-    return $result;
+    return;
 }
 
 sub storesurvey {
@@ -222,15 +218,10 @@ sub end_foilgroup {
 
     my $result;
     my $bubble_lines;
-    my $bubbles_per_line;
     my $answer_count;
     my $id   = $Apache::inputtags::response['-1'];
     my $part = $Apache::inputtags::part;
-    $bubbles_per_line = 
-	&Apache::response::get_response_param($Apache::inputtags::part."_$id",
-					      'numbubbles',
-					      $default_bubbles_per_line);
-
+    my $bubbles_per_line = &getbubblesnum($part,$id);
 
     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
 	$target eq 'tex' || $target eq 'analyze') {
@@ -260,7 +251,7 @@ sub end_foilgroup {
 	    $answer_count = scalar(@shown);
 
 	    if ($target eq 'web' || $target eq 'tex') {
-		$result=&displayfoils($target,
+                $result=&displayfoils($target,
 				      $answer, \@shown,
 				      $direction,
 				      $bubbles_per_line);
@@ -295,6 +286,20 @@ sub end_foilgroup {
     return $result;
 }
 
+sub getbubblesnum {
+    my ($part,$id) = @_;
+    my $bubbles_per_line;
+    my $default_numbubbles = $default_bubbles_per_line;
+    if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
+        ($env{'form.bubbles_per_row'} > 0)) {
+        $default_numbubbles = $env{'form.bubbles_per_row'};
+    }
+    $bubbles_per_line =
+        &Apache::response::get_response_param($part."_$id",'numbubbles',
+                                              $default_numbubbles);
+    return $bubbles_per_line;
+}
+
 sub getfoilcounts {
     my @names;
     my $truecnt=0;
@@ -555,8 +560,12 @@ sub whichfoils {
 	    $dosplice=0;
 	} else {
 	    if ($topcount>0 || $bottomcount>0) {
-		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
-		    + $topcount;
+                my $inc = 1;
+                if (($bottomcount > 0) && ($Apache::lonhomework::type ne 'exam')) {
+                    $inc = 2;
+                }
+                $answer=int(&Math::Random::random_uniform() * ($#whichfalse+$inc))
+                        + $topcount;
 	    }
 	}
 	&Apache::lonxml::debug("Answer now wants $answer");
@@ -644,6 +653,30 @@ sub displayfoils {
 	if ($target ne 'tex' && $direction eq 'horizontal') {
 	    $result.="<table><tr>";
 	}
+        my $numlines;
+        if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
+            my $numitems = scalar(@{ $whichfoils });
+            $numlines = int($numitems/$bubbles_per_line);
+            if (($numitems % $bubbles_per_line) != 0) {
+                $numlines ++;
+            }
+            if ($numlines < 1) {
+                $numlines = 1;
+            }
+            if ($numlines > 1) {
+                my $linetext;
+                for (my $i=0; $i<$numlines; $i++) {
+                    $linetext .= $Apache::lonxml::counter+$i.', ';
+                }
+                $linetext =~ s/,\s$//;
+                $result .= '\item[\small {\textbf{'.$linetext.'}}]'.
+                           ' {\footnotesize '.
+                           &mt('(Bubble once in [_1] lines)',$numlines).
+                           '} \hspace*{\fill} \\\\';
+            } else {
+                $result .= '\item[\textbf{'.$Apache::lonxml::counter.'}.]';
+            }
+        }
 	foreach my $name (@{ $whichfoils }) {
 	    if ($target ne 'tex') {
 		if ($direction eq 'horizontal') {
@@ -667,9 +700,12 @@ sub displayfoils {
 			$line++;
 			$i = 0;
 			$bubble_number = 0;
-			$result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
 		    }
-		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
+                    my $identifier;
+                    if ($numlines > 1) {
+                        $identifier = $Apache::lonxml::counter+$line;
+                    }
+                    $result .= '{\small \textbf{'.$identifier.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
 		    $i++;
 		    $bubble_number++;
 		} else {
@@ -848,6 +884,7 @@ sub insert_foil {
 <endouttext />
 </foil>';
 }
+
 1;
 __END__