Annotation of loncom/homework/edit.pm, revision 1.22
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.22 ! albertel 24: if (!$description) {
! 25: $description=&Apache::lonxml::description($token);
! 26: if (!$description) { $description="<$tag>"; }
! 27: }
1.10 albertel 28: $result.= &start_table($token)."<tr><td>$description</td>
1.14 albertel 29: <td>Delete".
1.8 albertel 30: &deletelist($target,$token)
31: ."</td>
1.4 albertel 32: <td>".
1.8 albertel 33: &insertlist($target,$token).
1.4 albertel 34: "</td>
1.1 albertel 35: </tr><tr><td colspan=\"3\">\n";
1.22 ! albertel 36: #<td>".
! 37: # &movebuttons($target,$token).
! 38: # "</tr><tr><td colspan=\"3\">\n";
1.4 albertel 39: }
1.1 albertel 40: return $result;
41: }
42:
43: sub tag_end {
1.9 albertel 44: my ($target,$token,$description) = @_;
1.1 albertel 45: my $result='';
1.4 albertel 46: if ($target eq 'edit') {
47: my $tag=$token->[1];
1.9 albertel 48: if (!defined($description)) {
1.14 albertel 49: $result.="</td></tr><tr><td></$tag></td><td colspan=\"2\"> </td>";
1.9 albertel 50: } else {
1.14 albertel 51: if ($description ne '') { $result.="</td></tr><tr><td>$description</td><td colspan=\"2\"> </td>"; }
1.9 albertel 52: }
1.12 albertel 53: $result.="</tr>".&end_table()."\n";
1.4 albertel 54: }
55: return $result;
56: }
1.1 albertel 57:
1.10 albertel 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++;
1.12 albertel 73: my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"5\">";
1.10 albertel 74: return $result;
75: }
76:
77: sub end_table {
78: $Apache::edit::colordepth--;
79: my $result="</table>";
80: return $result;
81: }
82:
1.22 ! albertel 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:
1.8 albertel 92: sub deletelist {
93: my ($target,$token) = @_;
94: my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
1.14 albertel 95: <option></option>
96: <option>Yes</option>
1.8 albertel 97: </select>";
98: return $result;
99: }
100:
1.14 albertel 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:
1.7 albertel 124: sub get_insert_list {
1.6 albertel 125: my ($token) = @_;
126: my $result='';
1.7 albertel 127: my @tagnums= ();
128: #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
1.6 albertel 129: if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
1.7 albertel 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"} });
1.6 albertel 135: }
136: }
1.7 albertel 137: if (@tagnums) {
138: foreach my $tagnum (@tagnums) {
139: $result.='<option value="'.$tagnum.'">'.$Apache::lonxml::insertlist{"$tagnum.description"}."</option>\n";
1.5 albertel 140: }
141: if ($result) { $result='<option selected="on"></option>'.$result; }
142: }
143: return $result;
144: }
145:
1.4 albertel 146: sub insertlist {
1.8 albertel 147: my ($target,$token) = @_;
1.4 albertel 148: my $result;
149: if ($target eq 'edit') {
1.5 albertel 150: my $optionlist= &get_insert_list($token);
151: if ($optionlist) {
152: $result = "Insert:
1.4 albertel 153: <select name=\"insert_$Apache::lonxml::curdepth\">
1.5 albertel 154: $optionlist
1.4 albertel 155: </select>"
1.11 albertel 156: } else {
157: $result=" ";
1.6 albertel 158: }
159: }
160: return $result;
161: }
162:
1.7 albertel 163: sub handle_insert {
1.15 albertel 164: if ($ENV{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
1.6 albertel 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"};
1.7 albertel 170: my $namespace;
171: if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
1.6 albertel 172: $result.="\n<$newtag>\n</$newtag>";
173: } else {
1.15 albertel 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.");
1.5 albertel 182: }
183: }
184: return $result;
1.16 albertel 185: }
186:
187: sub insert_responseparam {
188: return '
189: <responseparam />';
1.5 albertel 190: }
191:
1.15 albertel 192: sub insert_numericalresponse {
193: return '
194: <numericalresponse answer="">
195: <textline />
196: <hintgroup>
197: </hintgroup>
198: </numericalresponse>';
199: }
200:
1.18 albertel 201: sub insert_stringresponse {
202: return '
203: <stringresponse answer="" type="">
204: <textline />
205: <hintgroup>
206: </hintgroup>
207: </stringresponse>';
208: }
209:
1.7 albertel 210: sub insert_optionresponse {
211: return '
212: <optionresponse max="10">
213: <foilgroup options="">
214: </foilgroup>
1.14 albertel 215: <hintgroup>
216: </hintgroup>
1.7 albertel 217: </optionresponse>';
1.1 albertel 218: }
219:
1.21 albertel 220: sub insert_displayduedate { return '<displayduedate />'; }
221: sub insert_displaytitle { return '<displaytitle />'; }
1.22 ! albertel 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: }
1.21 albertel 235:
1.2 albertel 236: sub editfield {
1.5 albertel 237: my ($tag,$data,$description,$minwidth,$minheight)=@_;
1.22 ! albertel 238:
1.2 albertel 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; }
1.5 albertel 245: if ($maxlength < $minwidth) { $maxlength = $minwidth; }
246: if ( $count < $minheight) { $count = $minheight; }
247: if ($description) {
1.9 albertel 248: $description="<br />".$description."<br />";
1.2 albertel 249: }
1.9 albertel 250: return "$description\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea>\n";
1.2 albertel 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) = @_;
1.3 albertel 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: }
1.2 albertel 269: }
1.3 albertel 270: return $result;
1.5 albertel 271: }
272:
273: sub insert_startouttext {
1.14 albertel 274: return "<startouttext />\n<endouttext />";
275: }
276:
277: sub insert_script {
278: return "\n<script type=\"loncapa/perl\">\n</script>";
1.2 albertel 279: }
280:
1.15 albertel 281: # Returns a 1 if the token has been modified and you should rebuild the tag
1.12 albertel 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) {
1.20 albertel 287: #just want the string that it was set to
288: my $value=$token->[2]->{$arg};
1.12 albertel 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:
1.15 albertel 299: # looks for /> on start tags
1.12 albertel 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]})) {
1.20 albertel 306: $val=~s:^\s+|\s+$::g;
1.17 albertel 307: $val=~s:"::g; #"
1.12 albertel 308: &Apache::lonxml::debug("setting :$key: to :$val:");
309: $result.=' '.$key.'="'.$val.'"';
310: }
1.15 albertel 311: if ($token->[4] =~ m:/>$:) {
312: $result.=' />';
313: } else {
314: $result.='>';
315: }
1.12 albertel 316: } elsif ( $token->[0] eq 'E' ) {
317: $result = '</'.$token->[1].'>';
318: }
319: return $result;
320: }
1.13 albertel 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:
1.19 albertel 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: }
1.1 albertel 374: 1;
375: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>