--- loncom/homework/optionresponse.pm	2010/09/05 20:46:58	1.172
+++ loncom/homework/optionresponse.pm	2011/06/07 17:27:37	1.176
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: optionresponse.pm,v 1.172 2010/09/05 20:46:58 www Exp $
+# $Id: optionresponse.pm,v 1.176 2011/06/07 17:27:37 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -103,7 +103,7 @@ sub start_foilgroup {
   my $result='';
   %Apache::response::foilgroup=();
   $Apache::optionresponse::conceptgroup=0;
-  &Apache::response::pushrandomnumber();
+  &Apache::response::pushrandomnumber(undef,$target);
   if ($target eq 'edit') {
     my $optionlist="<option></option>\n";
     my $option;
@@ -225,7 +225,26 @@ sub end_foilgroup {
 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
     } elsif ( $target eq 'grade') {
+
+      my $nonlenient=0;
+      my $part=$Apache::inputtags::part;
+      my $lenientparm=&Apache::lonnet::EXT("resource.$part.lenient");
+
+      if ($Apache::lonhomework::scantronmode) {
+         $nonlenient=0;
+# Grading an exam: we are grading lenient unless told not to
+         if ($lenientparm=~/^0|off|no$/i) {
+            $nonlenient=1;
+         }
+      } else {
+# Web mode: we are non-lenient unless told to
+         $nonlenient=1;
+         if ($lenientparm=~/^1|on|yes$/i) {
+            $nonlenient=0;
+         }
+      }
       if ( &Apache::response::submitted()) {
+
 	my @whichopt = &whichfoils($max,$randomize);
 	my $temp=1;my $name;
 	my %responsehash;
@@ -262,7 +281,9 @@ sub end_foilgroup {
 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
 	    $responsestr;
 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
-	
+        if ($Apache::lonhomework::type eq 'randomizetry') {
+            $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
+        }
 	if (($Apache::lonhomework::type eq 'survey') ||
             ($Apache::lonhomework::type eq 'surveycred') ||
             ($Apache::lonhomework::type eq 'anonsurvey') ||
@@ -284,7 +305,10 @@ sub end_foilgroup {
 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
 		&Apache::response::handle_previous(\%previous,$ad);
 	    }
-	} elsif (!$Apache::lonhomework::scantronmode) {
+	} elsif ($nonlenient) {
+#
+# Non-lenient mode. All right or all wrong
+#
 	    my $ad;
 	    if ($wrong==0 && $ignored==0) {
 		$ad='EXACT_ANS';
@@ -300,6 +324,9 @@ sub end_foilgroup {
 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
 	    &Apache::response::handle_previous(\%previous,$ad);
 	} else {
+#
+# This is lenient mode
+#
 	    my $ad;
 	    if ($wrong==0 && $right==0) {
 		#nothing submitted only assign a score if we 
@@ -433,7 +460,6 @@ sub displayfoils {
   my $part=$Apache::inputtags::part;
   my $id=$Apache::inputtags::response[-1];
   my $break;
-  my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
   if ( ($target ne 'tex') &&
        &Apache::response::show_answer() ) {
     my $temp=1;
@@ -469,7 +495,19 @@ sub displayfoils {
     }
   } else {
     my $temp=1;
-    my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
+    my %lastresponse;
+    my $newvariation;
+    if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
+        ($Apache::lonhomework::type eq 'randomizetry')) &&
+        ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
+        if ($env{'form.'.$part.'.rndseed'} ne
+            $Apache::lonhomework::history{"resource.$part.rndseed"}) {
+            $newvariation = 1;
+        }
+    }
+    unless ($newvariation) {
+        %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
+    }
     my $internal_counter=$Apache::lonxml::counter;
     my $checkboxopt=&check_box_opt($target,$checkboxvalue,@opt);
     if ($checkboxopt) {
@@ -610,9 +648,12 @@ sub displayfoils {
   }
 
   if ($target eq 'web') {
-      &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,
-						[\@whichopt,
-						 'submissiongrading']);
+      my $data = [\@whichopt,'submissiongrading'];
+      my $questiontype;
+      if ($Apache::lonhomework::type eq 'randomizetry') {
+          $questiontype = $Apache::lonhomework::type,
+      }
+      &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,$data,$questiontype);
   }
 
   if ($target ne 'tex') {
@@ -809,6 +850,9 @@ sub end_foil {
 	  &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
 	  $name=$Apache::lonxml::curdepth;
       }
+      if ($name eq "0") {
+          &Apache::lonxml::error(&mt('Foil name [_1] is not supported. Please choose another name.','<b><tt>'.$name.'</tt></b>'));
+      }
       &Apache::lonxml::debug("Using a name of :$name:");
       if (defined($Apache::response::foilnames{$name})) {
 	  &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));