--- loncom/homework/imagechoice.pm	2004/01/12 19:53:54	1.2
+++ loncom/homework/imagechoice.pm	2004/01/14 22:59:18	1.3
@@ -1,4 +1,4 @@
-# $Id: imagechoice.pm,v 1.2 2004/01/12 19:53:54 albertel Exp $
+# $Id: imagechoice.pm,v 1.3 2004/01/14 22:59:18 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -29,7 +29,7 @@ use Apache::Constants qw(:common :http);
 
 sub deletedata {
     my ($id)=@_;
-    &Apache::lonnet::delenv("imagechoice\\.$id");
+    &Apache::lonnet::delenv("imagechoice\\.$id\\.coords");
 }
 
 sub closewindow {
@@ -51,7 +51,7 @@ ENDSUBM
 }
 
 sub storedata {
-    my ($r,$mode,$filename,$id)=@_;
+    my ($r,$type,$filename,$id)=@_;
 
     my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
 
@@ -65,7 +65,7 @@ sub storedata {
     }
 
     if ((defined($ENV{"imagechoice.$id.x"})) && (defined($ENV{"imagechoice.$id.y"})) && 
-	($mode ne 'pairtwo') && ($mode ne 'pairthree')) {
+	($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"}.
@@ -75,15 +75,7 @@ sub storedata {
 	    $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formy"}.
 		'.value='.$ENV{"imagechoice.$id.y"}.';';
 	}
-    } elsif ($mode eq 'pairthree') {
-	my $output='';
-	my $outputpair='('.$ENV{"imagechoice.$id.selx"}.','.$ENV{"imagechoice.$id.sely"}.')-('.$ENV{"imagechoice.$id.x"}.','.$ENV{"imagechoice.$id.y"}.')';
-
-	if ($ENV{"imagechoice.$id.formcoord"}) {
-	    $output.='opener.document.forms.'.$ENV{"imagechoice.$id.formname"}.'.'.$ENV{"imagechoice.$id.formcoord"}.
-		'.value="'.$outputpair.'";';
-	}
-    } elsif ($mode eq 'polygon') {
+    } elsif ($type eq 'polygon' or $type eq 'box') {
 	my $coordstr;
 	while (@coords) {
 	    $coordstr.='('.shift(@coords).','.shift(@coords).')-';
@@ -96,16 +88,23 @@ sub storedata {
 }
 
 sub getcoord {
-    my ($r,$mode,$filename,$id)=@_;
+    my ($r,$type,$filename,$id)=@_;
     my $heading='Position';
     my $nextstage='';
-    if ($mode eq 'pair') {
-	$heading='First Coordinate';
-	$nextstage='<input type="hidden" name="mode" value="pairtwo" />';
-    } elsif ($mode eq 'pairtwo') {
-	$heading='Second Coordinate';
-	$nextstage='<input type="hidden" name="mode" value="pairthree" />';
-    } elsif ($mode eq 'polygon') {
+    if ($type eq 'box') {
+	my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
+	my $step=scalar(@coords)/2;
+	if ($step == 0) { 
+	    $heading='First Coordinate';
+	    #$nextstage='<input type="hidden" name="type" value="pairtwo" />';
+	} elsif ($step == 1) {
+	    $heading='Second Coordinate';
+	    #$nextstage='<input type="hidden" name="type" value="pairthree" />';
+	} else {
+	    $heading='Finish or Cancel';
+	    $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" />';
     }
@@ -131,6 +130,7 @@ sub savecoord {
 			   $ENV{"form.image.y"}));
 	&Apache::lonnet::appenv("imagechoice.$id.coords"=>$data);
     }
+    return int(scalar(split(':',$ENV{"imagechoice.$id.coords"}))/2);
 }
 
 sub drawX {
@@ -175,17 +175,32 @@ sub drawPolygon {
     return %x;
 }
 
+sub drawBox {
+    my ($id,$imid)=@_;
+    my (undef,@coords)=split(':',$ENV{"imagechoice.$id.coords"});
+    my %x;
+    if (scalar(@coords) < 4) { return %x; }
+    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;
+}
+
 sub drawimage {
-    my ($r,$mode,$filename,$id)=@_;
+    my ($r,$type,$filename,$id)=@_;
     my $imid=&Apache::loncommon::get_cgi_id();
     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];
+    my $x=$coords[-2];
+    my $y=$coords[-1];
     %data=(%data,&drawX($imid,$x,$y));
-    if ($mode eq "polygon") { %data=(%data,&drawPolygon($id,$imid)); }
+    if ($type eq "polygon") { %data=(%data,&drawPolygon($id,$imid)); }
+    if ($type eq "box") { %data=(%data,&drawBox($id,$imid)); }
     &Apache::lonnet::appenv(%data);
     return "/adm/randomlabel.png?token=$imid"
 }
@@ -197,17 +212,21 @@ sub handler {
     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
     my $filename = &Apache::lonnet::unescape($ENV{"imagechoice.$id.file"});
     my $formname = $ENV{"imagechoice.$id.formname"};
-    my $mode=$ENV{"imagechoice.$id.mode"};
     if ($ENV{'form.cancel'} eq 'Cancel') {
 	&deletedata($id);
 	&closewindow($r,'',$filename);
+	return OK;
     }
-    &savecoord($id);
-    my $imurl=&drawimage($r,$mode,$filename,$id);
-    if ($ENV{'form.finish'} eq 'Finish') {
-	&storedata($r,$mode,$imurl,$id);
+    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 $imurl=&drawimage($r,$type,$filename,$id);
+    if (($ENV{'form.finish'} eq 'Finish')) {
+	&storedata($r,$type,$imurl,$id);
+    } else {
+	&getcoord($r,$type,$imurl,$id);
     }
-    &getcoord($r,$mode,$imurl,$id);
     return OK;
 }