Annotation of loncom/homework/imagechoice.pm, revision 1.4
1.4 ! albertel 1: # $Id: imagechoice.pm,v 1.3 2004/01/14 22:59:18 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);
28:
29:
1.2 albertel 30: sub deletedata {
31: my ($id)=@_;
1.3 albertel 32: &Apache::lonnet::delenv("imagechoice\\.$id\\.coords");
1.2 albertel 33: }
1.1 albertel 34:
35: sub closewindow {
1.4 ! albertel 36: my ($r,$output,$filename,$needimage)=@_;
! 37: if ($needimage) {
! 38: $needimage="<img name=\"pickimg\" src=\"$filename\" />";
! 39: }
1.1 albertel 40: $r->print(<<"ENDSUBM");
41: <html>
42: <script>
43: function submitthis() {
44: $output
45: self.close();
46: }
47: </script>
48: <body bgcolor="#FFFFFF" onLoad="submitthis()">
49: <h3>Position Selected</h3>
1.4 ! albertel 50: $needimage
1.1 albertel 51: </body>
52: </html>
53: ENDSUBM
54: }
55:
56: sub storedata {
1.3 albertel 57: my ($r,$type,$filename,$id)=@_;
1.1 albertel 58:
1.2 albertel 59: my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
1.1 albertel 60:
1.4 ! albertel 61: my ($output,$needimage);
1.1 albertel 62:
1.2 albertel 63: if ($ENV{"imagechoice.$id.formwidth"}) {
64: $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
1.4 ! albertel 65: $needimage=1;
1.1 albertel 66: }
1.2 albertel 67: if ($ENV{"imagechoice.$id.formheight"}) {
68: $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
1.4 ! albertel 69: $needimage=1;
1.1 albertel 70: }
71:
1.4 ! albertel 72: if ($type eq 'point') {
! 73: my (undef,$x,$y)=split(':',$ENV{"imagechoice.$id.coords"});
1.2 albertel 74: if ($ENV{"imagechoice.$id.formx"}) {
1.4 ! albertel 75: $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formx"}.'.value='.$x.';';
1.1 albertel 76: }
1.2 albertel 77: if ($ENV{"imagechoice.$id.formy"}) {
1.4 ! albertel 78: $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formy"}.'.value='.$y.';';
1.1 albertel 79: }
1.3 albertel 80: } elsif ($type eq 'polygon' or $type eq 'box') {
1.1 albertel 81: my $coordstr;
82: while (@coords) {
83: $coordstr.='('.shift(@coords).','.shift(@coords).')-';
84: }
85: chop($coordstr);
1.2 albertel 86: $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
1.1 albertel 87: }
1.4 ! albertel 88:
1.2 albertel 89: &deletedata($id);
1.4 ! albertel 90: &closewindow($r,$output,$filename,$needimage);
1.1 albertel 91: }
92:
93: sub getcoord {
1.3 albertel 94: my ($r,$type,$filename,$id)=@_;
1.4 ! albertel 95: my $heading='Select Position on Image';
1.1 albertel 96: my $nextstage='';
1.3 albertel 97: if ($type eq 'box') {
98: my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
99: my $step=scalar(@coords)/2;
100: if ($step == 0) {
1.4 ! albertel 101: $heading='Select First Coordinate on Image';
1.3 albertel 102: #$nextstage='<input type="hidden" name="type" value="pairtwo" />';
103: } elsif ($step == 1) {
1.4 ! albertel 104: $heading='Select Second Coordinate on Image';
1.3 albertel 105: #$nextstage='<input type="hidden" name="type" value="pairthree" />';
106: } else {
1.4 ! albertel 107: $heading='Select Finish to store selection.';
1.3 albertel 108: $nextstage='<input type="submit" name="finish" value="Finish" />';
109: }
110: } elsif ($type eq 'polygon') {
1.1 albertel 111: $heading='Enter Coordinate or click finish to close Polygon';
112: $nextstage='<input type="submit" name="finish" value="Finish" />';
1.4 ! albertel 113: } elsif ($type eq 'point') {
! 114: $heading='Click to select a Coordinate or click Finish to store current selection.';
! 115: $nextstage='<input type="submit" name="finish" value="Finish" />';
1.1 albertel 116: }
117: $r->print(<<"END");
118: <html>
119: <body bgcolor="#FFFFFF">
1.4 ! albertel 120: <h3>$heading</h3>
1.1 albertel 121: <form method="POST" action="/adm/imagechoice?token=$id">
122: $nextstage
1.2 albertel 123: <input type="submit" name="cancel" value="Cancel" />
124: <br />
1.1 albertel 125: <input name="image" type="image" src="$filename" />
126: </form>
127: </body>
128: </html>
129: END
130: }
131:
132: sub savecoord {
1.4 ! albertel 133: my ($id,$type)=@_;
1.1 albertel 134: if (defined($ENV{"form.image.x"}) && defined($ENV{"form.image.y"})) {
1.4 ! albertel 135: my $data;
! 136: if ($type eq 'point') {
! 137: $data=join(':',(undef,$ENV{"form.image.x"},$ENV{"form.image.y"}));
! 138: } else {
! 139: $data=join(':',($ENV{"imagechoice.$id.coords"},
! 140: $ENV{"form.image.x"},$ENV{"form.image.y"}));
! 141: }
1.2 albertel 142: &Apache::lonnet::appenv("imagechoice.$id.coords"=>$data);
1.1 albertel 143: }
1.3 albertel 144: return int(scalar(split(':',$ENV{"imagechoice.$id.coords"}))/2);
1.1 albertel 145: }
146:
147: sub drawX {
148: my ($imid,$x,$y)=@_;
149: my %x;
150: $x{"cgi.$imid.LINECOUNT"}=4;
151: my $length = 6;
152: my $width = 1;
153: my $extrawidth = 2;
154: $x{"cgi.$imid.LINE0"}=
155: join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
156: "FFFFFF",($width+$extrawidth)));
157: $x{"cgi.$imid.LINE1"}=
158: join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
159: "FFFFFF",($width+$extrawidth)));
160: $x{"cgi.$imid.LINE2"}=
161: join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
162: "FF0000",($width)));
163: $x{"cgi.$imid.LINE3"}=
164: join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
165: "FF0000",($width)));
166: return %x;
167: }
168:
169: sub drawPolygon {
170: my ($id,$imid)=@_;
1.2 albertel 171: my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
1.1 albertel 172: my $coordstr;
173: while (@coords) {
174: $coordstr.='('.shift(@coords).','.shift(@coords).')-';
175: }
176: chop($coordstr);
177: my %x;
178: my $width = 1;
179: my $extrawidth = 2;
180: my $i=$x{"cgi.$imid.POLYCOUNT"}++;
181: $x{"cgi.$imid.POLYOPT$i"}=join(':',("FFFFFF",($width+$extrawidth)),'1');
182: $x{"cgi.$imid.POLY$i"}=$coordstr;
183: $i=$x{"cgi.$imid.POLYCOUNT"}++;
184: $x{"cgi.$imid.POLYOPT$i"}=join(':',("00FF00",$width),'1');
185: $x{"cgi.$imid.POLY$i"}=$coordstr;
186: return %x;
187: }
188:
1.3 albertel 189: sub drawBox {
190: my ($id,$imid)=@_;
191: my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
192: my %x;
193: if (scalar(@coords) < 4) { return %x; }
194: my $width = 1;
195: my $extrawidth = 2;
196: my $i=$x{"cgi.$imid.BOXCOUNT"}++;
197: $x{"cgi.$imid.BOX$i"}=join(':',(@coords,"FFFFFF",($width+$extrawidth)));
198: $i=$x{"cgi.$imid.BOXCOUNT"}++;
199: $x{"cgi.$imid.BOX$i"}=join(':',(@coords,"00FF00",$width));
200: return %x;
201: }
202:
1.1 albertel 203: sub drawimage {
1.3 albertel 204: my ($r,$type,$filename,$id)=@_;
1.1 albertel 205: my $imid=&Apache::loncommon::get_cgi_id();
1.2 albertel 206: my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
207: if (scalar(@coords) < 2) { return &Apache::lonnet::hreflocation('',$filename); }
1.1 albertel 208: my %data;
209: $data{"cgi.$imid.BGIMG"}=$filename;
1.3 albertel 210: my $x=$coords[-2];
211: my $y=$coords[-1];
1.1 albertel 212: %data=(%data,&drawX($imid,$x,$y));
1.3 albertel 213: if ($type eq "polygon") { %data=(%data,&drawPolygon($id,$imid)); }
214: if ($type eq "box") { %data=(%data,&drawBox($id,$imid)); }
1.1 albertel 215: &Apache::lonnet::appenv(%data);
216: return "/adm/randomlabel.png?token=$imid"
217: }
218:
219: sub handler {
220: my ($r)=@_;
221: $r->content_type('text/html');
222: my %data;
223: my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
1.2 albertel 224: my $filename = &Apache::lonnet::unescape($ENV{"imagechoice.$id.file"});
225: my $formname = $ENV{"imagechoice.$id.formname"};
226: if ($ENV{'form.cancel'} eq 'Cancel') {
227: &deletedata($id);
228: &closewindow($r,'',$filename);
1.3 albertel 229: return OK;
1.2 albertel 230: }
1.3 albertel 231: my $type=$ENV{"imagechoice.$id.type"};
232: if (defined($ENV{'form.type'})) { $type=$ENV{'form.type'}; }
1.4 ! albertel 233: my $numcoords=&savecoord($id,$type);
1.3 albertel 234: &Apache::lonnet::logthis("num coords is $numcoords");
235: my $imurl=&drawimage($r,$type,$filename,$id);
236: if (($ENV{'form.finish'} eq 'Finish')) {
237: &storedata($r,$type,$imurl,$id);
238: } else {
239: &getcoord($r,$type,$imurl,$id);
1.1 albertel 240: }
241: return OK;
242: }
243:
244: 1;
245:
246: __END__
247:
248:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>