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

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

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