Annotation of loncom/homework/imagechoice.pm, revision 1.11

1.11    ! www         1: # $Id: imagechoice.pm,v 1.10 2006/04/13 18:49:29 albertel Exp $
1.1       albertel    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);
1.8       albertel   28: use Apache::lonnet;
1.11    ! www        29: use lib '/home/httpd/lib/perl/';
        !            30: use LONCAPA;
        !            31:  
1.1       albertel   32: 
1.2       albertel   33: sub deletedata {
                     34:     my ($id)=@_;
1.3       albertel   35:     &Apache::lonnet::delenv("imagechoice\\.$id\\.coords");
1.2       albertel   36: }
1.1       albertel   37: 
                     38: sub closewindow {
1.9       albertel   39:     my ($r,$output,$filename,$needimage,$display)=@_;
1.4       albertel   40:     if ($needimage) {
                     41: 	$needimage="<img name=\"pickimg\" src=\"$filename\" />";
                     42:     }
1.10      albertel   43:     my $js=<<"ENDSUBM";
                     44: <script type="text/javascript">
1.1       albertel   45:     function submitthis() {
                     46: 	$output
                     47: 	self.close();
                     48:     }
                     49: </script>
1.10      albertel   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
1.1       albertel   64: <h3>Position Selected</h3>
1.9       albertel   65: $display
1.4       albertel   66: $needimage
1.10      albertel   67: $end_page
1.1       albertel   68: ENDSUBM
                     69: }
                     70: 
                     71: sub storedata {
1.3       albertel   72:     my ($r,$type,$filename,$id)=@_;
1.1       albertel   73: 
1.8       albertel   74:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
1.1       albertel   75: 
1.4       albertel   76:     my ($output,$needimage);
1.1       albertel   77: 
1.8       albertel   78:     if ($env{"imagechoice.$id.formwidth"}) {
                     79: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
1.4       albertel   80: 	$needimage=1;
1.1       albertel   81:     }
1.8       albertel   82:     if ($env{"imagechoice.$id.formheight"}) {
                     83: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
1.4       albertel   84: 	$needimage=1;
1.1       albertel   85:     }
                     86: 
1.9       albertel   87:     my $display;
1.4       albertel   88:     if ($type eq 'point') {
1.8       albertel   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.';';
1.9       albertel   92: 	    $display.="<p>The X coordinate is $x</p>\n";
1.1       albertel   93: 	}
1.8       albertel   94: 	if ($env{"imagechoice.$id.formy"}) {
                     95: 	    $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formy"}.'.value='.$y.';';
1.9       albertel   96: 	    $display.="<p>The Y coordinate is $y</p>\n";
1.1       albertel   97: 	}
1.3       albertel   98:     } elsif ($type eq 'polygon' or $type eq 'box') {
1.1       albertel   99: 	my $coordstr;
                    100: 	while (@coords) {
                    101: 	    $coordstr.='('.shift(@coords).','.shift(@coords).')-';
                    102: 	}
                    103: 	chop($coordstr);
1.9       albertel  104: 	$display.="<p>The selected coordinates are <tt>$coordstr</tt></p>\n";
1.8       albertel  105: 	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
1.1       albertel  106:     }
1.9       albertel  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:     }
1.2       albertel  110:     &deletedata($id);
1.9       albertel  111:     &closewindow($r,$output,$filename,$needimage,$display);
1.1       albertel  112: }
                    113: 
                    114: sub getcoord {
1.3       albertel  115:     my ($r,$type,$filename,$id)=@_;
1.4       albertel  116:     my $heading='Select Position on Image';
1.1       albertel  117:     my $nextstage='';
1.3       albertel  118:     if ($type eq 'box') {
1.8       albertel  119: 	my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
1.3       albertel  120: 	my $step=scalar(@coords)/2;
                    121: 	if ($step == 0) { 
1.4       albertel  122: 	    $heading='Select First Coordinate on Image';
1.3       albertel  123: 	    #$nextstage='<input type="hidden" name="type" value="pairtwo" />';
                    124: 	} elsif ($step == 1) {
1.4       albertel  125: 	    $heading='Select Second Coordinate on Image';
1.3       albertel  126: 	    #$nextstage='<input type="hidden" name="type" value="pairthree" />';
                    127: 	} else {
1.4       albertel  128: 	    $heading='Select Finish to store selection.';
1.3       albertel  129: 	    $nextstage='<input type="submit" name="finish" value="Finish" />';
                    130: 	}
                    131:     } elsif ($type eq 'polygon') {
1.1       albertel  132: 	$heading='Enter Coordinate or click finish to close Polygon';
                    133: 	$nextstage='<input type="submit" name="finish" value="Finish" />';
1.4       albertel  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" />';
1.1       albertel  137:     }
1.10      albertel  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();
1.1       albertel  146:     $r->print(<<"END");
1.10      albertel  147: $start_page
1.4       albertel  148: <h3>$heading</h3>
1.1       albertel  149: <form method="POST" action="/adm/imagechoice?token=$id">
                    150: $nextstage
1.2       albertel  151: <input type="submit" name="cancel" value="Cancel" />
                    152: <br />
1.1       albertel  153: <input name="image" type="image" src="$filename" />
                    154: </form>
1.10      albertel  155: $end_page
1.1       albertel  156: END
                    157: }
                    158: 
                    159: sub savecoord {
1.4       albertel  160:     my ($id,$type)=@_;
1.8       albertel  161:     if (defined($env{"form.image.x"}) && defined($env{"form.image.y"})) {
1.4       albertel  162: 	my $data;
                    163: 	if ($type eq 'point') {
1.8       albertel  164: 	    $data=join(':',(undef,$env{"form.image.x"},$env{"form.image.y"}));
1.4       albertel  165: 	} else {
1.8       albertel  166: 	    $data=join(':',($env{"imagechoice.$id.coords"},
                    167: 			    $env{"form.image.x"},$env{"form.image.y"}));
1.4       albertel  168: 	}
1.2       albertel  169: 	&Apache::lonnet::appenv("imagechoice.$id.coords"=>$data);
1.1       albertel  170:     }
1.8       albertel  171:     return int(scalar(split(':',$env{"imagechoice.$id.coords"}))/2);
1.1       albertel  172: }
                    173: 
