--- loncom/homework/chemresponse.pm	2006/12/04 21:23:01	1.71
+++ loncom/homework/chemresponse.pm	2008/03/12 02:46:52	1.78
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # chemical equation style response
 #
-# $Id: chemresponse.pm,v 1.71 2006/12/04 21:23:01 albertel Exp $
+# $Id: chemresponse.pm,v 1.78 2008/03/12 02:46:52 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -186,10 +186,10 @@ sub jme_img {
     my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
     if ($options =~ /border/) { $result.= ' border="1"'; }
     $result.=' />';
-    &Apache::lonnet::appenv('cgi.'.$id.'.JME'   =>
-			    &escape($jme),
-			    'cgi.'.$id.'.PNG'   => 1,
-			    'cgi.'.$id.'.WIDTH' => $width);
+    &Apache::lonnet::appenv({'cgi.'.$id.'.JME'   =>
+			     &escape($jme),
+			     'cgi.'.$id.'.PNG'   => 1,
+			     'cgi.'.$id.'.WIDTH' => $width});
     return $result;
 }
 
@@ -327,15 +327,32 @@ sub end_organicresponse {
 	}
 	$result.=&Apache::response::answer_footer('organicresponse');
     }
+    if ($target eq 'web') {
+	&Apache::response::setup_prior_tries_hash(\&format_prior_answer_organic,
+						  ['molecule'])
+    }
 
     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
 	$target eq 'tex' || $target eq 'analyze') {
-	&Apache::lonxml::increment_counter(&Apache::response::repetition());
+	&Apache::lonxml::increment_counter(&Apache::response::repetition(), 
+					   "$partid.$id"); # part.response
+	if ($target eq 'analyze') {
+            $Apache::lonhomework::analyze{"$partid.$id.type"} = 'organicresponse';
+	    &Apache::lonhomework::set_bubble_lines();
+	}
     }
     &Apache::response::end_response();
     return $result;
 }
 
+sub format_prior_answer_organic {
+    my ($mode,$answer,$other_data) = @_;
+    my $result=&mt('Smile representation: "[_1]"','<tt>'.$answer.'</tt>');
+    my $jme=$other_data->[0];
+    $result.=&jme_img($jme,$answer,400);
+    return $result;
+}
+
 sub start_organicstructure {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result;
@@ -348,9 +365,9 @@ sub start_organicstructure {
 	if ($options =~ /border/) { $result.= ' border="1"'; }
 	$result.=' />';
 	&Apache::lonnet::appenv(
-            'cgi.'.$id.'.JME'   => &escape($molecule),
-	    'cgi.'.$id.'.PNG' => 1,
-	    'cgi.'.$id.'.WIDTH' => $width );
+            {'cgi.'.$id.'.JME'   => &escape($molecule),
+	     'cgi.'.$id.'.PNG' => 1,
+	     'cgi.'.$id.'.WIDTH' => $width});
     } elsif ($target eq 'tex') {
 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
 	my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
@@ -364,9 +381,9 @@ sub start_organicstructure {
 	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
 	my $id=$filename;
 	&Apache::lonnet::appenv(
-		     'cgi.'.$id.'.JME'   => &escape($molecule),
-		     'cgi.'.$id.'.PS' => 1,
-		     'cgi.'.$id.'.WIDTH' => $texwidth );
+		     {'cgi.'.$id.'.JME'   => &escape($molecule),
+		      'cgi.'.$id.'.PS' => 1,
+		      'cgi.'.$id.'.WIDTH' => $texwidth});
 	$id=&escape($id);
 	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
 	if ($options =~ /border/) { $result.= '\fbox{'; }
@@ -542,15 +559,28 @@ sub end_reactionresponse {
 	}
 	$result.=&Apache::response::answer_footer('reactionresponse');
     }
+    if ($target eq 'web') {
+	&Apache::response::setup_prior_tries_hash(\&format_prior_response_reaction);
+    }
 
     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
 	$target eq 'tex' || $target eq 'analyze') {
-	&Apache::lonxml::increment_counter(&Apache::response::repetition());
+	&Apache::lonxml::increment_counter(&Apache::response::repetition(), "$partid.$id");
+        if ($target eq 'analyze') {
+            $Apache::lonhomework::analyze{"$partid.$id.type"} = 'reactionresponse';
+            &Apache::lonhomework::set_bubble_lines();
+        }
     }
     &Apache::response::end_response();
     return $result;
 }
 
+sub format_prior_response_reaction {
+    my ($mode,$answer) =@_;
+    return '<span class="LC_prior_reaction">'.
+	    &HTML::Entities::encode($answer,'"<>&').'</span>';
+}
+
 sub start_chem {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
     my $result = '';