--- loncom/homework/edit.pm	2002/03/08 18:30:33	1.30
+++ loncom/homework/edit.pm	2002/11/03 19:16:19	1.38
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # edit mode helpers
 #
-# $Id: edit.pm,v 1.30 2002/03/08 18:30:33 matthew Exp $
+# $Id: edit.pm,v 1.38 2002/11/03 19:16:19 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -31,16 +31,19 @@
 package Apache::edit; 
 
 use strict;
-use Apache::lonnet;
+use Apache::lonnet();
+use HTML::Entities();
 
 # Global Vars
 # default list of colors to use in editing
 @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
 # depth of nesting of edit
 $Apache::edit::colordepth=0;
+@Apache::edit::inserttag=();
 
 sub initialize_edit {
   $Apache::edit::colordepth=0;
+  @Apache::edit::inserttag=();
 }
 
 sub tag_start {
@@ -95,13 +98,22 @@ sub start_table {
     $color = $Apache::edit::colorlist[$Apache::edit::colordepth];
   }
   $Apache::edit::colordepth++;
-  my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"5\">";
+  push(@Apache::edit::inserttag,$token->[1]);
+  my $result="<p align=\"right\"><table bgcolor=\"$color\" width=\"95%\" border=\"2\">";
   return $result;
 }
 
 sub end_table {
   $Apache::edit::colordepth--;
-  my $result="</table>";
+  my $result="</table></p>";
+  $result.=
+	"<p><table width=\"100%\"><tr><td>".
+        &innerinsertlist('edit',
+ (defined($Apache::edit::inserttag[-2])?$Apache::edit::inserttag[-2]:'problem')
+,
+ (defined($Apache::edit::inserttag[-1])?$Apache::edit::inserttag[-1]:'')).
+        "</td></tr></table></p>";
+  pop(@Apache::edit::inserttag);
   return $result;
 }
 
@@ -151,22 +163,25 @@ sub handle_delete {
 }
 
 sub get_insert_list {
-  my ($token) = @_;
+  my ($tagname) = @_;
   my $result='';
   my @tagnums= ();
   #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
-  if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
-    push (@tagnums, @{ $Apache::lonxml::insertlist{"$token->[1].which"} });
+  if ($Apache::lonxml::insertlist{"$tagname.which"}) {
+    push (@tagnums, @{ $Apache::lonxml::insertlist{"$tagname.which"} });
   }
   foreach my $namespace (@Apache::lonxml::namespace) {
-    if ($Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"}) {
-      push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"} });
+    if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) {
+      push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} });
     }
   }
   if (@tagnums) {
+    my %options;
     foreach my $tagnum (@tagnums) {
-      $result.='<option value="'.$tagnum.'">'.$Apache::lonxml::insertlist{"$tagnum.description"}."</option>\n";
+      my $descrip=$Apache::lonxml::insertlist{"$tagnum.description"};
+      $options{$descrip} ="<option value=\"$tagnum\">".$descrip."</option>\n";
     }
+    foreach my $option (sort(keys(%options))) { $result.=$options{$option}; }
     if ($result) { $result='<option selected="on"></option>'.$result; }
   }
   return $result;
@@ -174,12 +189,21 @@ sub get_insert_list {
 
 sub insertlist {
   my ($target,$token) = @_;
+  return &innerinsertlist($target,$token->[1]);
+}
+
+sub innerinsertlist {
+  my ($target,$tagname,$closingtag) = @_;
   my $result;
+  my $after='';
+  if ($closingtag) {
+     $after='_after_'.$closingtag; 
+  }
   if ($target eq 'edit') {
-    my $optionlist= &get_insert_list($token);
+    my $optionlist= &get_insert_list($tagname);
     if ($optionlist) {
       $result = "Insert:
-<select name=\"insert_$Apache::lonxml::curdepth\">
+<select name=\"insert$after\_$Apache::lonxml::curdepth\">
 $optionlist
 </select>"
     } else {
@@ -213,6 +237,32 @@ sub handle_insert {
   return $result;
 }
 
+sub handle_insertafter {
+  my $tagname=shift;
+  if ($ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '')
+     { return ''; }
+  my $result;
+  my $tagnum =$ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"};
+  my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
+  if ($func eq 'default') {
+    my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
+    my $namespace;
+    if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
+    $result.="\n<$newtag>\n</$newtag>";
+  } else {
+    if (defined(&$func)) {
+      {
+	no strict 'refs';
+	$result.=&$func();
+      }
+    } else {
+      my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
+      &Apache::lonxml::error("Unable to insert (after) tag $newtag, $func was not defined. ($tagname $tagnum)");
+    }
+  }
+  return $result;
+}
+
 sub insert_responseparam {
   return '
     <responseparam />';
@@ -245,6 +295,13 @@ sub insert_stringresponse {
 </stringresponse>';
 }
 
+sub insert_essayresponse {
+  return '
+<essayresponse>
+    <textfield></textfield>
+</essayresponse>';
+}
+
 sub insert_optionresponse {
   return '
 <optionresponse max="10">
@@ -281,6 +338,12 @@ sub insert_numericalhint {
 </numericalhint>';
 }
 
+sub insert_radiobuttonhint {
+  return '
+<radiobuttonhint>
+</radiobuttonhint>';
+}
+
 sub insert_startouttext {
   return "<startouttext />\n<endouttext />";
 }
@@ -301,6 +364,18 @@ sub textarea_sizes {
   return ($rows,$cols);
 }
 
+sub editline {
+    my ($tag,$data,$description,$size)=@_;
+    $data=&HTML::Entities::encode($data);
+    if ($description) { $description="<br />".$description."<br />"; }
+    my $result = <<"END";
+$description
+<input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
+       value="$data" size="$size" />
+END
+    return $result;
+}
+
 sub editfield {
   my ($tag,$data,$description,$minwidth,$minheight)=@_;
 
@@ -309,7 +384,9 @@ sub editfield {
   if ($cols < $minwidth ) { $cols = $minwidth; }
   if ($rows < $minheight) { $rows = $minheight; }
   if ($description) { $description="<br />".$description."<br />"; }
-  return "$description\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$rows\" cols=\"$cols\" name=\"homework_edit_".$Apache::lonxml::curdepth."\">$data</textarea>\n";
+  return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea rows="'.$rows.
+    '" cols="'.$cols.'" name="homework_edit_'.$Apache::lonxml::curdepth.'">'.
+      &HTML::Entities::encode($data).'</textarea>'."\n";
 }
 
 sub modifiedfield {
@@ -408,7 +485,7 @@ sub select_or_text_arg {
     }
   }
   $optionlist.="<option value=\"TYPEDINVALUE\">Type in value</option>\n";
-  if ($found) {
+  if (($found) || (!$selected)) {
     $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
        '.$optionlist.'
       </select>';
@@ -552,6 +629,11 @@ search($elementname) : provide a link wh
 searcher (lonsearchcat) and, once a file is selected, place the result in
 the form element $elementname.
 
+=item *
+editline(tag,data,description,size): Provide a <input type="text" ../> for
+single-line text entry.  This is to be used for text enclosed by tags, not
+arguements/parameters associated with a tag.
+
 =back
 
 incomplete...