Annotation of loncom/homework/edit.pm, revision 1.37
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # edit mode helpers
1.25 albertel 3: #
1.37 ! albertel 4: # $Id: edit.pm,v 1.36 2002/08/07 16:23:05 albertel Exp $
1.25 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.1 albertel 28: # 3/20 Guy
1.27 matthew 29: # 01/10/02 Matthew
1.29 matthew 30: # 03/06/02 Matthew
1.1 albertel 31: package Apache::edit;
32:
33: use strict;
1.32 albertel 34: use Apache::lonnet();
35: use HTML::Entities();
1.1 albertel 36:
1.10 albertel 37: # Global Vars
38: # default list of colors to use in editing
39: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
40: # depth of nesting of edit
41: $Apache::edit::colordepth=0;
42:
43: sub initialize_edit {
44: $Apache::edit::colordepth=0;
45: }
46:
1.1 albertel 47: sub tag_start {
1.9 albertel 48: my ($target,$token,$description) = @_;
1.1 albertel 49: my $result='';
1.5 albertel 50: if ($target eq "edit") {
1.4 albertel 51: my $tag=$token->[1];
1.22 albertel 52: if (!$description) {
53: $description=&Apache::lonxml::description($token);
54: if (!$description) { $description="<$tag>"; }
55: }
1.10 albertel 56: $result.= &start_table($token)."<tr><td>$description</td>
1.14 albertel 57: <td>Delete".
1.8 albertel 58: &deletelist($target,$token)
59: ."</td>
1.4 albertel 60: <td>".
1.27 matthew 61: &insertlist($target,$token).&end_row().&start_spanning_row();
62: #<td>".
1.22 albertel 63: # &movebuttons($target,$token).
64: # "</tr><tr><td colspan=\"3\">\n";
1.4 albertel 65: }
1.1 albertel 66: return $result;
67: }
68:
69: sub tag_end {
1.9 albertel 70: my ($target,$token,$description) = @_;
1.1 albertel 71: my $result='';
1.4 albertel 72: if ($target eq 'edit') {
73: my $tag=$token->[1];
1.9 albertel 74: if (!defined($description)) {
1.14 albertel 75: $result.="</td></tr><tr><td></$tag></td><td colspan=\"2\"> </td>";
1.9 albertel 76: } else {
1.14 albertel 77: if ($description ne '') { $result.="</td></tr><tr><td>$description</td><td colspan=\"2\"> </td>"; }
1.9 albertel 78: }
1.12 albertel 79: $result.="</tr>".&end_table()."\n";
1.4 albertel 80: }
81: return $result;
82: }
1.1 albertel 83:
1.10 albertel 84: sub start_table {
85: my ($token)=@_;
86: my $tag = $token->[1];
87: my $tagnum;
88: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1.23 albertel 89: my $testtag=$namespace.'::'.$tag;
1.10 albertel 90: $tagnum=$Apache::lonxml::insertlist{"$testtag.num"};
91: if (defined($tagnum)) { last; }
92: }
93: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
94: my $color = $Apache::lonxml::insertlist{"$tagnum.color"};
95: if (!defined($color)) {
96: $color = $Apache::edit::colorlist[$Apache::edit::colordepth];
97: }
98: $Apache::edit::colordepth++;
1.12 albertel 99: my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"5\">";
1.10 albertel 100: return $result;
101: }
102:
103: sub end_table {
104: $Apache::edit::colordepth--;
105: my $result="</table>";
106: return $result;
107: }
108:
1.27 matthew 109: sub start_spanning_row { return '<tr><td colspan="3">';}
110: sub start_row { return '<tr><td>'; }
111: sub end_row { return '</td></tr>'; }
112:
1.22 albertel 113: sub movebuttons {
114: my ($target,$token) = @_;
115: my $result='<input type="submit" name="moveup.'.
116: $Apache::lonxml::curdepth.'" value="Move Up" />';
117: $result.='<input type="submit" name="movedown.'.
118: $Apache::lonxml::curdepth.'" value="Move Down" />';
119: return $result;
120: }
121:
1.8 albertel 122: sub deletelist {
123: my ($target,$token) = @_;
124: my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
1.14 albertel 125: <option></option>
126: <option>Yes</option>
1.8 albertel 127: </select>";
128: return $result;
129: }
130:
1.14 albertel 131: sub handle_delete {
132: if (!$ENV{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
133: my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
134: my $result=0;
135: if ($space) {
136: my $sub1="$space\:\:delete_$token->[1]";
137: {
138: no strict 'refs';
139: if (defined &$sub1) {
140: $result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
141: }
142: }
143: }
144: if (!$result) {
145: my $endtag='/'.$token->[1];
146: my $bodytext=&Apache::lonxml::get_all_text($endtag,$$parser[$#$parser]);
147: $$parser['-1']->get_token();
148: &Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
149: &Apache::lonxml::end_tag($tagstack,$parstack,$token);
150: }
151: return 1;
152: }
153:
1.7 albertel 154: sub get_insert_list {
1.6 albertel 155: my ($token) = @_;
156: my $result='';
1.7 albertel 157: my @tagnums= ();
158: #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
1.6 albertel 159: if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
1.7 albertel 160: push (@tagnums, @{ $Apache::lonxml::insertlist{"$token->[1].which"} });
161: }
162: foreach my $namespace (@Apache::lonxml::namespace) {
163: if ($Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"}) {
164: push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"} });
1.6 albertel 165: }
166: }
1.7 albertel 167: if (@tagnums) {
1.33 albertel 168: my %options;
1.7 albertel 169: foreach my $tagnum (@tagnums) {
1.33 albertel 170: my $descrip=$Apache::lonxml::insertlist{"$tagnum.description"};
171: $options{$descrip} ="<option value=\"$tagnum\">".$descrip."</option>\n";
1.5 albertel 172: }
1.33 albertel 173: foreach my $option (sort(keys(%options))) { $result.=$options{$option}; }
1.5 albertel 174: if ($result) { $result='<option selected="on"></option>'.$result; }
175: }
176: return $result;
177: }
178:
1.4 albertel 179: sub insertlist {
1.8 albertel 180: my ($target,$token) = @_;
1.4 albertel 181: my $result;
182: if ($target eq 'edit') {
1.5 albertel 183: my $optionlist= &get_insert_list($token);
184: if ($optionlist) {
185: $result = "Insert:
1.4 albertel 186: <select name=\"insert_$Apache::lonxml::curdepth\">
1.5 albertel 187: $optionlist
1.4 albertel 188: </select>"
1.11 albertel 189: } else {
190: $result=" ";
1.6 albertel 191: }
192: }
193: return $result;
194: }
195:
1.7 albertel 196: sub handle_insert {
1.15 albertel 197: if ($ENV{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
1.6 albertel 198: my $result;
199: my $tagnum = $ENV{"form.insert_$Apache::lonxml::curdepth"};
200: my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
201: if ($func eq 'default') {
202: my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
1.7 albertel 203: my $namespace;
204: if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
1.6 albertel 205: $result.="\n<$newtag>\n</$newtag>";
206: } else {
1.15 albertel 207: if (defined(&$func)) {
208: {
209: no strict 'refs';
210: $result.=&$func();
211: }
212: } else {
213: my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
214: &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined.");
1.5 albertel 215: }
216: }
217: return $result;
1.16 albertel 218: }
219:
220: sub insert_responseparam {
221: return '
222: <responseparam />';
1.5 albertel 223: }
224:
1.24 albertel 225: sub insert_formularesponse {
226: return '
227: <formularesponse answer="" samples="">
228: <textline />
229: <hintgroup>
230: </hintgroup>
231: </formularesponse>';
232: }
233:
1.15 albertel 234: sub insert_numericalresponse {
235: return '
236: <numericalresponse answer="">
237: <textline />
238: <hintgroup>
239: </hintgroup>
240: </numericalresponse>';
241: }
242:
1.18 albertel 243: sub insert_stringresponse {
244: return '
245: <stringresponse answer="" type="">
246: <textline />
247: <hintgroup>
248: </hintgroup>
249: </stringresponse>';
1.36 albertel 250: }
251:
252: sub insert_essayresponse {
253: return '
254: <essayresponse>
255: <textfield></textfield>
256: </essayresponse>';
1.18 albertel 257: }
258:
1.7 albertel 259: sub insert_optionresponse {
260: return '
261: <optionresponse max="10">
262: <foilgroup options="">
263: </foilgroup>
1.14 albertel 264: <hintgroup>
265: </hintgroup>
1.7 albertel 266: </optionresponse>';
1.1 albertel 267: }
268:
1.23 albertel 269: sub insert_radiobuttonresponse {
270: return '
271: <radiobuttonresponse max="10">
272: <foilgroup>
273: </foilgroup>
274: <hintgroup>
275: </hintgroup>
276: </radiobuttonresponse>';
277: }
278:
1.21 albertel 279: sub insert_displayduedate { return '<displayduedate />'; }
280: sub insert_displaytitle { return '<displaytitle />'; }
1.22 albertel 281: sub insert_hintpart {
282: return '
283: <hintpart on="default">
284: <startouttext/>
285: <endouttext />
286: </hintpart>';
287: }
288:
289: sub insert_numericalhint {
290: return '
291: <numericalhint>
292: </numericalhint>';
1.37 ! albertel 293: }
! 294:
! 295: sub insert_radiobuttonhint {
! 296: return '
! 297: <radiobuttonhint>
! 298: </radiobuttonhint>';
1.22 albertel 299: }
1.21 albertel 300:
1.23 albertel 301: sub insert_startouttext {
302: return "<startouttext />\n<endouttext />";
303: }
304:
305: sub insert_script {
306: return "\n<script type=\"loncapa/perl\">\n</script>";
307: }
308:
1.25 albertel 309: sub textarea_sizes {
310: my ($data)=@_;
311: my $count=0;
312: my $maxlength=-1;
1.26 harris41 313: foreach (split ("\n", $$data)) { $count++;
1.25 albertel 314: if (length($_) > $maxlength) { $maxlength = length($_); }
1.26 harris41 315: }
1.25 albertel 316: my $rows = $count;
317: my $cols = $maxlength;
318: return ($rows,$cols);
319: }
320:
1.32 albertel 321: sub editline {
1.31 matthew 322: my ($tag,$data,$description,$size)=@_;
1.32 albertel 323: $data=&HTML::Entities::encode($data);
1.31 matthew 324: if ($description) { $description="<br />".$description."<br />"; }
325: my $result = <<"END";
326: $description
327: <input type="text" name="homework_edit_$Apache::lonxml::curdepth"
328: value="$data" size="$size" />
329: END
330: return $result;
331: }
332:
1.2 albertel 333: sub editfield {
1.5 albertel 334: my ($tag,$data,$description,$minwidth,$minheight)=@_;
1.22 albertel 335:
1.25 albertel 336: my ($rows,$cols)=&textarea_sizes(\$data);
337: if ($cols > 80) { $cols = 80; }
338: if ($cols < $minwidth ) { $cols = $minwidth; }
339: if ($rows < $minheight) { $rows = $minheight; }
340: if ($description) { $description="<br />".$description."<br />"; }
1.32 albertel 341: return $description."\n".' <textarea rows="'.$rows.
342: '" cols="'.$cols.'" name="homework_edit_'.$Apache::lonxml::curdepth.'">'.
343: &HTML::Entities::encode($data).'</textarea>'."\n";
1.2 albertel 344: }
345:
346: sub modifiedfield {
347: my ($token) = @_;
1.3 albertel 348: my $result;
349: # foreach my $envkey (sort keys %ENV) {
350: # &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
351: # }
352: # &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
353: # &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
354: $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
355: return $result;
1.2 albertel 356: }
357:
1.15 albertel 358: # Returns a 1 if the token has been modified and you should rebuild the tag
1.12 albertel 359: # side-effects, will modify the $token if new values are found
360: sub get_new_args {
361: my ($token,$parstack,$safeeval,@args)=@_;
362: my $rebuild=0;
363: foreach my $arg (@args) {
1.20 albertel 364: #just want the string that it was set to
365: my $value=$token->[2]->{$arg};
1.12 albertel 366: my $newvalue=$ENV{"form.$Apache::lonxml::curdepth.$arg"};
367: &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:");
368: if ($value ne $newvalue) {
369: $token->[2]->{$arg}=$newvalue;
370: $rebuild=1;
371: }
372: }
373: return $rebuild;
374: }
375:
1.15 albertel 376: # looks for /> on start tags
1.12 albertel 377: sub rebuild_tag {
378: my ($token) = @_;
379: my $result;
380: if ($token->[0] eq 'S') {
381: $result = '<'.$token->[1];
382: while (my ($key,$val)= each(%{$token->[2]})) {
1.20 albertel 383: $val=~s:^\s+|\s+$::g;
1.17 albertel 384: $val=~s:"::g; #"
1.12 albertel 385: &Apache::lonxml::debug("setting :$key: to :$val:");
386: $result.=' '.$key.'="'.$val.'"';
387: }
1.15 albertel 388: if ($token->[4] =~ m:/>$:) {
389: $result.=' />';
390: } else {
391: $result.='>';
392: }
1.12 albertel 393: } elsif ( $token->[0] eq 'E' ) {
394: $result = '</'.$token->[1].'>';
395: }
396: return $result;
397: }
1.13 albertel 398:
399: sub text_arg {
400: my ($description,$name,$token,$size) = @_;
401: my $result;
402: if (!defined $size) { $size=20; }
403: my $arg=$token->[2]{$name};
404: $result=$description.'<input name="'."$Apache::lonxml::curdepth.$name".
405: '" type="text" value="'.$arg.'" size="'.$size.'" />';
406: return $result;
407: }
408:
409: sub select_arg {
410: my ($description,$name,$list,$token) = @_;
411: my $result;
412: my $optionlist="";
413: my $selected=$token->[2]{$name};
414: foreach my $option (@$list) {
415: if ( $selected eq $option ) {
416: $optionlist.="<option selected=\"on\">$option</option>\n";
417: } else {
418: $optionlist.="<option>$option</option>\n";
419: }
420: }
421: $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
422: '.$optionlist.'
1.27 matthew 423: </select>';
1.13 albertel 424: return $result;
425: }
426:
1.19 albertel 427: sub select_or_text_arg {
428: my ($description,$name,$list,$token,$size) = @_;
429: my $result;
430: my $optionlist="";
431: my $found=0;
432: my $selected=$token->[2]{$name};
433: foreach my $option (@$list) {
434: if ( $selected eq $option ) {
435: $optionlist.="<option selected=\"on\">$option</option>\n";
436: $found=1;
437: } else {
438: $optionlist.="<option>$option</option>\n";
439: }
440: }
441: $optionlist.="<option value=\"TYPEDINVALUE\">Type in value</option>\n";
1.35 albertel 442: if (($found) || (!$selected)) {
1.19 albertel 443: $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
444: '.$optionlist.'
1.27 matthew 445: </select>';
1.19 albertel 446: } else {
447: $result.=&text_arg($description,$name,$token,$size);
448: }
449: return $result;
450: }
1.29 matthew 451:
452: #----------------------------------------------------- browse
453: sub browse {
454: # insert a link to call up the filesystem browser (lonindexer)
1.30 matthew 455: $_ = shift;
1.29 matthew 456: my $form = 'lonhomework';
1.30 matthew 457: my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
1.29 matthew 458: my $result = <<"ENDBUTTON";
459: <a href=\"javascript:openbrowser('$form','$element')\"\>Browse</a>
460: ENDBUTTON
461: return $result;
462: }
463:
1.30 matthew 464: #----------------------------------------------------- browse
465: sub search {
466: # insert a link to call up the filesystem browser (lonindexer)
467: $_ = shift;
468: my $form = 'lonhomework';
469: my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
470: my $result = <<"ENDBUTTON";
471: <a href=\"javascript:opensearcher('$form','$element')\"\>Search</a>
472: ENDBUTTON
473: return $result;
474: }
475:
476:
1.1 albertel 477: 1;
478: __END__
1.26 harris41 479:
480: =head1 NAME
481:
482: Apache::edit - edit mode helpers
483:
484: =head1 SYNOPSIS
485:
486: Invoked by many homework and xml related modules.
487:
488: &Apache::edit::SUBROUTINENAME(ARGUMENTS);
489:
490: =head1 INTRODUCTION
491:
492: This module outputs HTML syntax helpful for the rendering of edit
493: mode interfaces.
494:
495: This is part of the LearningOnline Network with CAPA project
496: described at http://www.lon-capa.org.
497:
498: =head1 HANDLER SUBROUTINE
499:
500: There is no handler subroutine.
501:
502: =head1 OTHER SUBROUTINES
503:
504: =over 4
505:
506: =item *
507:
508: initialize_edit() : initialize edit (set colordepth to zero)
509:
510: =item *
511:
512: tag_start($target,$token,$description) : provide deletion and insertion lists
513: for the manipulation of a start tag; return a scalar string
514:
515: =item *
516:
517: tag_end($target,$token,$description) : ending syntax corresponding to
518: &tag_start. return a scalar string.
519:
520: =item *
521:
522: start_table($token) : start table; update colordepth; return scalar string.
523:
524: =item *
525:
526: end_table() : reduce color depth; end table; return scalar string
1.27 matthew 527:
528: =item *
529:
530: start_spanning_row() : start a new table row spanning the 'edit' environment.
531:
532: =item *
533:
534: start_row() : start a new table row and element.
535:
536: =item *
537:
538: end_row() : end current table element and row.
1.26 harris41 539:
540: =item *
541:
542: movebuttons($target,$token) : move-up and move-down buttons; return scalar
543: string
544:
545: =item *
546:
547: deletelist($target,$token) : provide a yes option in an HTML select element;
548: return scalar string
549:
550: =item *
551:
552: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
553: $style) : respond to a user delete request by passing relevant stack
554: and array information to various rendering functions; return a scalar string
555:
556: =item *
557:
558: get_insert_list($token) : provide an insertion list based on possibilities
559: from lonxml; return a scalar string
560:
561: =item *
562:
563: insertlist($target,$token) : api that uses get_insert_list;
564: return a scalar string
565:
566: =item *
567:
568: handleinsert($token) : provide an insertion list based on possibilities
569: from lonxml; return a scalar string
570:
571: =item *
572:
573: get_insert_list($token) : provide an insertion list based on possibilities
574: from lonxml; return a scalar string
1.29 matthew 575:
576: =item *
577: browse($elementname) : provide a link which will open up the filesystem
578: browser (lonindexer) and, once a file is selected, place the result in
1.30 matthew 579: the form element $elementname.
580:
581: =item *
582: search($elementname) : provide a link which will open up the filesystem
583: searcher (lonsearchcat) and, once a file is selected, place the result in
1.29 matthew 584: the form element $elementname.
1.31 matthew 585:
1.34 harris41 586: =item *
1.32 albertel 587: editline(tag,data,description,size): Provide a <input type="text" ../> for
1.31 matthew 588: single-line text entry. This is to be used for text enclosed by tags, not
589: arguements/parameters associated with a tag.
1.26 harris41 590:
591: =back
592:
593: incomplete...
594:
595: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>