File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.94: download - view: text, annotated - select for diffs
Fri Aug 8 16:37:13 2008 UTC (15 years, 8 months ago) by bisitz
Branches: MAIN
CVS tags: version_2_8_X, version_2_7_X, version_2_7_1, version_2_7_0, HEAD
Localization:
- Added missing &mt() call to "Foils without names" warning
- Optimized &mt() call for "Foil name ... appears more than once" warning
- Added some other missing &mt() calls

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

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