File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.104: download - view: text, annotated - select for diffs
Fri Oct 12 12:45:46 2012 UTC (11 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- For perl 5.16 use of deprecated defined(@array) generates warnings.
  - eliminate use.

    1: #
    2: # The LearningOnline Network with CAPA
    3: # image click response style
    4: #
    5: # $Id: imageresponse.pm,v 1.104 2012/10/12 12:45:46 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: #FIXME LATER assumes multiple possible submissions but only one is possible 
   30: #currently
   31: 
   32: 
   33: =head1 NAME
   34: 
   35: Apache::imageresponse
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Coordinates the response to clicking an image.
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: =head1 SUBROUTINES
   45: 
   46: =over
   47: 
   48: =item start_imageresponse()
   49: 
   50: =item end_imageresponse()
   51: 
   52: =item start_foilgroup()
   53: 
   54: =item getfoilcounts()
   55: 
   56: =item whichfoils()
   57: 
   58: =item prep_image()
   59: 
   60: =item draw_image()
   61: 
   62: =item displayfoils()
   63: 
   64: =item format_prior_response()
   65: 
   66: =item display_answers()
   67: 
   68: =item clean_up_image()
   69: 
   70: =item gradefoils()
   71: 
   72: =item stringify_submission()
   73: 
   74: =item get_submission()
   75: 
   76: =item end_foilgroup()
   77: 
   78: =item start_conceptgroup()
   79: 
   80: =item end_conceptgroup()
   81: 
   82: =item insert_foil()
   83: 
   84: =item start_foil()
   85: 
   86: =item end_foil()
   87: 
   88: =item start_text()
   89: 
   90: =item end_text()
   91: 
   92: =item start_image()
   93: 
   94: =item end_image()
   95: 
   96: =item start_rectangle()
   97: 
   98: =item grade_rectangle()
   99: 
  100: =item end_rectangle()
  101: 
  102: =item start_polygon()
  103: 
  104: =item grade_polygon()
  105: 
  106: =item end_polygon()
  107: 
  108: =back
  109: 
  110: =cut
  111: 
  112: 
  113: package Apache::imageresponse;
  114: use strict;
  115: use Image::Magick();
  116: use Apache::randomlylabel();
  117: use Apache::londefdef();
  118: use Apache::Constants qw(:common :http);
  119: use Apache::lonlocal;
  120: use Apache::lonnet;
  121: use lib '/home/httpd/lib/perl/';
  122: use LONCAPA;
  123:  
  124: 
  125: BEGIN {
  126:     &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
  127: }
  128: 
  129: sub start_imageresponse {
  130:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  131:     my $result;
  132:     #when in a radiobutton response use these
  133:     &Apache::lonxml::register('Apache::imageresponse',
  134: 			      ('foilgroup','foil','text','image','rectangle',
  135: 			       'polygon','conceptgroup'));
  136:     push (@Apache::lonxml::namespace,'imageresponse');
  137:     my $id = &Apache::response::start_response($parstack,$safeeval);
  138:     undef(%Apache::response::foilnames);
  139:     if ($target eq 'meta') {
  140: 	$result=&Apache::response::meta_package_write('imageresponse');
  141:     } elsif ($target eq 'analyze') {
  142: 	my $part_id="$Apache::inputtags::part.$id";
  143:         $Apache::lonhomework::analyze{"$part_id.type"} = 'imageresponse';
  144: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
  145: 	push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} },
  146: 	      1);
  147:     } elsif ( $target eq 'edit' ) {
  148: 	$result .= &Apache::edit::tag_start($target,$token).
  149: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:',
  150: 				    'max',$token,'4').
  151: 	    &Apache::edit::end_row().
  152: 	    &Apache::edit::start_spanning_row();
  153:     } elsif ( $target eq 'modified' ) {
  154: 	my $constructtag=
  155: 	    &Apache::edit::get_new_args($token,$parstack,$safeeval,'max');
  156: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  157:     }
  158:     return $result;
  159: }
  160: 
  161: sub end_imageresponse {
  162:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  163: 
  164:     my $part_id     = $Apache::inputtags::part;
  165:     my $response_id = $Apache::inputtags::response[-1]; 
  166: 
  167:     pop(@Apache::lonxml::namespace);
  168:     &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
  169: 
  170:     my $result;
  171:     if ($target eq 'edit') {
  172: 	$result=&Apache::edit::end_table();
  173:     } elsif ($target eq 'tex'
  174: 	     && $Apache::lonhomework::type eq 'exam') {
  175: 	$result=&Apache::inputtags::exam_score_line($target);
  176:     }
  177: 
  178:     undef(%Apache::response::foilnames);
  179:     
  180:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  181: 	$target eq 'tex' || $target eq 'analyze') {
  182:         my $repetition = &Apache::response::repetition();
  183: 	&Apache::lonxml::increment_counter($repetition, 
  184: 					   "$part_id.$response_id");
  185: 	if ($target eq 'analyze') {
  186: 	    &Apache::lonhomework::set_bubble_lines();
  187: 	}
  188: 	
  189:     }
  190:     &Apache::response::end_response();
  191:     return $result;
  192: }
  193: 
  194: %Apache::response::foilgroup=();
  195: sub start_foilgroup {
  196:     my ($target) = @_;
  197:     %Apache::response::foilgroup=();
  198:     $Apache::imageresponse::conceptgroup=0;
  199:     &Apache::response::pushrandomnumber(undef,$target);
  200:     return '';
  201: }
  202: 
  203: sub getfoilcounts {
  204:     my ($parstack,$safeeval)=@_;
  205: 
  206:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  207:     # +1 since instructors will count from 1
  208:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  209:     if (&Apache::response::showallfoils()) { $max=$count; }
  210:     return ($count,$max);
  211: }
  212: 
  213: sub whichfoils {
  214:     my ($max)=@_;
  215:     my @names;
  216:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
  217:         @names = @{ $Apache::response::foilgroup{'names'} };
  218:     }
  219:     return if (!@names);
  220:     my @whichopt;
  221:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
  222: 	&Apache::lonxml::debug("Have $#whichopt max is $max");
  223: 	my $aopt;
  224: 	if (&Apache::response::showallfoils()) {
  225: 	    $aopt=0;
  226: 	} else {
  227: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
  228: 	}
  229: 	&Apache::lonxml::debug("From $#names elms, picking $aopt");
  230: 	$aopt=splice(@names,$aopt,1);
  231: 	&Apache::lonxml::debug("Picked $aopt");
  232: 	push(@whichopt,$aopt);
  233:     }
  234:     return @whichopt;
  235: }
  236: 
  237: sub prep_image {
  238:     my ($image,$mode,$name)=@_;
  239: 
  240:     my ($x,$y)= &get_submission($name);
  241:     &Apache::lonxml::debug("for $name drawing click at $x and $y");
  242:     &draw_image($mode,$image,$x,$y,$Apache::response::foilgroup{"$name.area"});
  243: }
  244: 
  245: sub draw_image {
  246:     my ($mode,$image,$x,$y,$areas) = @_;
  247: 
  248:     my $id=&Apache::loncommon::get_cgi_id();
  249: 
  250:     my (%x,$i);
  251:     $x{"cgi.$id.BGIMG"}=&escape($image);
  252: 
  253:     #draws 2 xs on the image at the clicked location
  254:     #one in white and then one in red on top of the one in white
  255: 
  256:     if (defined($x)    && $x =~/\S/ 
  257: 	&& defined($y) && $y =~/\S/ 
  258: 	&& ($mode eq 'submission' || !&Apache::response::show_answer())
  259: 	&& $mode ne 'answeronly') {
  260: 	my $length = 6;
  261: 	my $width = 1;
  262: 	my $extrawidth = 2;
  263: 	my $xmin=($x-$length);
  264: 	my $xmax=($x+$length); 
  265: 	my $ymin=($y-$length);
  266: 	my $ymax=($y+$length);
  267: 
  268: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  269: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  270: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  271: 				      "FFFFFF",($width+$extrawidth)));
  272: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  273: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  274: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  275: 				      "FFFFFF",($width+$extrawidth)));
  276: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  277: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  278: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  279: 				      "FF0000",($width)));
  280: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  281: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  282: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  283: 				      "FF0000",($width)));
  284:     }
  285:     if ($mode eq 'answer' || $mode eq 'answeronly') {
  286: 	my $width = 1;
  287: 	my $extrawidth = 2;
  288: 	foreach my $area (@{ $areas }) {
  289: 	    if ($area=~/^rectangle:/) {
  290: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  291: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  292: 		my ($x1,$y1,$x2,$y2)=
  293: 		    ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  294: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
  295: 					      ($width+$extrawidth)));
  296: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  297: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  298: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
  299: 	    } elsif ($area=~/^polygon:(.*)/) {
  300: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  301: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  302: 		$x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
  303: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  304: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  305: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  306: 		$x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
  307: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  308: 	    }
  309: 	}
  310:     }
  311:     &Apache::lonnet::appenv(\%x);
  312:     return $id;
  313: }
  314: 
  315: sub displayfoils {
  316:     my ($target,@whichopt) = @_;
  317:     my $result ='';
  318:     my $temp=1;
  319:     my @images;
  320:     foreach my $name (@whichopt) {
  321: 	$result.=$Apache::response::foilgroup{"$name.text"};
  322: 	&Apache::lonxml::debug("Text is $result");
  323: 	if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
  324: 	my $image=$Apache::response::foilgroup{"$name.image"};
  325: 	&Apache::lonxml::debug("image is $image");
  326: 	if ( ($target eq 'web' || $target eq 'answer') 
  327: 	     && $image !~ /^https?\:/ ) {
  328: 	    $image=&clean_up_image($image);
  329: 	}
  330: 	push(@images,$image);
  331: 	&Apache::lonxml::debug("image is $image");
  332: 	if ( &Apache::response::show_answer() ) {
  333: 	    if ($target eq 'tex') {
  334: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  335: 	    } else {
  336: 		my $token=&prep_image($image,'answer',$name);
  337: 		$result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
  338: 	    }
  339: 	} else {
  340: 	    if ($target eq 'tex') {
  341: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  342: 	    } else {
  343: 		my $respid=$Apache::inputtags::response['-1'];
  344: 		my $token=&prep_image($image,'submission',$name);
  345: 		my $input_id = "HWVAL_$respid:$temp";
  346: 		my $id = $env{'form.request.prefix'}.$input_id;
  347: 		$result.='<img onclick="image_response_click(\''.$id.'\',event);"
  348: 	                       src="/adm/randomlabel.png?token='.$token.'" 
  349:                                id="'.$id.'_imageresponse"
  350:                                 />'.
  351: 			       '<br />'.
  352: 			       '<input type="hidden" name="'.$input_id.'_token" value="'.$token.'" />'.
  353: 			       '<input type="hidden" name="'.$input_id.'" value="'.
  354: 			       join(':',&get_submission($name)).'" />';
  355: 	    }
  356: 	}
  357: 	$temp++;
  358:     }
  359:     if ($target eq 'web') {
  360:         &get_prior_options(\@images,\@whichopt);
  361:     }
  362:     return $result;
  363: }
  364: 
  365: sub get_prior_options {
  366:     my ($currimages,$curropt) = @_;
  367:     return unless((ref($curropt) eq 'ARRAY') && 
  368:                   (ref($currimages) eq 'ARRAY'));
  369:     my $part = $Apache::inputtags::part;
  370:     my $respid   = $Apache::inputtags::response[-1];
  371:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
  372:         my $partprefix = "$i:resource.$part";
  373:         my $sub_key = "$partprefix.$respid.submission";
  374:         next if (!exists($Apache::lonhomework::history{$sub_key}));
  375:         my $type_key = "$partprefix.type";
  376:         my @whichopt = ();
  377:         my @images = ();
  378:         if ($Apache::lonhomework::history{$type_key} eq 'randomizetry') {
  379:             my $order_key = "$partprefix.$respid.foilorder";
  380:             @whichopt = &Apache::lonnet::str2array($Apache::lonhomework::history{$order_key});
  381:             if (@whichopt > 0) {
  382:                 foreach my $name (@whichopt) {
  383:                     my $image=$Apache::response::foilgroup{"$name.image"};
  384:                     if ($image !~ /^https?\:/ ) {
  385:                         $image=&clean_up_image($image);
  386:                     }
  387:                     push(@images,$image);
  388:                 }
  389:             }
  390:         } else {
  391:             @whichopt = @{$curropt};
  392:             @images = @{$currimages};
  393:         }
  394:         my $submission = $Apache::lonhomework::history{$sub_key};
  395:         my $output =  &format_prior_response('grade',$submission,
  396:                                              [\@images,\@whichopt]);
  397:         if (defined($output)) {
  398:             $Apache::inputtags::submission_display{$sub_key} = $output;
  399:         }
  400:     }
  401: }
  402: 
  403: sub format_prior_response {
  404:     my ($mode,$answer,$other_data) = @_;
  405:     
  406:     my $result;
  407: 
  408:     # make a copy of the data in the refs
  409:     my @images = @{ $other_data->[0] };
  410:     my @foils = @{ $other_data->[1] };
  411:     foreach my $name (@foils) {
  412: 	my $image = pop(@images);
  413: 	my ($x,$y) = &get_submission($name,$answer);
  414: 	my $token = &draw_image('submission',$image,$x,$y);
  415: 	$result .=
  416: 	    '<img class="LC_prior_image"
  417:                   src="/adm/randomlabel.png?token='.$token.'" /><br />';
  418:     }
  419:     return $result;
  420: }
  421: 
  422: sub display_answers {
  423:     my ($target,$whichopt)=@_;
  424: 
  425:     my $result=&Apache::response::answer_header('imageresponse');
  426:     foreach my $name (@$whichopt) {
  427: 	my $image=$Apache::response::foilgroup{"$name.image"};
  428: 	&Apache::lonxml::debug("image is $image");
  429: 	if ( ($target eq 'web' || $target eq 'answer')
  430: 	     && $image !~ /^https?\:/ ) {
  431: 	    $image = &clean_up_image($image);
  432: 	} 
  433: 	my $token=&prep_image($image,'answeronly',$name);
  434: 
  435: 	$result.=&Apache::response::answer_part('imageresponse',"<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n");
  436:     }
  437:     $result.=&Apache::response::answer_footer('imageresponse');
  438:     return $result;
  439: }
  440: 
  441: sub clean_up_image {
  442:     my ($image)=@_;
  443:     if ($image =~ /\s*<img\s*/) {
  444: 	($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
  445: 	if ($image !~ /^https?\:/) {
  446: 	    $image=&Apache::lonnet::hreflocation('',$image);
  447: 	}
  448: 	if (!$image) {
  449: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  450: 	}
  451:     } else {
  452: 	$image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
  453: 	&Apache::lonxml::debug("repcopying: $image");
  454: 	if (&Apache::lonnet::repcopy($image) ne 'ok') {
  455: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  456: 	}
  457:     }
  458:     return $image;
  459: }
  460: 
  461: sub gradefoils {
  462:     my (@whichopt) = @_;
  463: 
  464:     my $partid = $Apache::inputtags::part;
  465:     my $id     = $Apache::inputtags::response['-1'];
  466: 
  467:     if ($Apache::lonhomework::type eq 'exam') {
  468: 	&Apache::response::scored_response($partid,$id);
  469: 	return;
  470:     }
  471:     
  472:     my @results;
  473:     my $temp=1;
  474:     my %response;
  475:     foreach my $name (@whichopt) {
  476: 	my ($x,$y) = split(':',$env{"form.HWVAL_$id:$temp"});
  477: 	$response{$name} = $env{"form.HWVAL_$id:$temp"};
  478: 	&Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
  479:         my @areas; 
  480:         if (ref($Apache::response::foilgroup{"$name.area"}) eq 'ARRAY') {
  481:             @areas = @{ $Apache::response::foilgroup{"$name.area"} };
  482:         }
  483: 	if (defined($x) && defined($y) && @areas) {
  484: 	    my $grade="INCORRECT";
  485: 	    foreach my $area (@areas) {
  486: 		&Apache::lonxml::debug("Area is $area for $name");
  487: 		$area =~ m/([a-z]*):/;
  488: 		&Apache::lonxml::debug("Area of type $1");
  489: 		if ($1 eq 'rectangle') {
  490: 		    $grade=&grade_rectangle($area,$x,$y);
  491: 		} elsif ($1 eq 'polygon') {
  492: 		    $grade=&grade_polygon($area,$x,$y);
  493: 		} else {
  494: 		    &Apache::lonxml::error(&mt('Unknown area style [_1]',$area));
  495: 		}
  496: 		&Apache::lonxml::debug("Area said $grade");
  497: 		if ($grade eq 'APPROX_ANS') { last; }
  498: 	    }
  499: 	    &Apache::lonxml::debug("Foil was $grade");
  500: 	    push(@results, $grade)
  501: 	} else {
  502: 	    push(@results, 'MISSING_ANSWER')
  503: 	}
  504: 	$temp++;
  505:     }
  506:     if ($Apache::lonhomework::type eq 'randomizetry') {
  507:         $Apache::lonhomework::results{"resource.$partid.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
  508:     }
  509:     my ($result) = &Apache::inputtags::finalizeawards(\@results,[]);
  510:     &Apache::lonxml::debug("Question is $result");
  511: 
  512:     my $part=$Apache::inputtags::part;
  513:     my %previous=
  514: 	&Apache::response::check_for_previous(&stringify_submission(\%response),
  515: 					      $part,$id);
  516:     if ($result) { 
  517: 	if ($Apache::lonhomework::type eq 'survey') { 
  518:             $result='SUBMITTED';
  519:         } elsif ($Apache::lonhomework::type eq 'surveycred') { 
  520:             $result='SUBMITTED_CREDIT'; 
  521:         } elsif ($Apache::lonhomework::type eq 'anonsurvey') { 
  522:             $result='ANONYMOUS'; 
  523:         } elsif ($Apache::lonhomework::type eq 'anonsurveycred') { 
  524:             $result='ANONYMOUS_CREDIT'; 
  525:         }
  526:     }
  527: 
  528:     &Apache::response::handle_previous(\%previous,$result);
  529:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  530: 	&stringify_submission(\%response);
  531:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
  532:     return;
  533: }
  534: 
  535: sub stringify_submission {
  536:     my ($response) = @_;
  537:     return &Apache::lonnet::hash2str(%{ $response });
  538: }
  539: 
  540: sub get_submission {
  541:     my ($name,$string) = @_;
  542: 
  543:     if (!defined($string)) {
  544: 	my $part=$Apache::inputtags::part;
  545: 	my $respid=$Apache::inputtags::response['-1'];
  546:    	$string = 
  547: 	    $Apache::lonhomework::history{"resource.$part.$respid.submission"};
  548:     }
  549: 
  550:     if ($string !~ /=/) {
  551: 	return split(':',$string);
  552:     } else {
  553: 	my %response = &Apache::lonnet::str2hash($string);
  554: 	return split(':',$response{$name});
  555:     }
  556: }
  557: 
  558: sub end_foilgroup {
  559:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  560:     my $result='';
  561:     my @whichopt;
  562: 
  563:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  564: 	$target eq 'analyze' || $target eq 'answer') {
  565: 
  566: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  567: 	if ($count>$max) { $count=$max }
  568: 	&Apache::lonxml::debug("Count is $count from $max");
  569: 
  570: 	@whichopt = &whichfoils($max);
  571: 
  572: 	if ($target eq 'web' || $target eq 'tex') {
  573: 	    $result=&displayfoils($target,@whichopt);
  574:             if ($Apache::lonhomework::type eq 'randomizetry') {
  575:                 if ($target eq 'web') {
  576:                     &get_prior_options($target,@whichopt);
  577:                 }
  578:             }
  579: 	    $Apache::lonxml::post_evaluate=0;
  580: 	} elsif ($target eq 'grade') {
  581: 	    if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
  582: 	} elsif ( $target eq 'analyze') {
  583: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
  584: 						      ['text','image','area']);
  585: 	} elsif ($target eq 'answer'
  586: 		 && $env{'form.answer_output_mode'} ne 'tex') {
  587: 	    $result=&display_answers($target,\@whichopt);
  588: 	}
  589: 
  590:     } elsif ($target eq 'edit') {
  591: 	$result=&Apache::edit::end_table();
  592:     }
  593:     &Apache::response::poprandomnumber();
  594:     return $result;
  595: }
  596: 
  597: sub start_conceptgroup {
  598:     $Apache::imageresponse::conceptgroup=1;
  599:     %Apache::response::conceptgroup=();
  600:     return '';
  601: }
  602: 
  603: sub end_conceptgroup {
  604:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  605:     $Apache::imageresponse::conceptgroup=0;
  606:     my $result;
  607:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  608: 	$target eq 'analyze') {
  609: 	&Apache::response::pick_foil_for_concept($target,
  610: 						 ['area','text','image'],
  611: 						 \%Apache::hint::image,
  612: 						 $parstack,$safeeval);
  613:     } elsif ($target eq 'edit') {
  614: 	$result=&Apache::edit::end_table();
  615:     }
  616:     return $result;
  617: }
  618: 
  619: sub insert_foil {
  620:     return '
  621:        <foil>
  622:            <image></image>
  623:            <text></text>
  624:            <rectangle></rectangle>
  625:        </foil>
  626: ';
  627: }
  628: 
  629: $Apache::imageresponse::curname='';
  630: sub start_foil {
  631:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  632:     my $result;
  633:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  634: 	$target eq 'analyze' || $target eq 'answer') {
  635: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  636: 	if ($name eq "") {
  637: 	    &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  638: 	    $name=$Apache::lonxml::curdepth;
  639: 	}
  640: 	if (defined($Apache::response::foilnames{$name})) {
  641: 	    &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique."
  642:                                       ,'<b><tt>'.$name.'</tt></b>'));
  643: 	}
  644: 	$Apache::response::foilnames{$name}++;
  645: 	if ( $Apache::imageresponse::conceptgroup
  646: 	     && !&Apache::response::showallfoils()
  647: 	     ) {
  648: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
  649: 	} else {
  650: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
  651: 	}
  652: 	$Apache::imageresponse::curname=$name;
  653:     } elsif ($target eq 'edit') {
  654: 	$result  = &Apache::edit::tag_start($target,$token);
  655: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
  656: 	$result .= &Apache::edit::end_row().
  657: 	    &Apache::edit::start_spanning_row();
  658:     } elsif ($target eq 'modified') {
  659: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  660: 						     $safeeval,'name');
  661: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  662:     } 
  663:     return $result;;
  664: }
  665: 
  666: sub end_foil {
  667:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  668:     my $result;
  669:     if ($target eq 'edit') {
  670: 	$result=&Apache::edit::end_table();
  671:     }
  672:     return $result;
  673: }
  674: 
  675: sub start_text {
  676:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  677:     my $result='';
  678:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  679: 	|| $target eq 'answer') { 
  680: 	&Apache::lonxml::startredirection; 
  681:     } elsif ($target eq 'edit') {
  682: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
  683: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
  684: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
  685: 	    &Apache::edit::end_row();
  686:     } elsif ($target eq "modified") {
  687: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
  688:     }
  689:     return $result;
  690: }
  691: 
  692: sub end_text {
  693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  694:     my $result;
  695:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  696: 	|| $target eq 'answer') {
  697: 	my $name = $Apache::imageresponse::curname;
  698: 	if ( $Apache::imageresponse::conceptgroup
  699: 	     && !&Apache::response::showallfoils()
  700: 	     ) {
  701: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  702: 	} else {
  703: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  704: 	}
  705:     } elsif ($target eq 'edit') {
  706: 	$result=&Apache::edit::end_table();
  707:     }
  708:     return $result;
  709: }
  710: 
  711: sub start_image {
  712:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  713:     my $result='';
  714:     my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
  715:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  716: 	|| $target eq 'answer') { 
  717: 	&Apache::lonxml::startredirection; 
  718:     } elsif ($target eq 'edit') {
  719: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
  720: 	$Apache::edit::bgimgsrc=$bgimg;
  721: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
  722: 
  723: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
  724: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
  725: 	$result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
  726: 	$result.=&Apache::edit::search(undef,'textnode').
  727: 	    &Apache::edit::end_row();
  728:     } elsif ($target eq "modified") {
  729: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
  730:     }
  731:     return $result;
  732: }
  733: 
  734: sub end_image {
  735:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  736:     my $result;
  737:     my $name = $Apache::imageresponse::curname;
  738:     if ($target eq 'web' || $target eq 'answer') {
  739: 	my $image = &Apache::lonxml::endredirection();
  740: 	&Apache::lonxml::debug("original image is $image");
  741: 	if ( $Apache::imageresponse::conceptgroup
  742: 	     && !&Apache::response::showallfoils()
  743: 	     ) {
  744: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  745: 	} else {
  746: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  747: 	}
  748:     } elsif ($target eq 'analyze') {
  749: 	my $image = &Apache::lonxml::endredirection();
  750: 	if ( $Apache::imageresponse::conceptgroup
  751: 	     && !&Apache::response::showallfoils()
  752: 	     ) {
  753: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  754: 	} else {
  755: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  756: 	}
  757:     } elsif ($target eq 'edit') {
  758: 	$result=&Apache::edit::end_table();
  759:     } elsif ($target eq 'tex') {
  760: 	my $src = &Apache::lonxml::endredirection();
  761: 
  762: 	#  There may be all sorts of whitespace on fore and aft:
  763: 
  764: 	$src =~ s/\s+$//s;
  765: 	$src =~ s/^\s+//s;
  766: 
  767: 	#
  768: 	#  Gnuplot e.g. just generates the latex to put inplace.
  769: 	#
  770: 	my $graphinclude;
  771: 	if ($src =~ /^%DYNAMICIMAGE/) {
  772: 	    # This is needed because the newline is not always passed -> tex.
  773: 	    # At present we don't care about the sizing info.
  774: 
  775: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
  776: 	    $graphinclude = $src;
  777: 	    $graphinclude =~ s/^$commentline//;
  778: 	} elsif (!($src =~ /\\/)) {
  779: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
  780: 	    my ($height_param,$width_param)=
  781: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
  782: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
  783: 	} else {
  784: 	    $graphinclude = $src;   # Already fully formed.
  785: 	}
  786: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
  787:     } 
  788:     return $result;
  789: }
  790: 
  791: sub start_rectangle {
  792:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  793:     my $result='';
  794:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  795: 	$target eq 'analyze' || $target eq 'answer') { 
  796: 	&Apache::lonxml::startredirection; 
  797:     } elsif ($target eq 'edit') {
  798: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
  799: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
  800: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
  801: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
  802: 	    &Apache::edit::end_row();
  803:     } elsif ($target eq "modified") {
  804: 	&Apache::edit::deletecoorddata();
  805: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
  806:     }
  807:     return $result;
  808: }
  809: 
  810: sub grade_rectangle {
  811:     my ($spec,$x,$y) = @_;
  812:     &Apache::lonxml::debug("Spec is $spec");
  813:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  814:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
  815:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  816:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  817:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
  818: 	return 'APPROX_ANS';
  819:     }
  820:     return 'INCORRECT';
  821: }
  822: 
  823: sub end_rectangle {
  824:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  825:     my $result;
  826:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  827: 	$target eq 'analyze' || $target eq 'answer') {
  828: 	my $name = $Apache::imageresponse::curname;
  829: 	my $area = &Apache::lonxml::endredirection;
  830: 	$area=~s/\s//g;
  831: 	&Apache::lonxml::debug("out is $area for $name");
  832: 	if ( $Apache::imageresponse::conceptgroup
  833: 	     && !&Apache::response::showallfoils()
  834: 	     ) {
  835: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  836: 	} else {
  837: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  838: 	}
  839:     } elsif ($target eq 'edit') {
  840: 	$result=&Apache::edit::end_table();
  841:     }
  842:     return $result;
  843: }
  844: 
  845: sub start_polygon {
  846:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  847:     my $result='';
  848:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  849: 	$target eq 'analyze' || $target eq 'answer') { 
  850: 	&Apache::lonxml::startredirection; 
  851:     } elsif ($target eq 'edit') {
  852: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
  853: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
  854: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
  855: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
  856: 	    &Apache::edit::end_row();
  857:     } elsif ($target eq "modified") {
  858: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
  859:     }
  860:     return $result;
  861: }
  862: 
  863: sub grade_polygon {
  864:     my ($spec,$x,$y) = @_;
  865:     &Apache::lonxml::debug("Spec is $spec");
  866:     $spec=~s/^polygon://;
  867:     my @polygon;
  868:     foreach my $coord (split('-',$spec)) {
  869: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
  870: 	&Apache::lonxml::debug("x $x y $y");
  871: 	push @polygon, {'x'=>$x,'y'=>$y};
  872:     }
  873:     #make end point start point
  874:     push @polygon, $polygon[0];
  875:     # cribbed from
  876:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
  877:     my $crossing = 0;    # the crossing number counter
  878: 
  879:     # loop through all edges of the polygon
  880:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
  881: 	if ((($polygon[$i]->{'y'} <= $y)
  882: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
  883: 	    || 
  884: 	    (($polygon[$i]->{'y'} > $y) 
  885: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
  886: 	    # compute the actual edge-ray intersect x-coordinate
  887:             my $vt = ($y - $polygon[$i]->{'y'}) 
  888: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
  889:             if ($x < $polygon[$i]->{'x'} + $vt * 
  890: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
  891:                 $crossing++;   # a valid crossing of y=P.y right of P.x
  892: 	    }
  893: 	}
  894:     }
  895: 
  896:     # 0 if even (out), and 1 if odd (in)
  897:     if ($crossing%2) {
  898: 	return 'APPROX_ANS';
  899:     } else {
  900: 	return 'INCORRECT';
  901:     }
  902: }
  903: 
  904: sub end_polygon {
  905:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  906:     my $result;
  907:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  908: 	$target eq 'analyze' || $target eq 'answer') {
  909: 	my $name = $Apache::imageresponse::curname;
  910: 	my $area = &Apache::lonxml::endredirection;
  911: 	$area=~s/\s*//g;
  912: 	&Apache::lonxml::debug("out is $area for $name");
  913: 	if ( $Apache::imageresponse::conceptgroup
  914: 	     && !&Apache::response::showallfoils()
  915: 	     ) {
  916: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
  917: 	} else {
  918: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
  919: 	}
  920:     } elsif ($target eq 'edit') {
  921: 	$result=&Apache::edit::end_table();
  922:     }
  923:     return $result;
  924: }
  925: 1;
  926: __END__
  927:  

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