File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.99: download - view: text, annotated - select for diffs
Sun Feb 28 23:37:44 2010 UTC (14 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_0_RC1, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD
- New awards for surveys:
 ANONYMOUS ANONYMOUS_CREDIT SUBMITTED_CREDIT
  - given for question types: anonsurvey, anonsurvey_cred, survey_cred.
    - anonsurvey -- course personnel can not view both identity of submitter
                    and submission details.
    - anonsurveycred -- same anonymity, but submitter receives 'awarded' for
                        submission
    - surveycred - submitter receives 'awarded' for submission to survey.

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

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