--- loncom/homework/response.pm	2004/05/07 17:20:19	1.98
+++ loncom/homework/response.pm	2004/12/23 22:53:41	1.112
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # various response type definitons response definition
 #
-# $Id: response.pm,v 1.98 2004/05/07 17:20:19 albertel Exp $
+# $Id: response.pm,v 1.112 2004/12/23 22:53:41 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -46,7 +46,8 @@ sub start_response {
     push (@Apache::inputtags::response,$id);
     push (@Apache::inputtags::responselist,$id);
     @Apache::inputtags::inputlist=();
-    if ($Apache::inputtags::part eq '') {
+    if ($Apache::inputtags::part eq '' && 
+	!$Apache::lonhomework::ignore_response_errors) {
 	&Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
     }
     if ($Apache::inputtags::response_with_no_part &&
@@ -84,31 +85,74 @@ sub end_hintresponse {
     return '';
 }
 
-# used by response to set the non-safe space random number generator to something
-# that is stable and unique based on the part number and response number
+my @randomseeds;
+sub pushrandomnumber {
+    my $rand_alg=&Apache::lonnet::get_rand_alg();
+    if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
+	$rand_alg eq '64bit2') {
+	# do nothing
+    } else {
+	my @seed=&Math::Random::random_get_seed();
+	push (@randomseeds,\@seed);
+    }
+    &Apache::response::setrandomnumber();
+}
+sub poprandomnumber {
+    my $rand_alg=&Apache::lonnet::get_rand_alg();
+    if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
+	$rand_alg eq '64bit2') {
+	return;
+    }
+    my $seed=pop(@randomseeds);
+    if ($seed) {
+	&Math::Random::random_set_seed(@$seed);
+    } else {
+	&Apache::lonxml::error("Unable to restore random algorithm.");
+    }
+}
 sub setrandomnumber {
     my $rndseed;
     $rndseed=&Apache::structuretags::setup_rndseed();
     if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
     &Apache::lonxml::debug("randseed $rndseed");
     #  $rndseed=unpack("%32i",$rndseed);
-    my $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
-    if (defined($Apache::inputtags::response['-1'])) {
-	$rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
-    }
-    if ($rndseed =~/,/) {
-	{
-	    use integer;
-	    my ($num1,$num2)=split(/,/,$rndseed);
-	    $num1+=$rndmod;
-	    $num2+=$rndmod;
-	    $rndseed="$num1,$num2";
+    my $rand_alg=&Apache::lonnet::get_rand_alg();
+    my $rndmod;
+    if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
+	$rand_alg eq '64bit2') {
+	$rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
+	if (defined($Apache::inputtags::response[-1])) {
+	    $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
+	}
+    } elsif ($rand_alg eq '64bit3') {
+	$rndmod=(&Apache::lonnet::numval2($Apache::inputtags::part) << 10);
+	if (defined($Apache::inputtags::response[-1])) {
+	    $rndmod+=&Apache::lonnet::numval2($Apache::inputtags::response[-1]);
 	}
     } else {
+	my $shift=(4*scalar(@Apache::inputtags::responselist))%30;
+	$rndmod=(&Apache::lonnet::numval3($Apache::inputtags::part) << (($shift+15)%30));
+	if (defined($Apache::inputtags::response[-1])) {
+	    $rndmod+=(&Apache::lonnet::numval3($Apache::inputtags::response[-1]) << $shift );
+	}
+    }
+    if ($rndseed =~/([,:])/) {
+	my $char=$1;
+	use integer;
+	my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
+	$num1+=$rndmod;
+	$num2+=$rndmod;
+	if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
+	$rndseed=$num1.$char.$num2;
+    } else {
 	$rndseed+=$rndmod;
+	if($Apache::lonnet::_64bit) {
+	    use integer;
+	    $rndseed=(($rndseed<<32)>>32);
+	}
     }
+    &Apache::lonxml::debug("randseed $rndmod $rndseed");
     &Apache::lonnet::setup_random_from_rndseed($rndseed);
-    &Apache::lonxml::debug("randseed $rndseed");
     return '';
 }
 
@@ -188,9 +232,16 @@ sub meta_part_order {
     if (@Apache::inputtags::partlist) {
 	my @parts=@Apache::inputtags::partlist;
 	shift(@parts);
-	return '<partorder>'.join(',',@parts).'</partorder>';
+	return '<partorder>'.join(',',@parts).'</partorder>'."\n";
     } else {
-	return '<partorder>0</partorder>';
+	return '<partorder>0</partorder>'."\n";
+    }
+}
+
+sub meta_response_order {
+    if (@Apache::inputtags::responselist) {
+	return '<responseorder>'.join(',',@Apache::inputtags::responselist).
+	    '</responseorder>'."\n";
     }
 }
 
@@ -228,10 +279,11 @@ sub handle_previous {
     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
 	if ($$previous{'last'}) {
 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
-	} else {
+	    push(@Apache::inputtags::previous_version,$$previous{'version'});
+	} elsif ($Apache::lonhomework::type ne 'survey') {
 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
+	    push(@Apache::inputtags::previous_version,$$previous{'version'});
 	}
-	push(@Apache::inputtags::previous_version,$$previous{'version'});
     }
 }
 
@@ -460,12 +512,15 @@ sub answer_footer {
 }
 
 sub showallfoils {
-    my $return=0;
-    if (defined($ENV{'form.showallfoils'}) &&
-	$ENV{'request.state'} eq 'construct') {
-	$return=1;
+    if (defined($ENV{'form.showallfoils'})) {
+	my ($symb)=&Apache::lonxml::whichuser();
+	if ($ENV{'request.state'} eq 'construct' || 
+	    ($ENV{'user.adv'} && $symb eq '')) {
+	    return 1;
+	}
     }
-    return $return;
+    if ($Apache::lonhomework::type eq 'survey') { return 1; }
+    return 0;
 }
 
 sub getresponse {
@@ -490,7 +545,11 @@ sub getresponse {
 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
 	    $response;
 	if ($resulttype ne 'letter') {
-	    $response = $let_to_num{$response};
+	    if ($resulttype eq 'A is 1') {
+		$response = $let_to_num{$response}+1;
+	    } else {
+		$response = $let_to_num{$response};
+	    }
 	}
     } else {
 	$response = $ENV{$formparm};
@@ -600,7 +659,7 @@ sub analyze_store_foilgroup {
 sub check_if_computed {
     my ($token,$parstack,$safeeval,$name)=@_;
     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
-    if ($value ne $token->[2]{$name}) {
+    if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
     }