--- loncom/homework/response.pm	2003/05/14 13:33:00	1.77
+++ loncom/homework/response.pm	2003/06/23 20:57:41	1.80
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # various response type definitons response definition
 #
-# $Id: response.pm,v 1.77 2003/05/14 13:33:00 albertel Exp $
+# $Id: response.pm,v 1.80 2003/06/23 20:57:41 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -52,7 +52,7 @@ sub start_response {
 }
 
 sub end_response {
-    pop @Apache::inputtags::response;
+    #pop @Apache::inputtags::response;
     @Apache::inputtags::inputlist=();
     return '';
 }
@@ -62,6 +62,7 @@ sub start_hintresponse {
     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
     push (@Apache::inputtags::response,$id);
+    push (@Apache::inputtags::responselist,$id);
     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
     return $id;
 }
@@ -367,13 +368,8 @@ sub answer_header {
 	$result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: '.
                   $Apache::inputtags::part.' \\\\ \hline ';
     } else {
-	if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
-	    $result = '<table border="1"><tr><th>Answer for Part:'.
-		$Apache::inputtags::part. '</th></tr><tr>'."\n";
-	} else {
-	    $result = '<table border="1"><tr><td>Answer for Part:'.
-		$Apache::inputtags::part. '</td>'."\n";
-	}
+	$result = '<table border="1"><tr><td>Answer for Part:'.
+	    $Apache::inputtags::part. '</td>'."\n";
     }
     return $result;
 }
@@ -384,11 +380,7 @@ sub answer_part {
     if ($ENV{'form.answer_output_mode'} eq 'tex') {
 	$result = ' '.$answer.'\\\\ \hline ';
     } else {
-	if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
-	    $result = '<td>'.$answer.'</td>';
-	} else {
-	    $result = '<td>'.$answer.'</td>';
-	}
+	$result = '<td>'.$answer.'</td>';
     }
     return $result;
 }
@@ -399,11 +391,7 @@ sub answer_footer {
     if ($ENV{'form.answer_output_mode'} eq 'tex') {
 	$result = ' \end{tabular} \vskip 0 mm ';
     } else {
-	if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
-	    $result = '</tr></table>';
-	} else {
-	    $result = '</tr></table>';
-	}
+	$result = '</tr></table>';
     }
     return $result;
 }
@@ -469,6 +457,58 @@ sub scored_response {
     return $repetition;
 }
 
+sub whichorder {
+    my ($max,$randomize,$showall,$hash)=@_;
+    #&Apache::lonxml::debug("man $max randomize $randomize");
+    if (!defined(@{ $$hash{'names'} })) { return; }
+    my @names = @{ $$hash{'names'} };
+    my @whichopt =();
+    my (%top,@toplist,%bottom,@bottomlist);
+    if (!($showall || ($randomize eq 'no'))) {
+	my $current=0;
+	foreach my $name (@names) {
+	    $current++;
+	    if ($$hash{"$name.location"} eq 'top') {
+		$top{$name}=$current;
+	    } elsif ($$hash{"$name.location"} eq 'bottom') {
+		$bottom{$name}=$current;
+	    }
+	}
+    }
+    my $topcount=0;
+    my $bottomcount=0;
+    while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
+	   && ($#names > -1)) {
+	#&Apache::lonxml::debug("Have $#whichopt max is $max");
+	my $aopt;
+	if ($showall || ($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;
+	    $topcount++;
+	} elsif ($bottom{$aopt}) {
+	    $bottomlist[$bottom{$aopt}]=$aopt;
+	    $bottomcount++;
+	} 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;
+}
+
 1;
 __END__