--- loncom/homework/optionresponse.pm	2003/04/07 22:28:08	1.74
+++ loncom/homework/optionresponse.pm	2003/07/01 20:12:55	1.79
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: optionresponse.pm,v 1.74 2003/04/07 22:28:08 albertel Exp $
+# $Id: optionresponse.pm,v 1.79 2003/07/01 20:12:55 sakharuk Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -210,24 +210,38 @@ sub end_foilgroup {
 	my %previous=&Apache::response::check_for_previous($responsestr,
 							   $part,$id);
 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
-	my $ad;
-	if ($wrong==0 && $ignored==0) {
-	  $ad='EXACT_ANS';
-	} elsif ($wrong==0 && $right==0) {
-	  #nothing submitted
-	} else {
-	  if ($ignored==0) {
-	    $ad='INCORRECT';
-	  } else {
-	    $ad='MISSING_ANSWER';
-	  }
-	}
 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
-	  $responsestr;
+	    $responsestr;
 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
-	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
-	  $ad;
-	&Apache::response::handle_previous(\%previous,$ad);
+	
+	if (!$Apache::lonhomework::scantronmode) {
+	    my $ad;
+	    if ($wrong==0 && $ignored==0) {
+		$ad='EXACT_ANS';
+	    } elsif ($wrong==0 && $right==0) {
+		#nothing submitted
+	    } else {
+		if ($ignored==0) {
+		    $ad='INCORRECT';
+		} else {
+		    $ad='MISSING_ANSWER';
+		}
+	    }
+	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
+	    &Apache::response::handle_previous(\%previous,$ad);
+	} else {
+	    my $ad;
+	    if ($wrong==0 && $right==0) {
+		#nothing submitted
+	    } else {
+		$ad='ASSIGNED_SCORE';
+	    }
+	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
+	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
+		$right/(scalar(@whichopt));
+	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
+		scalar(@whichopt);
+	}
       }
     }
     &Apache::lonxml::increment_counter(&getfoilcounts($max));
@@ -251,52 +265,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 {
@@ -319,7 +291,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');
@@ -368,6 +340,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*$/) {
@@ -422,7 +395,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 {
@@ -442,7 +416,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++;
 	      }
 	  }
       }
@@ -651,7 +626,7 @@ sub end_foil {
       } 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') {