Annotation of loncom/homework/edit.pm, revision 1.10

1.1       albertel    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: 
1.10    ! albertel    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: 
1.1       albertel   19: sub tag_start {
1.9       albertel   20:   my ($target,$token,$description) = @_;
1.1       albertel   21:   my $result='';
1.5       albertel   22:   if ($target eq "edit") {
1.4       albertel   23:     my $tag=$token->[1];
1.9       albertel   24:     if (!$description) { $description="<$tag>"; }
1.10    ! albertel   25:     $result.= &start_table($token)."<tr><td>$description</td>
1.8       albertel   26: <td>Delete:".
                     27:   &deletelist($target,$token)
                     28:   ."</td>
1.4       albertel   29: <td>".
1.8       albertel   30:   &insertlist($target,$token).
1.4       albertel   31:     "</td>
1.1       albertel   32: </tr><tr><td colspan=\"3\">\n";
1.4       albertel   33:   }
1.1       albertel   34:   return $result;
                     35: }
                     36: 
                     37: sub tag_end {
1.9       albertel   38:   my ($target,$token,$description) = @_;
1.1       albertel   39:   my $result='';
1.4       albertel   40:   if ($target eq 'edit') {
                     41:     my $tag=$token->[1];
1.9       albertel   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:     }
1.10    ! albertel   47:     $result.="</td></tr>".&end_table()."\n";
1.4       albertel   48:   }
                     49:   return $result;
                     50: }
1.1       albertel   51: 
1.10    ! albertel   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=\"2\">";
        !            68:   return $result;
        !            69: }
        !            70: 
        !            71: sub end_table {
        !            72:   $Apache::edit::colordepth--;
        !            73:   my $result="</table>";
        !            74:   return $result;
        !            75: }
        !            76: 
1.8       albertel   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: 
1.7       albertel   87: sub get_insert_list {
1.6       albertel   88:   my ($token) = @_;
                     89:   my $result='';
1.7       albertel   90:   my @tagnums= ();
                     91:   #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
1.6       albertel   92:   if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
1.7       albertel   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"} });
1.6       albertel   98:     }
                     99:   }
1.7       albertel  100:   if (@tagnums) {
                    101:     foreach my $tagnum (@tagnums) {
                    102:       $result.='<option value="'.$tagnum.'">'.$Apache::lonxml::insertlist{"$tagnum.description"}."</option>\n";
1.5       albertel  103:     }
                    104:     if ($result) { $result='<option selected="on"></option>'.$result; }
                    105:   }
                    106:   return $result;
                    107: }
                    108: 
1.4       albertel  109: sub insertlist {
1.8       albertel  110:   my ($target,$token) = @_;
1.4       albertel  111:   my $result;
                    112:   if ($target eq 'edit') {
1.5       albertel  113:     my $optionlist= &get_insert_list($token);
                    114:     if ($optionlist) {
                    115:       $result = "Insert:
1.4       albertel  116: <select name=\"insert_$Apache::lonxml::curdepth\">
1.5       albertel  117: $optionlist
1.4       albertel  118: </select>"
1.6       albertel  119:     }
                    120:   }
                    121:   return $result;
                    122: }
                    123: 
1.7       albertel  124: sub handle_insert {
1.6       albertel  125:   if (!$ENV{"form.insert_$Apache::lonxml::curdepth"}) { return ''; }
                    126:   my $result;
                    127:   my $tagnum = $ENV{"form.insert_$Apache::lonxml::curdepth"};
                    128:   my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
                    129:   if ($func eq 'default') {
                    130:     my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
1.7       albertel  131:     my $namespace;
                    132:     if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
1.6       albertel  133:     $result.="\n<$newtag>\n</$newtag>";
                    134:   } else {
                    135:     {
                    136:       no strict 'refs';
1.7       albertel  137:       $result.=&$func();
1.5       albertel  138:     }
                    139:   }
                    140:   return $result;
                    141: }
                    142: 
1.7       albertel  143: sub insert_optionresponse {
                    144:   return '
                    145: <optionresponse max="10">
                    146:     <foilgroup options="">
                    147:     </foilgroup>
                    148: </optionresponse>';
1.1       albertel  149: }
                    150: 
1.2       albertel  151: sub editfield {
1.5       albertel  152:   my ($tag,$data,$description,$minwidth,$minheight)=@_;
1.2       albertel  153:   
                    154:   my $count=0;
                    155:   my $maxlength=-1;
                    156:   map { $count++;
                    157: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
                    158:       } split ("\n", $data);
                    159:   if ($maxlength > 80) { $maxlength = 80; }
1.5       albertel  160:   if ($maxlength < $minwidth) { $maxlength = $minwidth; }
                    161:   if ( $count < $minheight) { $count = $minheight; }
                    162:   if ($description) {
1.9       albertel  163:     $description="<br />".$description."<br />";
1.2       albertel  164:   }
1.9       albertel  165:   return "$description\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea>\n";
1.2       albertel  166: #  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";
                    167: }
                    168: 
                    169: sub modifiedfield {
                    170:   my ($token) = @_;
1.3       albertel  171:   my $result;
                    172: #  foreach my $envkey (sort keys %ENV) {
                    173: #    &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
                    174: #  }
                    175: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
                    176: #  &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
                    177:   $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
                    178:   if (defined $token) {
                    179:     if (defined $token->[4]) {
                    180:       $result=$token->[4].$result;
                    181:     } else {
                    182:       $result=$result.$token->[2];
                    183:     }
1.2       albertel  184:   }
1.3       albertel  185:   return $result;
1.5       albertel  186: }
                    187: 
                    188: sub insert_startouttext {
                    189:   return "\n<startouttext />\n<endouttext />";
1.2       albertel  190: }
                    191: 
1.1       albertel  192: 1;
                    193: __END__

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