--- loncom/homework/imageresponse.pm	2002/05/03 20:44:46	1.18
+++ loncom/homework/imageresponse.pm	2002/08/24 15:26:43	1.21
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # image click response style
 #
-# $Id: imageresponse.pm,v 1.18 2002/05/03 20:44:46 albertel Exp $
+# $Id: imageresponse.pm,v 1.21 2002/08/24 15:26:43 harris41 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -30,6 +30,7 @@
 
 package Apache::imageresponse;
 use strict;
+use Image::Magick;
 
 BEGIN {
   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
@@ -55,9 +56,9 @@ sub end_imageresponse {
   return '';
 }
 
-%Apache::response::foilgroup={};
+%Apache::response::foilgroup=();
 sub start_foilgroup {
-  %Apache::response::foilgroup={};
+  %Apache::response::foilgroup=();
   $Apache::imageresponse::conceptgroup=0;
   &Apache::response::setrandomnumber();
   return '';
@@ -114,7 +115,6 @@ sub displayfoils {
 
 sub gradefoils {
   my (@whichopt) = @_;
-  my $result='';
   my $x;
   my $y;
   my $result;
@@ -174,7 +174,7 @@ sub end_foilgroup {
 
 sub start_conceptgroup {
   $Apache::imageresponse::conceptgroup=1;
-  %Apache::response::conceptgroup={};
+  %Apache::response::conceptgroup=();
   return '';
 }
 
@@ -244,12 +244,13 @@ sub end_text {
 
 sub start_image {
   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
-  if ($target eq 'web') { &Apache::lonxml::startredirection; }
+  if ($target eq 'web' || $target eq 'tex') { &Apache::lonxml::startredirection; }
   return '';
 }
 
 sub end_image {
-  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;            
+  my $currentstring = '';
   if ($target eq 'web') {
     my $name = $Apache::imageresponse::curname;
     my $image = &Apache::lonxml::endredirection;
@@ -260,8 +261,80 @@ sub end_image {
     } else {
       $Apache::response::foilgroup{"$name.image"} = $image;
     }
-  }
-  return '';
+  } elsif ($target eq 'tex') {
+    my $src = &Apache::lonxml::endredirection;
+    $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
+    my $width_param = '';
+    my $height_param = '';
+    my $scaling = .3;
+    my $image = Image::Magick->new;
+    my $current_figure = $image->Read($src);
+    $width_param = $image->Get('width') * $scaling;;
+    $height_param = $image->Get('height') * $scaling;;
+    undef $image;
+    my $epssrc = $src;
+    $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
+    if (not -e $epssrc) {
+	my $localfile = $epssrc;
+	$localfile =~ s/.*(\/res)/$1/;	
+	my $file;
+	my $path;	
+	if ($localfile =~ m!(.*)/([^/]*)$!) {
+	    $file = $2;
+	    $path = $1.'/'; 
+	}	
+	my $signal_eps = 0;
+	my @content_directory = &Apache::lonnet::dirlist($path);
+	for (my $iy=0;$iy<=$#content_directory;$iy++) {
+	    my @tempo_array = split(/&/,$content_directory[$iy]);
+	    $content_directory[$iy] = $tempo_array[0];
+	    if ($file eq $tempo_array[0]) {
+		$signal_eps = 1;
+		last;
+	    }
+	}
+	if ($signal_eps) {
+	    my $eps_file = &Apache::lonnet::getfile($localfile);
+	} else {
+	    $localfile = $src;
+	    $localfile =~ s/.*(\/res)/$1/;	
+	    my $as = &Apache::lonnet::getfile($src);		      
+	}
+    }
+    my $file;
+    my $path;	
+    if ($src =~ m!(.*)/([^/]*)$!) {
+	$file = $2;
+	$path = $1.'/'; 
+    }
+    my $newsrc = $src;
+    $newsrc =~ s/(\.gif|\.jpg)$/\.eps/i;
+    $file=~s/(\.gif|\.jpg)$/\.eps/i;
+    #do we have any specified size of the picture?
+    my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval); 
+    my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval); 
+    my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
+    if ($TeXwidth ne '') { 
+	$width_param = $TeXwidth; 
+    } elsif ($TeXheight ne '') { 
+	$width_param = $TeXheight/$height_param*$width_param;
+    } elsif ($width ne '') {
+	$width_param = $width*$scaling;      
+    }
+    #where can we find the picture?
+    if (-e $newsrc) {
+	if ($path) {
+	    $currentstring .= '\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\fbox{\includegraphics[width='.$width_param.' mm]{'.$file.'}} ';
+	}
+    } else {
+	my $temp_file;
+	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
+	$temp_file = Apache::File->new('>>'.$filename); 
+	print $temp_file "$src\n";
+	$currentstring .= '\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\fbox{\includegraphics[width='.$width_param.' mm]{'.$file.'}} ';
+    }
+ } 
+  return $currentstring;
 }
 
 sub start_rectangle {