File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.34: download - view: text, annotated - select for diffs
Fri Aug 1 15:50:43 2003 UTC (20 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- using the get_image in randomlylabel.pm

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

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