--- loncom/homework/imagechoice.pm	2004/01/14 22:59:18	1.3
+++ loncom/homework/imagechoice.pm	2005/04/07 06:56:21	1.8
@@ -1,4 +1,4 @@
-# $Id: imagechoice.pm,v 1.3 2004/01/14 22:59:18 albertel Exp $
+# $Id: imagechoice.pm,v 1.8 2005/04/07 06:56:21 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,7 +25,7 @@
 package Apache::imagechoice;
 use strict;
 use Apache::Constants qw(:common :http);
-
+use Apache::lonnet;
 
 sub deletedata {
     my ($id)=@_;
@@ -33,7 +33,10 @@ sub deletedata {
 }
 
 sub closewindow {
-    my ($r,$output,$filename)=@_;
+    my ($r,$output,$filename,$needimage)=@_;
+    if ($needimage) {
+	$needimage="<img name=\"pickimg\" src=\"$filename\" />";
+    }
     $r->print(<<"ENDSUBM");
 <html>
 <script>
@@ -44,7 +47,7 @@ sub closewindow {
 </script>
 <body bgcolor="#FFFFFF" onLoad="submitthis()">
 <h3>Position Selected</h3>
-<!--<img name="pickimg" src="$filename" />-->
+$needimage
 </body>
 </html>
 ENDSUBM
@@ -53,27 +56,26 @@ ENDSUBM
 sub storedata {
     my ($r,$type,$filename,$id)=@_;
 
-    my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
+    my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
 
-    my $output;
+    my ($output,$needimage);
 
-    if ($ENV{"imagechoice.$id.formwidth"}) {
-	$output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
+    if ($env{"imagechoice.$id.formwidth"}) {
+	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formwidth"}.'.value=document.pickimg.width;';
+	$needimage=1;
     }
-    if ($ENV{"imagechoice.$id.formheight"}) {
-	$output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
+    if ($env{"imagechoice.$id.formheight"}) {
+	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formheight"}.'.value=document.pickimg.height;';
+	$needimage=1;
     }
 
-    if ((defined($ENV{"imagechoice.$id.x"})) && (defined($ENV{"imagechoice.$id.y"})) && 
-	($type ne 'pairtwo') && ($type ne 'pairthree')) {
-	my $output='';
-	if ($ENV{"imagechoice.$id.formx"}) {
-	    $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formx"}.
-		'.value='.$ENV{"imagechoice.$id.x"}.';';
+    if ($type eq 'point') {
+	my (undef,$x,$y)=split(':',$env{"imagechoice.$id.coords"});
+	if ($env{"imagechoice.$id.formx"}) {
+	    $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formx"}.'.value='.$x.';';
 	}
-	if ($ENV{"imagechoice.$id.formy"}) {
-	    $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formy"}.
-		'.value='.$ENV{"imagechoice.$id.y"}.';';
+	if ($env{"imagechoice.$id.formy"}) {
+	    $output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formy"}.'.value='.$y.';';
 	}
     } elsif ($type eq 'polygon' or $type eq 'box') {
 	my $coordstr;
@@ -81,37 +83,41 @@ sub storedata {
 	    $coordstr.='('.shift(@coords).','.shift(@coords).')-';
 	}
 	chop($coordstr);
-	$output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
+	$output.='opener.document.forms.'.$env{"imagechoice.$id.formname"}.'.'.$env{"imagechoice.$id.formcoord"}.'.value="'.$coordstr.'";';
     }
+
     &deletedata($id);
-    &closewindow($r,$output,$filename);
+    &closewindow($r,$output,$filename,$needimage);
 }
 
 sub getcoord {
     my ($r,$type,$filename,$id)=@_;
-    my $heading='Position';
+    my $heading='Select Position on Image';
     my $nextstage='';
     if ($type eq 'box') {
-	my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
+	my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
 	my $step=scalar(@coords)/2;
 	if ($step == 0) { 
-	    $heading='First Coordinate';
+	    $heading='Select First Coordinate on Image';
 	    #$nextstage='<input type="hidden" name="type" value="pairtwo" />';
 	} elsif ($step == 1) {
-	    $heading='Second Coordinate';
+	    $heading='Select Second Coordinate on Image';
 	    #$nextstage='<input type="hidden" name="type" value="pairthree" />';
 	} else {
-	    $heading='Finish or Cancel';
+	    $heading='Select Finish to store selection.';
 	    $nextstage='<input type="submit" name="finish" value="Finish" />';
 	}
     } elsif ($type eq 'polygon') {
 	$heading='Enter Coordinate or click finish to close Polygon';
 	$nextstage='<input type="submit" name="finish" value="Finish" />';
+    } elsif ($type eq 'point') {
+	$heading='Click to select a Coordinate or click Finish to store current selection.';
+	$nextstage='<input type="submit" name="finish" value="Finish" />';
     }
     $r->print(<<"END");
 <html>
 <body bgcolor="#FFFFFF">
-<h3>Select $heading on Image</h3>
+<h3>$heading</h3>
 <form method="POST" action="/adm/imagechoice?token=$id">
 $nextstage
 <input type="submit" name="cancel" value="Cancel" />
@@ -124,105 +130,109 @@ END
 }
 
 sub savecoord {
-    my ($id)=@_;
-    if (defined($ENV{"form.image.x"}) && defined($ENV{"form.image.y"})) {
-	my $data=join(':',($ENV{"imagechoice.$id.coords"},$ENV{"form.image.x"},
-			   $ENV{"form.image.y"}));
+    my ($id,$type)=@_;
+    if (defined($env{"form.image.x"}) && defined($env{"form.image.y"})) {
+	my $data;
+	if ($type eq 'point') {
+	    $data=join(':',(undef,$env{"form.image.x"},$env{"form.image.y"}));
+	} else {
+	    $data=join(':',($env{"imagechoice.$id.coords"},
+			    $env{"form.image.x"},$env{"form.image.y"}));
+	}
 	&Apache::lonnet::appenv("imagechoice.$id.coords"=>$data);
     }
-    return int(scalar(split(':',$ENV{"imagechoice.$id.coords"}))/2);
+    return int(scalar(split(':',$env{"imagechoice.$id.coords"}))/2);
+}
+
+sub add_obj {
+    my ($x,$id,$type,$args,$extra)=@_;
+
+    $$x{"cgi.$id.OBJTYPE"}.=$type.':';
+    my $i=$$x{"cgi.$id.OBJCOUNT"}++;
+    $$x{"cgi.$id.OBJ$i"}=$args;
+    if (defined($extra)) { $$x{"cgi.$id.OBJEXTRA$i"}=$extra; }
 }
 
 sub drawX {
-    my ($imid,$x,$y)=@_;
-    my %x;
-    $x{"cgi.$imid.LINECOUNT"}=4;
+    my ($data,$imid,$x,$y)=@_;
     my $length = 6;
     my $width = 1;
     my $extrawidth = 2;
-    $x{"cgi.$imid.LINE0"}=
-	join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
-		  "FFFFFF",($width+$extrawidth)));
-    $x{"cgi.$imid.LINE1"}=
+    &add_obj($data,$imid,'LINE',
+	     join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
+		       "FFFFFF",($width+$extrawidth))));
+    &add_obj($data,$imid,'LINE',
 	join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
-		  "FFFFFF",($width+$extrawidth)));
-    $x{"cgi.$imid.LINE2"}=
+		  "FFFFFF",($width+$extrawidth))));
+    &add_obj($data,$imid,'LINE',
 	join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
-		  "FF0000",($width)));
-    $x{"cgi.$imid.LINE3"}=
+		  "FF0000",($width))));
+    &add_obj($data,$imid,'LINE',
 	join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
-		  "FF0000",($width)));
-    return %x;
+		  "FF0000",($width))));
 }
 
 sub drawPolygon {
-    my ($id,$imid)=@_;
-    my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
+    my ($data,$id,$imid)=@_;
+    my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
     my $coordstr;
     while (@coords) {
 	$coordstr.='('.shift(@coords).','.shift(@coords).')-';
     }
     chop($coordstr);
-    my %x;
     my $width = 1;
     my $extrawidth = 2;
-    my $i=$x{"cgi.$imid.POLYCOUNT"}++;
-    $x{"cgi.$imid.POLYOPT$i"}=join(':',("FFFFFF",($width+$extrawidth)),'1');
-    $x{"cgi.$imid.POLY$i"}=$coordstr;
-    $i=$x{"cgi.$imid.POLYCOUNT"}++;
-    $x{"cgi.$imid.POLYOPT$i"}=join(':',("00FF00",$width),'1');
-    $x{"cgi.$imid.POLY$i"}=$coordstr;
-    return %x;
+    &add_obj($data,$imid,'POLYGON',
+	     join(':',("FFFFFF",($width+$extrawidth)),'1'),$coordstr);
+    &add_obj($data,$imid,'POLYGON',
+	     join(':',("00FF00",($width)),'1'),$coordstr);
 }
 
 sub drawBox {
-    my ($id,$imid)=@_;
-    my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
-    my %x;
-    if (scalar(@coords) < 4) { return %x; }
+    my ($data,$id,$imid)=@_;
+    my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
+    if (scalar(@coords) < 4) { return ''; }
     my $width = 1;
     my $extrawidth = 2;
-    my $i=$x{"cgi.$imid.BOXCOUNT"}++;
-    $x{"cgi.$imid.BOX$i"}=join(':',(@coords,"FFFFFF",($width+$extrawidth)));
-    $i=$x{"cgi.$imid.BOXCOUNT"}++;
-    $x{"cgi.$imid.BOX$i"}=join(':',(@coords,"00FF00",$width));
-    return %x;
+    &add_obj($data,$imid,'RECTANGLE',
+	     join(':',(@coords,"FFFFFF",($width+$extrawidth))));
+    &add_obj($data,$imid,'RECTANGLE',join(':',(@coords,"00FF00",$width)));
 }
 
 sub drawimage {
     my ($r,$type,$filename,$id)=@_;
     my $imid=&Apache::loncommon::get_cgi_id();
-    my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
+    my (undef,@coords)=split(':',$env{"imagechoice.$id.coords"});
     if (scalar(@coords) < 2) { return &Apache::lonnet::hreflocation('',$filename); }
     my %data;
     $data{"cgi.$imid.BGIMG"}=$filename;
     my $x=$coords[-2];
     my $y=$coords[-1];
-    %data=(%data,&drawX($imid,$x,$y));
-    if ($type eq "polygon") { %data=(%data,&drawPolygon($id,$imid)); }
-    if ($type eq "box") { %data=(%data,&drawBox($id,$imid)); }
+    &drawX(\%data,$imid,$x,$y);
+    if ($type eq "polygon") { &drawPolygon(\%data,$id,$imid); }
+    if ($type eq "box") { &drawBox(\%data,$id,$imid); }
     &Apache::lonnet::appenv(%data);
     return "/adm/randomlabel.png?token=$imid"
 }
 
 sub handler {
     my ($r)=@_;
-    $r->content_type('text/html');
+    &Apache::loncommon::content_type($r,'text/html');
+    $r->send_http_header;
     my %data;
     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
-    my $filename = &Apache::lonnet::unescape($ENV{"imagechoice.$id.file"});
-    my $formname = $ENV{"imagechoice.$id.formname"};
-    if ($ENV{'form.cancel'} eq 'Cancel') {
+    my $filename = &Apache::lonnet::unescape($env{"imagechoice.$id.file"});
+    my $formname = $env{"imagechoice.$id.formname"};
+    if ($env{'form.cancel'} eq 'Cancel') {
 	&deletedata($id);
 	&closewindow($r,'',$filename);
 	return OK;
     }
-    my $type=$ENV{"imagechoice.$id.type"};
-    if (defined($ENV{'form.type'})) { $type=$ENV{'form.type'}; }
-    my $numcoords=&savecoord($id);
-    &Apache::lonnet::logthis("num coords is $numcoords");
+    my $type=$env{"imagechoice.$id.type"};
+    if (defined($env{'form.type'})) { $type=$env{'form.type'}; }
+    my $numcoords=&savecoord($id,$type);
     my $imurl=&drawimage($r,$type,$filename,$id);
-    if (($ENV{'form.finish'} eq 'Finish')) {
+    if (($env{'form.finish'} eq 'Finish')) {
 	&storedata($r,$type,$imurl,$id);
     } else {
 	&getcoord($r,$type,$imurl,$id);