File:
[LON-CAPA] /
loncom /
homework /
imagechoice.pm
Revision
1.18:
download - view:
text,
annotated -
select for diffs
Fri Feb 14 17:01:30 2014 UTC (10 years, 6 months ago) by
bisitz
Branches:
MAIN
CVS tags:
version_2_12_X,
version_2_11_X,
version_2_11_5_msu,
version_2_11_5,
version_2_11_4_uiuc,
version_2_11_4_msu,
version_2_11_4,
version_2_11_3_uiuc,
version_2_11_3_msu,
version_2_11_3,
version_2_11_2_uiuc,
version_2_11_2_msu,
version_2_11_2_educog,
version_2_11_2,
version_2_11_1,
version_2_11_0_RC3,
version_2_11_0,
HEAD
- Internationalization:
- Added missing &mt() calls
- More flexible usage of button texts / re-use existing phrases
- Standard... headline, size, button texts, styles
- Improved wording; punctuation
- Mechanism which button was clicked more robust now
1: # $Id: imagechoice.pm,v 1.18 2014/02/14 17:01:30 bisitz 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 Apache::lonlocal;
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: $r->print(
63: $start_page
64: .'<h1>'.&mt('Position Selected').'</h1>'
65: .$display
66: .$needimage
67: .$end_page);
68: }
69:
70: sub storedata {
71: my ($r,$type,$filename,$id)=@_;
72:
73: my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
74:
75: my ($output,$needimage);
76:
77: if ($env{"imagechoice.$id.formwidth"}) {
78: $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
79: $needimage=1;
80: }
81: if ($env{"imagechoice.$id.formheight"}) {
82: $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
83: $needimage=1;
84: }
85:
86: my $display;
87: if ($type eq 'point') {
88: my (undef,$x,$y)=split(':',$env{"imagechoice.$id.coords"});
89: if ($env{"imagechoice.$id.formx"}) {
90: $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formx"}.'.value='.$x.';';
91: $display.='<p>'.&mt('The X coordinate is [_1]',$x)."</p>\n";
92: }
93: if ($env{"imagechoice.$id.formy"}) {
94: $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formy"}.'.value='.$y.';';
95: $display.='<p>'.&mt('The Y coordinate is [_1]',$y)."</p>\n";
96: }
97: } elsif ($type eq 'polygon' or $type eq 'box') {
98: my $coordstr;
99: while (@coords) {
100: $coordstr.='('.shift(@coords).','.shift(@coords).')-';
101: }
102: chop($coordstr);
103: $display.='<p>'.&mt('The selected coordinates are [_1]',"<tt>$coordstr</tt>")."</p>\n";
104: $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
105: }
106: if ($display) {
107: $display.='<p class="LC_info">'
108: .&mt('If this window fails to close you may need to manually replace the old coordinates with the above value.')
109: ."</p>\n";
110: }
111: &deletedata($id);
112: &closewindow($r,$output,$filename,$needimage,$display);
113: }
114:
115: sub getcoord {
116: my ($r,$type,$filename,$id)=@_;
117: my $heading=&mt('Select Position on Image');
118: my $nextstage='';
119: if ($type eq 'box') {
120: my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
121: my $step=scalar(@coords)/2;
122: if ($step == 0) {
123: $heading=&mt('Select First Coordinate on Image.');
124: #$nextstage='<input type="hidden" name="type" value="pairtwo" />';
125: } elsif ($step == 1) {
126: $heading=&mt('Select Second Coordinate on Image.');
127: #$nextstage='<input type="hidden" name="type" value="pairthree" />';
128: } else {
129: $heading=&mt('Select [_1] to save selection.','"'.&mt('Save').'"');
130: $nextstage='<input type="submit" name="finish" value="'.&mt('Save').'" />';
131: }
132: } elsif ($type eq 'polygon') {
133: $heading=&mt('Click to select a Coordinate or click [_1] to close Polygon.',
134: '"'.&mt('Save').'"');
135: $nextstage='<input type="submit" name="finish" value="'.&mt('Save').'" />';
136: } elsif ($type eq 'point') {
137: $heading=&mt('Click to select a Coordinate or click [_1] to save current selection.',
138: '"'.&mt('Save').'"');
139: $nextstage='<input type="submit" name="finish" value="'.&mt('Save').'" />';
140: }
141:
142: my $headline = 'Get Coordinates';
143: my $start_page =
144: &Apache::loncommon::start_page($headline,undef,
145: {'bgcolor' => '#FFFFFF',
146: 'only_body' => 1,});
147: my $end_page =
148: &Apache::loncommon::end_page();
149: $headline = &mt($headline);
150: my $canceltext=&mt('Cancel');
151: $r->print(<<"END");
152: $start_page
153: <h1>$headline</h1>
154: <p>$heading</p>
155: <form method="post" action="/adm/imagechoice?token=$id">
156: $nextstage
157: <input type="submit" name="cancel" value="$canceltext" />
158: <br />
159: <input name="image" type="image" src="$filename" />
160: </form>
161: $end_page
162: END
163: }
164:
165: sub savecoord {
166: my ($id,$type)=@_;
167: if (defined($env{"form.image.x"}) && defined($env{"form.image.y"})) {
168: my $data;
169: if ($type eq 'point') {
170: $data=join(':',(undef,$env{"form.image.x"},$env{"form.image.y"}));
171: } else {
172: $data=join(':',($env{"imagechoice.$id.coords"},
173: $env{"form.image.x"},$env{"form.image.y"}));
174: }
175: &Apache::lonnet::appenv({"imagechoice.$id.coords"=>$data});
176: }
177: return int(scalar(split(':',$env{"imagechoice.$id.coords"}))/2);
178: }
179:
180: sub add_obj {
181: my ($x,$id,$type,$args,$extra)=@_;
182:
183: $$x{"cgi.$id.OBJTYPE"}.=$type.':';
184: my $i=$$x{"cgi.$id.OBJCOUNT"}++;
185: $$x{"cgi.$id.OBJ$i"}=$args;
186: if (defined($extra)) { $$x{"cgi.$id.OBJEXTRA$i"}=$extra; }
187: }
188:
189: sub drawX {
190: my ($data,$imid,$x,$y)=@_;
191: my $length = 6;
192: my $width = 1;
193: my $extrawidth = 2;
194: &add_obj($data,$imid,'LINE',
195: join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
196: "FFFFFF",($width+$extrawidth))));
197: &add_obj($data,$imid,'LINE',
198: join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
199: "FFFFFF",($width+$extrawidth))));
200: &add_obj($data,$imid,'LINE',
201: join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
202: "FF0000",($width))));
203: &add_obj($data,$imid,'LINE',
204: join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
205: "FF0000",($width))));
206: }
207:
208: sub drawPolygon {
209: my ($data,$id,$imid)=@_;
210: my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
211: my $coordstr;
212: while (@coords) {
213: $coordstr.='('.shift(@coords).','.shift(@coords).')-';
214: }
215: chop($coordstr);
216: my $width = 1;
217: my $extrawidth = 2;
218: &add_obj($data,$imid,'POLYGON',
219: join(':',("FFFFFF",($width+$extrawidth)),'1'),$coordstr);
220: &add_obj($data,$imid,'POLYGON',
221: join(':',("00FF00",($width)),'1'),$coordstr);
222: }
223:
224: sub drawBox {
225: my ($data,$id,$imid)=@_;
226: my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
227: if (scalar(@coords) < 4) { return ''; }
228: my $width = 1;
229: my $extrawidth = 2;
230: &add_obj($data,$imid,'RECTANGLE',
231: join(':',(@coords,"FFFFFF",($width+$extrawidth))));
232: &add_obj($data,$imid,'RECTANGLE',join(':',(@coords,"00FF00",$width)));
233: }
234:
235: sub drawimage {
236: my ($r,$type,$filename,$id)=@_;
237: my $imid=&Apache::loncommon::get_cgi_id();
238: my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
239: if (scalar(@coords) < 2) { return &Apache::lonnet::hreflocation('',$filename); }
240: my %data;
241: $data{"cgi.$imid.BGIMG"}=$filename;
242: my $x=$coords[-2];
243: my $y=$coords[-1];
244: &drawX(\%data,$imid,$x,$y);
245: if ($type eq "polygon") { &drawPolygon(\%data,$id,$imid); }
246: if ($type eq "box") { &drawBox(\%data,$id,$imid); }
247: &Apache::lonnet::appenv(\%data);
248: return "/adm/randomlabel.png?token=$imid"
249: }
250:
251: sub handler {
252: my ($r)=@_;
253: &Apache::loncommon::content_type($r,'text/html');
254: $r->send_http_header;
255: my %data;
256: my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
257: my $filename = &unescape($env{"imagechoice.$id.file"});
258: my $formname = $env{"imagechoice.$id.formname"};
259: if ($env{'form.cancel'}) { # eq &mt('Cancel')) {
260: &deletedata($id);
261: &closewindow($r,'',$filename);
262: return OK;
263: }
264: my $type=$env{"imagechoice.$id.type"};
265: if (defined($env{'form.type'})) { $type=$env{'form.type'}; }
266: my $numcoords=&savecoord($id,$type);
267: my $imurl=&drawimage($r,$type,$filename,$id);
268: if ($env{'form.finish'}) { # eq &mt('Save')) {
269: &storedata($r,$type,$imurl,$id);
270: } else {
271: &getcoord($r,$type,$imurl,$id);
272: }
273: return OK;
274: }
275:
276: 1;
277:
278: __END__
279:
280:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>