--- loncom/homework/externalresponse.pm	2013/09/11 10:25:50	1.24
+++ loncom/homework/externalresponse.pm	2014/12/04 15:41:30	1.27
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # external style responses
 #
-# $Id: externalresponse.pm,v 1.24 2013/09/11 10:25:50 kruse Exp $
+# $Id: externalresponse.pm,v 1.27 2014/12/04 15:41:30 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -173,6 +173,7 @@ sub start_externalresponse {
 sub end_externalresponse {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result;
+    my $response;
     my $part=$Apache::inputtags::part;
     my $id = $Apache::inputtags::response['-1'];
     my $increment     = 1;
@@ -180,7 +181,7 @@ sub end_externalresponse {
 	if (  &Apache::response::submitted('scantron') ) {
 	    my $increment=&Apache::response::scored_response($part,$id);
 	} elsif ( &Apache::response::submitted() ) {
-            my $response      = $env{'form.HWVAL_'.$id};
+            $response      = $env{'form.HWVAL_'.$id};
             my $jspart=$part;
             $jspart=~s/\./_/g;
             my $filename = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'} ||
@@ -194,7 +195,7 @@ sub end_externalresponse {
             if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
                 $was_draft = 1;
             }
-            if (($response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/) ||
+            if (($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/) ||
                 (@deletions > 0) || ($was_draft && $is_submit)) {
                 my $award='DRAFT';
                 if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
@@ -210,7 +211,22 @@ sub end_externalresponse {
                 }
                 my $uploadedflag=0;
                 my $totalsize=0;
-                &Apache::essayresponse::file_submission($part,$id,\$award,\$uploadedflag,\$totalsize,\@deletions);
+                my %info;
+                my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
+                if ($url =~ m{^https?://([^/]+)/}) {
+                    use Socket;
+                    my $ip = gethostbyname($1);
+                    if (length($ip) eq 4) {
+                        $info{'ip'} = inet_ntoa($ip);
+                    }
+                } elsif ($url =~ m{^/}) {
+                    my $ip = &Apache::lonnet::get_host_ip($Apache::lonnet::perlvar{'lonHostID'});
+                    if ($ip =~ /^[\d\.]+$/) {
+                        $info{'ip'} = $ip;
+                    }
+                }
+                &Apache::essayresponse::file_submission($part,$id,\$award,\$uploadedflag,\$totalsize,
+                                                        \@deletions,'externalresponse',\%info);
                 $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
                 $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
                 my %previous=&Apache::response::check_for_previous($response,$part,$id);
@@ -239,9 +255,12 @@ sub end_externalresponse {
                 }
             ($award eq 'DRAFT') ? ($externalgrade = 0) : ($externalgrade =1);
             }
-          if($externalgrade){
-	    my $response = &Apache::response::getresponse();
+          if (&Apache::response::getresponse() =~ /[^\s]/) {
+               $response = &Apache::response::getresponse();
+               $externalgrade = 1;
+          }  
 
+          if ($externalgrade) {
             my $filesresults=0;
 # Previously turned in files
             my %previously_turned_in=();
@@ -320,7 +339,8 @@ sub end_externalresponse {
                                                    'uploadedurl']);
         if (&Apache::response::show_answer()) {
            $result.='<table border="1"><tr><th>'.&mt('Your answer:').'</th></tr><tr><td><pre>'.
-                    $Apache::lonhomework::history{"resource.$part.$id.submission"}.
+                    &HTML::Entities::encode(
+                        $Apache::lonhomework::history{"resource.$part.$id.submission"},'"<>&').
                     '</pre></td></table><br />'.&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval).
                     '<br />';
         }