File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.26: download - view: text, annotated - select for diffs
Mon May 5 22:36:54 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- some cleanup work so Gerd can modify it
- all tags should prperly close of in edit mode
- stopped using deficient get_text, now use get_all_text

    1: # The LearningOnline Network with CAPA
    2: # image click response style
    3: #
    4: # $Id: imageresponse.pm,v 1.26 2003/05/05 22:36:54 albertel Exp $
    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: #
   28: 
   29: #FIXME LATER assumes multiple possible submissions but only one is possible 
   30: #currently
   31: 
   32: package Apache::imageresponse;
   33: use strict;
   34: use Image::Magick;
   35: 
   36: BEGIN {
   37:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
   38: }
   39: 
   40: sub start_imageresponse {
   41:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   42:   my $result;
   43:   #when in a radiobutton response use these
   44:   &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
   45:   push (@Apache::lonxml::namespace,'imageresponse');
   46:   my $id = &Apache::response::start_response($parstack,$safeeval);
   47:   if ($target eq 'meta') {
   48:     $result=&Apache::response::meta_package_write('imageresponse');
   49:   }
   50:   return $result;
   51: }
   52: 
   53: sub end_imageresponse {
   54:   &Apache::response::end_response;
   55:   pop @Apache::lonxml::namespace;
   56:   &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
   57:   return '';
   58: }
   59: 
   60: %Apache::response::foilgroup=();
   61: sub start_foilgroup {
   62:   %Apache::response::foilgroup=();
   63:   $Apache::imageresponse::conceptgroup=0;
   64:   &Apache::response::setrandomnumber();
   65:   return '';
   66: }
   67: 
   68: sub getfoilcounts {
   69:   my ($parstack,$safeeval)=@_;
   70: 
   71:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
   72:   # +1 since instructors will count from 1
   73:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
   74:   if (&Apache::response::showallfoils()) { $max=$count; }
   75:   return ($count,$max);
   76: }
   77: 
   78: sub whichfoils {
   79:   my ($max)=@_;
   80:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
   81:   my @names = @{ $Apache::response::foilgroup{'names'} };
   82:   my @whichopt =();
   83:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
   84:     &Apache::lonxml::debug("Have $#whichopt max is $max");
   85:     my $aopt;
   86:     if (&Apache::response::showallfoils()) {
   87:       $aopt=0;
   88:     } else {
   89:       $aopt=int(&Math::Random::random_uniform() * ($#names+1));
   90:     }
   91:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
   92:     $aopt=splice(@names,$aopt,1);
   93:     &Apache::lonxml::debug("Picked $aopt");
   94:     push (@whichopt,$aopt);
   95:   }
   96:   return @whichopt;
   97: }
   98: 
   99: sub displayfoils {
  100:   my ($target,@whichopt) = @_;
  101:   my $result ='';
  102:   my $name;
  103:   my $temp=1;
  104:   foreach $name (@whichopt) {
  105:     $result.=$Apache::response::foilgroup{"$name.text"};
  106:     if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
  107:     my $image=$Apache::response::foilgroup{"$name.image"};
  108:     if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  109:       if ($target eq 'tex') {
  110: 	$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  111:       } else {
  112:         $result.="<img src=\"$image\"/> <br />\n";
  113:       }
  114:     } else {
  115:       if ($target eq 'tex') {
  116: 	$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  117:       } else {
  118:         $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
  119:       }
  120:     }
  121:     $temp++;
  122:   }
  123:   return $result;
  124: }
  125: 
  126: sub gradefoils {
  127:   my (@whichopt) = @_;
  128:   my $x;
  129:   my $y;
  130:   my $result;
  131:   my $id=$Apache::inputtags::response['-1'];
  132:   my $temp=1;
  133:   foreach my $name (@whichopt) {
  134:     $x=$ENV{"form.HWVAL_$id:$temp.x"};
  135:     $y=$ENV{"form.HWVAL_$id:$temp.y"};
  136:     &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
  137:     if (defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
  138:       my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
  139:       my $grade="INCORRECT";
  140:       foreach my $area (@areas) {
  141: 	&Apache::lonxml::debug("Area is $area for $name");
  142: 	$area =~ m/([a-z]*):/;
  143: 	&Apache::lonxml::debug("Area of type $1");
  144: 	if ($1 eq 'rectangle') {
  145: 	  $grade=&grade_rectangle($area,$x,$y);
  146: 	} else {
  147: 	  &Apache::lonxml::error("Unknown area style $area");
  148: 	}
  149: 	&Apache::lonxml::debug("Area said $grade");
  150: 	if ($grade eq 'APPROX_ANS') { last; }
  151:       }
  152:       &Apache::lonxml::debug("Foil was $grade");
  153:       if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
  154:       if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
  155:       &Apache::lonxml::debug("Question is $result");
  156:       $temp++;
  157:     }
  158:   }
  159:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
  160:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
  161:   return '';
  162: }
  163: 
  164: sub end_foilgroup {
  165:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  166:   my $result='';
  167:   my @whichopt;
  168:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  169:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  170:     if ($count>$max) { $count=$max }
  171:     &Apache::lonxml::debug("Count is $count from $max");
  172:     @whichopt = &whichfoils($max);
  173:   } elsif ($target eq 'web' || $target eq 'tex') {
  174:     $result=&displayfoils($target,@whichopt);
  175:   } elsif ($target eq 'grade') {
  176:     if ( defined $ENV{'form.submitted'}) {
  177:       &gradefoils(@whichopt);
  178:     }
  179:   } elsif ($target eq 'edit') {
  180:       $result=&Apache::edit::end_table();
  181:   }
  182:   return $result;
  183: }
  184: 
  185: sub start_conceptgroup {
  186:   $Apache::imageresponse::conceptgroup=1;
  187:   %Apache::response::conceptgroup=();
  188:   return '';
  189: }
  190: 
  191: sub end_conceptgroup {
  192:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  193:   $Apache::imageresponse::conceptgroup=0;
  194:   my $result;
  195:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  196:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  197:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  198:       my $pick=int(&Math::Random::random_uniform() * ($#names+1));
  199:       my $name=$names[$pick];
  200:       if (defined(@{ $Apache::response::conceptgroup{"$name.area"} })) {
  201: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  202: 	$Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
  203: 	$Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
  204: 	push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
  205: 	my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  206: 	$Apache::response::foilgroup{"$name.concept"} = $concept;
  207: 	&Apache::lonxml::debug("Selecting $name in $concept");
  208:       }
  209:     }
  210:   } elsif ($target eq 'edit') {
  211:       $result=&Apache::edit::end_table();
  212:   }
  213:   return $result;
  214: }
  215: 
  216: $Apache::imageresponse::curname='';
  217: sub start_foil {
  218:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  219:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  220:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  221:     if ($name eq '') { $name=$Apache::lonxml::curdepth; }
  222:     if ( $Apache::imageresponse::conceptgroup
  223: 	 && !&Apache::response::showallfoils()) {
  224:       push(@{ $Apache::response::conceptgroup{'names'} }, $name);
  225:     } else {
  226:       push(@{ $Apache::response::foilgroup{'names'} }, $name);
  227:     }
  228:     $Apache::imageresponse::curname=$name;
  229:   }
  230:   return '';
  231: }
  232: 
  233: sub end_foil {
  234:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  235:     my $result;
  236:     if ($target eq 'edit') {
  237: 	$result=&Apache::edit::end_table();
  238:     }
  239:     return $result;
  240: }
  241: 
  242: sub start_text {
  243:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  244:   my $result='';
  245:   if ($target eq 'web' || $target eq 'tex') { 
  246:      &Apache::lonxml::startredirection; 
  247:   } elsif ($target eq 'edit') {
  248:     my $descr=&Apache::lonxml::get_all_text('/text',$parser);
  249:     $result=&Apache::edit::tag_start($target,$token,'Task Description').
  250: 	&Apache::edit::editfield($token->[1],$descr,'Text',60,2).
  251:         &Apache::edit::end_row();
  252:   } elsif ($target eq "modified") {
  253:     my $descr=&Apache::lonxml::get_all_text('/text',$parser);
  254:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  255:     &Apache::lonxml::debug($result);
  256:   }
  257:   return $result;
  258: }
  259: 
  260: sub end_text {
  261:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  262:   my $result;
  263:   if ($target eq 'web' || $target eq 'tex') {
  264:     my $name = $Apache::imageresponse::curname;
  265:     if ( $Apache::imageresponse::conceptgroup
  266:        && !&Apache::response::showallfoils() ) {
  267:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  268:     } else {
  269:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  270:     }
  271:   } elsif ($target eq 'edit') {
  272:       $result=&Apache::edit::end_table();
  273:   }
  274:   return $result;
  275: }
  276: 
  277: sub start_image {
  278:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  279:   my $result='';
  280:   if ($target eq 'web' || $target eq 'tex') { 
  281:       &Apache::lonxml::startredirection; 
  282:   } elsif ($target eq 'edit') {
  283:     my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
  284:     $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
  285: 	&Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
  286:     $result.=&Apache::edit::browse(undef,'textnode').' ';
  287:     $result.=&Apache::edit::search(undef,'textnode').
  288:         &Apache::edit::end_row();
  289:   } elsif ($target eq "modified") {
  290:     my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
  291:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  292:     &Apache::lonxml::debug($result);
  293:   }
  294:   return $result;
  295: }
  296: 
  297: sub end_image {
  298:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  299:   my $result;
  300:   my $name = $Apache::imageresponse::curname;
  301:   if ($target eq 'web') {
  302:     my $image = &Apache::lonxml::endredirection;
  303:     &Apache::lonxml::debug("out is $image");
  304:     if ( $Apache::imageresponse::conceptgroup
  305: 	 && !&Apache::response::showallfoils()) {
  306:       $Apache::response::conceptgroup{"$name.image"} = $image;
  307:     } else {
  308:       $Apache::response::foilgroup{"$name.image"} = $image;
  309:     }
  310:   } elsif ($target eq 'edit') {
  311:       $result=&Apache::edit::end_table();
  312:   } elsif ($target eq 'tex') {
  313:     my $src = &Apache::lonxml::endredirection;
  314:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
  315:     my $width_param = '';
  316:     my $height_param = '';
  317:     my $scaling = .3;
  318:     my $image = Image::Magick->new;
  319:     my $current_figure = $image->Read($src);
  320:     $width_param = $image->Get('width') * $scaling;;
  321:     $height_param = $image->Get('height') * $scaling;;
  322:     undef $image;
  323:     my $epssrc = $src;
  324:     $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
  325:     if (not -e $epssrc) {
  326: 	my $localfile = $epssrc;
  327: 	$localfile =~ s/.*(\/res)/$1/;	
  328: 	my $file;
  329: 	my $path;	
  330: 	if ($localfile =~ m!(.*)/([^/]*)$!) {
  331: 	    $file = $2;
  332: 	    $path = $1.'/'; 
  333: 	}	
  334: 	my $signal_eps = 0;
  335: 	my @content_directory = &Apache::lonnet::dirlist($path);
  336: 	for (my $iy=0;$iy<=$#content_directory;$iy++) {
  337: 	    my @tempo_array = split(/&/,$content_directory[$iy]);
  338: 	    $content_directory[$iy] = $tempo_array[0];
  339: 	    if ($file eq $tempo_array[0]) {
  340: 		$signal_eps = 1;
  341: 		last;
  342: 	    }
  343: 	}
  344: 	if ($signal_eps) {
  345: 	    my $eps_file = &Apache::lonnet::getfile($localfile);
  346: 	} else {
  347: 	    $localfile = $src;
  348: 	    $localfile =~ s/.*(\/res)/$1/;	
  349: 	    my $as = &Apache::lonnet::getfile($src);		      
  350: 	}
  351:     }
  352:     my $file;
  353:     my $path;	
  354:     if ($src =~ m!(.*)/([^/]*)$!) {
  355: 	$file = $2;
  356: 	$path = $1.'/'; 
  357:     }
  358:     my $newsrc = $src;
  359:     $newsrc =~ s/(\.gif|\.jpg)$/\.eps/i;
  360:     $file=~s/(\.gif|\.jpg)$/\.eps/i;
  361:     #do we have any specified size of the picture?
  362:     my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval); 
  363:     my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval); 
  364:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
  365:     if ($TeXwidth ne '') { 
  366: 	$width_param = $TeXwidth; 
  367:     } elsif ($TeXheight ne '') { 
  368: 	$width_param = $TeXheight/$height_param*$width_param;
  369:     } elsif ($width ne '') {
  370: 	$width_param = $width*$scaling;      
  371:     }
  372:     #where can we find the picture?
  373:     if (-e $newsrc) {
  374: 	if ($path) {
  375: 	  $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
  376: 	}
  377:     } else {
  378: 	my $temp_file;
  379: 	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
  380: 	$temp_file = Apache::File->new('>>'.$filename); 
  381: 	print $temp_file "$src\n";
  382: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
  383:     }
  384:   } 
  385:   return $result;
  386: }
  387: 
  388: sub start_rectangle {
  389:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  390:   my $result='';
  391:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') { 
  392:      &Apache::lonxml::startredirection; 
  393:   } elsif ($target eq 'edit') {
  394:     my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
  395:     $result=&Apache::edit::tag_start($target,$token,'Rectangle').
  396: 	&Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
  397:         &Apache::edit::end_row();
  398:   } elsif ($target eq "modified") {
  399:     my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
  400:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  401:     &Apache::lonxml::debug($result);
  402:   }
  403:   return $result;
  404: }
  405: 
  406: sub grade_rectangle {
  407:   my ($spec,$x,$y) = @_;
  408:   &Apache::lonxml::debug("Spec is $spec");
  409:   $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
  410:   my $x1=$1;
  411:   my $y1=$2;
  412:   my $x2=$3;
  413:   my $y2=$4;
  414:   &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
  415:   if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  416:   if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  417:   if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
  418:     return 'APPROX_ANS';
  419:   }
  420:   return 'INCORRECT';
  421: }
  422: 
  423: sub end_rectangle {
  424:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  425:   my $result;
  426:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  427:     my $name = $Apache::imageresponse::curname;
  428:     my $area = &Apache::lonxml::endredirection;
  429:     &Apache::lonxml::debug("out is $area for $name");
  430:     if ( $Apache::imageresponse::conceptgroup
  431: 	 && !&Apache::response::showallfoils()) {
  432:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  433:     } else {
  434:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  435:     }
  436:   } elsif ($target eq 'edit') {
  437:       $result=&Apache::edit::end_table();
  438:   }
  439:   return $result;
  440: }
  441: 1;
  442: __END__
  443:  

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