--- loncom/homework/optionresponse.pm	2003/05/16 20:16:18	1.77
+++ loncom/homework/optionresponse.pm	2003/10/24 17:56:42	1.91
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: optionresponse.pm,v 1.77 2003/05/16 20:16:18 albertel Exp $
+# $Id: optionresponse.pm,v 1.91 2003/10/24 17:56:42 sakharuk Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -152,9 +152,11 @@ sub end_foilgroup {
   
   my $result;
   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
+    my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
     my $name;
     my @opt;
     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
+    if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
     &Apache::lonxml::debug("Options are $#opt");
     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
@@ -244,7 +246,7 @@ sub end_foilgroup {
 	}
       }
     }
-    &Apache::lonxml::increment_counter(&getfoilcounts($max));
+    if ($target ne 'answer') {&Apache::lonxml::increment_counter(&getfoilcounts($max));}
   } elsif ($target eq 'edit') {
     $result.=&Apache::edit::end_table();
   }
@@ -291,7 +293,7 @@ sub displayfoils {
   my @names = @{ $Apache::response::foilgroup{'names'} };
   my @truelist;
   my @falselist;
-  my $result;
+  my $result;  
   my $name;
   my $displayoptionintex=0;
   my @alphabet = ('A'..'Z');
@@ -300,12 +302,13 @@ sub displayfoils {
   my $id=$Apache::inputtags::response[-1];
   my $break;
   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
-  my $status=$Apache::inputtags::status[-1];
-  if (
-      ($target ne 'tex') &&
-      (($solved =~ /^correct/) || ($status eq 'SHOW_ANSWER')) ) {
+  if ( ($target ne 'tex') &&
+       &Apache::response::show_answer() ) {
+    my $temp=1;
     foreach $name (@whichopt) {
 	my $text=$Apache::response::foilgroup{$name.'.text'};
+	my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
+	my $lastopt=$lastresponse{$name};
 	if ($text!~/^\s*$/) {
 	    if ($target eq 'tex') {
 		$break='\vskip 0 mm ';
@@ -326,20 +329,23 @@ sub displayfoils {
 	    $result.=$text."\n";
       } elsif ($target eq 'tex') {
 	  $Apache::response::foilgroup{$name.'.text'}=~s/\\item//;
- 	  $result .='\item \textit{'.$Apache::response::foilgroup{$name.'.value'}.'}'.
+	  if ($max>1) {$result .='\item ';}
+ 	  $result .=' \textit{'.$Apache::response::foilgroup{$name.'.value'}.'}'.
 	      ":".$Apache::response::foilgroup{$name.'.text'}."\n";
       }
       if ($Apache::lonhomework::type eq 'exam') {
 	if ($target ne 'tex') {
-	  $result.=&webbubbles(\@opt,\@alphabet);
+	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
         } else {
 	  $result.=&bubbles(\@alphabet,\@opt);
         }
       }
+      $temp++;
     }
   } else {
     my $temp=1;
     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
+    my $internal_counter=$Apache::lonxml::counter;
     foreach $name (@whichopt) {
       my $text=$Apache::response::foilgroup{$name.'.text'};
       if ($text!~/^\s*$/) {
@@ -371,7 +377,7 @@ sub displayfoils {
 	  }
 	  $result.=$break.$text."\n";
 	  if ($Apache::lonhomework::type eq 'exam') {
-	    $result.=&webbubbles(\@opt,\@alphabet,$temp);
+	    $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
 	  }
 	  $temp++;
       } else {
@@ -394,7 +400,8 @@ sub displayfoils {
 		  }
 	      }
 	      if ($Apache::lonhomework::type eq 'exam') {
-		  $result.='\vskip -2 mm\parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]\parbox{\textwidth - 5 mm}{'.&bubbles(\@alphabet,\@opt).'}\end{enumerate}} \vskip -9 mm \strut ';
+		  $result.='\vskip -2 mm\parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]\parbox{\textwidth - 5 mm}{'.&bubbles(\@alphabet,\@opt).'}\end{enumerate}} \vskip -9 mm \strut ';
+		  $internal_counter++;
 	      }
 	      $displayoptionintex=1;
 	  } else {
@@ -414,7 +421,8 @@ sub displayfoils {
 		  }
 	      }
 	      if ($Apache::lonhomework::type eq 'exam') {
-		  $result.='\vskip -2 mm \parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]\parbox{\textwidth - 5 mm}{'.&bubbles(\@alphabet,\@opt).'}\end{enumerate}} \vskip -9 mm \strut ';
+		  $result.='\vskip -2 mm \parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]\parbox{\textwidth - 5 mm}{'.&bubbles(\@alphabet,\@opt).'}\end{enumerate}} \vskip -9 mm \strut ';
+		  $internal_counter++;
 	      }
 	  }
       }
@@ -431,33 +439,43 @@ sub displayfoils {
 sub optionlist_correction {
 
     my $texoptionlist = shift;
-    $texoptionlist =~ s/<option><\/option>/\\item \[\] Possible answers are:/;
-    $texoptionlist =~ s/<option>/\{\\bf /g;
-    $texoptionlist =~ s/<option selected="on">/\{\\bf /g;
-    $texoptionlist =~ s/<\/option>/\},/g;
-    $texoptionlist =~ s/,$/\./g;
-    $texoptionlist =~ s/>/\$>\$/g;
-    $texoptionlist =~ s/</\$<\$/g;
-    $texoptionlist =~ s/=/\$=\$/g;
-    $texoptionlist =~ s/\^(\d+)/<m>\$$1\$<\/m>/g;
+    if ($texoptionlist=~/<option selected/ or $texoptionlist=~/<option>[^<]+<\/option>/) {
+	$texoptionlist =~ s/<option><\/option>/\\item \[\] Choices: /;
+	$texoptionlist =~ s/<option>/\{\\bf /g;
+	$texoptionlist =~ s/<option selected="on">/\{\\bf /g;
+	$texoptionlist =~ s/<\/option>/\},/g;
+	$texoptionlist =~ s/,$/\./g;
+	$texoptionlist =~ s/>/\$>\$/g;
+	$texoptionlist =~ s/</\$<\$/g;
+	$texoptionlist =~ s/=/\$=\$/g;
+	$texoptionlist =~ s/\^(\d+)/<m>\$$1\$<\/m>/g;
+    } else {
+	$texoptionlist =~ s/<option><\/option>/\\item \[\] \\vskip -5 mm/;
+    }
     return $texoptionlist;
 }
 
 
 sub webbubbles {
 
-    my ($ropt,$ralphabet,$temp)=@_;
+    my ($ropt,$ralphabet,$temp,$lastopt)=@_;
     my @opt=@$ropt; 
     my @alphabet=@$ralphabet;
     my $result='';
-	my $number_of_bubbles = $#opt + 1;
-	$result.= '<table border="1"><tr>';
-	for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
-	   $result.='<td><input type="radio" name="HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp.
-                    '" value='.$opt[$ind].'>'.$alphabet[$ind].': '.$opt[$ind].'</td>';
+    my $number_of_bubbles = $#opt + 1;
+    $result.= '<table border="1"><tr>';
+    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
+	my $checked='';
+	if ($lastopt eq $opt[$ind]) {
+	    $checked=' checked="on" ';
 	}
-	$result.='</tr></table>';
-	return $result;
+	$result.='<td><input type="radio" name="HWVAL_'.
+	    $Apache::inputtags::response['-1'].':'.$temp.
+	    '" value="'.$opt[$ind].'" '.$checked.' />'.$alphabet[$ind].': '.
+	    $opt[$ind].'</td>';
+    }
+    $result.='</tr></table>';
+    return $result;
 }
 
 
@@ -593,7 +611,7 @@ sub end_foil {
   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   my $text ='';
   my $result = '';
-  if ($target eq 'web' || $target eq 'tex') { 
+  if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
       $text=&Apache::lonxml::endredirection;
       if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') { $text = '\vspace*{-2 mm}\item '.$text; }
   }
@@ -613,24 +631,16 @@ sub end_foil {
 	if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
 	    $Apache::response::conceptgroup{"$name.text"} = '\vskip 4 mm $\triangleright$ '.$text;
 	} else {
-	    if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
-		$Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
-	    } else {
-		$Apache::response::conceptgroup{"$name.text"} = $text;
-	    }
+	    $Apache::response::conceptgroup{"$name.text"} = $text;
 	}
 	$Apache::response::conceptgroup{"$name.location"} = $location;
       } else {
 	push @{ $Apache::response::foilgroup{'names'} }, $name;
 	$Apache::response::foilgroup{"$name.value"} = $value;
-	if ($Apache::lonhomework::type eq 'exam') {
+	if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
 	    $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
 	} else {
-	    if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
-		$Apache::response::foilgroup{"$name.text"} = ' $\triangleright$ '.$text;
-	    } else {
-		$Apache::response::foilgroup{"$name.text"} = $text;
-	    }
+	    $Apache::response::foilgroup{"$name.text"} = $text;
 	}
 	$Apache::response::foilgroup{"$name.location"} = $location;
       }