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

1.58      albertel    1: 
1.12      albertel    2: # The LearningOnline Network with CAPA
1.14      albertel    3: # image click response style
                      4: #
1.81    ! albertel    5: # $Id: imageresponse.pm,v 1.80 2006/11/06 23:44:42 foxr 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);
1.81    ! albertel  349: 	    $Apache::lonxml::post_evaluate=0;
1.36      albertel  350: 	} elsif ($target eq 'grade') {
1.65      albertel  351: 	    if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
1.37      albertel  352: 	} elsif ( $target eq 'analyze') {
                    353: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
                    354: 						      ['text','image','area']);
1.76      albertel  355: 	} elsif ($target eq 'answer'
                    356: 		 && $env{'form.answer_output_mode'} ne 'tex') {
1.75      albertel  357: 	    $result=&display_answers($target,\@whichopt);
1.37      albertel  358: 	}
1.75      albertel  359: 
1.36      albertel  360:     } elsif ($target eq 'edit') {
                    361: 	$result=&Apache::edit::end_table();
                    362:     }
1.53      albertel  363:     &Apache::response::poprandomnumber();
1.36      albertel  364:     return $result;
1.1       albertel  365: }
                    366: 
                    367: sub start_conceptgroup {
1.36      albertel  368:     $Apache::imageresponse::conceptgroup=1;
                    369:     %Apache::response::conceptgroup=();
                    370:     return '';
1.1       albertel  371: }
                    372: 
                    373: sub end_conceptgroup {
1.36      albertel  374:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    375:     $Apache::imageresponse::conceptgroup=0;
                    376:     my $result;
1.37      albertel  377:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
                    378: 	$target eq 'analyze') {
                    379: 	&Apache::response::pick_foil_for_concept($target,
                    380: 						 ['area','text','image'],
                    381: 						 \%Apache::hint::image,
                    382: 						 $parstack,$safeeval);
1.36      albertel  383:     } elsif ($target eq 'edit') {
                    384: 	$result=&Apache::edit::end_table();
                    385:     }
                    386:     return $result;
1.31      albertel  387: }
                    388: 
                    389: sub insert_foil {
                    390:     return '
                    391:        <foil>
                    392:            <image></image>
                    393:            <text></text>
                    394:            <rectangle></rectangle>
                    395:        </foil>
                    396: ';
1.1       albertel  397: }
                    398: 
1.12      albertel  399: $Apache::imageresponse::curname='';
1.1       albertel  400: sub start_foil {
1.36      albertel  401:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.67      albertel  402:     my $result;
1.37      albertel  403:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  404: 	$target eq 'analyze' || $target eq 'answer') {
1.36      albertel  405: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.72      albertel  406: 	if ($name eq "") {
1.71      albertel  407: 	    &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.59      albertel  408: 	    $name=$Apache::lonxml::curdepth;
                    409: 	}
1.51      albertel  410: 	if (defined($Apache::response::foilnames{$name})) {
                    411: 	    &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    412: 	}
1.52      albertel  413: 	$Apache::response::foilnames{$name}++;
1.36      albertel  414: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  415: 	     #&& !&Apache::response::showallfoils()
                    416: 	     ) {
1.36      albertel  417: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    418: 	} else {
                    419: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    420: 	}
                    421: 	$Apache::imageresponse::curname=$name;
1.67      albertel  422:     } elsif ($target eq 'edit') {
                    423: 	$result  = &Apache::edit::tag_start($target,$token);
                    424: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
                    425: 	$result .= &Apache::edit::end_row().
                    426: 	    &Apache::edit::start_spanning_row();
                    427:     } elsif ($target eq 'modified') {
                    428: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    429: 						     $safeeval,'name');
                    430: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    431:     } 
                    432:     return $result;;
1.1       albertel  433: }
                    434: 
                    435: sub end_foil {
1.26      albertel  436:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    437:     my $result;
                    438:     if ($target eq 'edit') {
                    439: 	$result=&Apache::edit::end_table();
                    440:     }
                    441:     return $result;
1.1       albertel  442: }
                    443: 
                    444: sub start_text {
1.36      albertel  445:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    446:     my $result='';
1.75      albertel  447:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    448: 	|| $target eq 'answer') { 
1.36      albertel  449: 	&Apache::lonxml::startredirection; 
                    450:     } elsif ($target eq 'edit') {
1.73      albertel  451: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36      albertel  452: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
                    453: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
                    454: 	    &Apache::edit::end_row();
                    455:     } elsif ($target eq "modified") {
1.39      albertel  456: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36      albertel  457:     }
                    458:     return $result;
