--- loncom/homework/externalresponse.pm	2005/04/07 06:56:21	1.4
+++ loncom/homework/externalresponse.pm	2008/02/01 22:05:45	1.13
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # external style responses
 #
-# $Id: externalresponse.pm,v 1.4 2005/04/07 06:56:21 albertel Exp $
+# $Id: externalresponse.pm,v 1.13 2008/02/01 22:05:45 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,7 +25,7 @@
 #
 # http://www.lon-capa.org/
 #
-# 1/26 Guy
+
 package Apache::externalresponse;
 use strict;
 use HTTP::Request::Common;
@@ -33,65 +33,110 @@ use Apache::loncapagrade;
 use Apache::lonnet;
 
 BEGIN {
-  &Apache::lonxml::register('Apache::externalresponse',('externalresponse'));
+    &Apache::lonxml::register('Apache::externalresponse',('externalresponse'));
 }
 
 #FIXME
-# send of response params and there current values
-# accept a string to be printed out (Where does it get shown?) (.message)
-# need to get a correct answer from somewhere (probably here since randomized)
+# send of response params and their current values (form good enough?
+# what parameters to send?)
+# Need to get returned message displayed 
 sub start_externalresponse {
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  my $result;
-  my $id = &Apache::response::start_response($parstack,$safeeval);
-  return $result;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+    my $result;
+    my $id = &Apache::response::start_response($parstack,$safeeval);
+
+    if ($target eq 'edit') {
+  	$result .=&Apache::edit::tag_start($target,$token);
+	$result .=&Apache::edit::text_arg('URL:','url',$token,60).'<br />';
+	$result .=&Apache::edit::text_arg('Answer:','answer',$token);
+	$result .=&Apache::edit::text_arg('Form:','form',$token);
+	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
+    } elsif ($target eq 'modified') {
+	my $constructtag =
+	    &Apache::edit::get_new_args($token,$parstack,$safeeval,
+					'answer','form','url');
+	if ($constructtag) {
+	    $result = &Apache::edit::rebuild_tag($token);
+	}
+    } elsif ($target eq 'meta') {
+  	$result=&Apache::response::meta_package_write('externalresponse');
+    }
+    
+    return $result;
 }
 
 sub end_externalresponse {
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  my $result;
-  if ($target eq 'grade') {
-    if ( defined $env{'form.submitted'}) {
-      my $id = $Apache::inputtags::response['-1'];
-      my $response = $env{'form.HWVAL_'.$id};
-      if ( $response =~ /[^\s]/) {
-	my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
-	my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
-	my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
-	$form{'LONCAPA_student_response'}=$response;
-	$form{'LONCAPA_correct_answer'}=$answer;
-	$form{'LONCAPA_language'}=
-	  &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
-	&Apache::lonxml::debug("Asking $url, with:");
-	&Apache::lonhomework::showhash(%form);
-	my $ua = LWP::UserAgent->new;
-	my $res = $ua->request(POST $url, \%form);
-	my $partid = $Apache::inputtags::part;
-	my $id = $Apache::inputtags::response['-1'];
-	my %previous = &Apache::response::check_for_previous($response,
-							     $partid,$id);
-	%Apache::loncapagrade::results=();
-        if ($res->{_rc} != '200') {
-	  $Apache::loncapagrade::results{'awardetail'}='ERROR';
-	} else {
-	  &Apache::lonxml::register('Apache::loncapagrade',('loncapagrade'));
-	  my $result=&Apache::scripttag::xmlparse($res->{_content});
-	  &Apache::lonxml::debug("Got a result of :$result:");
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+    my $result;
+    my $part=$Apache::inputtags::part;
+    my $id = $Apache::inputtags::response['-1'];
+    my $increment     = 1;
+    if ($target eq 'grade') {
+	if (  &Apache::response::submitted('scantron') ) {
+	    my $increment=&Apache::response::scored_response($part,$id);
+	} elsif ( &Apache::response::submitted() ) {
+	    my $response = &Apache::response::getresponse();
+	    if ( $response =~ /[^\s]/) {
+		my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
+		my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
+		my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
+		$form{'LONCAPA_student_response'}=$response;
+		$form{'LONCAPA_correct_answer'}=$answer;
+		$form{'LONCAPA_language'}=
+		    &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
+		&Apache::lonxml::debug("Asking $url, with:");
+		&Apache::lonhomework::showhash(%form);
+		my $ua = LWP::UserAgent->new;
+		my $res = $ua->request(POST $url, \%form);
+		my %previous = &Apache::response::check_for_previous($response,
+								     $part,$id);
+		%Apache::loncapagrade::results=();
+		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
+		if ($res->is_error()) {
+		    $Apache::loncapagrade::results{'awarddetail'}='ERROR';
+		} else {
+		    &Apache::lonxml::register('Apache::loncapagrade',
+					      ('loncapagrade'));
+		    @Apache::scripttag::parser_env = @_;
+		    my $result=&Apache::scripttag::xmlparse($res->{_content});
+		    &Apache::lonxml::debug("Got a result of :$result:");
+		}
+		foreach my $key (keys(%Apache::loncapagrade::results)) {
+		    $Apache::lonhomework::results{"resource.$part.$id.$key"}=
+			$Apache::loncapagrade::results{$key};
+		}
+		&Apache::response::handle_previous(\%previous,
+						   $Apache::loncapagrade::results{'awarddetail'});
+		&Apache::lonxml::debug("response of");
+		&Apache::lonhomework::showhash(%$res);
+		&Apache::lonxml::debug("capagrade of");
+		&Apache::lonhomework::showhash(%Apache::loncapagrade::results);
+		&Apache::lonxml::debug("results of");
+		&Apache::lonhomework::showhash(%Apache::lonhomework::results);
+	    }
 	}
-	foreach my $key (%Apache::loncapagrade::results) {
-	  $Apache::lonhomework::results{"resource.$partid.$id.$key"}=
-	    $Apache::loncapagrade::results{$key};
+    }
+    if ($target eq 'web') {
+	&Apache::response::setup_prior_tries_hash(\&format_prior_response);
+    }
+
+    if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
+	$target eq 'tex' || $target eq 'analyze') {
+	&Apache::lonxml::increment_counter($increment, "$part.$id");
+
+	if ($target eq 'analyze') {
+            $Apache::lonhomework::analyze{"$part.$id.type"} = 'externalresponse';
+	    &Apache::lonhomework::set_bubble_lines();
 	}
-	&Apache::response::handle_previous(\%previous,
-		    $Apache::loncapagrade::results{'awarddetail'});
-	&Apache::lonxml::debug("response of");
-	&Apache::lonhomework::showhash(%$res);
-	&Apache::lonhomework::showhash(%Apache::loncapagrade::results);
-      }
     }
-  }
-  &Apache::response::end_response;
-  return $result;
+    &Apache::response::end_response();
+    return $result;
+}
+
+sub format_prior_response {
+    my ($mode,$answer) =@_;
+    return '<span class="LC_prior_external">'.
+	    &HTML::Entities::encode($answer,'"<>&').'</span>';
 }
 
 1;