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

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

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