1.1       albertel  459: }
                    460: 
                    461: sub end_text {
1.36      albertel  462:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    463:     my $result;
1.75      albertel  464:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    465: 	|| $target eq 'answer') {
1.36      albertel  466: 	my $name = $Apache::imageresponse::curname;
                    467: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  468: 	     #&& !&Apache::response::showallfoils()
                    469: 	     ) {
1.36      albertel  470: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    471: 	} else {
                    472: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    473: 	}
                    474:     } elsif ($target eq 'edit') {
                    475: 	$result=&Apache::edit::end_table();
                    476:     }
                    477:     return $result;
1.1       albertel  478: }
                    479: 
                    480: sub start_image {
1.36      albertel  481:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    482:     my $result='';
1.75      albertel  483:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    484: 	|| $target eq 'answer') { 
1.36      albertel  485: 	&Apache::lonxml::startredirection; 
                    486:     } elsif ($target eq 'edit') {
1.73      albertel  487: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36      albertel  488: 	$Apache::edit::bgimgsrc=$bgimg;
                    489: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
                    490: 
                    491: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
                    492: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
                    493: 	$result.=&Apache::edit::browse(undef,'textnode').' ';
                    494: 	$result.=&Apache::edit::search(undef,'textnode').
                    495: 	    &Apache::edit::end_row();
                    496:     } elsif ($target eq "modified") {
1.39      albertel  497: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36      albertel  498:     }
                    499:     return $result;
1.1       albertel  500: }
                    501: 
                    502: sub end_image {
1.36      albertel  503:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    504:     my $result;
                    505:     my $name = $Apache::imageresponse::curname;
1.75      albertel  506:     if ($target eq 'web' || $target eq 'answer') {
1.62      albertel  507: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  508: 	&Apache::lonxml::debug("original image is $image");
1.37      albertel  509: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  510: 	     #&& !&Apache::response::showallfoils()
                    511: 	     ) {
1.37      albertel  512: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    513: 	} else {
                    514: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    515: 	}
                    516:     } elsif ($target eq 'analyze') {
1.62      albertel  517: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  518: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  519: 	     #&& !&Apache::response::showallfoils()
                    520: 	     ) {
1.36      albertel  521: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    522: 	} else {
                    523: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    524: 	}
                    525:     } elsif ($target eq 'edit') {
                    526: 	$result=&Apache::edit::end_table();
                    527:     } elsif ($target eq 'tex') {
1.62      albertel  528: 	my $src = &Apache::lonxml::endredirection();
1.68      foxr      529: 
                    530: 	#  There may be all sorts of whitespace on fore and aft:
                    531: 
1.69      foxr      532: 	$src =~ s/\s+$//s;
                    533: 	$src =~ s/^\s+//s;
1.68      foxr      534: 
                    535: 	#
                    536: 	#  Gnuplot e.g. just generates the latex to put inplace.
                    537: 	#
                    538: 	my $graphinclude;
1.70      foxr      539: 	if ($src =~ /^%DYNAMICIMAGE/) {
                    540: 	    # This is needed because the newline is not always passed -> tex.
                    541: 	    # At present we don't care about the sizing info.
                    542: 
                    543: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
1.68      foxr      544: 	    $graphinclude = $src;
1.70      foxr      545: 	    $graphinclude =~ s/^$commentline//;
1.68      foxr      546: 	} else {
                    547: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
                    548: 	    my ($height_param,$width_param)=
                    549: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
                    550: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
                    551: 	}
                    552: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36      albertel  553:     } 
                    554:     return $result;
1.1       albertel  555: }
                    556: 
                    557: sub start_rectangle {
1.36      albertel  558:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    559:     my $result='';
1.38      albertel  560:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  561: 	$target eq 'analyze' || $target eq 'answer') { 
1.36      albertel  562: 	&Apache::lonxml::startredirection; 
                    563:     } elsif ($target eq 'edit') {
1.73      albertel  564: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36      albertel  565: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
                    566: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44      albertel  567: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36      albertel  568: 	    &Apache::edit::end_row();
                    569:     } elsif ($target eq "modified") {
1.44      albertel  570: 	&Apache::edit::deletecoorddata();
1.39      albertel  571: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36      albertel  572:     }
                    573:     return $result;
