File:  [LON-CAPA] / loncom / homework / imagechoice.pm
Revision 1.3: download - view: text, annotated - select for diffs
Wed Jan 14 22:59:18 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- imagechoice.pm now understands boxes, <rectangle> on images works with this now
- BUG#2576

    1: # $Id: imagechoice.pm,v 1.3 2004/01/14 22:59:18 albertel 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: 
   29: 
   30: sub deletedata {
   31:     my ($id)=@_;
   32:     &Apache::lonnet::delenv("imagechoice\\.$id\\.coords");
   33: }
   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>
   47: <!--<img name="pickimg" src="$filename" />-->
   48: </body>
   49: </html>
   50: ENDSUBM
   51: }
   52: 
   53: sub storedata {
   54:     my ($r,$type,$filename,$id)=@_;
   55: 
   56:     my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
   57: 
   58:     my $output;
   59: 
   60:     if ($ENV{"imagechoice.$id.formwidth"}) {
   61: 	$output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
   62:     }
   63:     if ($ENV{"imagechoice.$id.formheight"}) {
   64: 	$output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
   65:     }
   66: 
   67:     if ((defined($ENV{"imagechoice.$id.x"})) && (defined($ENV{"imagechoice.$id.y"})) && 
   68: 	($type ne 'pairtwo') && ($type ne 'pairthree')) {
   69: 	my $output='';
   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"}.';';
   73: 	}
   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"}.';';
   77: 	}
   78:     } elsif ($type eq 'polygon' or $type eq 'box') {
   79: 	my $coordstr;
   80: 	while (@coords) {
   81: 	    $coordstr.='('.shift(@coords).','.shift(@coords).')-';
   82: 	}
   83: 	chop($coordstr);
   84: 	$output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
   85:     }
   86:     &deletedata($id);
   87:     &closewindow($r,$output,$filename);
   88: }
   89: 
   90: sub getcoord {
   91:     my ($r,$type,$filename,$id)=@_;
   92:     my $heading='Position';
   93:     my $nextstage='';
   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') {
  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
  117: <input type="submit" name="cancel" value="Cancel" />
  118: <br />
  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"})) {
  129: 	my $data=join(':',($ENV{"imagechoice.$id.coords"},$ENV{"form.image.x"},
  130: 			   $ENV{"form.image.y"}));
  131: 	&Apache::lonnet::appenv("imagechoice.$id.coords"=>$data);
  132:     }
  133:     return int(scalar(split(':',$ENV{"imagechoice.$id.coords"}))/2);
  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)=@_;
  160:     my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
  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: 
  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: 
  192: sub drawimage {
  193:     my ($r,$type,$filename,$id)=@_;
  194:     my $imid=&Apache::loncommon::get_cgi_id();
  195:     my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
  196:     if (scalar(@coords) < 2) { return &Apache::lonnet::hreflocation('',$filename); }
  197:     my %data;
  198:     $data{"cgi.$imid.BGIMG"}=$filename;
  199:     my $x=$coords[-2];
  200:     my $y=$coords[-1];
  201:     %data=(%data,&drawX($imid,$x,$y));
  202:     if ($type eq "polygon") { %data=(%data,&drawPolygon($id,$imid)); }
  203:     if ($type eq "box") { %data=(%data,&drawBox($id,$imid)); }
  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'});
  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);
  218: 	return OK;
  219:     }
  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);
  229:     }
  230:     return OK;
  231: }
  232: 
  233: 1;
  234: 
  235: __END__
  236: 
  237: 

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