Annotation of loncom/homework/imageresponse.pm, revision 1.24

1.12      albertel    1: # The LearningOnline Network with CAPA
1.14      albertel    2: # image click response style
                      3: #
1.24    ! sakharuk    4: # $Id: imageresponse.pm,v 1.23 2003/01/16 02:55:05 albertel Exp $
1.14      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
1.12      albertel   29: #FIXME assumes multiple possbile submissions but only one is possible currently
1.3       albertel   30: 
1.1       albertel   31: package Apache::imageresponse;
                     32: use strict;
1.21      harris41   33: use Image::Magick;
1.1       albertel   34: 
1.16      harris41   35: BEGIN {
1.12      albertel   36:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
1.1       albertel   37: }
                     38: 
                     39: sub start_imageresponse {
1.12      albertel   40:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      albertel   41:   my $result;
1.12      albertel   42:   #when in a radiobutton response use these
                     43:   &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
                     44:   push (@Apache::lonxml::namespace,'imageresponse');
                     45:   my $id = &Apache::response::start_response($parstack,$safeeval);
1.13      albertel   46:   if ($target eq 'meta') {
                     47:     $result=&Apache::response::meta_package_write('imageresponse');
                     48:   }
                     49:   return $result;
1.1       albertel   50: }
                     51: 
                     52: sub end_imageresponse {
1.12      albertel   53:   &Apache::response::end_response;
                     54:   pop @Apache::lonxml::namespace;
1.15      albertel   55:   &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
1.12      albertel   56:   return '';
1.1       albertel   57: }
                     58: 
1.20      albertel   59: %Apache::response::foilgroup=();
1.1       albertel   60: sub start_foilgroup {
1.20      albertel   61:   %Apache::response::foilgroup=();
1.12      albertel   62:   $Apache::imageresponse::conceptgroup=0;
                     63:   &Apache::response::setrandomnumber();
                     64:   return '';
1.1       albertel   65: }
                     66: 
1.2       albertel   67: sub getfoilcounts {
1.12      albertel   68:   my ($parstack,$safeeval)=@_;
                     69: 
                     70:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                     71:   # +1 since instructors will count from 1
                     72:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.18      albertel   73:   if (&Apache::response::showallfoils()) { $max=$count; }
1.12      albertel   74:   return ($count,$max);
1.2       albertel   75: }
                     76: 
                     77: sub whichfoils {
1.12      albertel   78:   my ($max)=@_;
                     79:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                     80:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     81:   my @whichopt =();
                     82:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                     83:     &Apache::lonxml::debug("Have $#whichopt max is $max");
1.18      albertel   84:     my $aopt;
                     85:     if (&Apache::response::showallfoils()) {
                     86:       $aopt=0;
                     87:     } else {
1.22      albertel   88:       $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.18      albertel   89:     }
1.12      albertel   90:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
                     91:     $aopt=splice(@names,$aopt,1);
                     92:     &Apache::lonxml::debug("Picked $aopt");
                     93:     push (@whichopt,$aopt);
                     94:   }
                     95:   return @whichopt;
1.2       albertel   96: }
                     97: 
                     98: sub displayfoils {
1.24    ! sakharuk   99:   my ($target,@whichopt) = @_;
1.12      albertel  100:   my $result ='';
                    101:   my $name;
                    102:   my $temp=1;
                    103:   foreach $name (@whichopt) {
1.24    ! sakharuk  104:     $result.=$Apache::response::foilgroup{"$name.text"};
        !           105:     if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
1.12      albertel  106:     my $image=$Apache::response::foilgroup{"$name.image"};
                    107:     if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
1.24    ! sakharuk  108:       if ($target eq 'tex') {
        !           109: 	$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
        !           110:       } else {
        !           111:         $result.="<img src=\"$image\"/> <br />\n";
        !           112:       }
1.12      albertel  113:     } else {
1.24    ! sakharuk  114:       if ($target eq 'tex') {
        !           115: 	$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
        !           116:       } else {
        !           117:         $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
        !           118:       }
1.12      albertel  119:     }
                    120:     $temp++;
                    121:   }
                    122:   return $result;
1.2       albertel  123: }
                    124: 
1.3       albertel  125: sub gradefoils {
1.12      albertel  126:   my (@whichopt) = @_;
                    127:   my $x;
                    128:   my $y;
                    129:   my $result;
                    130:   my $id=$Apache::inputtags::response['-1'];
                    131:   my $temp=1;
                    132:   foreach my $name (@whichopt) {
                    133:     $x=$ENV{"form.HWVAL_$id:$temp.x"};
                    134:     $y=$ENV{"form.HWVAL_$id:$temp.y"};
                    135:     &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
                    136:     if (defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
                    137:       my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
                    138:       my $grade="INCORRECT";
                    139:       foreach my $area (@areas) {
                    140: 	&Apache::lonxml::debug("Area is $area for $name");
                    141: 	$area =~ m/([a-z]*):/;
                    142: 	&Apache::lonxml::debug("Area of type $1");
                    143: 	if ($1 eq 'rectangle') {
                    144: 	  $grade=&grade_rectangle($area,$x,$y);
                    145: 	} else {
                    146: 	  &Apache::lonxml::error("Unknown area style $area");
1.9       albertel  147: 	}
1.12      albertel  148: 	&Apache::lonxml::debug("Area said $grade");
                    149: 	if ($grade eq 'APPROX_ANS') { last; }
                    150:       }
                    151:       &Apache::lonxml::debug("Foil was $grade");
                    152:       if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
                    153:       if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
                    154:       &Apache::lonxml::debug("Question is $result");
                    155:       $temp++;
                    156:     }
                    157:   }
                    158:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
                    159:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
                    160:   return '';
1.3       albertel  161: }
                    162: 