1.1       albertel  574: }
                    575: 
1.3       albertel  576: sub grade_rectangle {
1.36      albertel  577:     my ($spec,$x,$y) = @_;
                    578:     &Apache::lonxml::debug("Spec is $spec");
1.43      albertel  579:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36      albertel  580:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    581:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    582:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    583:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
                    584: 	return 'APPROX_ANS';
                    585:     }
                    586:     return 'INCORRECT';
1.3       albertel  587: }
                    588: 
1.1       albertel  589: sub end_rectangle {
1.36      albertel  590:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    591:     my $result;
1.38      albertel  592:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  593: 	$target eq 'analyze' || $target eq 'answer') {
1.36      albertel  594: 	my $name = $Apache::imageresponse::curname;
                    595: 	my $area = &Apache::lonxml::endredirection;
1.54      albertel  596: 	$area=~s/\s//g;
1.36      albertel  597: 	&Apache::lonxml::debug("out is $area for $name");
                    598: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  599: 	     #&& !&Apache::response::showallfoils()
                    600: 	     ) {
1.36      albertel  601: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    602: 	} else {
                    603: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43      albertel  604: 	}
                    605:     } elsif ($target eq 'edit') {
                    606: 	$result=&Apache::edit::end_table();
                    607:     }
                    608:     return $result;
                    609: }
                    610: 
                    611: sub start_polygon {
                    612:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    613:     my $result='';
                    614:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  615: 	$target eq 'analyze' || $target eq 'answer') { 
1.43      albertel  616: 	&Apache::lonxml::startredirection; 
                    617:     } elsif ($target eq 'edit') {
1.73      albertel  618: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43      albertel  619: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
                    620: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44      albertel  621: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43      albertel  622: 	    &Apache::edit::end_row();
                    623:     } elsif ($target eq "modified") {
                    624: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
                    625:     }
                    626:     return $result;
                    627: }
                    628: 
                    629: sub grade_polygon {
                    630:     my ($spec,$x,$y) = @_;
                    631:     &Apache::lonxml::debug("Spec is $spec");
                    632:     $spec=~s/^polygon://;
                    633:     my @polygon;
                    634:     foreach my $coord (split('-',$spec)) {
                    635: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    636: 	&Apache::lonxml::debug("x $x y $y");
                    637: 	push @polygon, {'x'=>$x,'y'=>$y};
                    638:     }
                    639:     #make end point start point
                    640:     push @polygon, $polygon[0];
                    641:     # cribbed from
                    642:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
                    643:     my $crossing = 0;    # the crossing number counter
                    644: 
                    645:     # loop through all edges of the polygon
                    646:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
                    647: 	if ((($polygon[$i]->{'y'} <= $y)
                    648: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
                    649: 	    || 
                    650: 	    (($polygon[$i]->{'y'} > $y) 
                    651: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
                    652: 	    # compute the actual edge-ray intersect x-coordinate
                    653:             my $vt = ($y - $polygon[$i]->{'y'}) 
                    654: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
                    655:             if ($x < $polygon[$i]->{'x'} + $vt * 
                    656: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
                    657:                 $crossing++;   # a valid crossing of y=P.y right of P.x
                    658: 	    }
                    659: 	}
                    660:     }
                    661: 
                    662:     # 0 if even (out), and 1 if odd (in)
                    663:     if ($crossing%2) {
                    664: 	return 'APPROX_ANS';
                    665:     } else {
                    666: 	return 'INCORRECT';
                    667:     }
                    668: }
                    669: 
                    670: sub end_polygon {
                    671:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    672:     my $result;
                    673:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  674: 	$target eq 'analyze' || $target eq 'answer') {
1.43      albertel  675: 	my $name = $Apache::imageresponse::curname;
                    676: 	my $area = &Apache::lonxml::endredirection;
1.48      albertel  677: 	$area=~s/\s*//g;
1.43      albertel  678: 	&Apache::lonxml::debug("out is $area for $name");
                    679: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  680: 	     #&& !&Apache::response::showallfoils()
                    681: 	     ) {
1.43      albertel  682: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
                    683: 	} else {
                    684: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36      albertel  685: 	}
                    686:     } elsif ($target eq 'edit') {
                    687: 	$result=&Apache::edit::end_table();
                    688:     }
                    689:     return $result;
1.1       albertel  690: }
                    691: 1;
                    692: __END__
                    693:  

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