File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.29: download - view: text, annotated - select for diffs
Mon Jun 9 22:29:45 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixes submitting an answer when not really doing so (part of #1678)

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

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