File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.11: download - view: text, annotated - select for diffs
Sat Jun 16 18:35:27 2001 UTC (22 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- edit interface added ability to tweak more options for option response
  style questions

    1: # The LearningOnline Network with CAPA 
    2: # edit mode helpers
    3: # 3/20 Guy
    4: package Apache::edit; 
    5: 
    6: use strict;
    7: use Apache::lonnet;
    8: 
    9: # Global Vars
   10: # default list of colors to use in editing
   11: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
   12: # depth of nesting of edit
   13: $Apache::edit::colordepth=0;
   14: 
   15: sub initialize_edit {
   16:   $Apache::edit::colordepth=0;
   17: }
   18: 
   19: sub tag_start {
   20:   my ($target,$token,$description) = @_;
   21:   my $result='';
   22:   if ($target eq "edit") {
   23:     my $tag=$token->[1];
   24:     if (!$description) { $description="<$tag>"; }
   25:     $result.= &start_table($token)."<tr><td>$description</td>
   26: <td>Delete:".
   27:   &deletelist($target,$token)
   28:   ."</td>
   29: <td>".
   30:   &insertlist($target,$token).
   31:     "</td>
   32: </tr><tr><td colspan=\"3\">\n";
   33:   }
   34:   return $result;
   35: }
   36: 
   37: sub tag_end {
   38:   my ($target,$token,$description) = @_;
   39:   my $result='';
   40:   if ($target eq 'edit') {
   41:     my $tag=$token->[1];
   42:     if (!defined($description)) {
   43:       $result.="</td></tr><tr><td>&lt;/$tag&gt;";
   44:     } else {
   45:       if ($description ne '') { $result.="</td></tr><tr><td>$description"; }
   46:     }
   47:     $result.="</td><td>&nbsp;</td><td>&nbsp;</td></tr>".&end_table()."\n";
   48:   }
   49:   return $result;
   50: }
   51: 
   52: sub start_table {
   53:   my ($token)=@_;
   54:   my $tag = $token->[1];
   55:   my $tagnum;
   56:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
   57:     my $testtag=$Apache::lonxml::namespace['-1'].'::'.$tag;
   58:     $tagnum=$Apache::lonxml::insertlist{"$testtag.num"};
   59:     if (defined($tagnum)) { last; }
   60:   }
   61:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
   62:   my $color = $Apache::lonxml::insertlist{"$tagnum.color"};
   63:   if (!defined($color)) {
   64:     $color = $Apache::edit::colorlist[$Apache::edit::colordepth];
   65:   }
   66:   $Apache::edit::colordepth++;
   67:   my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"10\">";
   68:   return $result;
   69: }
   70: 
   71: sub end_table {
   72:   $Apache::edit::colordepth--;
   73:   my $result="</table>";
   74:   return $result;
   75: }
   76: 
   77: sub deletelist {
   78:   my ($target,$token) = @_;
   79:   my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
   80: <option>Nothing</option>
   81: <option>Tag</option>
   82: <option>Subtags</option>
   83: </select>";
   84:   return $result;
   85: }
   86: 
   87: sub get_insert_list {
   88:   my ($token) = @_;
   89:   my $result='';
   90:   my @tagnums= ();
   91:   #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
   92:   if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
   93:     push (@tagnums, @{ $Apache::lonxml::insertlist{"$token->[1].which"} });
   94:   }
   95:   foreach my $namespace (@Apache::lonxml::namespace) {
   96:     if ($Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"}) {
   97:       push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"} });
   98:     }
   99:   }
  100:   if (@tagnums) {
  101:     foreach my $tagnum (@tagnums) {
  102:       $result.='<option value="'.$tagnum.'">'.$Apache::lonxml::insertlist{"$tagnum.description"}."</option>\n";
  103:     }
  104:     if ($result) { $result='<option selected="on"></option>'.$result; }
  105:   }
  106:   return $result;
  107: }
  108: 
  109: sub insertlist {
  110:   my ($target,$token) = @_;
  111:   my $result;
  112:   if ($target eq 'edit') {
  113:     my $optionlist= &get_insert_list($token);
  114:     if ($optionlist) {
  115:       $result = "Insert:
  116: <select name=\"insert_$Apache::lonxml::curdepth\">
  117: $optionlist
  118: </select>"
  119:     } else {
  120:       $result="&nbsp;";
  121:     }
  122:   }
  123:   return $result;
  124: }
  125: 
  126: sub handle_insert {
  127:   if (!$ENV{"form.insert_$Apache::lonxml::curdepth"}) { return ''; }
  128:   my $result;
  129:   my $tagnum = $ENV{"form.insert_$Apache::lonxml::curdepth"};
  130:   my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
  131:   if ($func eq 'default') {
  132:     my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
  133:     my $namespace;
  134:     if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
  135:     $result.="\n<$newtag>\n</$newtag>";
  136:   } else {
  137:     {
  138:       no strict 'refs';
  139:       $result.=&$func();
  140:     }
  141:   }
  142:   return $result;
  143: }
  144: 
  145: sub insert_optionresponse {
  146:   return '
  147: <optionresponse max="10">
  148:     <foilgroup options="">
  149:     </foilgroup>
  150: </optionresponse>';
  151: }
  152: 
  153: sub editfield {
  154:   my ($tag,$data,$description,$minwidth,$minheight)=@_;
  155:   
  156:   my $count=0;
  157:   my $maxlength=-1;
  158:   map { $count++;
  159: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
  160:       } split ("\n", $data);
  161:   if ($maxlength > 80) { $maxlength = 80; }
  162:   if ($maxlength < $minwidth) { $maxlength = $minwidth; }
  163:   if ( $count < $minheight) { $count = $minheight; }
  164:   if ($description) {
  165:     $description="<br />".$description."<br />";
  166:   }
  167:   return "$description\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea>\n";
  168: #  return "<br />\n&lt;$tag&gt;<br />\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br />\n&lt;/$tag&gt;<br />\n";
  169: }
  170: 
  171: sub modifiedfield {
  172:   my ($token) = @_;
  173:   my $result;
  174: #  foreach my $envkey (sort keys %ENV) {
  175: #    &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
  176: #  }
  177: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
  178: #  &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
  179:   $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
  180:   if (defined $token) {
  181:     if (defined $token->[4]) {
  182:       $result=$token->[4].$result;
  183:     } else {
  184:       $result=$result.$token->[2];
  185:     }
  186:   }
  187:   return $result;
  188: }
  189: 
  190: sub insert_startouttext {
  191:   return "\n<startouttext />\n<endouttext />";
  192: }
  193: 
  194: 1;
  195: __END__

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