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></$tag>";
44: } else {
45: if ($description ne '') { $result.="</td></tr><tr><td>$description"; }
46: }
47: $result.="</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=\"2\">";
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: }
120: }
121: return $result;
122: }
123:
124: sub handle_insert {
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"};
131: my $namespace;
132: if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
133: $result.="\n<$newtag>\n</$newtag>";
134: } else {
135: {
136: no strict 'refs';
137: $result.=&$func();
138: }
139: }
140: return $result;
141: }
142:
143: sub insert_optionresponse {
144: return '
145: <optionresponse max="10">
146: <foilgroup options="">
147: </foilgroup>
148: </optionresponse>';
149: }
150:
151: sub editfield {
152: my ($tag,$data,$description,$minwidth,$minheight)=@_;
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; }
160: if ($maxlength < $minwidth) { $maxlength = $minwidth; }
161: if ( $count < $minheight) { $count = $minheight; }
162: if ($description) {
163: $description="<br />".$description."<br />";
164: }
165: return "$description\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea>\n";
166: # return "<br />\n<$tag><br />\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br />\n</$tag><br />\n";
167: }
168:
169: sub modifiedfield {
170: my ($token) = @_;
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: }
184: }
185: return $result;
186: }
187:
188: sub insert_startouttext {
189: return "\n<startouttext />\n<endouttext />";
190: }
191:
192: 1;
193: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>