Annotation of loncom/homework/edit.pm, revision 1.12
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></$tag>";
44: } else {
45: if ($description ne '') { $result.="</td></tr><tr><td>$description"; }
46: }
1.12 ! albertel 47: $result.="</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++;
1.12 ! albertel 67: my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"5\">";
1.10 albertel 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.11 albertel 119: } else {
120: $result=" ";
1.6 albertel 121: }
122: }
123: return $result;
124: }
125:
1.7 albertel 126: sub handle_insert {
1.6 albertel 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"};
1.7 albertel 133: my $namespace;
134: if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
1.6 albertel 135: $result.="\n<$newtag>\n</$newtag>";
136: } else {
137: {
138: no strict 'refs';
1.7 albertel 139: $result.=&$func();
1.5 albertel 140: }
141: }
142: return $result;
143: }
144:
1.7 albertel 145: sub insert_optionresponse {
146: return '
147: <optionresponse max="10">
148: <foilgroup options="">
149: </foilgroup>
150: </optionresponse>';
1.1 albertel 151: }
152:
1.2 albertel 153: sub editfield {
1.5 albertel 154: my ($tag,$data,$description,$minwidth,$minheight)=@_;
1.2 albertel 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; }
1.5 albertel 162: if ($maxlength < $minwidth) { $maxlength = $minwidth; }
163: if ( $count < $minheight) { $count = $minheight; }
164: if ($description) {
1.9 albertel 165: $description="<br />".$description."<br />";
1.2 albertel 166: }
1.9 albertel 167: return "$description\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea>\n";
1.2 albertel 168: # return "<br />\n<$tag><br />\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br />\n</$tag><br />\n";
169: }
170:
171: sub modifiedfield {
172: my ($token) = @_;
1.3 albertel 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: }
1.2 albertel 186: }
1.3 albertel 187: return $result;
1.5 albertel 188: }
189:
190: sub insert_startouttext {
191: return "\n<startouttext />\n<endouttext />";
1.2 albertel 192: }
193:
1.12 ! albertel 194: # Returns a 1 if the toekn has been modified and you should rebuild the tag
! 195: # side-effects, will modify the $token if new values are found
! 196: sub get_new_args {
! 197: my ($token,$parstack,$safeeval,@args)=@_;
! 198: my $rebuild=0;
! 199: foreach my $arg (@args) {
! 200: my $value=&Apache::lonxml::get_param($arg,$parstack,$safeeval);
! 201: my $newvalue=$ENV{"form.$Apache::lonxml::curdepth.$arg"};
! 202: &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:");
! 203: if ($value ne $newvalue) {
! 204: $token->[2]->{$arg}=$newvalue;
! 205: $rebuild=1;
! 206: }
! 207: }
! 208: return $rebuild;
! 209: }
! 210:
! 211: sub rebuild_tag {
! 212: my ($token) = @_;
! 213: my $result;
! 214: if ($token->[0] eq 'S') {
! 215: $result = '<'.$token->[1];
! 216: while (my ($key,$val)= each(%{$token->[2]})) {
! 217: &Apache::lonxml::debug("setting :$key: to :$val:");
! 218: $result.=' '.$key.'="'.$val.'"';
! 219: }
! 220: $result.=">";
! 221: } elsif ( $token->[0] eq 'E' ) {
! 222: $result = '</'.$token->[1].'>';
! 223: }
! 224: return $result;
! 225: }
1.1 albertel 226: 1;
227: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>