1.1       albertel  163: sub end_foilgroup {
1.12      albertel  164:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    165:   my $result='';
                    166:   my @whichopt;
1.24    ! sakharuk  167:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  168:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    169:     if ($count>$max) { $count=$max }
                    170:     &Apache::lonxml::debug("Count is $count from $max");
                    171:     @whichopt = &whichfoils($max);
                    172:   }
1.24    ! sakharuk  173:   if ($target eq 'web' || $target eq 'tex') {
        !           174:     $result=&displayfoils($target,@whichopt);
1.12      albertel  175:   }
                    176:   if ($target eq 'grade') {
                    177:     if ( defined $ENV{'form.submitted'}) {
                    178:       &gradefoils(@whichopt);
1.3       albertel  179:     }
1.12      albertel  180:   }
                    181:   return $result;
1.1       albertel  182: }
                    183: 
                    184: sub start_conceptgroup {
1.12      albertel  185:   $Apache::imageresponse::conceptgroup=1;
1.20      albertel  186:   %Apache::response::conceptgroup=();
1.12      albertel  187:   return '';
1.1       albertel  188: }
                    189: 
                    190: sub end_conceptgroup {
1.12      albertel  191:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    192:   $Apache::imageresponse::conceptgroup=0;  
1.24    ! sakharuk  193:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  194:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    195:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.22      albertel  196:       my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1.12      albertel  197:       my $name=$names[$pick];
                    198:       if (defined(@{ $Apache::response::conceptgroup{"$name.area"} })) {
                    199: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    200: 	$Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
                    201: 	$Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
                    202: 	push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
                    203: 	my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    204: 	$Apache::response::foilgroup{"$name.concept"} = $concept;
                    205: 	&Apache::lonxml::debug("Selecting $name in $concept");
                    206:       }
1.9       albertel  207:     }
1.12      albertel  208:   }
                    209:   return '';
1.1       albertel  210: }
                    211: 
1.12      albertel  212: $Apache::imageresponse::curname='';
1.1       albertel  213: sub start_foil {
1.12      albertel  214:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.24    ! sakharuk  215:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  216:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    217:     if ($name eq '') { $name=$Apache::lonxml::curdepth; }
1.18      albertel  218:     if ( $Apache::imageresponse::conceptgroup
                    219: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  220:       push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    221:     } else {
                    222:       push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    223:     }
                    224:     $Apache::imageresponse::curname=$name;
                    225:   }
                    226:   return '';
