1: # The LearningOnline Network with CAPA
2: # edit mode helpers
3: #
4: # $Id: edit.pm,v 1.139 2011/11/24 13:28:01 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: </functionplotelements>
405: <functionplotruleset>
406: </functionplotruleset>
407: </functionplotresponse>';
408: }
409:
410: sub insert_spline {
411: return '
412: <spline />';
413: }
414:
415: sub insert_backgroundplot {
416: return '
417: <backgroundplot />';
418: }
419:
420: sub insert_plotobject {
421: return '
422: <plotobject />';
423: }
424:
425: sub insert_plotvector {
426: return '
427: <plotvector />';
428: }
429:
430: sub insert_functionplotrule {
431: return '
432: <functionplotrule />';
433: }
434:
435: sub insert_functionplotvectorrule {
436: return '
437: <functionplotvectorrule />';
438: }
439:
440: sub insert_functionplotvectorsumrule {
441: return '
442: <functionplotvectorsumrule />';
443: }
444:
445: sub insert_functionplotcustomrule {
446: return '
447: <functionplotcustomrule>
448: <answer type="loncapa/perl">
449: # &fpr_val("label"), &fpr_f($x), &fpr_dfdx($x), &fpr_d2fdx2($x)
450: # ($xs,$xe,$ys,$ye)=&fpr_vectorcoords("Name"), ($x,$y)=&fpr_objectcoords("Name")
451: # &fpr_vectorlength("Name"), &fpr_vectorangle("Name")
452:
453: # Return 0 or 1
454: return 1;
455: </answer>
456: </functionplotcustomrule>';
457: }
458:
459: sub insert_functionplotruleset {
460: return '
461: <functionplotruleset>
462: <functionplotrule />
463: </functionplotruleset>';
464: }
465:
466: sub insert_functionplotelements {
467: return '
468: <functionplotelements>
469: <spline />
470: </functionplotelements>';
471: }
472:
473: sub insert_numericalresponse {
474: return '
475: <numericalresponse answer="">
476: <responseparam type="tolerance" default="5%" name="tol" description="Numerical Tolerance" />
477: <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />
478: <textline />
479: <hintgroup>
480: <startouttext /><endouttext />
481: </hintgroup>
482: </numericalresponse>';
483: }
484:
485: sub insert_customresponse {
486: return '
487: <customresponse>
488: <answer type="loncapa/perl">
489: </answer>
490: <textline />
491: <hintgroup>
492: <startouttext /><endouttext />
493: </hintgroup>
494: </customresponse>';
495: }
496:
497: sub insert_customresponse_answer {
498: return '
499: <answer type="loncapa/perl">
500: </answer>
501: ';
502: }
503:
504: sub insert_customhint {
505: return '
506: <customhint>
507: <answer type="loncapa/perl">
508: </answer>
509: </customhint>';
510: }
511:
512: sub insert_customhint_answer {
513: return '
514: <answer type="loncapa/perl">
515: </answer>
516: ';
517: }
518:
519: sub insert_mathresponse {
520: return '
521: <mathresponse>
522: <answer>
523: </answer>
524: <textline />
525: <hintgroup>
526: <startouttext />
527: <endouttext />
528: </hintgroup>
529: </mathresponse>';
530: }
531:
532: sub insert_mathresponse_answer {
533: return '
534: <answer>
535: </answer>
536: ';
537: }
538:
539: sub insert_mathhint {
540: return '
541: <mathhint>
542: <answer>
543: </answer>
544: </mathhint>';
545: }
546:
547: sub insert_mathhint_answer {
548: return '
549: <answer>
550: </answer>
551: ';
552: }
553:
554: sub insert_stringresponse {
555: return '
556: <stringresponse answer="" type="">
557: <textline />
558: <hintgroup>
559: <startouttext /><endouttext />
560: </hintgroup>
561: </stringresponse>';
562: }
563:
564: sub insert_essayresponse {
565: return '
566: <essayresponse>
567: <textfield></textfield>
568: </essayresponse>';
569: }
570:
571: sub insert_imageresponse {
572: return '
573: <imageresponse max="1">
574: <foilgroup>
575: <foil>
576: </foil>
577: </foilgroup>
578: <hintgroup>
579: <startouttext /><endouttext />
580: </hintgroup>
581: </imageresponse>';
582: }
583:
584: sub insert_optionresponse {
585: return '
586: <optionresponse max="10">
587: <foilgroup options="">
588: <foil>
589: <startouttext /><endouttext />
590: </foil>
591: </foilgroup>
592: <hintgroup>
593: <startouttext /><endouttext />
594: </hintgroup>
595: </optionresponse>';
596: }
597:
598: sub insert_organicresponse {
599: return '
600: <organicresponse>
601: <textline />
602: <hintgroup>
603: <startouttext /><endouttext />
604: </hintgroup>
605: </organicresponse>';
606: }
607:
608: sub insert_organicstructure {
609: return '
610: <organicstructure />
611: ';
612: }
613:
614: sub insert_radiobuttonresponse {
615: return '
616: <radiobuttonresponse max="10">
617: <foilgroup>
618: <foil>
619: <startouttext /><endouttext />
620: </foil>
621: </foilgroup>
622: <hintgroup>
623: <startouttext /><endouttext />
624: </hintgroup>
625: </radiobuttonresponse>';
626: }
627:
628: sub insert_reactionresponse {
629: return '
630: <reactionresponse>
631: <textline />
632: <hintgroup>
633: <startouttext /><endouttext />
634: </hintgroup>
635: </reactionresponse>';
636: }
637:
638: sub insert_rankresponse {
639: return '
640: <rankresponse max="10">
641: <foilgroup options="">
642: <foil>
643: <startouttext /><endouttext />
644: </foil>
645: </foilgroup>
646: <hintgroup>
647: <startouttext /><endouttext />
648: </hintgroup>
649: </rankresponse>';
650: }
651:
652: sub insert_matchresponse {
653: return '
654: <matchresponse max="10">
655: <foilgroup options="">
656: <itemgroup>
657: </itemgroup>
658: <foil>
659: <startouttext /><endouttext />
660: </foil>
661: </foilgroup>
662: <hintgroup>
663: <startouttext /><endouttext />
664: </hintgroup>
665: </matchresponse>';
666: }
667:
668: sub insert_startpartmarker { return '<startpartmarker />'; }
669: sub insert_endpartmarker { return '<endpartmarker />'; }
670:
671: sub insert_displayduedate { return '<displayduedate />'; }
672: sub insert_displaytitle { return '<displaytitle />'; }
673: sub insert_hintpart {
674: return '
675: <hintpart on="default">
676: <startouttext/><endouttext />
677: </hintpart>';
678: }
679:
680: sub insert_hintgroup {
681: return '
682: <hintgroup>
683: <startouttext /><endouttext />
684: </hintgroup>';
685: }
686:
687: sub insert_numericalhint {
688: return '
689: <numericalhint>
690: </numericalhint>';
691: }
692:
693: sub insert_reactionhint {
694: return '
695: <reactionhint>
696: </reactionhint>';
697: }
698:
699: sub insert_organichint {
700: return '
701: <organichint>
702: </organichint>';
703: }
704:
705: sub insert_stringhint {
706: return '
707: <stringhint>
708: </stringhint>';
709: }
710:
711: sub insert_formulahint {
712: return '
713: <formulahint>
714: </formulahint>';
715: }
716:
717: sub insert_radiobuttonhint {
718: return '
719: <radiobuttonhint>
720: </radiobuttonhint>';
721: }
722:
723: sub insert_optionhint {
724: return '
725: <optionhint>
726: </optionhint>';
727: }
728:
729: sub insert_startouttext {
730: return "<startouttext /><endouttext />";
731: }
732:
733: sub insert_script {
734: return "\n<script type=\"loncapa/perl\"></script>";
735: }
736:
737: sub js_change_detection {
738: my $unsaved=&mt("There are unsaved changes");
739: return (<<SCRIPT);
740: <script type="text/javascript">
741: var clean = true;
742: var is_submit = false;
743: var still_ask = false;
744: function compareForm(event_) {
745: if (!event_ && window.event) {
746: event_ = window.event;
747: }
748: if ((!is_submit || (is_submit && still_ask)) && !clean) {
749: still_ask = false;
750: is_submit = false;
751: event_.returnValue = "$unsaved";
752: return "$unsaved";
753: }
754: }
755: function unClean() {
756: clean=false;
757: }
758: window.onbeforeunload = compareForm;
759: </script>
760: SCRIPT
761: }
762:
763: sub form_change_detection {
764: return ' onsubmit="is_submit=true;" ';
765: }
766:
767: sub element_change_detection {
768: return ' onchange="unClean();" ';
769: }
770:
771: sub submit_ask_anyway {
772: my ($extra_action) = @_;
773: return ' onclick="still_ask=true;'.$extra_action.'" ';
774: }
775:
776: sub submit_dont_ask {
777: my ($extra_action) = @_;
778: return ' onclick="is_submit=true;'.$extra_action.'" ';
779: }
780:
781:
782: sub textarea_sizes {
783: my ($data)=@_;
784: my $count=0;
785: my $maxlength=-1;
786: foreach (split ("\n", $$data)) {
787: $count+=int(length($_)/79);
788: $count++;
789: if (length($_) > $maxlength) { $maxlength = length($_); }
790: }
791: my $rows = $count;
792: my $cols = $maxlength;
793: return ($rows,$cols);
794: }
795:
796: sub editline {
797: my ($tag,$data,$description,$size)=@_;
798: $data=&HTML::Entities::encode($data,'<>&"');
799: if ($description) { $description=$description."<br />"; }
800: my $change_code = &element_change_detection();
801: my $result = <<"END";
802: $description
803: <input type="text" name="homework_edit_$Apache::lonxml::curdepth"
804: value="$data" size="$size" $change_code />
805: END
806: return $result;
807: }
808:
809: sub editfield {
810: my ($tag,$data,$description,$minwidth,$minheight,$usehtmlarea)=@_;
811:
812: my ($rows,$cols)=&textarea_sizes(\$data);
813: my $textareaclass;
814:
815: if (&Apache::lonhtmlcommon::htmlareabrowser() && $usehtmlarea) {
816: $rows+=7; # make room for HTMLarea
817: $minheight+=7; # make room for HTMLarea
818: $textareaclass = ' class="LC_richDefaultOff"';
819: }
820: if ($cols > 80) { $cols = 80; }
821: if ($cols < $minwidth ) { $cols = $minwidth; }
822: if ($rows < $minheight) { $rows = $minheight; }
823: if ($description) { $description=$description."<br />"; }
824:
825: # remove typesetting whitespace from between data and the end tag
826: # to make the edit look prettier
827: $data =~ s/\n?[ \t]*$//;
828:
829: return $description."\n".'<textarea style="width:99%" rows="'.$rows.
830: '" cols="'.$cols.'" name="homework_edit_'.
831: $Apache::lonxml::curdepth.'" id="homework_edit_'.
832: $Apache::lonxml::curdepth.'" '.&element_change_detection().
833: $textareaclass.'>'.
834: &HTML::Entities::encode($data,'<>&"').'</textarea>'.
835: ($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
836: 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
837: }
838:
839: sub modifiedfield {
840: my ($endtag,$parser) = @_;
841: my $result;
842: $result=$env{"form.homework_edit_$Apache::lonxml::curdepth"};
843: my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
844: # textareas throw away intial \n
845: if ($bodytext=~/^\n/) {
846: $result="\n".$result;
847: }
848: # if there is typesetting whitespace from between the data and the end tag
849: # restore to keep the source looking pretty
850: if ($bodytext =~ /(\n?[ \t]*)$/) {
851: $result .= $1;
852: }
853: return $result;
854: }
855:
856: # Returns a 1 if the token has been modified and you should rebuild the tag
857: # side-effects, will modify the $token if new values are found
858: sub get_new_args {
859: my ($token,$parstack,$safeeval,@args)=@_;
860: my $rebuild=0;
861: foreach my $arg (@args) {
862: #just want the string that it was set to
863: my $value=$token->[2]->{$arg};
864: my $element=&html_element_name($arg);
865: my $newvalue=$env{"form.$element"};
866: &Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
867: if (defined($newvalue) && $value ne $newvalue) {
868: if (ref($newvalue) eq 'ARRAY') {
869: $token->[2]->{$arg}=join(',',@$newvalue);
870: } else {
871: $token->[2]->{$arg}=$newvalue;
872: }
873: $rebuild=1;
874: # add new attributes to the of the attribute seq
875: if (!grep { $arg eq $_ } (@{ $token->[3] })) {
876: push(@{ $token->[3] },$arg);
877: }
878: } elsif (!defined($newvalue) && defined($value)) {
879: delete($token->[2]->{$arg});
880: $rebuild=1;
881: }
882: }
883: return $rebuild;
884: }
885:
886: # looks for /> on start tags
887: sub rebuild_tag {
888: my ($token) = @_;
889: my $result;
890: if ($token->[0] eq 'S') {
891: $result = '<'.$token->[1];
892: foreach my $attribute (@{ $token->[3] }) {
893: my $value = $token->[2]{$attribute};
894: next if ($value eq '');
895: $value =~s/^\s+|\s+$//g;
896: $value =~s/\"//g;
897: &Apache::lonxml::debug("setting :$attribute: to :$value:");
898: $result.=' '.$attribute.'="'.$value.'"';
899: }
900: if ($token->[4] =~ m:/>$:) {
901: $result.=' />';
902: } else {
903: $result.='>';
904: }
905: } elsif ( $token->[0] eq 'E' ) {
906: $result = '</'.$token->[1].'>';
907: }
908: return $result;
909: }
910:
911: sub html_element_name {
912: my ($name) = @_;
913: return $name.'_'.$Apache::lonxml::curdepth;
914: }
915:
916: sub hidden_arg {
917: my ($name,$token) = @_;
918: my $result;
919: my $arg=$token->[2]{$name};
920: $result='<input name="'.&html_element_name($name).
921: '" type="hidden" value="'.$arg.'" />';
922: return $result;
923: }
924:
925: sub checked_arg {
926: my ($description,$name,$list,$token) = @_;
927: my $result;
928: my $optionlist="";
929: my $allselected=$token->[2]{$name};
930: $result=&mt($description);
931: foreach my $option (@$list) {
932: my ($value,$text);
933: if ( ref($option) eq 'ARRAY') {
934: $value='value="'.$$option[0].'"';
935: $text=$$option[1];
936: $option=$$option[0];
937: } else {
938: $text=$option;
939: $value='value="'.$option.'"';
940: }
941: $result.='<span class="LC_nobreak"><label><input type="checkbox" '.$value.' name="'.
942: &html_element_name($name).'"';
943: foreach my $selected (split(/,/,$allselected)) {
944: if ( $selected eq $option ) {
945: $result.=' checked="checked" ';
946: last;
947: }
948: }
949: $result.=&element_change_detection().' />'.$text.'</label></span>'."\n";
950: }
951: return $result;
952: }
953:
954: sub text_arg {
955: my ($description,$name,$token,$size) = @_;
956: my $result;
957: if (!defined $size) { $size=20; }
958: my $arg=$token->[2]{$name};
959: $result=&mt($description).' <input name="'.&html_element_name($name).
960: '" type="text" value="'.$arg.'" size="'.$size.'" '.
961: &element_change_detection().'/>';
962: return '<span class="LC_nobreak">'.$result.'</span>';
963: }
964:
965: sub select_arg {
966: my ($description,$name,$list,$token) = @_;
967: my $result;
968: my $optionlist="";
969: my $selected=$token->[2]{$name};
970: if (ref($list) eq 'ARRAY') {
971: foreach my $option (@{$list}) {
972: my ($text,$value);
973: if (ref($option) eq 'ARRAY') {
974: $value='value="'.&HTML::Entities::encode($option->[0]).'"';
975: $text=$option->[1];
976: $option=$option->[0];
977: } else {
978: $text=$option;
979: $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
980: }
981: if ( $selected eq $option ) {
982: $optionlist.="<option $value selected=\"selected\">".&mt($text)."</option>\n";
983: } else {
984: $optionlist.="<option $value >".&mt($text)."</option>\n";
985: }
986: }
987: }
988: $result.='<span class="LC_nobreak">'.&mt($description).' <select name="'.
989: &html_element_name($name).'" '.&element_change_detection().' >
990: '.$optionlist.'
991: </select></span>';
992: return $result;
993: }
994:
995: sub select_or_text_arg {
996: my ($description,$name,$list,$token,$size) = @_;
997: my $result;
998: my $optionlist="";
999: my $found=0;
1000: my $selected=$token->[2]{$name};
1001: if (ref($list) eq 'ARRAY') {
1002: foreach my $option (@{$list}) {
1003: my ($text,$value);
1004: if (ref($option) eq 'ARRAY') {
1005: $value='value="'.&HTML::Entities::encode($option->[0]).'"';
1006: $text=$option->[1];
1007: $option=$option->[0];
1008: } else {
1009: $text=$option;
1010: $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
1011: }
1012: if ( $selected eq $option ) {
1013: $optionlist.="<option $value selected=\"selected\">$text</option>\n";
1014: $found=1;
1015: } else {
1016: $optionlist.="<option $value>$text</option>\n";
1017: }
1018: }
1019: }
1020: $optionlist.="<option value=\"TYPEDINVALUE\"".
1021: ((!$found)?' selected="selected"':'').
1022: ">".&mt('Type-in value')."</option>\n";
1023: #
1024: my $change_code=&element_change_detection();
1025: my $element=&html_element_name($name);
1026: my $selectelement='select_list_'.$element;
1027: my $typeinelement='type_in_'.$element;
1028: my $typeinvalue=($found?'':$selected);
1029: #
1030: my $hiddenvalue='this.form.'.$element.'.value';
1031: my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
1032: my $selectedvalue='this.form.'.$selectelement.
1033: '.options['.$selectedindex.'].value';
1034: my $typedinvalue='this.form.'.$typeinelement.'.value';
1035: my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
1036: $description=&mt($description);
1037: #
1038: return (<<ENDSELECTORTYPE);
1039: <span class="LC_nobreak">
1040: $description
1041: <select name="$selectelement"
1042: onChange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }" >
1043: $optionlist
1044: </select>
1045: <input type="text" size="$size" name="$typeinelement"
1046: value="$typeinvalue"
1047: onChange="$hiddenvalue=$typedinvalue;"
1048: onFocus="$selectedindex=$selecttypeinindex-1;" />
1049: <input type="hidden" name="$element" value="$selected" $change_code />
1050: </span>
1051: ENDSELECTORTYPE
1052: }
1053:
1054: #----------------------------------------------------- image coordinates
1055: # single image coordinates, x, y
1056: sub entercoords {
1057: my ($idx,$idy,$mode,$width,$height) = @_;
1058: unless ($Apache::edit::bgimgsrc) { return ''; }
1059: if ($idx) { $idx.='_'; }
1060: if ($idy) { $idy.='_'; }
1061: my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1062: my $form = 'lonhomework';
1063: my $element;
1064: if (! defined($mode) || $mode eq 'attribute') {
1065: $element = &escape("$Apache::lonxml::curdepth");
1066: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1067: $element = &escape('homework_edit_'.
1068: $Apache::lonxml::curdepth);
1069: }
1070: my $id=$Apache::lonxml::curdepth;
1071: my %data=("imagechoice.$id.type" =>'point',
1072: "imagechoice.$id.formname" =>$form,
1073: "imagechoice.$id.formx" =>"$idx$element",
1074: "imagechoice.$id.formy" =>"$idy$element",
1075: "imagechoice.$id.file" =>$bgfile,
1076: "imagechoice.$id.formcoord" =>$element);
1077: if ($height) {
1078: $data{"imagechoice.$id.formheight"}=$height.'_'.
1079: $Apache::edit::bgimgsrccurdepth;
1080: }
1081: if ($width) {
1082: $data{"imagechoice.$id.formwidth"}=$width.'_'.
1083: $Apache::edit::bgimgsrccurdepth;
1084: }
1085: &Apache::lonnet::appenv(\%data);
1086: my $text="Click Coordinates";
1087: my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1088: return $result;
1089: }
1090:
1091: # coordinates (x1,y1)-(x2,y2)...
1092: # mode can be either box, or polygon
1093: sub entercoord {
1094: my ($idx,$mode,$width,$height,$type) = @_;
1095: unless ($Apache::edit::bgimgsrc) { return ''; }
1096: my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1097: my $form = 'lonhomework';
1098: my $element;
1099: if (! defined($mode) || $mode eq 'attribute') {
1100: $element = &escape("$idx\_$Apache::lonxml::curdepth");
1101: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1102: $element = &escape('homework_edit_'.
1103: $Apache::lonxml::curdepth);
1104: }
1105: my $id=$Apache::lonxml::curdepth;
1106: my %data=("imagechoice.$id.type" =>$type,
1107: "imagechoice.$id.formname" =>$form,
1108: "imagechoice.$id.file" =>$bgfile,
1109: "imagechoice.$id.formcoord" =>$element);
1110: if ($height) {
1111: $data{"imagechoice.$id.formheight"}=$height.'_'.
1112: $Apache::edit::bgimgsrccurdepth;
1113: }
1114: if ($width) {
1115: $data{"imagechoice.$id.formwidth"}=$width.'_'.
1116: $Apache::edit::bgimgsrccurdepth;
1117: }
1118: &Apache::lonnet::appenv(\%data);
1119: my $text="Enter Coordinates";
1120: if ($type eq 'polygon') { $text='Create Polygon Data'; }
1121: my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1122: return $result;
1123: }
1124:
1125: sub deletecoorddata {
1126: &Apache::lonnet::delenv('imagechoice.');
1127: }
1128:
1129: #----------------------------------------------------- browse
1130: sub browse {
1131: # insert a link to call up the filesystem browser (lonindexer)
1132: my ($id, $mode, $titleid, $only) = @_;
1133: my $form = 'lonhomework';
1134: my $element;
1135: if (! defined($mode) || $mode eq 'attribute') {
1136: $element = &escape("$id\_$Apache::lonxml::curdepth");
1137: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1138: $element = &escape('homework_edit_'.
1139: $Apache::lonxml::curdepth);
1140: }
1141: my $titleelement;
1142: if ($titleid) {
1143: $titleelement=",'$only','','".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1144: } else {
1145: $titleelement=",'$only'";
1146: }
1147: my $result = <<"ENDBUTTON";
1148: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
1149: ENDBUTTON
1150: return $result;
1151: }
1152:
1153: #----------------------------------------------------- browse
1154: sub search {
1155: # insert a link to call up the filesystem browser (lonindexer)
1156: my ($id, $mode, $titleid) = @_;
1157: my $form = 'lonhomework';
1158: my $element;
1159: if (! defined($mode) || $mode eq 'attribute') {
1160: $element = &escape("$id\_$Apache::lonxml::curdepth");
1161: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1162: $element = &escape('homework_edit_'.
1163: $Apache::lonxml::curdepth);
1164: }
1165: my $titleelement;
1166: if ($titleid) {
1167: $titleelement=",'".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1168: }
1169: my $result = <<"ENDBUTTON";
1170: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>
1171: ENDBUTTON
1172: return $result;
1173: }
1174:
1175:
1176: 1;
1177: __END__
1178:
1179:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>