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) {
25: $description=&Apache::lonxml::description($token);
26: if (!$description) { $description="<$tag>"; }
27: }
28: $result.= &start_table($token)."<tr><td>$description</td>
29: <td>Delete".
30: &deletelist($target,$token)
31: ."</td>
32: <td>".
33: &insertlist($target,$token).
34: "</td>
35: </tr><tr><td colspan=\"3\">\n";
36: #<td>".
37: # &movebuttons($target,$token).
38: # "</tr><tr><td colspan=\"3\">\n";
39: }
40: return $result;
41: }
42:
43: sub tag_end {
44: my ($target,$token,$description) = @_;
45: my $result='';
46: if ($target eq 'edit') {
47: my $tag=$token->[1];
48: if (!defined($description)) {
49: $result.="</td></tr><tr><td></$tag></td><td colspan=\"2\"> </td>";
50: } else {
51: if ($description ne '') { $result.="</td></tr><tr><td>$description</td><td colspan=\"2\"> </td>"; }
52: }
53: $result.="</tr>".&end_table()."\n";
54: }
55: return $result;
56: }
57:
58: sub start_table {
59: my ($token)=@_;
60: my $tag = $token->[1];
61: my $tagnum;
62: foreach my $namespace (reverse @Apache::lonxml::namespace) {
63: my $testtag=$Apache::lonxml::namespace['-1'].'::'.$tag;
64: $tagnum=$Apache::lonxml::insertlist{"$testtag.num"};
65: if (defined($tagnum)) { last; }
66: }
67: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
68: my $color = $Apache::lonxml::insertlist{"$tagnum.color"};
69: if (!defined($color)) {
70: $color = $Apache::edit::colorlist[$Apache::edit::colordepth];
71: }
72: $Apache::edit::colordepth++;
73: my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"5\">";
74: return $result;
75: }
76:
77: sub end_table {
78: $Apache::edit::colordepth--;
79: my $result="</table>";
80: return $result;
81: }
82:
83: sub movebuttons {
84: my ($target,$token) = @_;
85: my $result='<input type="submit" name="moveup.'.
86: $Apache::lonxml::curdepth.'" value="Move Up" />';
87: $result.='<input type="submit" name="movedown.'.
88: $Apache::lonxml::curdepth.'" value="Move Down" />';
89: return $result;
90: }
91:
92: sub deletelist {
93: my ($target,$token) = @_;
94: my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
95: <option></option>
96: <option>Yes</option>
97: </select>";
98: return $result;
99: }
100:
101: sub handle_delete {
102: if (!$ENV{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
103: my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
104: my $result=0;
105: if ($space) {
106: my $sub1="$space\:\:delete_$token->[1]";
107: {
108: no strict 'refs';
109: if (defined &$sub1) {
110: $result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
111: }
112: }
113: }
114: if (!$result) {
115: my $endtag='/'.$token->[1];
116: my $bodytext=&Apache::lonxml::get_all_text($endtag,$$parser[$#$parser]);
117: $$parser['-1']->get_token();
118: &Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
119: &Apache::lonxml::end_tag($tagstack,$parstack,$token);
120: }
121: return 1;
122: }
123:
124: sub get_insert_list {
125: my ($token) = @_;
126: my $result='';
127: my @tagnums= ();
128: #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
129: if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
130: push (@tagnums, @{ $Apache::lonxml::insertlist{"$token->[1].which"} });
131: }
132: foreach my $namespace (@Apache::lonxml::namespace) {
133: if ($Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"}) {
134: push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"} });
135: }
136: }
137: if (@tagnums) {
138: foreach my $tagnum (@tagnums) {
139: $result.='<option value="'.$tagnum.'">'.$Apache::lonxml::insertlist{"$tagnum.description"}."</option>\n";
140: }
141: if ($result) { $result='<option selected="on"></option>'.$result; }
142: }
143: return $result;
144: }
145:
146: sub insertlist {
147: my ($target,$token) = @_;
148: my $result;
149: if ($target eq 'edit') {
150: my $optionlist= &get_insert_list($token);
151: if ($optionlist) {
152: $result = "Insert:
153: <select name=\"insert_$Apache::lonxml::curdepth\">
154: $optionlist
155: </select>"
156: } else {
157: $result=" ";
158: }
159: }
160: return $result;
161: }
162:
163: sub handle_insert {
164: if ($ENV{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
165: my $result;
166: my $tagnum = $ENV{"form.insert_$Apache::lonxml::curdepth"};
167: my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
168: if ($func eq 'default') {
169: my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
170: my $namespace;
171: if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
172: $result.="\n<$newtag>\n</$newtag>";
173: } else {
174: if (defined(&$func)) {
175: {
176: no strict 'refs';
177: $result.=&$func();
178: }
179: } else {
180: my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
181: &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined.");
182: }
183: }
184: return $result;
185: }
186:
187: sub insert_responseparam {
188: return '
189: <responseparam />';
190: }
191:
192: sub insert_numericalresponse {
193: return '
194: <numericalresponse answer="">
195: <textline />
196: <hintgroup>
197: </hintgroup>
198: </numericalresponse>';
199: }
200:
201: sub insert_stringresponse {
202: return '
203: <stringresponse answer="" type="">
204: <textline />
205: <hintgroup>
206: </hintgroup>
207: </stringresponse>';
208: }
209:
210: sub insert_optionresponse {
211: return '
212: <optionresponse max="10">
213: <foilgroup options="">
214: </foilgroup>
215: <hintgroup>
216: </hintgroup>
217: </optionresponse>';
218: }
219:
220: sub insert_displayduedate { return '<displayduedate />'; }
221: sub insert_displaytitle { return '<displaytitle />'; }
222: sub insert_hintpart {
223: return '
224: <hintpart on="default">
225: <startouttext/>
226: <endouttext />
227: </hintpart>';
228: }
229:
230: sub insert_numericalhint {
231: return '
232: <numericalhint>
233: </numericalhint>';
234: }
235:
236: sub editfield {
237: my ($tag,$data,$description,$minwidth,$minheight)=@_;
238:
239: my $count=0;
240: my $maxlength=-1;
241: map { $count++;
242: if (length($_) > $maxlength) { $maxlength = length ($_); }
243: } split ("\n", $data);
244: if ($maxlength > 80) { $maxlength = 80; }
245: if ($maxlength < $minwidth) { $maxlength = $minwidth; }
246: if ( $count < $minheight) { $count = $minheight; }
247: if ($description) {
248: $description="<br />".$description."<br />";
249: }
250: return "$description\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea>\n";
251: # return "<br />\n<$tag><br />\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br />\n</$tag><br />\n";
252: }
253:
254: sub modifiedfield {
255: my ($token) = @_;
256: my $result;
257: # foreach my $envkey (sort keys %ENV) {
258: # &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
259: # }
260: # &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
261: # &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
262: $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
263: if (defined $token) {
264: if (defined $token->[4]) {
265: $result=$token->[4].$result;
266: } else {
267: $result=$result.$token->[2];
268: }
269: }
270: return $result;
271: }
272:
273: sub insert_startouttext {
274: return "<startouttext />\n<endouttext />";
275: }
276:
277: sub insert_script {
278: return "\n<script type=\"loncapa/perl\">\n</script>";
279: }
280:
281: # Returns a 1 if the token has been modified and you should rebuild the tag
282: # side-effects, will modify the $token if new values are found
283: sub get_new_args {
284: my ($token,$parstack,$safeeval,@args)=@_;
285: my $rebuild=0;
286: foreach my $arg (@args) {
287: #just want the string that it was set to
288: my $value=$token->[2]->{$arg};
289: my $newvalue=$ENV{"form.$Apache::lonxml::curdepth.$arg"};
290: &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:");
291: if ($value ne $newvalue) {
292: $token->[2]->{$arg}=$newvalue;
293: $rebuild=1;
294: }
295: }
296: return $rebuild;
297: }
298:
299: # looks for /> on start tags
300: sub rebuild_tag {
301: my ($token) = @_;
302: my $result;
303: if ($token->[0] eq 'S') {
304: $result = '<'.$token->[1];
305: while (my ($key,$val)= each(%{$token->[2]})) {
306: $val=~s:^\s+|\s+$::g;
307: $val=~s:"::g; #"
308: &Apache::lonxml::debug("setting :$key: to :$val:");
309: $result.=' '.$key.'="'.$val.'"';
310: }
311: if ($token->[4] =~ m:/>$:) {
312: $result.=' />';
313: } else {
314: $result.='>';
315: }
316: } elsif ( $token->[0] eq 'E' ) {
317: $result = '</'.$token->[1].'>';
318: }
319: return $result;
320: }
321:
322: sub text_arg {
323: my ($description,$name,$token,$size) = @_;
324: my $result;
325: if (!defined $size) { $size=20; }
326: my $arg=$token->[2]{$name};
327: $result=$description.'<input name="'."$Apache::lonxml::curdepth.$name".
328: '" type="text" value="'.$arg.'" size="'.$size.'" />';
329: return $result;
330: }
331:
332: sub select_arg {
333: my ($description,$name,$list,$token) = @_;
334: my $result;
335: my $optionlist="";
336: my $selected=$token->[2]{$name};
337: foreach my $option (@$list) {
338: if ( $selected eq $option ) {
339: $optionlist.="<option selected=\"on\">$option</option>\n";
340: } else {
341: $optionlist.="<option>$option</option>\n";
342: }
343: }
344: $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
345: '.$optionlist.'
346: </select></td></tr><tr><td colspan="3">';
347: return $result;
348: }
349:
350: sub select_or_text_arg {
351: my ($description,$name,$list,$token,$size) = @_;
352: my $result;
353: my $optionlist="";
354: my $found=0;
355: my $selected=$token->[2]{$name};
356: foreach my $option (@$list) {
357: if ( $selected eq $option ) {
358: $optionlist.="<option selected=\"on\">$option</option>\n";
359: $found=1;
360: } else {
361: $optionlist.="<option>$option</option>\n";
362: }
363: }
364: $optionlist.="<option value=\"TYPEDINVALUE\">Type in value</option>\n";
365: if ($found) {
366: $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
367: '.$optionlist.'
368: </select></td></tr><tr><td colspan="3">';
369: } else {
370: $result.=&text_arg($description,$name,$token,$size);
371: }
372: return $result;
373: }
374: 1;
375: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>