File:  [LON-CAPA] / loncom / homework / imagechoice.pm
Revision 1.10: download - view: text, annotated - select for diffs
Thu Apr 13 18:49:29 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- start_page

    1: # $Id: imagechoice.pm,v 1.10 2006/04/13 18:49:29 albertel 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: 
   30: sub deletedata {
   31:     my ($id)=@_;
   32:     &Apache::lonnet::delenv("imagechoice\\.$id\\.coords");
   33: }
   34: 
   35: sub closewindow {
   36:     my ($r,$output,$filename,$needimage,$display)=@_;
   37:     if ($needimage) {
   38: 	$needimage="<img name=\"pickimg\" src=\"$filename\" />";
   39:     }
   40:     my $js=<<"ENDSUBM";
   41: <script type="text/javascript">
   42:     function submitthis() {
   43: 	$output
   44: 	self.close();
   45:     }
   46: </script>
   47: ENDSUBM
   48: 
   49:     my $start_page =
   50:         &Apache::loncommon::start_page('Close Window',$js,
   51: 				       {'bgcolor'     => '#FFFFFF',
   52: 					'only_body'   => 1,
   53: 					'add_entries' => {
   54: 					    onload => 'submitthis();'},});
   55: 
   56:     my $end_page =
   57:         &Apache::loncommon::end_page();
   58: 
   59:     my $js=<<"ENDSUBM";
   60: $start_page
   61: <h3>Position Selected</h3>
   62: $display
   63: $needimage
   64: $end_page
   65: ENDSUBM
   66: }
   67: 
   68: sub storedata {
   69:     my ($r,$type,$filename,$id)=@_;
   70: 
   71:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
   72: 
   73:     my ($output,$needimage);
   74: 
   75:     if ($env{"imagechoice.$id.formwidth"}) {
   76: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
   77: 	$needimage=1;
   78:     }
   79:     if ($env{"imagechoice.$id.formheight"}) {
   80: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
   81: 	$needimage=1;
   82:     }
   83: 
   84:     my $display;
   85:     if ($type eq 'point') {
   86: 	my (undef,$x,$y)=split(':',$env{"imagechoice.$id.coords"});
   87: 	if ($env{"imagechoice.$id.formx"}) {
   88: 	    $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formx"}.'.value='.$x.';';
   89: 	    $display.="<p>The X coordinate is $x</p>\n";
   90: 	}
   91: 	if ($env{"imagechoice.$id.formy"}) {
   92: 	    $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formy"}.'.value='.$y.';';
   93: 	    $display.="<p>The Y coordinate is $y</p>\n";
   94: 	}
   95:     } elsif ($type eq 'polygon' or $type eq 'box') {
   96: 	my $coordstr;
   97: 	while (@coords) {
   98: 	    $coordstr.='('.shift(@coords).','.shift(@coords).')-';
   99: 	}
  100: 	chop($coordstr);
  101: 	$display.="<p>The selected coordinates are <tt>$coordstr</tt></p>\n";
  102: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
  103:     }
  104:     if ($display) {
  105: 	$display.="<p>If this window fails to close you may need to manually replace the old coordinates with the above value.</p>\n";
  106:     }
  107:     &deletedata($id);
  108:     &closewindow($r,$output,$filename,$needimage,$display);
  109: }
  110: 
  111: sub getcoord {
  112:     my ($r,$type,$filename,$id)=@_;
  113:     my $heading='Select Position on Image';
  114:     my $nextstage='';
  115:     if ($type eq 'box') {
  116: 	my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  117: 	my $step=scalar(@coords)/2;
  118: 	if ($step == 0) { 
  119: 	    $heading='Select First Coordinate on Image';
  120: 	    #$nextstage='<input type="hidden" name="type" value="pairtwo" />';
  121: 	} elsif ($step == 1) {
  122: 	    $heading='Select Second Coordinate on Image';
  123: 	    #$nextstage='<input type="hidden" name="type" value="pairthree" />';
  124: 	} else {
  125: 	    $heading='Select Finish to store selection.';
  126: 	    $nextstage='<input type="submit" name="finish" value="Finish" />';
  127: 	}
  128:     } elsif ($type eq 'polygon') {
  129: 	$heading='Enter Coordinate or click finish to close Polygon';
  130: 	$nextstage='<input type="submit" name="finish" value="Finish" />';
  131:     } elsif ($type eq 'point') {
  132: 	$heading='Click to select a Coordinate or click Finish to store current selection.';
  133: 	$nextstage='<input type="submit" name="finish" value="Finish" />';
  134:     }
  135: 
  136:     my $start_page =
  137:         &Apache::loncommon::start_page('Get Coordinates',undef,
  138: 				       {'bgcolor'     => '#FFFFFF',
  139: 					'only_body'   => 1,});
  140: 
  141:     my $end_page =
  142:         &Apache::loncommon::end_page();
  143:     $r->print(<<"END");
  144: $start_page
  145: <h3>$heading</h3>
  146: <form method="POST" action="/adm/imagechoice?token=$id">
  147: $nextstage
  148: <input type="submit" name="cancel" value="Cancel" />
  149: <br />
  150: <input name="image" type="image" src="$filename" />
  151: </form>
  152: $end_page
  153: END
  154: }
  155: 
  156: sub savecoord {
  157:     my ($id,$type)=@_;
  158:     if (defined($env{"form.image.x"}) && defined($env{"form.image.y"})) {
  159: 	my $data;
  160: 	if ($type eq 'point') {
  161: 	    $data=join(':',(undef,$env{"form.image.x"},$env{"form.image.y"}));
  162: 	} else {
  163: 	    $data=join(':',($env{"imagechoice.$id.coords"},
  164: 			    $env{"form.image.x"},$env{"form.image.y"}));
  165: 	}
  166: 	&Apache::lonnet::appenv("imagechoice.$id.coords"=>$data);
  167:     }
  168:     return int(scalar(split(':',$env{"imagechoice.$id.coords"}))/2);
  169: }
  170: 
  171: sub add_obj {
  172:     my ($x,$id,$type,$args,$extra)=@_;
  173: 
  174:     $$x{"cgi.$id.OBJTYPE"}.=$type.':';
  175:     my $i=$$x{"cgi.$id.OBJCOUNT"}++;
  176:     $$x{"cgi.$id.OBJ$i"}=$args;
  177:     if (defined($extra)) { $$x{"cgi.$id.OBJEXTRA$i"}=$extra; }
  178: }
  179: 
  180: sub drawX {
  181:     my ($data,$imid,$x,$y)=@_;
  182:     my $length = 6;
  183:     my $width = 1;
  184:     my $extrawidth = 2;
  185:     &add_obj($data,$imid,'LINE',
  186: 	     join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
  187: 		       "FFFFFF",($width+$extrawidth))));
  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: 		  "FF0000",($width))));
  194:     &add_obj($data,$imid,'LINE',
  195: 	join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
  196: 		  "FF0000",($width))));
  197: }
  198: 
  199: sub drawPolygon {
  200:     my ($data,$id,$imid)=@_;
  201:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  202:     my $coordstr;
  203:     while (@coords) {
  204: 	$coordstr.='('.shift(@coords).','.shift(@coords).')-';
  205:     }
  206:     chop($coordstr);
  207:     my $width = 1;
  208:     my $extrawidth = 2;
  209:     &add_obj($data,$imid,'POLYGON',
  210: 	     join(':',("FFFFFF",($width+$extrawidth)),'1'),$coordstr);
  211:     &add_obj($data,$imid,'POLYGON',
  212: 	     join(':',("00FF00",($width)),'1'),$coordstr);
  213: }
  214: 
  215: sub drawBox {
  216:     my ($data,$id,$imid)=@_;
  217:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  218:     if (scalar(@coords) < 4) { return ''; }
  219:     my $width = 1;
  220:     my $extrawidth = 2;
  221:     &add_obj($data,$imid,'RECTANGLE',
  222: 	     join(':',(@coords,"FFFFFF",($width+$extrawidth))));
  223:     &add_obj($data,$imid,'RECTANGLE',join(':',(@coords,"00FF00",$width)));
  224: }
  225: 
  226: sub drawimage {
  227:     my ($r,$type,$filename,$id)=@_;
  228:     my $imid=&Apache::loncommon::get_cgi_id();
  229:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
  230:     if (scalar(@coords) < 2) { return &Apache::lonnet::hreflocation('',$filename); }
  231:     my %data;
  232:     $data{"cgi.$imid.BGIMG"}=$filename;
  233:     my $x=$coords[-2];
  234:     my $y=$coords[-1];
  235:     &drawX(\%data,$imid,$x,$y);
  236:     if ($type eq "polygon") { &drawPolygon(\%data,$id,$imid); }
  237:     if ($type eq "box") { &drawBox(\%data,$id,$imid); }
  238:     &Apache::lonnet::appenv(%data);
  239:     return "/adm/randomlabel.png?token=$imid"
  240: }
  241: 
  242: sub handler {
  243:     my ($r)=@_;
  244:     &Apache::loncommon::content_type($r,'text/html');
  245:     $r->send_http_header;
  246:     my %data;
  247:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
  248:     my $filename = &Apache::lonnet::unescape($env{"imagechoice.$id.file"});
  249:     my $formname = $env{"imagechoice.$id.formname"};
  250:     if ($env{'form.cancel'} eq 'Cancel') {
  251: 	&deletedata($id);
  252: 	&closewindow($r,'',$filename);
  253: 	return OK;
  254:     }
  255:     my $type=$env{"imagechoice.$id.type"};
  256:     if (defined($env{'form.type'})) { $type=$env{'form.type'}; }
  257:     my $numcoords=&savecoord($id,$type);
  258:     my $imurl=&drawimage($r,$type,$filename,$id);
  259:     if (($env{'form.finish'} eq 'Finish')) {
  260: 	&storedata($r,$type,$imurl,$id);
  261:     } else {
  262: 	&getcoord($r,$type,$imurl,$id);
  263:     }
  264:     return OK;
  265: }
  266: 
  267: 1;
  268: 
  269: __END__
  270: 
  271: 

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