File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.87: download - view: text, annotated - select for diffs
Tue Sep 25 23:47:59 2007 UTC (16 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#1566 - finish supprot for imageresponse in a .page

    1: 
    2: # The LearningOnline Network with CAPA
    3: # image click response style
    4: #
    5: # $Id: imageresponse.pm,v 1.87 2007/09/25 23:47:59 albertel 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: package Apache::imageresponse;
   33: use strict;
   34: use Image::Magick();
   35: use Apache::randomlylabel();
   36: use Apache::londefdef();
   37: use Apache::Constants qw(:common :http);
   38: use Apache::lonlocal;
   39: use Apache::lonnet;
   40: use lib '/home/httpd/lib/perl/';
   41: use LONCAPA;
   42:  
   43: 
   44: BEGIN {
   45:     &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
   46: }
   47: 
   48: sub start_imageresponse {
   49:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   50:     my $result;
   51:     #when in a radiobutton response use these
   52:     &Apache::lonxml::register('Apache::imageresponse',
   53: 			      ('foilgroup','foil','text','image','rectangle',
   54: 			       'polygon','conceptgroup'));
   55:     push (@Apache::lonxml::namespace,'imageresponse');
   56:     my $id = &Apache::response::start_response($parstack,$safeeval);
   57:     undef(%Apache::response::foilnames);
   58:     if ($target eq 'meta') {
   59: 	$result=&Apache::response::meta_package_write('imageresponse');
   60:     } elsif ($target eq 'analyze') {
   61: 	my $part_id="$Apache::inputtags::part.$id";
   62: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   63: 	push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} },
   64: 	      1);
   65:     }
   66:     return $result;
   67: }
   68: 
   69: sub end_imageresponse {
   70:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   71: 
   72:     pop(@Apache::lonxml::namespace);
   73:     &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
   74: 
   75:     my $result;
   76:     if ($target eq 'edit') {
   77: 	$result=&Apache::edit::end_table();
   78:     } elsif ($target eq 'tex'
   79: 	     && $Apache::lonhomework::type eq 'exam') {
   80: 	$result=&Apache::inputtags::exam_score_line($target);
   81:     }
   82: 
   83:     undef(%Apache::response::foilnames);
   84:     
   85:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
   86: 	$target eq 'tex' || $target eq 'analyze') {
   87: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
   88:     }
   89:     &Apache::response::end_response();
   90: 
   91:     return $result;
   92: }
   93: 
   94: %Apache::response::foilgroup=();
   95: sub start_foilgroup {
   96:     %Apache::response::foilgroup=();
   97:     $Apache::imageresponse::conceptgroup=0;
   98:     &Apache::response::pushrandomnumber();
   99:     return '';
  100: }
  101: 
  102: sub getfoilcounts {
  103:     my ($parstack,$safeeval)=@_;
  104: 
  105:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  106:     # +1 since instructors will count from 1
  107:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  108:     #if (&Apache::response::showallfoils()) { $max=$count; }
  109:     return ($count,$max);
  110: }
  111: 
  112: sub whichfoils {
  113:     my ($max)=@_;
  114:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
  115:     my @names = @{ $Apache::response::foilgroup{'names'} };
  116:     my @whichopt =();
  117:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
  118: 	&Apache::lonxml::debug("Have $#whichopt max is $max");
  119: 	my $aopt;
  120: #	if (&Apache::response::showallfoils()) {
  121: #	    $aopt=0;
  122: #	} else {
  123: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
  124: #	}
  125: 	&Apache::lonxml::debug("From $#names elms, picking $aopt");
  126: 	$aopt=splice(@names,$aopt,1);
  127: 	&Apache::lonxml::debug("Picked $aopt");
  128: 	push (@whichopt,$aopt);
  129:     }
  130:     return @whichopt;
  131: }
  132: 
  133: sub prep_image {
  134:     my ($image,$mode,$name)=@_;
  135: 
  136:     my ($x,$y)= &get_submission($name);
  137:     &Apache::lonxml::debug("for $name drawing click at $x and $y");
  138:     &draw_image($mode,$image,$x,$y,$Apache::response::foilgroup{"$name.area"});
  139: }
  140: 
  141: sub draw_image {
  142:     my ($mode,$image,$x,$y,$areas) = @_;
  143: 
  144:     my $id=&Apache::loncommon::get_cgi_id();
  145: 
  146:     my (%x,$i);
  147:     $x{"cgi.$id.BGIMG"}=&escape($image);
  148: 
  149:     #draws 2 xs on the image at the clicked location
  150:     #one in white and then one in red on top of the one in white
  151: 
  152:     if (defined($x)    && $x =~/\S/ 
  153: 	&& defined($y) && $y =~/\S/ 
  154: 	&& ($mode eq 'submission' || !&Apache::response::show_answer())
  155: 	&& $mode ne 'answeronly') {
  156: 	my $length = 6;
  157: 	my $width = 1;
  158: 	my $extrawidth = 2;
  159: 	my $xmin=($x-$length);
  160: 	my $xmax=($x+$length); 
  161: 	my $ymin=($y-$length);
  162: 	my $ymax=($y+$length);
  163: 
  164: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  165: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  166: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  167: 				      "FFFFFF",($width+$extrawidth)));
  168: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  169: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  170: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  171: 				      "FFFFFF",($width+$extrawidth)));
  172: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  173: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  174: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  175: 				      "FF0000",($width)));
  176: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  177: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  178: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  179: 				      "FF0000",($width)));
  180:     }
  181:     if ($mode eq 'answer' || $mode eq 'answeronly') {
  182: 	my $width = 1;
  183: 	my $extrawidth = 2;
  184: 	foreach my $area (@{ $areas }) {
  185: 	    if ($area=~/^rectangle:/) {
  186: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  187: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  188: 		my ($x1,$y1,$x2,$y2)=
  189: 		    ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  190: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
  191: 					      ($width+$extrawidth)));
  192: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  193: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  194: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
  195: 	    } elsif ($area=~/^polygon:(.*)/) {
  196: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  197: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  198: 		$x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
  199: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  200: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  201: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  202: 		$x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
  203: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  204: 	    }
  205: 	}
  206:     }
  207:     &Apache::lonnet::appenv(%x);
  208:     return $id;
  209: }
  210: 
  211: sub displayfoils {
  212:     my ($target,@whichopt) = @_;
  213:     my $result ='';
  214:     my $temp=1;
  215:     my @images;
  216:     foreach my $name (@whichopt) {
  217: 	$result.=$Apache::response::foilgroup{"$name.text"};
  218: 	&Apache::lonxml::debug("Text is $result");
  219: 	if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
  220: 	my $image=$Apache::response::foilgroup{"$name.image"};
  221: 	&Apache::lonxml::debug("image is $image");
  222: 	if ( $target eq 'web' && $image !~ /^http:/ ) {
  223: 	    $image=&clean_up_image($image);
  224: 	}
  225: 	push(@images,$image);
  226: 	&Apache::lonxml::debug("image is $image");
  227: 	if ( &Apache::response::show_answer() ) {
  228: 	    if ($target eq 'tex') {
  229: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  230: 	    } else {
  231: 		my $token=&prep_image($image,'answer',$name);
  232: 		$result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
  233: 	    }
  234: 	} else {
  235: 	    if ($target eq 'tex') {
  236: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  237: 	    } else {
  238: 		my $respid=$Apache::inputtags::response['-1'];
  239: 		my $token=&prep_image($image,'submission',$name);
  240: 		my $input_id = "HWVAL_$respid:$temp";
  241: 		my $id = $env{'form.request.prefix'}.$input_id;
  242: 		$result.='<img onclick="image_response_click(\''.$id.'\',event);"
  243: 	                       src="/adm/randomlabel.png?token='.$token.'" 
  244:                                id="'.$id.'_imageresponse"
  245:                                 />'.
  246: 			       '<br />'.
  247: 			       '<input type="hidden" name="'.$input_id.'_token" value="'.$token.'" />'.
  248: 			       '<input type="hidden" name="'.$input_id.'" value="'.
  249: 			       join(':',&get_submission($name)).'" />';
  250: 	    }
  251: 	}
  252: 	$temp++;
  253:     }
  254:     if ($target eq 'web') {
  255: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
  256: 						  [\@images,\@whichopt]);
  257:     }
  258:     return $result;
  259: }
  260: 
  261: sub format_prior_response {
  262:     my ($mode,$answer,$other_data) = @_;
  263:     
  264:     my $result;
  265: 
  266:     # make a copy of the data in the refs
  267:     my @images = @{ $other_data->[0] };
  268:     my @foils = @{ $other_data->[1] };
  269:     foreach my $name (@foils) {
  270: 	my $image = pop(@images);
  271: 	my ($x,$y) = &get_submission($name,$answer);
  272: 	my $token = &draw_image('submission',$image,$x,$y);
  273: 	$result .=
  274: 	    '<img class="LC_prior_image"
  275:                   src="/adm/randomlabel.png?token='.$token.'" /><br />';
  276:     }
  277:     return $result;
  278: }
  279: 
  280: sub display_answers {
  281:     my ($target,$whichopt)=@_;
  282: 
  283:     my $result;
  284:     foreach my $name (@$whichopt) {
  285: 	my $image=$Apache::response::foilgroup{"$name.image"};
  286: 	&Apache::lonxml::debug("image is $image");
  287: 	if ( $target eq 'web' && $image !~ /^http:/ ) {
  288: 	    $image = &clean_up_image($image);
  289: 	} 
  290: 	my $token=&prep_image($image,'answeronly',$name);
  291: 
  292: 	$result.=&Apache::response::answer_header('imageresponse');
  293: 	$result.=&Apache::response::answer_part('imageresponse',"<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n");
  294: 	$result.=&Apache::response::answer_footer('imageresponse');
  295:     }
  296:     return $result;
  297: }
  298: 
  299: sub clean_up_image {
  300:     my ($image)=@_;
  301:     if ($image =~ /\s*<img\s*/) {
  302: 	($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
  303: 	if ($image !~ /^http:/) {
  304: 	    $image=&Apache::lonnet::hreflocation('',$image);
  305: 	}
  306: 	if (!$image) {
  307: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  308: 	}
  309:     } else {
  310: 	$image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
  311: 	&Apache::lonxml::debug("repcopying: $image");
  312: 	if (&Apache::lonnet::repcopy($image) ne 'ok') {
  313: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  314: 	}
  315:     }
  316:     return $image;
  317: }
  318: 
  319: sub gradefoils {
  320:     my (@whichopt) = @_;
  321: 
  322:     my $partid = $Apache::inputtags::part;
  323:     my $id     = $Apache::inputtags::response['-1'];
  324: 
  325:     if ($Apache::lonhomework::type eq 'exam') {
  326: 	&Apache::response::scored_response($partid,$id);
  327: 	return;
  328:     }
  329:     
  330:     my @results;
  331:     my $temp=1;
  332:     my %response;
  333:     foreach my $name (@whichopt) {
  334: 	my ($x,$y) = split(':',$env{"form.HWVAL_$id:$temp"});
  335: 	$response{$name} = $env{"form.HWVAL_$id:$temp"};
  336: 	&Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
  337: 	if (defined($x) && defined($y) &&
  338: 	    defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
  339: 	    my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
  340: 	    my $grade="INCORRECT";
  341: 	    foreach my $area (@areas) {
  342: 		&Apache::lonxml::debug("Area is $area for $name");
  343: 		$area =~ m/([a-z]*):/;
  344: 		&Apache::lonxml::debug("Area of type $1");
  345: 		if ($1 eq 'rectangle') {
  346: 		    $grade=&grade_rectangle($area,$x,$y);
  347: 		} elsif ($1 eq 'polygon') {
  348: 		    $grade=&grade_polygon($area,$x,$y);
  349: 		} else {
  350: 		    &Apache::lonxml::error("Unknown area style $area");
  351: 		}
  352: 		&Apache::lonxml::debug("Area said $grade");
  353: 		if ($grade eq 'APPROX_ANS') { last; }
  354: 	    }
  355: 	    &Apache::lonxml::debug("Foil was $grade");
  356: 	    push(@results, $grade)
  357: 	} else {
  358: 	    push(@results, 'MISSING_ANSWER')
  359: 	}
  360: 	$temp++;
  361:     }
  362:     my ($result) = &Apache::inputtags::finalizeawards(\@results,[]);
  363:     &Apache::lonxml::debug("Question is $result");
  364: 
  365:     my $part=$Apache::inputtags::part;
  366:     my %previous=
  367: 	&Apache::response::check_for_previous(&stringify_submission(\%response),
  368: 					      $part,$id);
  369:     if ($result 
  370: 	&& $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
  371:     &Apache::response::handle_previous(\%previous,$result);
  372:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  373: 	&stringify_submission(\%response);
  374:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
  375:     return;
  376: }
  377: 
  378: sub stringify_submission {
  379:     my ($response) = @_;
  380:     return &Apache::lonnet::hash2str(%{ $response });
  381: 
  382:     
  383: }
  384: 
  385: sub get_submission {
  386:     my ($name,$string) = @_;
  387: 
  388:     if (!defined($string)) {
  389: 	my $part=$Apache::inputtags::part;
  390: 	my $respid=$Apache::inputtags::response['-1'];
  391:    	$string = 
  392: 	    $Apache::lonhomework::history{"resource.$part.$respid.submission"};
  393:     }
  394: 
  395:     if ($string !~ /=/) {
  396: 	return split(':',$string);
  397:     } else {
  398: 	my %response = &Apache::lonnet::str2hash($string);
  399: 	return split(':',$response{$name});
  400:     }
  401: }
  402: 
  403: sub end_foilgroup {
  404:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  405:     my $result='';
  406:     my @whichopt;
  407: 
  408:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  409: 	$target eq 'analyze' || $target eq 'answer') {
  410: 
  411: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  412: 	if ($count>$max) { $count=$max }
  413: 	&Apache::lonxml::debug("Count is $count from $max");
  414: 
  415: 	@whichopt = &whichfoils($max);
  416: 
  417: 	if ($target eq 'web' || $target eq 'tex') {
  418: 	    $result=&displayfoils($target,@whichopt);
  419: 	    $Apache::lonxml::post_evaluate=0;
  420: 	} elsif ($target eq 'grade') {
  421: 	    if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
  422: 	} elsif ( $target eq 'analyze') {
  423: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
  424: 						      ['text','image','area']);
  425: 	} elsif ($target eq 'answer'
  426: 		 && $env{'form.answer_output_mode'} ne 'tex') {
  427: 	    $result=&display_answers($target,\@whichopt);
  428: 	}
  429: 
  430:     } elsif ($target eq 'edit') {
  431: 	$result=&Apache::edit::end_table();
  432:     }
  433:     &Apache::response::poprandomnumber();
  434:     return $result;
  435: }
  436: 
  437: sub start_conceptgroup {
  438:     $Apache::imageresponse::conceptgroup=1;
  439:     %Apache::response::conceptgroup=();
  440:     return '';
  441: }
  442: 
  443: sub end_conceptgroup {
  444:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  445:     $Apache::imageresponse::conceptgroup=0;
  446:     my $result;
  447:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  448: 	$target eq 'analyze') {
  449: 	&Apache::response::pick_foil_for_concept($target,
  450: 						 ['area','text','image'],
  451: 						 \%Apache::hint::image,
  452: 						 $parstack,$safeeval);
  453:     } elsif ($target eq 'edit') {
  454: 	$result=&Apache::edit::end_table();
  455:     }
  456:     return $result;
  457: }
  458: 
  459: sub insert_foil {
  460:     return '
  461:        <foil>
  462:            <image></image>
  463:            <text></text>
  464:            <rectangle></rectangle>
  465:        </foil>
  466: ';
  467: }
  468: 
  469: $Apache::imageresponse::curname='';
  470: sub start_foil {
  471:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  472:     my $result;
  473:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  474: 	$target eq 'analyze' || $target eq 'answer') {
  475: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  476: 	if ($name eq "") {
  477: 	    &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
  478: 	    $name=$Apache::lonxml::curdepth;
  479: 	}
  480: 	if (defined($Apache::response::foilnames{$name})) {
  481: 	    &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
  482: 	}
  483: 	$Apache::response::foilnames{$name}++;
  484: 	if ( $Apache::imageresponse::conceptgroup
  485: 	     #&& !&Apache::response::showallfoils()
  486: 	     ) {
  487: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
  488: 	} else {
  489: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
  490: 	}
  491: 	$Apache::imageresponse::curname=$name;
  492:     } elsif ($target eq 'edit') {
  493: 	$result  = &Apache::edit::tag_start($target,$token);
  494: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
  495: 	$result .= &Apache::edit::end_row().
  496: 	    &Apache::edit::start_spanning_row();
  497:     } elsif ($target eq 'modified') {
  498: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  499: 						     $safeeval,'name');
  500: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  501:     } 
  502:     return $result;;
  503: }
  504: 
  505: sub end_foil {
  506:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  507:     my $result;
  508:     if ($target eq 'edit') {
  509: 	$result=&Apache::edit::end_table();
  510:     }
  511:     return $result;
  512: }
  513: 
  514: sub start_text {
  515:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  516:     my $result='';
  517:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  518: 	|| $target eq 'answer') { 
  519: 	&Apache::lonxml::startredirection; 
  520:     } elsif ($target eq 'edit') {
  521: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
  522: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
  523: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
  524: 	    &Apache::edit::end_row();
  525:     } elsif ($target eq "modified") {
  526: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
  527:     }
  528:     return $result;
  529: }
  530: 
  531: sub end_text {
  532:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  533:     my $result;
  534:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  535: 	|| $target eq 'answer') {
  536: 	my $name = $Apache::imageresponse::curname;
  537: 	if ( $Apache::imageresponse::conceptgroup
  538: 	     #&& !&Apache::response::showallfoils()
  539: 	     ) {
  540: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  541: 	} else {
  542: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  543: 	}
  544:     } elsif ($target eq 'edit') {
  545: 	$result=&Apache::edit::end_table();
  546:     }
  547:     return $result;
  548: }
  549: 
  550: sub start_image {
  551:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  552:     my $result='';
  553:     my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
  554:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  555: 	|| $target eq 'answer') { 
  556: 	&Apache::lonxml::startredirection; 
  557:     } elsif ($target eq 'edit') {
  558: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
  559: 	$Apache::edit::bgimgsrc=$bgimg;
  560: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
  561: 
  562: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
  563: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
  564: 	$result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
  565: 	$result.=&Apache::edit::search(undef,'textnode').
  566: 	    &Apache::edit::end_row();
  567:     } elsif ($target eq "modified") {
  568: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
  569:     }
  570:     return $result;
  571: }
  572: 
  573: sub end_image {
  574:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  575:     my $result;
  576:     my $name = $Apache::imageresponse::curname;
  577:     if ($target eq 'web' || $target eq 'answer') {
  578: 	my $image = &Apache::lonxml::endredirection();
  579: 	&Apache::lonxml::debug("original image is $image");
  580: 	if ( $Apache::imageresponse::conceptgroup
  581: 	     #&& !&Apache::response::showallfoils()
  582: 	     ) {
  583: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  584: 	} else {
  585: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  586: 	}
  587:     } elsif ($target eq 'analyze') {
  588: 	my $image = &Apache::lonxml::endredirection();
  589: 	if ( $Apache::imageresponse::conceptgroup
  590: 	     #&& !&Apache::response::showallfoils()
  591: 	     ) {
  592: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  593: 	} else {
  594: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  595: 	}
  596:     } elsif ($target eq 'edit') {
  597: 	$result=&Apache::edit::end_table();
  598:     } elsif ($target eq 'tex') {
  599: 	my $src = &Apache::lonxml::endredirection();
  600: 
  601: 	#  There may be all sorts of whitespace on fore and aft:
  602: 
  603: 	$src =~ s/\s+$//s;
  604: 	$src =~ s/^\s+//s;
  605: 
  606: 	#
  607: 	#  Gnuplot e.g. just generates the latex to put inplace.
  608: 	#
  609: 	my $graphinclude;
  610: 	if ($src =~ /^%DYNAMICIMAGE/) {
  611: 	    # This is needed because the newline is not always passed -> tex.
  612: 	    # At present we don't care about the sizing info.
  613: 
  614: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
  615: 	    $graphinclude = $src;
  616: 	    $graphinclude =~ s/^$commentline//;
  617: 	} else {
  618: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
  619: 	    my ($height_param,$width_param)=
  620: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
  621: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
  622: 	}
  623: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
  624:     } 
  625:     return $result;
  626: }
  627: 
  628: sub start_rectangle {
  629:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  630:     my $result='';
  631:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  632: 	$target eq 'analyze' || $target eq 'answer') { 
  633: 	&Apache::lonxml::startredirection; 
  634:     } elsif ($target eq 'edit') {
  635: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
  636: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
  637: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
  638: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
  639: 	    &Apache::edit::end_row();
  640:     } elsif ($target eq "modified") {
  641: 	&Apache::edit::deletecoorddata();
  642: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
  643:     }
  644:     return $result;
  645: }
  646: 
  647: sub grade_rectangle {
  648:     my ($spec,$x,$y) = @_;
  649:     &Apache::lonxml::debug("Spec is $spec");
  650:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  651:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
  652:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  653:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  654:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
  655: 	return 'APPROX_ANS';
  656:     }
  657:     return 'INCORRECT';
  658: }
  659: 
  660: sub end_rectangle {
  661:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  662:     my $result;
  663:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  664: 	$target eq 'analyze' || $target eq 'answer') {
  665: 	my $name = $Apache::imageresponse::curname;
  666: 	my $area = &Apache::lonxml::endredirection;
  667: 	$area=~s/\s//g;
  668: 	&Apache::lonxml::debug("out is $area for $name");
  669: 	if ( $Apache::imageresponse::conceptgroup
  670: 	     #&& !&Apache::response::showallfoils()
  671: 	     ) {
  672: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  673: 	} else {
  674: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  675: 	}
  676:     } elsif ($target eq 'edit') {
  677: 	$result=&Apache::edit::end_table();
  678:     }
  679:     return $result;
  680: }
  681: 
  682: sub start_polygon {
  683:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  684:     my $result='';
  685:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  686: 	$target eq 'analyze' || $target eq 'answer') { 
  687: 	&Apache::lonxml::startredirection; 
  688:     } elsif ($target eq 'edit') {
  689: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
  690: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
  691: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
  692: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
  693: 	    &Apache::edit::end_row();
  694:     } elsif ($target eq "modified") {
  695: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
  696:     }
  697:     return $result;
  698: }
  699: 
  700: sub grade_polygon {
  701:     my ($spec,$x,$y) = @_;
  702:     &Apache::lonxml::debug("Spec is $spec");
  703:     $spec=~s/^polygon://;
  704:     my @polygon;
  705:     foreach my $coord (split('-',$spec)) {
  706: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
  707: 	&Apache::lonxml::debug("x $x y $y");
  708: 	push @polygon, {'x'=>$x,'y'=>$y};
  709:     }
  710:     #make end point start point
  711:     push @polygon, $polygon[0];
  712:     # cribbed from
  713:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
  714:     my $crossing = 0;    # the crossing number counter
  715: 
  716:     # loop through all edges of the polygon
  717:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
  718: 	if ((($polygon[$i]->{'y'} <= $y)
  719: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
  720: 	    || 
  721: 	    (($polygon[$i]->{'y'} > $y) 
  722: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
  723: 	    # compute the actual edge-ray intersect x-coordinate
  724:             my $vt = ($y - $polygon[$i]->{'y'}) 
  725: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
  726:             if ($x < $polygon[$i]->{'x'} + $vt * 
  727: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
  728:                 $crossing++;   # a valid crossing of y=P.y right of P.x
  729: 	    }
  730: 	}
  731:     }
  732: 
  733:     # 0 if even (out), and 1 if odd (in)
  734:     if ($crossing%2) {
  735: 	return 'APPROX_ANS';
  736:     } else {
  737: 	return 'INCORRECT';
  738:     }
  739: }
  740: 
  741: sub end_polygon {
  742:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  743:     my $result;
  744:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  745: 	$target eq 'analyze' || $target eq 'answer') {
  746: 	my $name = $Apache::imageresponse::curname;
  747: 	my $area = &Apache::lonxml::endredirection;
  748: 	$area=~s/\s*//g;
  749: 	&Apache::lonxml::debug("out is $area for $name");
  750: 	if ( $Apache::imageresponse::conceptgroup
  751: 	     #&& !&Apache::response::showallfoils()
  752: 	     ) {
  753: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
  754: 	} else {
  755: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
  756: 	}
  757:     } elsif ($target eq 'edit') {
  758: 	$result=&Apache::edit::end_table();
  759:     }
  760:     return $result;
  761: }
  762: 1;
  763: __END__
  764:  

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