File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.28: download - view: text, annotated - select for diffs
Tue May 6 15:48:55 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: version_0_99_1, version_0_99_0, conference_2003, HEAD
- in my attempts to retabinate I screwed up the logic, web tex and grade should work correctly again

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

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