1.1       albertel  227: }
                    228: 
                    229: sub end_foil {
1.12      albertel  230:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    231:   return '';
1.1       albertel  232: }
                    233: 
                    234: sub start_text {
1.12      albertel  235:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.24    ! sakharuk  236:   if ($target eq 'web' || $target eq 'tex') { &Apache::lonxml::startredirection; }
1.12      albertel  237:   return '';
1.1       albertel  238: }
                    239: 
                    240: sub end_text {
1.12      albertel  241:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.24    ! sakharuk  242:   if ($target eq 'web' || $target eq 'tex') {
1.12      albertel  243:     my $name = $Apache::imageresponse::curname;
1.18      albertel  244:     if ( $Apache::imageresponse::conceptgroup
                    245:        && !&Apache::response::showallfoils() ) {
1.12      albertel  246:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    247:     } else {
                    248:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
1.7       albertel  249:     }
1.12      albertel  250:   }
                    251:   return '';
1.1       albertel  252: }
                    253: 
                    254: sub start_image {
1.12      albertel  255:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.19      sakharuk  256:   if ($target eq 'web' || $target eq 'tex') { &Apache::lonxml::startredirection; }
1.12      albertel  257:   return '';
1.1       albertel  258: }
                    259: 
                    260: sub end_image {
1.19      sakharuk  261:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;            
1.24    ! sakharuk  262:   my $name = $Apache::imageresponse::curname;
1.12      albertel  263:   if ($target eq 'web') {
                    264:     my $image = &Apache::lonxml::endredirection;
                    265:     &Apache::lonxml::debug("out is $image");
1.18      albertel  266:     if ( $Apache::imageresponse::conceptgroup
                    267: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  268:       $Apache::response::conceptgroup{"$name.image"} = $image;
                    269:     } else {
                    270:       $Apache::response::foilgroup{"$name.image"} = $image;
1.7       albertel  271:     }
1.19      sakharuk  272:   } elsif ($target eq 'tex') {
                    273:     my $src = &Apache::lonxml::endredirection;
                    274:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
                    275:     my $width_param = '';
                    276:     my $height_param = '';
                    277:     my $scaling = .3;
                    278:     my $image = Image::Magick->new;
                    279:     my $current_figure = $image->Read($src);
                    280:     $width_param = $image->Get('width') * $scaling;;
                    281:     $height_param = $image->Get('height') * $scaling;;
                    282:     undef $image;
                    283:     my $epssrc = $src;
                    284:     $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
                    285:     if (not -e $epssrc) {
                    286: 	my $localfile = $epssrc;
                    287: 	$localfile =~ s/.*(\/res)/$1/;	
                    288: 	my $file;
                    289: 	my $path;	
                    290: 	if ($localfile =~ m!(.*)/([^/]*)$!) {
                    291: 	    $file = $2;
                    292: 	    $path = $1.'/'; 
                    293: 	}	
                    294: 	my $signal_eps = 0;
                    295: 	my @content_directory = &Apache::lonnet::dirlist($path);
                    296: 	for (my $iy=0;$iy<=$#content_directory;$iy++) {
                    297: 	    my @tempo_array = split(/&/,$content_directory[$iy]);
                    298: 	    $content_directory[$iy] = $tempo_array[0];
                    299: 	    if ($file eq $tempo_array[0]) {
                    300: 		$signal_eps = 1;
                    301: 		last;
                    302: 	    }
                    303: 	}
                    304: 	if ($signal_eps) {
                    305: 	    my $eps_file = &Apache::lonnet::getfile($localfile);
                    306: 	} else {
                    307: 	    $localfile = $src;
                    308: 	    $localfile =~ s/.*(\/res)/$1/;	
                    309: 	    my $as = &Apache::lonnet::getfile($src);		      
                    310: 	}
                    311:     }
                    312:     my $file;
                    313:     my $path;	
                    314:     if ($src =~ m!(.*)/([^/]*)$!) {
                    315: 	$file = $2;
                    316: 	$path = $1.'/'; 
                    317:     }
                    318:     my $newsrc = $src;
                    319:     $newsrc =~ s/(\.gif|\.jpg)$/\.eps/i;
                    320:     $file=~s/(\.gif|\.jpg)$/\.eps/i;
                    321:     #do we have any specified size of the picture?
                    322:     my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval); 
                    323:     my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval); 
                    324:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
                    325:     if ($TeXwidth ne '') { 
                    326: 	$width_param = $TeXwidth; 
                    327:     } elsif ($TeXheight ne '') { 
                    328: 	$width_param = $TeXheight/$height_param*$width_param;
                    329:     } elsif ($width ne '') {
                    330: 	$width_param = $width*$scaling;      
                    331:     }
                    332:     #where can we find the picture?
                    333:     if (-e $newsrc) {
                    334: 	if ($path) {
1.24    ! sakharuk  335: 	  $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19      sakharuk  336: 	}
                    337:     } else {
                    338: 	my $temp_file;
                    339: 	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
                    340: 	$temp_file = Apache::File->new('>>'.$filename); 
                    341: 	print $temp_file "$src\n";
1.24    ! sakharuk  342: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19      sakharuk  343:     }
1.24    ! sakharuk  344:   } 
        !           345:   return '';
1.1       albertel  346: }
                    347: 
                    348: sub start_rectangle {
1.12      albertel  349:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.24    ! sakharuk  350:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') { &Apache::lonxml::startredirection; }
1.12      albertel  351:   return '';
1.1       albertel  352: }
                    353: 
1.3       albertel  354: sub grade_rectangle {
1.12      albertel  355:   my ($spec,$x,$y) = @_;
                    356:   &Apache::lonxml::debug("Spec is $spec");
                    357:   $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
                    358:   my $x1=$1;
                    359:   my $y1=$2;
                    360:   my $x2=$3;
                    361:   my $y2=$4;
                    362:   &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    363:   if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    364:   if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
1.17      albertel  365:   if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
                    366:     return 'APPROX_ANS';
                    367:   }
1.12      albertel  368:   return 'INCORRECT';
1.3       albertel  369: }
                    370: 
1.1       albertel  371: sub end_rectangle {
1.12      albertel  372:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.24    ! sakharuk  373:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  374:     my $name = $Apache::imageresponse::curname;
                    375:     my $area = &Apache::lonxml::endredirection;
                    376:     &Apache::lonxml::debug("out is $area for $name");
1.18      albertel  377:     if ( $Apache::imageresponse::conceptgroup
                    378: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  379:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    380:     } else {
                    381:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.7       albertel  382:     }
1.12      albertel  383:   }
                    384:   return '';
1.1       albertel  385: }
                    386: 1;
                    387: __END__
                    388:  

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>