File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.89: download - view: text, annotated - select for diffs
Mon Oct 8 09:22:50 2007 UTC (16 years, 7 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
bz4074 - Use lonxml subs to maintain the number of bubble lines required
for responses to these types of problems.

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

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