--- loncom/homework/response.pm	2002/02/15 22:16:04	1.60
+++ loncom/homework/response.pm	2003/04/21 20:59:02	1.72
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # various response type definitons response definition
 #
-# $Id: response.pm,v 1.60 2002/02/15 22:16:04 albertel Exp $
+# $Id: response.pm,v 1.72 2003/04/21 20:59:02 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -33,7 +33,7 @@ package Apache::response;
 use strict;
 
 BEGIN {
-  &Apache::lonxml::register('Apache::response',('responseparam','parameter','caparesponse','numericalresponse','formularesponse','stringresponse','radiobuttonresponse','optionresponse','imageresponse','essayresponse','dataresponse','externalresponse'));
+  &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
 }
 
 sub start_response {
@@ -68,7 +68,10 @@ sub start_hintresponse {
 
 sub end_hintresponse {
   pop @Apache::inputtags::response;
-  %Apache::inputtags::params=@{pop(@Apache::inputtags::paramstack)};
+  if (defined($Apache::inputtags::paramstack[-1])) {
+      %Apache::inputtags::params=
+	  @{ pop(@Apache::inputtags::paramstack) };
+  }
   return '';
 }
 
@@ -91,7 +94,7 @@ sub setrandomnumber {
     $rndseed=$rndseed
       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
   }
-  srand($rndseed);
+  &Math::Random::random_set_seed_from_phrase($rndseed);
   &Apache::lonxml::debug("randseed $rndseed");
   return '';
 }
@@ -172,7 +175,7 @@ sub check_for_previous {
   my ($curresponse,$partid,$id) = @_;
   my %previous;
   $previous{'used'} = 0;
-  foreach my $key (reverse(sort(keys(%Apache::lonhomework::history)))) {
+  foreach my $key (sort(keys(%Apache::lonhomework::history))) {
     if ($key =~ /resource\.$partid\.$id\.submission/) {
       &Apache::lonxml::debug("Trying $key");
       my $pastresponse=$Apache::lonhomework::history{$key};
@@ -183,16 +186,17 @@ sub check_for_previous {
 	  $history=$1;
 	  $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
 	  $previous{'last'}='0';
+	  push(@{ $previous{'version'} },$history);
 	} else {
 	  $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
 	  $previous{'last'}='1';
 	}
 	if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
 	&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
-	last;
       }
     }
   }
+  &Apache::lonhomework::showhash(%previous);
   return %previous;
 }
 
@@ -204,74 +208,10 @@ sub handle_previous {
     } else {
       push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
     }
+    push(@Apache::inputtags::previous_version,$$previous{'version'});
   }
 }
 
-sub start_caparesponse {
-  require Apache::caparesponse; 
-  import Apache::caparesponse; 
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-#  print "\n<br />\nsimple caparesponse\n";
-  return &Apache::caparesponse::start_caparesponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_stringresponse {
-  require Apache::caparesponse;
-  import Apache::caparesponse;
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::caparesponse::start_stringresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_formularesponse {
-  require Apache::caparesponse;
-  import Apache::caparesponse;
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::caparesponse::start_formularesponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_numericalresponse {
-  require Apache::caparesponse; 
-  import Apache::caparesponse; 
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::caparesponse::start_numericalresponse
-    ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_radiobuttonresponse {
-  require Apache::radiobuttonresponse; 
-  import Apache::radiobuttonresponse; 
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::radiobuttonresponse::start_radiobuttonresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_optionresponse {
-  require Apache::optionresponse; 
-  import Apache::optionresponse; 
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::optionresponse::start_optionresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_imageresponse {
-  require Apache::imageresponse; 
-  import Apache::imageresponse; 
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::imageresponse::start_imageresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_essayresponse {
-  require Apache::essayresponse; 
-  import Apache::essayresponse; 
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::essayresponse::start_essayresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
-sub start_externalresponse {
-  require Apache::externalresponse; 
-  import Apache::externalresponse; 
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  return &Apache::externalresponse::start_externalresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
-}
-
 sub view_or_modify {
   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
   my $myself=0;
@@ -310,10 +250,10 @@ sub end_dataresponse {
       my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
       my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
       if ($allowed) {
-	&Apache::response::setup_params('datasubmission');
+	&Apache::response::setup_params('dataresponse');
 	my $partid = $Apache::inputtags::part;
 	my $id = $Apache::inputtags::response['-1'];
-	my $response = $ENV{'form.HWVAL'.$id};
+	my $response = $ENV{'form.HWVAL_'.$id};
 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
 	if ( $response =~ /[^\s]/) {
 	  $Apache::lonhomework::results{"resource.$partid.$id.$name"}=
@@ -358,12 +298,12 @@ sub start_responseparam {
       $result = &Apache::edit::rebuild_tag($token);
       $result.=&Apache::edit::handle_insert();
     }
-  } elsif ($target eq 'grade' || $target eq 'answer' ||
-	   $target eq 'web'   || $target eq 'tex') {
+  } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
+	   $target eq 'tex' || $target eq 'analyze' ) {
     if ($ENV{'request.state'} eq 'construct') {
       my $name    = &Apache::lonxml::get_param('name',$parstack,$safeeval);
       my $default = &Apache::lonxml::get_param('default',$parstack,$safeeval);
-      $Apache::inputtags::params{$name}=$default;
+      if ($name) {$Apache::inputtags::params{$name}=$default;}
     }
   }
   return $result;
@@ -385,11 +325,14 @@ sub end_parameter {
   return $result;
 }
 
+sub reset_params {
+    %Apache::inputtags::params=();
+}
+
 sub setup_params {
   my ($tag) = @_;
 
   if ($ENV{'request.state'} eq 'construct') { return; }
-  %Apache::inputtags::params=();
   my %paramlist=();
   foreach my $key (keys(%Apache::lonnet::packagetab)) {
     if ($key =~ /^$tag/) {
@@ -451,6 +394,67 @@ sub answer_footer {
   return $result;
 }
 
+sub showallfoils {
+  my $return=0;
+  if (defined($ENV{'form.showallfoils'}) &&
+      $ENV{'request.state'} eq 'construct') {
+    $return=1;
+  }
+  return $return;
+}
+
+sub getresponse {
+    my ($temp)=@_;
+    my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
+    my $response;
+    if (!defined($temp)) {
+	$temp=1;
+    } else {
+	$formparm.=":$temp";
+    }
+    my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
+		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
+		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
+		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
+    if ($ENV{'form.submitted'} eq 'scantron') {
+	my $part  = $Apache::inputtags::part;
+	my $id    = $Apache::inputtags::response[-1];
+	$response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
+			 '.answer'};
+	# save bubbled letter for later
+	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
+	    $response;
+	$response = $let_to_num{$response};
+    } else {
+	$response = $ENV{$formparm};
+    }
+    return $response;
+}
+
+sub repetition {
+    my $id = $Apache::inputtags::part;
+    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
+    my $repetition = int $weight/9;
+    if ($weight % 9 != 0) {$repetition++;} 
+    return $repetition;
+}
+
+sub scored_response {
+    my ($part,$id)=@_;
+    my $repetition=&repetition();
+    my $score=0;
+    for (my $i=0;$i<$repetition;$i++) {
+	my $increase=&Apache::response::getresponse($i+1);
+	if ($increase ne '') { $score+=$increase+1; }
+    }
+    my $weight = &Apache::lonnet::EXT("resource.$part.weight");
+    my $pcr=$score/$weight;
+    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
+    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
+	'ASSIGNED_SCORE';
+    return $repetition;
+}
+
 1;
 __END__