1: # The LearningOnline Network with CAPA
2: # edit mode helpers
3: #
4: # $Id: edit.pm,v 1.133 2010/11/13 03:22:58 www Exp $
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: #
28:
29: =pod
30:
31: =head1 NAME
32:
33: Apache::edit - edit mode helpers
34:
35: =head1 SYNOPSIS
36:
37: Invoked by many homework and xml related modules.
38:
39: &Apache::edit::SUBROUTINENAME(ARGUMENTS);
40:
41: =head1 INTRODUCTION
42:
43: This module outputs HTML syntax helpful for the rendering of edit
44: mode interfaces.
45:
46: This is part of the LearningOnline Network with CAPA project
47: described at http://www.lon-capa.org.
48:
49: =head1 SUBROUTINES
50:
51: =over 4
52:
53: =item initialize_edit()
54:
55: initialize edit (set colordepth to zero)
56:
57: =item tag_start($target,$token,$description)
58:
59: provide deletion and insertion lists
60: for the manipulation of a start tag; return a scalar string
61:
62: =item tag_end($target,$token,$description)
63:
64: ending syntax corresponding to
65: &tag_start. return a scalar string.
66:
67: =item start_table($token)
68:
69: start table; update colordepth; return scalar string.
70:
71: =item end_table()
72:
73: reduce color depth; end table; return scalar string
74:
75: =item start_spanning_row()
76:
77: start a new table row spanning the 'edit' environment.
78:
79: =item start_row()
80:
81: start a new table row and element.
82:
83: =item end_row()
84:
85: end current table element and row.
86:
87: =item movebuttons($target,$token)
88:
89: move-up and move-down buttons; return scalar string
90:
91: =item deletelist($target,$token)
92:
93: provide a yes option in an HTML select element; return scalar string
94:
95: =item handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
96: $style)
97:
98: respond to a user delete request by passing relevant stack
99: and array information to various rendering functions; return a scalar string
100:
101: =item get_insert_list($token)
102:
103: provide an insertion list based on possibilities from lonxml; return a scalar string
104:
105: =item insertlist($target,$token)
106:
107: api that uses get_insert_list; return a scalar string
108:
109: =item handleinsert($token)
110:
111: provide an insertion list based on possibilities from lonxml; return a scalar string
112:
113: =item get_insert_list($token)
114:
115: provide an insertion list based on possibilities from lonxml; return a scalar string
116:
117: =item browse($elementname)
118:
119: provide a link which will open up the filesystem browser (lonindexer) and, once a file is selected, place the result in the form element $elementname.
120:
121: =item search($elementname)
122:
123: provide a link which will open up the filesystem searcher (lonsearchcat) and, once a file is selected, place the result in the form element $elementname.
124:
125: =item editline(tag,data,description,size)
126:
127: Provide a <input type="text" ../> for single-line text entry. This is to be used for text enclosed by tags, not arguements/parameters associated with a tag.
128:
129: =back
130:
131: =cut
132:
133: package Apache::edit;
134:
135: use strict;
136: use Apache::lonnet;
137: use HTML::Entities();
138: use Apache::lonlocal;
139: use lib '/home/httpd/lib/perl/';
140: use LONCAPA;
141:
142:
143: # Global Vars
144: # default list of colors to use in editing
145: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
146: # depth of nesting of edit
147: $Apache::edit::colordepth=0;
148: @Apache::edit::inserttag=();
149: # image-type responses: active background image and curdepth at definition
150: $Apache::edit::bgimgsrc='';
151: $Apache::edit::bgimgsrccurdepth='';
152:
153: sub initialize_edit {
154: $Apache::edit::colordepth=0;
155: @Apache::edit::inserttag=();
156: }
157:
158: sub tag_start {
159: my ($target,$token,$description) = @_;
160: my $result='';
161: if ($target eq "edit") {
162: my $tag=$token->[1];
163: if (!$description) {
164: $description=&Apache::lonxml::description($token);
165: if (!$description) { $description="<$tag>"; }
166: }
167: $result.= &start_table($token)."<tr><td>$description</td>
168: <td>".&mt('Delete?').' '.
169: &deletelist($target,$token)
170: ."</td>
171: <td>".
172: &insertlist($target,$token);
173: #<td>".
174: # &movebuttons($target,$token).
175: # "</tr><tr><td colspan=\"3\">\n";
176: my @help = Apache::lonxml::helpinfo($token);
177: if ($help[0]) {
178: $result .= '</td><td align="right" valign="top">' .
179: Apache::loncommon::help_open_topic(@help);
180: } else { $result .= "</td><td> "; }
181: $result .= &end_row().&start_spanning_row();
182: }
183: return $result;
184: }
185:
186: sub tag_end {
187: my ($target,$token,$description) = @_;
188: my $result='';
189: if ($target eq 'edit') {
190: $result.="</td></tr>".&end_table()."\n";
191: }
192: return $result;
193: }
194:
195: sub start_table {
196: my ($token)=@_;
197: my $tag = &Apache::lonxml::get_tag($token);
198:
199: my $color = $Apache::lonxml::insertlist{"$tag.color"};
200: &Apache::lonxml::debug(" $tag -- $color");
201: if (!defined($color)) {
202: $color = $Apache::edit::colorlist[$Apache::edit::colordepth];
203: }
204: $Apache::edit::colordepth++;
205: push(@Apache::edit::inserttag,$token->[1]);
206: my $result='<div align="right">';
207: $result.='<table bgcolor="'.$color.'" width="97%" border="0" cellspacing="3" cellpadding="2">';
208: return $result;
209: }
210:
211: sub end_table {
212: $Apache::edit::colordepth--;
213: my $result='</table></div>';
214: $result.='<div align="left"><table><tr><td>';
215:
216: my ($tagname,$closingtag);
217: if (defined($Apache::edit::inserttag[-2])) {
218: $tagname=$Apache::edit::inserttag[-2];
219: } else {
220: if ($Apache::lonhomework::parsing_a_task) {
221: $tagname='Task';
222: } else {
223: $tagname='problem';
224: }
225: }
226: if (defined($Apache::edit::inserttag[-1])) {
227: $closingtag=$Apache::edit::inserttag[-1];
228: }
229: $result.=&innerinsertlist('edit',$tagname,$closingtag).
230: "</td></tr></table></div>";
231: my $last = pop(@Apache::edit::inserttag);
232: return $result;
233: }
234:
235: sub start_spanning_row { return '<tr><td colspan="5" bgcolor="#F0F0F0">';}
236: sub start_row { return '<tr><td bgcolor="#DDDDDD">'; }
237: sub end_row { return '</td></tr>'; }
238:
239: sub movebuttons {
240: my ($target,$token) = @_;
241: my $result='<input type="submit" name="moveup.'.
242: $Apache::lonxml::curdepth.'" value="Move Up" />';
243: $result.='<input type="submit" name="movedown.'.
244: $Apache::lonxml::curdepth.'" value="Move Down" />';
245: return $result;
246: }
247:
248: sub deletelist {
249: my ($target,$token) = @_;
250: my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
251: <option></option>
252: <option>".&mt('yes')."</option>
253: </select>";
254: return $result;
255: }
256:
257: sub handle_delete {
258: if (!$env{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
259: my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
260: my $result=0;
261: if ($space) {
262: my $sub1="$space\:\:delete_$token->[1]";
263: {
264: no strict 'refs';
265: if (defined &$sub1) {
266: $result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
267: }
268: }
269: }
270: if (!$result) {
271: my $endtag='/'.$token->[1];
272: my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser,$style);
273: $$parser['-1']->get_token();
274: &Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
275: &Apache::lonxml::end_tag($tagstack,$parstack,$token);
276: }
277: return 1;
278: }
279:
280: sub get_insert_list {
281: my ($tagname) = @_;
282: my $result='';
283: my @tags= ();
284: #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
285: if ($Apache::lonxml::insertlist{"$tagname.which"}) {
286: push (@tags, @{ $Apache::lonxml::insertlist{"$tagname.which"} });
287: }
288: foreach my $namespace (@Apache::lonxml::namespace) {
289: if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) {
290: push (@tags, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} });
291: }
292: }
293: if (@tags) {
294: my %options;
295: foreach my $tag (@tags) {
296: my $descrip=$Apache::lonxml::insertlist{"$tag.description"};
297: my $tagnum =$Apache::lonxml::insertlist{"$tag.num"};
298: $options{$descrip} ="<option value=\"$tagnum\">".
299: $descrip."</option>\n";
300: }
301: foreach my $option (sort(keys(%options))) {$result.=$options{$option};}
302: if ($result) { $result='<option selected="selected"></option>'.$result; }
303: }
304: return $result;
305: }
306:
307: sub insertlist {
308: my ($target,$token) = @_;
309: return &innerinsertlist($target,$token->[1]);
310: }
311:
312: sub innerinsertlist {
313: my ($target,$tagname,$closingtag) = @_;
314: my $result;
315: my $after='';
316: if ($closingtag) {
317: $after='_after_'.$closingtag;
318: }
319: if ($target eq 'edit') {
320: my $optionlist= &get_insert_list($tagname);
321: if ($optionlist) {
322: $result = &mt('Insert:')."
323: <select name=\"insert$after\_$Apache::lonxml::curdepth\">
324: $optionlist
325: </select>"
326: } else {
327: $result=" ";
328: }
329: }
330: return $result;
331: }
332:
333: sub handle_insert {
334: if ($env{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
335: my $tagnum = $env{"form.insert_$Apache::lonxml::curdepth"};
336: return &do_insert($tagnum);
337: }
338:
339: sub handle_insertafter {
340: my $tagname=shift;
341: if ($env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '') {
342: return '';
343: }
344: my $tagnum =$env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"};
345: return &do_insert($tagnum,1);
346: }
347:
348: sub do_insert {
349: my ($tagnum,$after) = @_;
350: my $result;
351:
352: my $newtag = $Apache::lonxml::insertlist{"$tagnum.tag"};
353: my $func = $Apache::lonxml::insertlist{"$newtag.function"};
354: if ($func eq 'default') {
355: my $namespace;
356: if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
357: my $depth = scalar(@Apache::lonxml::depthcounter);
358: $depth -- if ($after);
359: my $inset = "\t"x$depth;
360: $result.="\n$inset<$newtag></$newtag>";
361: } else {
362: if (defined(&$func)) {
363: {
364: no strict 'refs';
365: $result.=&$func();
366: }
367: } else {
368: &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined. ($tagnum)");
369: }
370: }
371: return $result;
372: }
373:
374: sub insert_img {
375: return '
376: <img />';
377: }
378:
379: sub insert_responseparam {
380: return '
381: <responseparam />';
382: }
383:
384: sub insert_parameter {
385: return '
386: <parameter />';
387: }
388:
389: sub insert_formularesponse {
390: return '
391: <formularesponse answer="" samples="">
392: <responseparam description="Numerical Tolerance" type="tolerance" default="0.00001" name="tol" />
393: <textline size="25"/>
394: <hintgroup>
395: <startouttext /><endouttext />
396: </hintgroup>
397: </formularesponse>';
398: }
399:
400: sub insert_functionplotresponse {
401: return '
402: <functionplotresponse>
403: <functionplotelements>
404: <spline />
405: </functionplotelements>
406: <functionplotruleset>
407: <functionplotrule />
408: </functionplotruleset>
409: </functionplotresponse>';
410: }
411:
412: sub insert_spline {
413: return '
414: <spline />';
415: }
416:
417: sub insert_backgroundplot {
418: return '
419: <backgroundplot />';
420: }
421:
422: sub insert_functionplotrule {
423: return '
424: <functionplotrule />';
425: }
426:
427: sub insert_functionplotruleset {
428: return '
429: <functionplotruleset>
430: <functionplotrule />
431: </functionplotruleset>';
432: }
433:
434: sub insert_functionplotelements {
435: return '
436: <functionplotelements>
437: <spline />
438: </functionplotelements>';
439: }
440:
441: sub insert_numericalresponse {
442: return '
443: <numericalresponse answer="">
444: <responseparam type="tolerance" default="5%" name="tol" description="Numerical Tolerance" />
445: <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />
446: <textline />
447: <hintgroup>
448: <startouttext /><endouttext />
449: </hintgroup>
450: </numericalresponse>';
451: }
452:
453: sub insert_customresponse {
454: return '
455: <customresponse>
456: <answer type="loncapa/perl">
457: </answer>
458: <textline />
459: <hintgroup>
460: <startouttext /><endouttext />
461: </hintgroup>
462: </customresponse>';
463: }
464:
465: sub insert_customresponse_answer {
466: return '
467: <answer type="loncapa/perl">
468: </answer>
469: ';
470: }
471:
472: sub insert_customhint {
473: return '
474: <customhint>
475: <answer type="loncapa/perl">
476: </answer>
477: </customhint>';
478: }
479:
480: sub insert_customhint_answer {
481: return '
482: <answer type="loncapa/perl">
483: </answer>
484: ';
485: }
486:
487: sub insert_mathresponse {
488: return '
489: <mathresponse>
490: <answer>
491: </answer>
492: <textline />
493: <hintgroup>
494: <startouttext />
495: <endouttext />
496: </hintgroup>
497: </mathresponse>';
498: }
499:
500: sub insert_mathresponse_answer {
501: return '
502: <answer>
503: </answer>
504: ';
505: }
506:
507: sub insert_mathhint {
508: return '
509: <mathhint>
510: <answer>
511: </answer>
512: </mathhint>';
513: }
514:
515: sub insert_mathhint_answer {
516: return '
517: <answer>
518: </answer>
519: ';
520: }
521:
522: sub insert_stringresponse {
523: return '
524: <stringresponse answer="" type="">
525: <textline />
526: <hintgroup>
527: <startouttext /><endouttext />
528: </hintgroup>
529: </stringresponse>';
530: }
531:
532: sub insert_essayresponse {
533: return '
534: <essayresponse>
535: <textfield></textfield>
536: </essayresponse>';
537: }
538:
539: sub insert_imageresponse {
540: return '
541: <imageresponse max="1">
542: <foilgroup>
543: <foil>
544: </foil>
545: </foilgroup>
546: <hintgroup>
547: <startouttext /><endouttext />
548: </hintgroup>
549: </imageresponse>';
550: }
551:
552: sub insert_optionresponse {
553: return '
554: <optionresponse max="10">
555: <foilgroup options="">
556: <foil>
557: <startouttext /><endouttext />
558: </foil>
559: </foilgroup>
560: <hintgroup>
561: <startouttext /><endouttext />
562: </hintgroup>
563: </optionresponse>';
564: }
565:
566: sub insert_organicresponse {
567: return '
568: <organicresponse>
569: <textline />
570: <hintgroup>
571: <startouttext /><endouttext />
572: </hintgroup>
573: </organicresponse>';
574: }
575:
576: sub insert_organicstructure {
577: return '
578: <organicstructure />
579: ';
580: }
581:
582: sub insert_radiobuttonresponse {
583: return '
584: <radiobuttonresponse max="10">
585: <foilgroup>
586: <foil>
587: <startouttext /><endouttext />
588: </foil>
589: </foilgroup>
590: <hintgroup>
591: <startouttext /><endouttext />
592: </hintgroup>
593: </radiobuttonresponse>';
594: }
595:
596: sub insert_reactionresponse {
597: return '
598: <reactionresponse>
599: <textline />
600: <hintgroup>
601: <startouttext /><endouttext />
602: </hintgroup>
603: </reactionresponse>';
604: }
605:
606: sub insert_rankresponse {
607: return '
608: <rankresponse max="10">
609: <foilgroup options="">
610: <foil>
611: <startouttext /><endouttext />
612: </foil>
613: </foilgroup>
614: <hintgroup>
615: <startouttext /><endouttext />
616: </hintgroup>
617: </rankresponse>';
618: }
619:
620: sub insert_matchresponse {
621: return '
622: <matchresponse max="10">
623: <foilgroup options="">
624: <itemgroup>
625: </itemgroup>
626: <foil>
627: <startouttext /><endouttext />
628: </foil>
629: </foilgroup>
630: <hintgroup>
631: <startouttext /><endouttext />
632: </hintgroup>
633: </matchresponse>';
634: }
635:
636: sub insert_startpartmarker { return '<startpartmarker />'; }
637: sub insert_endpartmarker { return '<endpartmarker />'; }
638:
639: sub insert_displayduedate { return '<displayduedate />'; }
640: sub insert_displaytitle { return '<displaytitle />'; }
641: sub insert_hintpart {
642: return '
643: <hintpart on="default">
644: <startouttext/><endouttext />
645: </hintpart>';
646: }
647:
648: sub insert_hintgroup {
649: return '
650: <hintgroup>
651: <startouttext /><endouttext />
652: </hintgroup>';
653: }
654:
655: sub insert_numericalhint {
656: return '
657: <numericalhint>
658: </numericalhint>';
659: }
660:
661: sub insert_reactionhint {
662: return '
663: <reactionhint>
664: </reactionhint>';
665: }
666:
667: sub insert_organichint {
668: return '
669: <organichint>
670: </organichint>';
671: }
672:
673: sub insert_stringhint {
674: return '
675: <stringhint>
676: </stringhint>';
677: }
678:
679: sub insert_formulahint {
680: return '
681: <formulahint>
682: </formulahint>';
683: }
684:
685: sub insert_radiobuttonhint {
686: return '
687: <radiobuttonhint>
688: </radiobuttonhint>';
689: }
690:
691: sub insert_optionhint {
692: return '
693: <optionhint>
694: </optionhint>';
695: }
696:
697: sub insert_startouttext {
698: return "<startouttext /><endouttext />";
699: }
700:
701: sub insert_script {
702: return "\n<script type=\"loncapa/perl\"></script>";
703: }
704:
705: sub js_change_detection {
706: my $unsaved=&mt("There are unsaved changes");
707: return (<<SCRIPT);
708: <script type="text/javascript">
709: var clean = true;
710: var is_submit = false;
711: var still_ask = false;
712: function compareForm(event_) {
713: if (!event_ && window.event) {
714: event_ = window.event;
715: }
716: if ((!is_submit || (is_submit && still_ask)) && !clean) {
717: still_ask = false;
718: is_submit = false;
719: event_.returnValue = "$unsaved";
720: return "$unsaved";
721: }
722: }
723: function unClean() {
724: clean=false;
725: }
726: window.onbeforeunload = compareForm;
727: </script>
728: SCRIPT
729: }
730:
731: sub form_change_detection {
732: return ' onsubmit="is_submit=true;" ';
733: }
734:
735: sub element_change_detection {
736: return ' onchange="unClean();" ';
737: }
738:
739: sub submit_ask_anyway {
740: my ($extra_action) = @_;
741: return ' onclick="still_ask=true;'.$extra_action.'" ';
742: }
743:
744: sub textarea_sizes {
745: my ($data)=@_;
746: my $count=0;
747: my $maxlength=-1;
748: foreach (split ("\n", $$data)) {
749: $count+=int(length($_)/79);
750: $count++;
751: if (length($_) > $maxlength) { $maxlength = length($_); }
752: }
753: my $rows = $count;
754: my $cols = $maxlength;
755: return ($rows,$cols);
756: }
757:
758: sub editline {
759: my ($tag,$data,$description,$size)=@_;
760: $data=&HTML::Entities::encode($data,'<>&"');
761: if ($description) { $description=$description."<br />"; }
762: my $change_code = &element_change_detection();
763: my $result = <<"END";
764: $description
765: <input type="text" name="homework_edit_$Apache::lonxml::curdepth"
766: value="$data" size="$size" $change_code />
767: END
768: return $result;
769: }
770:
771: sub editfield {
772: my ($tag,$data,$description,$minwidth,$minheight,$usehtmlarea)=@_;
773:
774: my ($rows,$cols)=&textarea_sizes(\$data);
775: my $textareaclass;
776:
777: if (&Apache::lonhtmlcommon::htmlareabrowser() && $usehtmlarea) {
778: $rows+=7; # make room for HTMLarea
779: $minheight+=7; # make room for HTMLarea
780: $textareaclass = ' class="LC_richDefaultOff"';
781: }
782: if ($cols > 80) { $cols = 80; }
783: if ($cols < $minwidth ) { $cols = $minwidth; }
784: if ($rows < $minheight) { $rows = $minheight; }
785: if ($description) { $description=$description."<br />"; }
786:
787: # remove typesetting whitespace from between data and the end tag
788: # to make the edit look prettier
789: $data =~ s/\n?[ \t]*$//;
790:
791: return $description."\n".'<textarea style="width:100%" rows="'.$rows.
792: '" cols="'.$cols.'" name="homework_edit_'.
793: $Apache::lonxml::curdepth.'" id="homework_edit_'.
794: $Apache::lonxml::curdepth.'" '.&element_change_detection().
795: $textareaclass.'>'.
796: &HTML::Entities::encode($data,'<>&"').'</textarea>'.
797: ($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
798: 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
799: }
800:
801: sub modifiedfield {
802: my ($endtag,$parser) = @_;
803: my $result;
804: $result=$env{"form.homework_edit_$Apache::lonxml::curdepth"};
805: my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
806: # textareas throw away intial \n
807: if ($bodytext=~/^\n/) {
808: $result="\n".$result;
809: }
810: # if there is typesetting whitespace from between the data and the end tag
811: # restore to keep the source looking pretty
812: if ($bodytext =~ /(\n?[ \t]*)$/) {
813: $result .= $1;
814: }
815: return $result;
816: }
817:
818: # Returns a 1 if the token has been modified and you should rebuild the tag
819: # side-effects, will modify the $token if new values are found
820: sub get_new_args {
821: my ($token,$parstack,$safeeval,@args)=@_;
822: my $rebuild=0;
823: foreach my $arg (@args) {
824: #just want the string that it was set to
825: my $value=$token->[2]->{$arg};
826: my $element=&html_element_name($arg);
827: my $newvalue=$env{"form.$element"};
828: &Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
829: if (defined($newvalue) && $value ne $newvalue) {
830: if (ref($newvalue) eq 'ARRAY') {
831: $token->[2]->{$arg}=join(',',@$newvalue);
832: } else {
833: $token->[2]->{$arg}=$newvalue;
834: }
835: $rebuild=1;
836: # add new attributes to the of the attribute seq
837: if (!grep { $arg eq $_ } (@{ $token->[3] })) {
838: push(@{ $token->[3] },$arg);
839: }
840: } elsif (!defined($newvalue) && defined($value)) {
841: delete($token->[2]->{$arg});
842: $rebuild=1;
843: }
844: }
845: return $rebuild;
846: }
847:
848: # looks for /> on start tags
849: sub rebuild_tag {
850: my ($token) = @_;
851: my $result;
852: if ($token->[0] eq 'S') {
853: $result = '<'.$token->[1];
854: foreach my $attribute (@{ $token->[3] }) {
855: my $value = $token->[2]{$attribute};
856: next if ($value eq '');
857: $value =~s/^\s+|\s+$//g;
858: $value =~s/\"//g;
859: &Apache::lonxml::debug("setting :$attribute: to :$value:");
860: $result.=' '.$attribute.'="'.$value.'"';
861: }
862: if ($token->[4] =~ m:/>$:) {
863: $result.=' />';
864: } else {
865: $result.='>';
866: }
867: } elsif ( $token->[0] eq 'E' ) {
868: $result = '</'.$token->[1].'>';
869: }
870: return $result;
871: }
872:
873: sub html_element_name {
874: my ($name) = @_;
875: return $name.'_'.$Apache::lonxml::curdepth;
876: }
877:
878: sub hidden_arg {
879: my ($name,$token) = @_;
880: my $result;
881: my $arg=$token->[2]{$name};
882: $result='<input name="'.&html_element_name($name).
883: '" type="hidden" value="'.$arg.'" />';
884: return $result;
885: }
886:
887: sub checked_arg {
888: my ($description,$name,$list,$token) = @_;
889: my $result;
890: my $optionlist="";
891: my $allselected=$token->[2]{$name};
892: $result=&mt($description);
893: foreach my $option (@$list) {
894: my ($value,$text);
895: if ( ref($option) eq 'ARRAY') {
896: $value='value="'.$$option[0].'"';
897: $text=$$option[1];
898: $option=$$option[0];
899: } else {
900: $text=$option;
901: $value='value="'.$option.'"';
902: }
903: $result.='<span class="LC_nobreak"><label><input type="checkbox" '.$value.' name="'.
904: &html_element_name($name).'"';
905: foreach my $selected (split(/,/,$allselected)) {
906: if ( $selected eq $option ) {
907: $result.=' checked="checked" ';
908: last;
909: }
910: }
911: $result.=&element_change_detection().' />'.$text.'</label></span>'."\n";
912: }
913: return $result;
914: }
915:
916: sub text_arg {
917: my ($description,$name,$token,$size) = @_;
918: my $result;
919: if (!defined $size) { $size=20; }
920: my $arg=$token->[2]{$name};
921: $result=&mt($description).' <input name="'.&html_element_name($name).
922: '" type="text" value="'.$arg.'" size="'.$size.'" '.
923: &element_change_detection().'/>';
924: return '<span class="LC_nobreak">'.$result.'</span>';
925: }
926:
927: sub select_arg {
928: my ($description,$name,$list,$token) = @_;
929: my $result;
930: my $optionlist="";
931: my $selected=$token->[2]{$name};
932: if (ref($list) eq 'ARRAY') {
933: foreach my $option (@{$list}) {
934: my ($text,$value);
935: if (ref($option) eq 'ARRAY') {
936: $value='value="'.&HTML::Entities::encode($option->[0]).'"';
937: $text=$option->[1];
938: $option=$option->[0];
939: } else {
940: $text=$option;
941: $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
942: }
943: if ( $selected eq $option ) {
944: $optionlist.="<option $value selected=\"selected\">".&mt($text)."</option>\n";
945: } else {
946: $optionlist.="<option $value >".&mt($text)."</option>\n";
947: }
948: }
949: }
950: $result.='<span class="LC_nobreak">'.&mt($description).' <select name="'.
951: &html_element_name($name).'" '.&element_change_detection().' >
952: '.$optionlist.'
953: </select></span>';
954: return $result;
955: }
956:
957: sub select_or_text_arg {
958: my ($description,$name,$list,$token,$size) = @_;
959: my $result;
960: my $optionlist="";
961: my $found=0;
962: my $selected=$token->[2]{$name};
963: if (ref($list) eq 'ARRAY') {
964: foreach my $option (@{$list}) {
965: my ($text,$value);
966: if (ref($option) eq 'ARRAY') {
967: $value='value="'.&HTML::Entities::encode($option->[0]).'"';
968: $text=$option->[1];
969: $option=$option->[0];
970: } else {
971: $text=$option;
972: $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
973: }
974: if ( $selected eq $option ) {
975: $optionlist.="<option $value selected=\"selected\">$text</option>\n";
976: $found=1;
977: } else {
978: $optionlist.="<option $value>$text</option>\n";
979: }
980: }
981: }
982: $optionlist.="<option value=\"TYPEDINVALUE\"".
983: ((!$found)?' selected="selected"':'').
984: ">".&mt('Type-in value')."</option>\n";
985: #
986: my $change_code=&element_change_detection();
987: my $element=&html_element_name($name);
988: my $selectelement='select_list_'.$element;
989: my $typeinelement='type_in_'.$element;
990: my $typeinvalue=($found?'':$selected);
991: #
992: my $hiddenvalue='this.form.'.$element.'.value';
993: my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
994: my $selectedvalue='this.form.'.$selectelement.
995: '.options['.$selectedindex.'].value';
996: my $typedinvalue='this.form.'.$typeinelement.'.value';
997: my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
998: $description=&mt($description);
999: #
1000: return (<<ENDSELECTORTYPE);
1001: <span class="LC_nobreak">
1002: $description
1003: <select name="$selectelement"
1004: onChange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }" >
1005: $optionlist
1006: </select>
1007: <input type="text" size="$size" name="$typeinelement"
1008: value="$typeinvalue"
1009: onChange="$hiddenvalue=$typedinvalue;"
1010: onFocus="$selectedindex=$selecttypeinindex-1;" />
1011: <input type="hidden" name="$element" value="$selected" $change_code />
1012: </span>
1013: ENDSELECTORTYPE
1014: }
1015:
1016: #----------------------------------------------------- image coordinates
1017: # single image coordinates, x, y
1018: sub entercoords {
1019: my ($idx,$idy,$mode,$width,$height) = @_;
1020: unless ($Apache::edit::bgimgsrc) { return ''; }
1021: if ($idx) { $idx.='_'; }
1022: if ($idy) { $idy.='_'; }
1023: my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1024: my $form = 'lonhomework';
1025: my $element;
1026: if (! defined($mode) || $mode eq 'attribute') {
1027: $element = &escape("$Apache::lonxml::curdepth");
1028: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1029: $element = &escape('homework_edit_'.
1030: $Apache::lonxml::curdepth);
1031: }
1032: my $id=$Apache::lonxml::curdepth;
1033: my %data=("imagechoice.$id.type" =>'point',
1034: "imagechoice.$id.formname" =>$form,
1035: "imagechoice.$id.formx" =>"$idx$element",
1036: "imagechoice.$id.formy" =>"$idy$element",
1037: "imagechoice.$id.file" =>$bgfile,
1038: "imagechoice.$id.formcoord" =>$element);
1039: if ($height) {
1040: $data{"imagechoice.$id.formheight"}=$height.'_'.
1041: $Apache::edit::bgimgsrccurdepth;
1042: }
1043: if ($width) {
1044: $data{"imagechoice.$id.formwidth"}=$width.'_'.
1045: $Apache::edit::bgimgsrccurdepth;
1046: }
1047: &Apache::lonnet::appenv(\%data);
1048: my $text="Click Coordinates";
1049: my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1050: return $result;
1051: }
1052:
1053: # coordinates (x1,y1)-(x2,y2)...
1054: # mode can be either box, or polygon
1055: sub entercoord {
1056: my ($idx,$mode,$width,$height,$type) = @_;
1057: unless ($Apache::edit::bgimgsrc) { return ''; }
1058: my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1059: my $form = 'lonhomework';
1060: my $element;
1061: if (! defined($mode) || $mode eq 'attribute') {
1062: $element = &escape("$idx\_$Apache::lonxml::curdepth");
1063: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1064: $element = &escape('homework_edit_'.
1065: $Apache::lonxml::curdepth);
1066: }
1067: my $id=$Apache::lonxml::curdepth;
1068: my %data=("imagechoice.$id.type" =>$type,
1069: "imagechoice.$id.formname" =>$form,
1070: "imagechoice.$id.file" =>$bgfile,
1071: "imagechoice.$id.formcoord" =>$element);
1072: if ($height) {
1073: $data{"imagechoice.$id.formheight"}=$height.'_'.
1074: $Apache::edit::bgimgsrccurdepth;
1075: }
1076: if ($width) {
1077: $data{"imagechoice.$id.formwidth"}=$width.'_'.
1078: $Apache::edit::bgimgsrccurdepth;
1079: }
1080: &Apache::lonnet::appenv(\%data);
1081: my $text="Enter Coordinates";
1082: if ($type eq 'polygon') { $text='Create Polygon Data'; }
1083: my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1084: return $result;
1085: }
1086:
1087: sub deletecoorddata {
1088: &Apache::lonnet::delenv('imagechoice.');
1089: }
1090:
1091: #----------------------------------------------------- browse
1092: sub browse {
1093: # insert a link to call up the filesystem browser (lonindexer)
1094: my ($id, $mode, $titleid, $only) = @_;
1095: my $form = 'lonhomework';
1096: my $element;
1097: if (! defined($mode) || $mode eq 'attribute') {
1098: $element = &escape("$id\_$Apache::lonxml::curdepth");
1099: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1100: $element = &escape('homework_edit_'.
1101: $Apache::lonxml::curdepth);
1102: }
1103: my $titleelement;
1104: if ($titleid) {
1105: $titleelement=",'$only','','".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1106: } else {
1107: $titleelement=",'$only'";
1108: }
1109: my $result = <<"ENDBUTTON";
1110: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
1111: ENDBUTTON
1112: return $result;
1113: }
1114:
1115: #----------------------------------------------------- browse
1116: sub search {
1117: # insert a link to call up the filesystem browser (lonindexer)
1118: my ($id, $mode, $titleid) = @_;
1119: my $form = 'lonhomework';
1120: my $element;
1121: if (! defined($mode) || $mode eq 'attribute') {
1122: $element = &escape("$id\_$Apache::lonxml::curdepth");
1123: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1124: $element = &escape('homework_edit_'.
1125: $Apache::lonxml::curdepth);
1126: }
1127: my $titleelement;
1128: if ($titleid) {
1129: $titleelement=",'".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1130: }
1131: my $result = <<"ENDBUTTON";
1132: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>
1133: ENDBUTTON
1134: return $result;
1135: }
1136:
1137:
1138: 1;
1139: __END__
1140:
1141:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>