Annotation of loncom/homework/imageresponse.pm, revision 1.78

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

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