File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.100: download - view: text, annotated - select for diffs
Sun Dec 19 01:08:11 2010 UTC (13 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0, loncapaMITrelate_1, HEAD
- New Question Type - randomizetry
  New routine: &get_prior_options() in place of response::setup_prior_tries_hash
    for determination of image and options for prior tries.

    1: #
    2: # The LearningOnline Network with CAPA
    3: # image click response style
    4: #
    5: # $Id: imageresponse.pm,v 1.100 2010/12/19 01:08:11 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:     my ($target) = @_;
  196:     %Apache::response::foilgroup=();
  197:     $Apache::imageresponse::conceptgroup=0;
  198:     &Apache::response::pushrandomnumber(undef,$target);
  199:     return '';
  200: }
  201: 
  202: sub getfoilcounts {
  203:     my ($parstack,$safeeval)=@_;
  204: 
  205:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  206:     # +1 since instructors will count from 1
  207:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  208:     if (&Apache::response::showallfoils()) { $max=$count; }
  209:     return ($count,$max);
  210: }
  211: 
  212: sub whichfoils {
  213:     my ($max)=@_;
  214:     return if (!defined(@{ $Apache::response::foilgroup{'names'} }));
  215:     my @names = @{ $Apache::response::foilgroup{'names'} };
  216:     my @whichopt;
  217:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
  218: 	&Apache::lonxml::debug("Have $#whichopt max is $max");
  219: 	my $aopt;
  220: 	if (&Apache::response::showallfoils()) {
  221: 	    $aopt=0;
  222: 	} else {
  223: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
  224: 	}
  225: 	&Apache::lonxml::debug("From $#names elms, picking $aopt");
  226: 	$aopt=splice(@names,$aopt,1);
  227: 	&Apache::lonxml::debug("Picked $aopt");
  228: 	push(@whichopt,$aopt);
  229:     }
  230:     return @whichopt;
  231: }
  232: 
  233: sub prep_image {
  234:     my ($image,$mode,$name)=@_;
  235: 
  236:     my ($x,$y)= &get_submission($name);
  237:     &Apache::lonxml::debug("for $name drawing click at $x and $y");
  238:     &draw_image($mode,$image,$x,$y,$Apache::response::foilgroup{"$name.area"});
  239: }
  240: 
  241: sub draw_image {
  242:     my ($mode,$image,$x,$y,$areas) = @_;
  243: 
  244:     my $id=&Apache::loncommon::get_cgi_id();
  245: 
  246:     my (%x,$i);
  247:     $x{"cgi.$id.BGIMG"}=&escape($image);
  248: 
  249:     #draws 2 xs on the image at the clicked location
  250:     #one in white and then one in red on top of the one in white
  251: 
  252:     if (defined($x)    && $x =~/\S/ 
  253: 	&& defined($y) && $y =~/\S/ 
  254: 	&& ($mode eq 'submission' || !&Apache::response::show_answer())
  255: 	&& $mode ne 'answeronly') {
  256: 	my $length = 6;
  257: 	my $width = 1;
  258: 	my $extrawidth = 2;
  259: 	my $xmin=($x-$length);
  260: 	my $xmax=($x+$length); 
  261: 	my $ymin=($y-$length);
  262: 	my $ymax=($y+$length);
  263: 
  264: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  265: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  266: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  267: 				      "FFFFFF",($width+$extrawidth)));
  268: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  269: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  270: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  271: 				      "FFFFFF",($width+$extrawidth)));
  272: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  273: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  274: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  275: 				      "FF0000",($width)));
  276: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  277: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  278: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  279: 				      "FF0000",($width)));
  280:     }
  281:     if ($mode eq 'answer' || $mode eq 'answeronly') {
  282: 	my $width = 1;
  283: 	my $extrawidth = 2;
  284: 	foreach my $area (@{ $areas }) {
  285: 	    if ($area=~/^rectangle:/) {
  286: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  287: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  288: 		my ($x1,$y1,$x2,$y2)=
  289: 		    ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  290: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
  291: 					      ($width+$extrawidth)));
  292: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  293: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  294: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
  295: 	    } elsif ($area=~/^polygon:(.*)/) {
  296: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  297: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  298: 		$x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
  299: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  300: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  301: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  302: 		$x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
  303: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  304: 	    }
  305: 	}
  306:     }
  307:     &Apache::lonnet::appenv(\%x);
  308:     return $id;
  309: }
  310: 
  311: sub displayfoils {
  312:     my ($target,@whichopt) = @_;
  313:     my $result ='';
  314:     my $temp=1;
  315:     my @images;
  316:     foreach my $name (@whichopt) {
  317: 	$result.=$Apache::response::foilgroup{"$name.text"};
  318: 	&Apache::lonxml::debug("Text is $result");
  319: 	if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
  320: 	my $image=$Apache::response::foilgroup{"$name.image"};
  321: 	&Apache::lonxml::debug("image is $image");
  322: 	if ( ($target eq 'web' || $target eq 'answer') 
  323: 	     && $image !~ /^https?\:/ ) {
  324: 	    $image=&clean_up_image($image);
  325: 	}
  326: 	push(@images,$image);
  327: 	&Apache::lonxml::debug("image is $image");
  328: 	if ( &Apache::response::show_answer() ) {
  329: 	    if ($target eq 'tex') {
  330: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  331: 	    } else {
  332: 		my $token=&prep_image($image,'answer',$name);
  333: 		$result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
  334: 	    }
  335: 	} else {
  336: 	    if ($target eq 'tex') {
  337: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  338: 	    } else {
  339: 		my $respid=$Apache::inputtags::response['-1'];
  340: 		my $token=&prep_image($image,'submission',$name);
  341: 		my $input_id = "HWVAL_$respid:$temp";
  342: 		my $id = $env{'form.request.prefix'}.$input_id;
  343: 		$result.='<img onclick="image_response_click(\''.$id.'\',event);"
  344: 	                       src="/adm/randomlabel.png?token='.$token.'" 
  345:                                id="'.$id.'_imageresponse"
  346:                                 />'.
  347: 			       '<br />'.
  348: 			       '<input type="hidden" name="'.$input_id.'_token" value="'.$token.'" />'.
  349: 			       '<input type="hidden" name="'.$input_id.'" value="'.
  350: 			       join(':',&get_submission($name)).'" />';
  351: 	    }
  352: 	}
  353: 	$temp++;
  354:     }
  355:     if ($target eq 'web') {
  356:         &get_prior_options(\@images,\@whichopt);
  357:     }
  358:     return $result;
  359: }
  360: 
  361: sub get_prior_options {
  362:     my ($currimages,$curropt) = @_;
  363:     return unless((ref($curropt) eq 'ARRAY') && 
  364:                   (ref($currimages) eq 'ARRAY'));
  365:     my $part = $Apache::inputtags::part;
  366:     my $respid   = $Apache::inputtags::response[-1];
  367:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
  368:         my $partprefix = "$i:resource.$part";
  369:         my $sub_key = "$partprefix.$respid.submission";
  370:         next if (!exists($Apache::lonhomework::history{$sub_key}));
  371:         my $type_key = "$partprefix.type";
  372:         my @whichopt = ();
  373:         my @images = ();
  374:         if ($Apache::lonhomework::history{$type_key} eq 'randomizetry') {
  375:             my $order_key = "$partprefix.$respid.foilorder";
  376:             @whichopt = &Apache::lonnet::str2array($Apache::lonhomework::history{$order_key});
  377:             if (@whichopt > 0) {
  378:                 foreach my $name (@whichopt) {
  379:                     my $image=$Apache::response::foilgroup{"$name.image"};
  380:                     if ($image !~ /^https?\:/ ) {
  381:                         $image=&clean_up_image($image);
  382:                     }
  383:                     push(@images,$image);
  384:                 }
  385:             }
  386:         } else {
  387:             @whichopt = @{$curropt};
  388:             @images = @{$currimages};
  389:         }
  390:         my $submission = $Apache::lonhomework::history{$sub_key};
  391:         my $output =  &format_prior_response('grade',$submission,
  392:                                              [\@images,\@whichopt]);
  393:         if (defined($output)) {
  394:             $Apache::inputtags::submission_display{$sub_key} = $output;
  395:         }
  396:     }
  397: }
  398: 
  399: sub format_prior_response {
  400:     my ($mode,$answer,$other_data) = @_;
  401:     
  402:     my $result;
  403: 
  404:     # make a copy of the data in the refs
  405:     my @images = @{ $other_data->[0] };
  406:     my @foils = @{ $other_data->[1] };
  407:     foreach my $name (@foils) {
  408: 	my $image = pop(@images);
  409: 	my ($x,$y) = &get_submission($name,$answer);
  410: 	my $token = &draw_image('submission',$image,$x,$y);
  411: 	$result .=
  412: 	    '<img class="LC_prior_image"
  413:                   src="/adm/randomlabel.png?token='.$token.'" /><br />';
  414:     }
  415:     return $result;
  416: }
  417: 
  418: sub display_answers {
  419:     my ($target,$whichopt)=@_;
  420: 
  421:     my $result=&Apache::response::answer_header('imageresponse');
  422:     foreach my $name (@$whichopt) {
  423: 	my $image=$Apache::response::foilgroup{"$name.image"};
  424: 	&Apache::lonxml::debug("image is $image");
  425: 	if ( ($target eq 'web' || $target eq 'answer')
  426: 	     && $image !~ /^https?\:/ ) {
  427: 	    $image = &clean_up_image($image);
  428: 	} 
  429: 	my $token=&prep_image($image,'answeronly',$name);
  430: 
  431: 	$result.=&Apache::response::answer_part('imageresponse',"<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n");
  432:     }
  433:     $result.=&Apache::response::answer_footer('imageresponse');
  434:     return $result;
  435: }
  436: 
  437: sub clean_up_image {
  438:     my ($image)=@_;
  439:     if ($image =~ /\s*<img\s*/) {
  440: 	($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
  441: 	if ($image !~ /^https?\:/) {
  442: 	    $image=&Apache::lonnet::hreflocation('',$image);
  443: 	}
  444: 	if (!$image) {
  445: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  446: 	}
  447:     } else {
  448: 	$image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
  449: 	&Apache::lonxml::debug("repcopying: $image");
  450: 	if (&Apache::lonnet::repcopy($image) ne 'ok') {
  451: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  452: 	}
  453:     }
  454:     return $image;
  455: }
  456: 
  457: sub gradefoils {
  458:     my (@whichopt) = @_;
  459: 
  460:     my $partid = $Apache::inputtags::part;
  461:     my $id     = $Apache::inputtags::response['-1'];
  462: 
  463:     if ($Apache::lonhomework::type eq 'exam') {
  464: 	&Apache::response::scored_response($partid,$id);
  465: 	return;
  466:     }
  467:     
  468:     my @results;
  469:     my $temp=1;
  470:     my %response;
  471:     foreach my $name (@whichopt) {
  472: 	my ($x,$y) = split(':',$env{"form.HWVAL_$id:$temp"});
  473: 	$response{$name} = $env{"form.HWVAL_$id:$temp"};
  474: 	&Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
  475: 	if (defined($x) && defined($y) &&
  476: 	    defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
  477: 	    my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
  478: 	    my $grade="INCORRECT";
  479: 	    foreach my $area (@areas) {
  480: 		&Apache::lonxml::debug("Area is $area for $name");
  481: 		$area =~ m/([a-z]*):/;
  482: 		&Apache::lonxml::debug("Area of type $1");
  483: 		if ($1 eq 'rectangle') {
  484: 		    $grade=&grade_rectangle($area,$x,$y);
  485: 		} elsif ($1 eq 'polygon') {
  486: 		    $grade=&grade_polygon($area,$x,$y);
  487: 		} else {
  488: 		    &Apache::lonxml::error(&mt('Unknown area style [_1]',$area));
  489: 		}
  490: 		&Apache::lonxml::debug("Area said $grade");
  491: 		if ($grade eq 'APPROX_ANS') { last; }
  492: 	    }
  493: 	    &Apache::lonxml::debug("Foil was $grade");
  494: 	    push(@results, $grade)
  495: 	} else {
  496: 	    push(@results, 'MISSING_ANSWER')
  497: 	}
  498: 	$temp++;
  499:     }
  500:     if ($Apache::lonhomework::type eq 'randomizetry') {
  501:         $Apache::lonhomework::results{"resource.$partid.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
  502:     }
  503:     my ($result) = &Apache::inputtags::finalizeawards(\@results,[]);
  504:     &Apache::lonxml::debug("Question is $result");
  505: 
  506:     my $part=$Apache::inputtags::part;
  507:     my %previous=
  508: 	&Apache::response::check_for_previous(&stringify_submission(\%response),
  509: 					      $part,$id);
  510:     if ($result) { 
  511: 	if ($Apache::lonhomework::type eq 'survey') { 
  512:             $result='SUBMITTED';
  513:         } elsif ($Apache::lonhomework::type eq 'surveycred') { 
  514:             $result='SUBMITTED_CREDIT'; 
  515:         } elsif ($Apache::lonhomework::type eq 'anonsurvey') { 
  516:             $result='ANONYMOUS'; 
  517:         } elsif ($Apache::lonhomework::type eq 'anonsurveycred') { 
  518:             $result='ANONYMOUS_CREDIT'; 
  519:         }
  520:     }
  521: 
  522:     &Apache::response::handle_previous(\%previous,$result);
  523:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  524: 	&stringify_submission(\%response);
  525:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
  526:     return;
  527: }
  528: 
  529: sub stringify_submission {
  530:     my ($response) = @_;
  531:     return &Apache::lonnet::hash2str(%{ $response });
  532: }
  533: 
  534: sub get_submission {
  535:     my ($name,$string) = @_;
  536: 
  537:     if (!defined($string)) {
  538: 	my $part=$Apache::inputtags::part;
  539: 	my $respid=$Apache::inputtags::response['-1'];
  540:    	$string = 
  541: 	    $Apache::lonhomework::history{"resource.$part.$respid.submission"};
  542:     }
  543: 
  544:     if ($string !~ /=/) {
  545: 	return split(':',$string);
  546:     } else {
  547: 	my %response = &Apache::lonnet::str2hash($string);
  548: 	return split(':',$response{$name});
  549:     }
  550: }
  551: 
  552: sub end_foilgroup {
  553:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  554:     my $result='';
  555:     my @whichopt;
  556: 
  557:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  558: 	$target eq 'analyze' || $target eq 'answer') {
  559: 
  560: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  561: 	if ($count>$max) { $count=$max }
  562: 	&Apache::lonxml::debug("Count is $count from $max");
  563: 
  564: 	@whichopt = &whichfoils($max);
  565: 
  566: 	if ($target eq 'web' || $target eq 'tex') {
  567: 	    $result=&displayfoils($target,@whichopt);
  568:             if ($Apache::lonhomework::type eq 'randomizetry') {
  569:                 if ($target eq 'web') {
  570:                     &get_prior_options($target,@whichopt);
  571:                 }
  572:             }
  573: 	    $Apache::lonxml::post_evaluate=0;
  574: 	} elsif ($target eq 'grade') {
  575: 	    if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
  576: 	} elsif ( $target eq 'analyze') {
  577: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
  578: 						      ['text','image','area']);
  579: 	} elsif ($target eq 'answer'
  580: 		 && $env{'form.answer_output_mode'} ne 'tex') {
  581: 	    $result=&display_answers($target,\@whichopt);
  582: 	}
  583: 
  584:     } elsif ($target eq 'edit') {
  585: 	$result=&Apache::edit::end_table();
  586:     }
  587:     &Apache::response::poprandomnumber();
  588:     return $result;
  589: }
  590: 
  591: sub start_conceptgroup {
  592:     $Apache::imageresponse::conceptgroup=1;
  593:     %Apache::response::conceptgroup=();
  594:     return '';
  595: }
  596: 
  597: sub end_conceptgroup {
  598:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  599:     $Apache::imageresponse::conceptgroup=0;
  600:     my $result;
  601:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  602: 	$target eq 'analyze') {
  603: 	&Apache::response::pick_foil_for_concept($target,
  604: 						 ['area','text','image'],
  605: 						 \%Apache::hint::image,
  606: 						 $parstack,$safeeval);
  607:     } elsif ($target eq 'edit') {
  608: 	$result=&Apache::edit::end_table();
  609:     }
  610:     return $result;
  611: }
  612: 
  613: sub insert_foil {
  614:     return '
  615:        <foil>
  616:            <image></image>
  617:            <text></text>
  618:            <rectangle></rectangle>
  619:        </foil>
  620: ';
  621: }
  622: 
  623: $Apache::imageresponse::curname='';
  624: sub start_foil {
  625:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  626:     my $result;
  627:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  628: 	$target eq 'analyze' || $target eq 'answer') {
  629: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  630: 	if ($name eq "") {
  631: 	    &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  632: 	    $name=$Apache::lonxml::curdepth;
  633: 	}
  634: 	if (defined($Apache::response::foilnames{$name})) {
  635: 	    &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique."
  636:                                       ,'<b><tt>'.$name.'</tt></b>'));
  637: 	}
  638: 	$Apache::response::foilnames{$name}++;
  639: 	if ( $Apache::imageresponse::conceptgroup
  640: 	     && !&Apache::response::showallfoils()
  641: 	     ) {
  642: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
  643: 	} else {
  644: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
  645: 	}
  646: 	$Apache::imageresponse::curname=$name;
  647:     } elsif ($target eq 'edit') {
  648: 	$result  = &Apache::edit::tag_start($target,$token);
  649: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
  650: 	$result .= &Apache::edit::end_row().
  651: 	    &Apache::edit::start_spanning_row();
  652:     } elsif ($target eq 'modified') {
  653: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  654: 						     $safeeval,'name');
  655: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  656:     } 
  657:     return $result;;
  658: }
  659: 
  660: sub end_foil {
  661:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  662:     my $result;
  663:     if ($target eq 'edit') {
  664: 	$result=&Apache::edit::end_table();
  665:     }
  666:     return $result;
  667: }
  668: 
  669: sub start_text {
  670:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  671:     my $result='';
  672:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  673: 	|| $target eq 'answer') { 
  674: 	&Apache::lonxml::startredirection; 
  675:     } elsif ($target eq 'edit') {
  676: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
  677: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
  678: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
  679: 	    &Apache::edit::end_row();
  680:     } elsif ($target eq "modified") {
  681: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
  682:     }
  683:     return $result;
  684: }
  685: 
  686: sub end_text {
  687:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  688:     my $result;
  689:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  690: 	|| $target eq 'answer') {
  691: 	my $name = $Apache::imageresponse::curname;
  692: 	if ( $Apache::imageresponse::conceptgroup
  693: 	     && !&Apache::response::showallfoils()
  694: 	     ) {
  695: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  696: 	} else {
  697: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  698: 	}
  699:     } elsif ($target eq 'edit') {
  700: 	$result=&Apache::edit::end_table();
  701:     }
  702:     return $result;
  703: }
  704: 
  705: sub start_image {
  706:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  707:     my $result='';
  708:     my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
  709:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
  710: 	|| $target eq 'answer') { 
  711: 	&Apache::lonxml::startredirection; 
  712:     } elsif ($target eq 'edit') {
  713: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
  714: 	$Apache::edit::bgimgsrc=$bgimg;
  715: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
  716: 
  717: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
  718: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
  719: 	$result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
  720: 	$result.=&Apache::edit::search(undef,'textnode').
  721: 	    &Apache::edit::end_row();
  722:     } elsif ($target eq "modified") {
  723: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
  724:     }
  725:     return $result;
  726: }
  727: 
  728: sub end_image {
  729:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  730:     my $result;
  731:     my $name = $Apache::imageresponse::curname;
  732:     if ($target eq 'web' || $target eq 'answer') {
  733: 	my $image = &Apache::lonxml::endredirection();
  734: 	&Apache::lonxml::debug("original image is $image");
  735: 	if ( $Apache::imageresponse::conceptgroup
  736: 	     && !&Apache::response::showallfoils()
  737: 	     ) {
  738: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  739: 	} else {
  740: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  741: 	}
  742:     } elsif ($target eq 'analyze') {
  743: 	my $image = &Apache::lonxml::endredirection();
  744: 	if ( $Apache::imageresponse::conceptgroup
  745: 	     && !&Apache::response::showallfoils()
  746: 	     ) {
  747: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  748: 	} else {
  749: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  750: 	}
  751:     } elsif ($target eq 'edit') {
  752: 	$result=&Apache::edit::end_table();
  753:     } elsif ($target eq 'tex') {
  754: 	my $src = &Apache::lonxml::endredirection();
  755: 
  756: 	#  There may be all sorts of whitespace on fore and aft:
  757: 
  758: 	$src =~ s/\s+$//s;
  759: 	$src =~ s/^\s+//s;
  760: 
  761: 	#
  762: 	#  Gnuplot e.g. just generates the latex to put inplace.
  763: 	#
  764: 	my $graphinclude;
  765: 	if ($src =~ /^%DYNAMICIMAGE/) {
  766: 	    # This is needed because the newline is not always passed -> tex.
  767: 	    # At present we don't care about the sizing info.
  768: 
  769: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
  770: 	    $graphinclude = $src;
  771: 	    $graphinclude =~ s/^$commentline//;
  772: 	} elsif (!($src =~ /\\/)) {
  773: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
  774: 	    my ($height_param,$width_param)=
  775: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
  776: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
  777: 	} else {
  778: 	    $graphinclude = $src;   # Already fully formed.
  779: 	}
  780: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
  781:     } 
  782:     return $result;
  783: }
  784: 
  785: sub start_rectangle {
  786:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  787:     my $result='';
  788:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  789: 	$target eq 'analyze' || $target eq 'answer') { 
  790: 	&Apache::lonxml::startredirection; 
  791:     } elsif ($target eq 'edit') {
  792: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
  793: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
  794: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
  795: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
  796: 	    &Apache::edit::end_row();
  797:     } elsif ($target eq "modified") {
  798: 	&Apache::edit::deletecoorddata();
  799: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
  800:     }
  801:     return $result;
  802: }
  803: 
  804: sub grade_rectangle {
  805:     my ($spec,$x,$y) = @_;
  806:     &Apache::lonxml::debug("Spec is $spec");
  807:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  808:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
  809:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  810:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  811:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
  812: 	return 'APPROX_ANS';
  813:     }
  814:     return 'INCORRECT';
  815: }
  816: 
  817: sub end_rectangle {
  818:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  819:     my $result;
  820:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  821: 	$target eq 'analyze' || $target eq 'answer') {
  822: 	my $name = $Apache::imageresponse::curname;
  823: 	my $area = &Apache::lonxml::endredirection;
  824: 	$area=~s/\s//g;
  825: 	&Apache::lonxml::debug("out is $area for $name");
  826: 	if ( $Apache::imageresponse::conceptgroup
  827: 	     && !&Apache::response::showallfoils()
  828: 	     ) {
  829: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  830: 	} else {
  831: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  832: 	}
  833:     } elsif ($target eq 'edit') {
  834: 	$result=&Apache::edit::end_table();
  835:     }
  836:     return $result;
  837: }
  838: 
  839: sub start_polygon {
  840:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  841:     my $result='';
  842:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  843: 	$target eq 'analyze' || $target eq 'answer') { 
  844: 	&Apache::lonxml::startredirection; 
  845:     } elsif ($target eq 'edit') {
  846: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
  847: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
  848: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
  849: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
  850: 	    &Apache::edit::end_row();
  851:     } elsif ($target eq "modified") {
  852: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
  853:     }
  854:     return $result;
  855: }
  856: 
  857: sub grade_polygon {
  858:     my ($spec,$x,$y) = @_;
  859:     &Apache::lonxml::debug("Spec is $spec");
  860:     $spec=~s/^polygon://;
  861:     my @polygon;
  862:     foreach my $coord (split('-',$spec)) {
  863: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
  864: 	&Apache::lonxml::debug("x $x y $y");
  865: 	push @polygon, {'x'=>$x,'y'=>$y};
  866:     }
  867:     #make end point start point
  868:     push @polygon, $polygon[0];
  869:     # cribbed from
  870:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
  871:     my $crossing = 0;    # the crossing number counter
  872: 
  873:     # loop through all edges of the polygon
  874:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
  875: 	if ((($polygon[$i]->{'y'} <= $y)
  876: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
  877: 	    || 
  878: 	    (($polygon[$i]->{'y'} > $y) 
  879: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
  880: 	    # compute the actual edge-ray intersect x-coordinate
  881:             my $vt = ($y - $polygon[$i]->{'y'}) 
  882: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
  883:             if ($x < $polygon[$i]->{'x'} + $vt * 
  884: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
  885:                 $crossing++;   # a valid crossing of y=P.y right of P.x
  886: 	    }
  887: 	}
  888:     }
  889: 
  890:     # 0 if even (out), and 1 if odd (in)
  891:     if ($crossing%2) {
  892: 	return 'APPROX_ANS';
  893:     } else {
  894: 	return 'INCORRECT';
  895:     }
  896: }
  897: 
  898: sub end_polygon {
  899:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  900:     my $result;
  901:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  902: 	$target eq 'analyze' || $target eq 'answer') {
  903: 	my $name = $Apache::imageresponse::curname;
  904: 	my $area = &Apache::lonxml::endredirection;
  905: 	$area=~s/\s*//g;
  906: 	&Apache::lonxml::debug("out is $area for $name");
  907: 	if ( $Apache::imageresponse::conceptgroup
  908: 	     && !&Apache::response::showallfoils()
  909: 	     ) {
  910: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
  911: 	} else {
  912: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
  913: 	}
  914:     } elsif ($target eq 'edit') {
  915: 	$result=&Apache::edit::end_table();
  916:     }
  917:     return $result;
  918: }
  919: 1;
  920: __END__
  921:  

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