File:  [LON-CAPA] / loncom / homework / imagechoice.pm
Revision 1.11: download - view: text, annotated - select for diffs
Tue May 30 12:45:37 2006 UTC (17 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

    1: # $Id: imagechoice.pm,v 1.11 2006/05/30 12:45:37 www Exp $
    2: #
    3: # Copyright Michigan State University Board of Trustees
    4: #
    5: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    6: #
    7: # LON-CAPA is free software; you can redistribute it and/or modify
    8: # it under the terms of the GNU General Public License as published by
    9: # the Free Software Foundation; either version 2 of the License, or
   10: # (at your option) any later version.
   11: #
   12: # LON-CAPA is distributed in the hope that it will be useful,
   13: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: # GNU General Public License for more details.
   16: #
   17: # You should have received a copy of the GNU General Public License
   18: # along with LON-CAPA; if not, write to the Free Software
   19: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   20: #
   21: # /home/httpd/cgi-bin/plot.gif
   22: #
   23: # http://www.lon-capa.org/
   24: #
   25: package Apache::imagechoice;
   26: use strict;
   27: use Apache::Constants qw(:common :http);
   28: use Apache::lonnet;
   29: use lib '/home/httpd/lib/perl/';
   30: use LONCAPA;
   31:  
   32: 
   33: sub deletedata {
   34:     my ($id)=@_;
   35:     &Apache::lonnet::delenv("imagechoice\\.$id\\.coords");
   36: }
   37: 
   38: sub closewindow {
   39:     my ($r,$output,$filename,$needimage,$display)=@_;
   40:     if ($needimage) {
   41: 	$needimage="<img name=\"pickimg\" src=\"$filename\" />";
   42:     }
   43:     my $js=<<"ENDSUBM";
   44: <script type="text/javascript">
   45:     function submitthis() {
   46: 	$output
   47: 	self.close();
   48:     }
   49: </script>
   50: ENDSUBM
   51: 
   52:     my $start_page =
   53:         &Apache::loncommon::start_page('Close Window',$js,
   54: 				       {'bgcolor'     => '#FFFFFF',
   55: 					'only_body'   => 1,
   56: 					'add_entries' => {
   57: 					    onload => 'submitthis();'},});
   58: 
   59:     my $end_page =
   60:         &Apache::loncommon::end_page();
   61: 
   62:     my $js=<<"ENDSUBM";
   63: $start_page
   64: <h3>Position Selected</h3>
   65: $display
   66: $needimage
   67: $end_page
   68: ENDSUBM
   69: }
   70: 
   71: sub storedata {
   72:     my ($r,$type,$filename,$id)=@_;
   73: 
   74:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
   75: 
   76:     my ($output,$needimage);
   77: 
   78:     if ($env{"imagechoice.$id.formwidth"}) {
   79: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
   80: 	$needimage=1;
   81:     }
   82:     if ($env{"imagechoice.$id.formheight"}) {
   83: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
   84: 	$needimage=1;
   85:     }
   86: 
   87:     my $display;
   88:     if ($type eq 'point') {
   89: 	my (undef,$x,$y)=split(':',$env{"imagechoice.$id.coords"});
   90: 	if ($env{"imagechoice.$id.formx"}) {
   91: 	    $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formx"}.'.value='.$x.';';
   92: 	    $display.="<p>The X coordinate is $x</p>\n";
   93: 	}
   94: 	if ($env{"imagechoice.$id.formy"}) {
   95: 	    $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formy"}.'.value='.$y.';';
   96: 	    $display.="<p>The Y coordinate is $y</p>\n";
   97: 	}
   98:     } elsif ($type eq 'polygon' or $type eq 'box') {
   99: 	my $coordstr;
  100: 	while (@coords) {
  101: 	    $coordstr.='('.shift(@coords).','.shift(@coords).')-';
  102: 	}
  103: 	chop($coordstr);
  104: 	$display.="<p>The selected coordinates are <tt>$coordstr</tt></p>\n";
  105: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
  106:     }
  107:     if ($display) {
  108: 	$display.="<p>If this window fails to close you may need to manually replace the old coordinates with the above value.</p>\n";
  109:     }
  110:     &deletedata($id);
  111:     &closewindow($r,$output,$filename,$needimage,$display);
  112: }
  113: 
  114: sub getcoord {
  115:     my ($r,$type,$filename,$id)=@_;
  116:     my $heading='Select Position on Image';
  117:     my $nextstage='';
  118:     if ($type eq 'box') {
  119: 	my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  120: 	my $step=scalar(@coords)/2;
  121: 	if ($step == 0) { 
  122: 	    $heading='Select First Coordinate on Image';
  123: 	    #$nextstage='<input type="hidden" name="type" value="pairtwo" />';
  124: 	} elsif ($step == 1) {
  125: 	    $heading='Select Second Coordinate on Image';
  126: 	    #$nextstage='<input type="hidden" name="type" value="pairthree" />';
  127: 	} else {
  128: 	    $heading='Select Finish to store selection.';
  129: 	    $nextstage='<input type="submit" name="finish" value="Finish" />';
  130: 	}
  131:     } elsif ($type eq 'polygon') {
  132: 	$heading='Enter Coordinate or click finish to close Polygon';
  133: 	$nextstage='<input type="submit" name="finish" value="Finish" />';
  134:     } elsif ($type eq 'point') {
  135: 	$heading='Click to select a Coordinate or click Finish to store current selection.';
  136: 	$nextstage='<input type="submit" name="finish" value="Finish" />';
  137:     }
  138: 
  139:     my $start_page =
  140:         &Apache::loncommon::start_page('Get Coordinates',undef,
  141: 				       {'bgcolor'     => '#FFFFFF',
  142: 					'only_body'   => 1,});
  143: 
  144:     my $end_page =
  145:         &Apache::loncommon::end_page();
  146:     $r->print(<<"END");
  147: $start_page
  148: <h3>$heading</h3>
  149: <form method="POST" action="/adm/imagechoice?token=$id">
  150: $nextstage
  151: <input type="submit" name="cancel" value="Cancel" />
  152: <br />
  153: <input name="image" type="image" src="$filename" />
  154: </form>
  155: $end_page
  156: END
  157: }
  158: 
  159: sub savecoord {
  160:     my ($id,$type)=@_;
  161:     if (defined($env{"form.image.x"}) && defined($env{"form.image.y"})) {
  162: 	my $data;
  163: 	if ($type eq 'point') {
  164: 	    $data=join(':',(undef,$env{"form.image.x"},$env{"form.image.y"}));
  165: 	} else {
  166: 	    $data=join(':',($env{"imagechoice.$id.coords"},
  167: 			    $env{"form.image.x"},$env{"form.image.y"}));
  168: 	}
  169: 	&Apache::lonnet::appenv("imagechoice.$id.coords"=>$data);
  170:     }
  171:     return int(scalar(split(':',$env{"imagechoice.$id.coords"}))/2);
  172: }
  173: 
  174: sub add_obj {
  175:     my ($x,$id,$type,$args,$extra)=@_;
  176: 
  177:     $$x{"cgi.$id.OBJTYPE"}.=$type.':';
  178:     my $i=$$x{"cgi.$id.OBJCOUNT"}++;
  179:     $$x{"cgi.$id.OBJ$i"}=$args;
  180:     if (defined($extra)) { $$x{"cgi.$id.OBJEXTRA$i"}=$extra; }
  181: }
  182: 
  183: sub drawX {
  184:     my ($data,$imid,$x,$y)=@_;
  185:     my $length = 6;
  186:     my $width = 1;
  187:     my $extrawidth = 2;
  188:     &add_obj($data,$imid,'LINE',
  189: 	     join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
  190: 		       "FFFFFF",($width+$extrawidth))));
  191:     &add_obj($data,$imid,'LINE',
  192: 	join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
  193: 		  "FFFFFF",($width+$extrawidth))));
  194:     &add_obj($data,$imid,'LINE',
  195: 	join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
  196: 		  "FF0000",($width))));
  197:     &add_obj($data,$imid,'LINE',
  198: 	join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
  199: 		  "FF0000",($width))));
  200: }
  201: 
  202: sub drawPolygon {
  203:     my ($data,$id,$imid)=@_;
  204:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  205:     my $coordstr;
  206:     while (@coords) {
  207: 	$coordstr.='('.shift(@coords).','.shift(@coords).')-';
  208:     }
  209:     chop($coordstr);
  210:     my $width = 1;
  211:     my $extrawidth = 2;
  212:     &add_obj($data,$imid,'POLYGON',
  213: 	     join(':',("FFFFFF",($width+$extrawidth)),'1'),$coordstr);
  214:     &add_obj($data,$imid,'POLYGON',
  215: 	     join(':',("00FF00",($width)),'1'),$coordstr);
  216: }
  217: 
  218: sub drawBox {
  219:     my ($data,$id,$imid)=@_;
  220:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  221:     if (scalar(@coords) < 4) { return ''; }
  222:     my $width = 1;
  223:     my $extrawidth = 2;
  224:     &add_obj($data,$imid,'RECTANGLE',
  225: 	     join(':',(@coords,"FFFFFF",($width+$extrawidth))));
  226:     &add_obj($data,$imid,'RECTANGLE',join(':',(@coords,"00FF00",$width)));
  227: }
  228: 
  229: sub drawimage {
  230:     my ($r,$type,$filename,$id)=@_;
  231:     my $imid=&Apache::loncommon::get_cgi_id();
  232:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  233:     if (scalar(@coords) < 2) { return &Apache::lonnet::hreflocation('',$filename); }
  234:     my %data;
  235:     $data{"cgi.$imid.BGIMG"}=$filename;
  236:     my $x=$coords[-2];
  237:     my $y=$coords[-1];
  238:     &drawX(\%data,$imid,$x,$y);
  239:     if ($type eq "polygon") { &drawPolygon(\%data,$id,$imid); }
  240:     if ($type eq "box") { &drawBox(\%data,$id,$imid); }
  241:     &Apache::lonnet::appenv(%data);
  242:     return "/adm/randomlabel.png?token=$imid"
  243: }
  244: 
  245: sub handler {
  246:     my ($r)=@_;
  247:     &Apache::loncommon::content_type($r,'text/html');
  248:     $r->send_http_header;
  249:     my %data;
  250:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
  251:     my $filename = &unescape($env{"imagechoice.$id.file"});
  252:     my $formname = $env{"imagechoice.$id.formname"};
  253:     if ($env{'form.cancel'} eq 'Cancel') {
  254: 	&deletedata($id);
  255: 	&closewindow($r,'',$filename);
  256: 	return OK;
  257:     }
  258:     my $type=$env{"imagechoice.$id.type"};
  259:     if (defined($env{'form.type'})) { $type=$env{'form.type'}; }
  260:     my $numcoords=&savecoord($id,$type);
  261:     my $imurl=&drawimage($r,$type,$filename,$id);
  262:     if (($env{'form.finish'} eq 'Finish')) {
  263: 	&storedata($r,$type,$imurl,$id);
  264:     } else {
  265: 	&getcoord($r,$type,$imurl,$id);
  266:     }
  267:     return OK;
  268: }
  269: 
  270: 1;
  271: 
  272: __END__
  273: 
  274: 

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