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

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

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