1.5       albertel  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: 
1.1       albertel  183: sub drawX {
1.5       albertel  184:     my ($data,$imid,$x,$y)=@_;
1.1       albertel  185:     my $length = 6;
                    186:     my $width = 1;
                    187:     my $extrawidth = 2;
1.5       albertel  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',
1.1       albertel  192: 	join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
1.5       albertel  193: 		  "FFFFFF",($width+$extrawidth))));
                    194:     &add_obj($data,$imid,'LINE',
1.1       albertel  195: 	join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
1.5       albertel  196: 		  "FF0000",($width))));
                    197:     &add_obj($data,$imid,'LINE',
1.1       albertel  198: 	join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
1.5       albertel  199: 		  "FF0000",($width))));
1.1       albertel  200: }
                    201: 
                    202: sub drawPolygon {
1.5       albertel  203:     my ($data,$id,$imid)=@_;
1.8       albertel  204:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
1.1       albertel  205:     my $coordstr;
                    206:     while (@coords) {
                    207: 	$coordstr.='('.shift(@coords).','.shift(@coords).')-';
                    208:     }
                    209:     chop($coordstr);
                    210:     my $width = 1;
                    211:     my $extrawidth = 2;
1.5       albertel  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);
1.1       albertel  216: }
                    217: 
1.3       albertel  218: sub drawBox {
1.5       albertel  219:     my ($data,$id,$imid)=@_;
1.8       albertel  220:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
1.5       albertel  221:     if (scalar(@coords) < 4) { return ''; }
1.3       albertel  222:     my $width = 1;
                    223:     my $extrawidth = 2;
1.5       albertel  224:     &add_obj($data,$imid,'RECTANGLE',
                    225: 	     join(':',(@coords,"FFFFFF",($width+$extrawidth))));
                    226:     &add_obj($data,$imid,'RECTANGLE',join(':',(@coords,"00FF00",$width)));
1.3       albertel  227: }
                    228: 
1.1       albertel  229: sub drawimage {
1.3       albertel  230:     my ($r,$type,$filename,$id)=@_;
1.1       albertel  231:     my $imid=&Apache::loncommon::get_cgi_id();
1.8       albertel  232:     my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
1.2       albertel  233:     if (scalar(@coords) < 2) { return &Apache::lonnet::hreflocation('',$filename); }
1.1       albertel  234:     my %data;
                    235:     $data{"cgi.$imid.BGIMG"}=$filename;
1.3       albertel  236:     my $x=$coords[-2];
                    237:     my $y=$coords[-1];
1.5       albertel  238:     &drawX(\%data,$imid,$x,$y);
                    239:     if ($type eq "polygon") { &drawPolygon(\%data,$id,$imid); }
                    240:     if ($type eq "box") { &drawBox(\%data,$id,$imid); }
1.1       albertel  241:     &Apache::lonnet::appenv(%data);
                    242:     return "/adm/randomlabel.png?token=$imid"
                    243: }
                    244: 
                    245: sub handler {
                    246:     my ($r)=@_;
1.7       albertel  247:     &Apache::loncommon::content_type($r,'text/html');
                    248:     $r->send_http_header;
1.1       albertel  249:     my %data;
                    250:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
1.11    ! www       251:     my $filename = &unescape($env{"imagechoice.$id.file"});
1.8       albertel  252:     my $formname = $env{"imagechoice.$id.formname"};
                    253:     if ($env{'form.cancel'} eq 'Cancel') {
1.2       albertel  254: 	&deletedata($id);
                    255: 	&closewindow($r,'',$filename);
1.3       albertel  256: 	return OK;
1.2       albertel  257:     }
1.8       albertel  258:     my $type=$env{"imagechoice.$id.type"};
                    259:     if (defined($env{'form.type'})) { $type=$env{'form.type'}; }
1.4       albertel  260:     my $numcoords=&savecoord($id,$type);
1.3       albertel  261:     my $imurl=&drawimage($r,$type,$filename,$id);
1.8       albertel  262:     if (($env{'form.finish'} eq 'Finish')) {
1.3       albertel  263: 	&storedata($r,$type,$imurl,$id);
                    264:     } else {
                    265: 	&getcoord($r,$type,$imurl,$id);
1.1       albertel  266:     }
                    267:     return OK;
                    268: }
                    269: 
                    270: 1;
                    271: 
                    272: __END__
                    273: 
                    274: 

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