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

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

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