--- loncom/homework/optionresponse.pm	2003/04/24 15:49:46	1.76
+++ loncom/homework/optionresponse.pm	2003/10/13 19:25:30	1.89
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: optionresponse.pm,v 1.76 2003/04/24 15:49:46 albertel Exp $
+# $Id: optionresponse.pm,v 1.89 2003/10/13 19:25:30 albertel 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,
@@ -265,52 +267,10 @@ sub getfoilcounts {
 }
 
 sub whichfoils {
-  my ($max,$randomize)=@_;
-  $max = &getfoilcounts($max);
-  &Apache::lonxml::debug("randomize $randomize");
-  if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
-  my @names = @{ $Apache::response::foilgroup{'names'} };
-  my @whichopt =();
-  my (%top,@toplist,%bottom,@bottomlist);
-  if (!(&Apache::response::showallfoils() || ($randomize eq 'no'))) {
-      my $current=0;
-      foreach my $name (@names) {
-	  $current++;
-	  if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
-	      $top{$name}=$current;
-	  } elsif ($Apache::response::foilgroup{$name.'.location'} eq
-		   'bottom') {
-	      $bottom{$name}=$current;
-	  }
-      }
-  }
-  while ((($#whichopt+1) < $max) && ($#names > -1)) {
-    &Apache::lonxml::debug("Have $#whichopt max is $max");
-    my $aopt;
-    if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
-      $aopt=0;
-    } else {
-      $aopt=int(&Math::Random::random_uniform() * ($#names+1));
-    }
-    &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
-    $aopt=splice(@names,$aopt,1);
-    &Apache::lonxml::debug("Picked $aopt");
-    if ($top{$aopt}) {
-	$toplist[$top{$aopt}]=$aopt;
-    } elsif ($bottom{$aopt}) {
-	$bottomlist[$bottom{$aopt}]=$aopt;
-    } else {
-	push (@whichopt,$aopt);
-    }
-  }
-  for (my $i=0;$i<=$#toplist;$i++) {
-      if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
-  }
-  for (my $i=0;$i<=$#bottomlist;$i++) {
-      if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
-  }
-
-  return @whichopt;
+    my ($max,$randomize)=@_;
+    return &Apache::response::whichorder($max,$randomize,
+					 &Apache::response::showallfoils(),
+					 \%Apache::response::foilgroup);
 }
 
 sub displayanswers {
@@ -333,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');
@@ -342,10 +302,8 @@ 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() ) {
     foreach $name (@whichopt) {
 	my $text=$Apache::response::foilgroup{$name.'.text'};
 	if ($text!~/^\s*$/) {
@@ -368,7 +326,8 @@ 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') {
@@ -382,6 +341,7 @@ sub displayfoils {
   } 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*$/) {
@@ -436,7 +396,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 {
@@ -456,7 +417,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++;
 	      }
 	  }
       }
@@ -473,15 +435,19 @@ 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;
 }
 
@@ -635,7 +601,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; }
   }
@@ -655,24 +621,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;
       }