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