File:
[LON-CAPA] /
loncom /
homework /
edit.pm
Revision
1.157:
download - view:
text,
annotated -
select for diffs
Mon Mar 27 18:41:06 2023 UTC (16 months, 3 weeks ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_12_X,
HEAD
- "In course" authoring.
Improved handling of case where no suitable resources authored in course
exist when (a) using "Import from Course Resources" in Course Editor, and
(b) using Choose File > "Use a course file" to select published file as src
in img tag in colorful editor.
1: # The LearningOnline Network with CAPA
2: # edit mode helpers
3: #
4: # $Id: edit.pm,v 1.157 2023/03/27 18:41:06 raeburn 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 Apache::loncommon;
138: use HTML::Entities();
139: use Apache::lonlocal;
140: use lib '/home/httpd/lib/perl/';
141: use LONCAPA qw(:DEFAULT :match);
142:
143:
144: # Global Vars
145: # default list of colors to use in editing
146: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
147: # depth of nesting of edit
148: $Apache::edit::colordepth=0;
149: @Apache::edit::inserttag=();
150: # image-type responses: active background image and curdepth at definition
151: $Apache::edit::bgimgsrc='';
152: $Apache::edit::bgimgsrccurdepth='';
153:
154: sub initialize_edit {
155: $Apache::edit::colordepth=0;
156: @Apache::edit::inserttag=();
157: }
158:
159: sub tag_start {
160: my ($target,$token,$description) = @_;
161: my $result='';
162: if ($target eq "edit") {
163: my $tag=$token->[1];
164: if (!$description) {
165: $description=&mt(&Apache::lonxml::description($token));
166: if (!$description) { $description="<$tag>"; }
167: }
168: $result.= &start_table($token)."<tr><td>".&Apache::loncommon::insert_folding_button().
169: " $description</td><td>".&mt('Delete?')." ".&deletelist($target,$token).
170: "</td><td>".&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 class="LC_edit_problem_latexhelper">'.
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>';
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><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 {
234: return '<tr name="foldblock_'.$Apache::lonxml::curdepth.
235: '" style="visibility: \'\'"><td colspan="5" bgcolor="#F0F0F0">';
236: }
237: sub start_row { return '<tr><td bgcolor="#DDDDDD">'; }
238: sub end_row { return '</td></tr>'; }
239:
240: sub movebuttons {
241: my ($target,$token) = @_;
242: my $result='<input type="submit" name="moveup.'.
243: $Apache::lonxml::curdepth.'" value="Move Up" />';
244: $result.='<input type="submit" name="movedown.'.
245: $Apache::lonxml::curdepth.'" value="Move Down" />';
246: return $result;
247: }
248:
249: sub deletelist {
250: my ($target,$token) = @_;
251: my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
252: <option></option>
253: <option>".&mt('yes')."</option>
254: </select>";
255: return $result;
256: }
257:
258: sub handle_delete {
259: if (!$env{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
260: my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
261: my $result=0;
262: if ($space) {
263: my $sub1="$space\:\:delete_$token->[1]";
264: {
265: no strict 'refs';
266: if (defined &$sub1) {
267: $result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
268: }
269: }
270: }
271: if (!$result) {
272: my $endtag='/'.$token->[1];
273: my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser,$style);
274: $$parser['-1']->get_token();
275: &Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
276: &Apache::lonxml::end_tag($tagstack,$parstack,$token);
277: }
278: return 1;
279: }
280:
281: sub get_insert_list {
282: my ($tagname) = @_;
283: my $result='';
284: my @tags= ();
285: #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
286: if ($Apache::lonxml::insertlist{"$tagname.which"}) {
287: push (@tags, @{ $Apache::lonxml::insertlist{"$tagname.which"} });
288: }
289: foreach my $namespace (@Apache::lonxml::namespace) {
290: if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) {
291: push (@tags, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} });
292: }
293: }
294: if (@tags) {
295: my %options;
296: foreach my $tag (@tags) {
297: my $descrip=$Apache::lonxml::insertlist{"$tag.description"};
298: my $tagnum =$Apache::lonxml::insertlist{"$tag.num"};
299: $options{$descrip} ="<option value=\"$tagnum\">".
300: &mt($descrip)."</option>\n";
301: }
302: foreach my $option (sort(keys(%options))) {$result.=$options{$option};}
303: if ($result) { $result='<option selected="selected"></option>'.$result; }
304: }
305: return $result;
306: }
307:
308: sub insertlist {
309: my ($target,$token) = @_;
310: return &innerinsertlist($target,$token->[1]);
311: }
312:
313: sub innerinsertlist {
314: my ($target,$tagname,$closingtag) = @_;
315: my $result;
316: my $after='';
317: if ($closingtag) {
318: $after='_after_'.$closingtag;
319: }
320: if ($target eq 'edit') {
321: my $optionlist= &get_insert_list($tagname);
322: if ($optionlist) {
323: $result = &mt('Insert:')."
324: <select name=\"insert$after\_$Apache::lonxml::curdepth\">
325: $optionlist
326: </select>"
327: } else {
328: $result=" ";
329: }
330: }
331: return $result;
332: }
333:
334: sub handle_insert {
335: if ($env{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
336: my $tagnum = $env{"form.insert_$Apache::lonxml::curdepth"};
337: return &do_insert($tagnum);
338: }
339:
340: sub handle_insertafter {
341: my $tagname=shift;
342: if ($env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '') {
343: return '';
344: }
345: my $tagnum =$env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"};
346: return &do_insert($tagnum,1);
347: }
348:
349: sub do_insert {
350: my ($tagnum,$after) = @_;
351: my $result;
352:
353: my $newtag = $Apache::lonxml::insertlist{"$tagnum.tag"};
354: my $func = $Apache::lonxml::insertlist{"$newtag.function"};
355: if ($func eq 'default') {
356: my $namespace;
357: if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
358: my $depth = scalar(@Apache::lonxml::depthcounter);
359: $depth -- if ($after);
360: my $inset = "\t"x$depth;
361: $result.="\n$inset<$newtag></$newtag>";
362: } else {
363: if (defined(&$func)) {
364: {
365: no strict 'refs';
366: $result.=&$func();
367: }
368: } else {
369: &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined. ($tagnum)");
370: }
371: }
372: return $result;
373: }
374:
375: sub insert_img {
376: return '
377: <img />';
378: }
379:
380: sub insert_responseparam {
381: return '
382: <responseparam />';
383: }
384:
385: sub insert_parameter {
386: return '
387: <parameter />';
388: }
389:
390: sub insert_formularesponse {
391: return '
392: <formularesponse answer="" samples="">
393: <responseparam description="Numerical Tolerance" type="tolerance" default="0.00001" name="tol" />
394: <textline size="25"/>
395: <hintgroup>
396: <startouttext /><endouttext />
397: </hintgroup>
398: </formularesponse>';
399: }
400:
401: sub insert_functionplotresponse {
402: return '
403: <functionplotresponse>
404: <functionplotelements>
405: </functionplotelements>
406: <functionplotruleset>
407: </functionplotruleset>
408: </functionplotresponse>';
409: }
410:
411: sub insert_spline {
412: return '
413: <spline />';
414: }
415:
416: sub insert_backgroundplot {
417: return '
418: <backgroundplot />';
419: }
420:
421: sub insert_plotobject {
422: return '
423: <plotobject />';
424: }
425:
426: sub insert_plotvector {
427: return '
428: <plotvector />';
429: }
430:
431: sub insert_drawvectorsum {
432: return '
433: <drawvectorsum />';
434: }
435:
436:
437: sub insert_functionplotrule {
438: return '
439: <functionplotrule />';
440: }
441:
442: sub insert_functionplotvectorrule {
443: return '
444: <functionplotvectorrule />';
445: }
446:
447: sub insert_functionplotvectorsumrule {
448: return '
449: <functionplotvectorsumrule />';
450: }
451:
452: sub insert_functionplotcustomrule {
453: return '
454: <functionplotcustomrule>
455: <answer type="loncapa/perl">
456: # &fpr_val("label"), &fpr_f($x), &fpr_dfdx($x), &fpr_d2fdx2($x)
457: # ($xs,$xe,$ys,$ye)=&fpr_vectorcoords("Name"), ($x,$y)=&fpr_objectcoords("Name")
458: # &fpr_vectorlength("Name"), &fpr_vectorangle("Name")
459:
460: # Return 0 or 1
461: return 1;
462: </answer>
463: </functionplotcustomrule>';
464: }
465:
466: sub insert_functionplotruleset {
467: return '
468: <functionplotruleset>
469: <functionplotrule />
470: </functionplotruleset>';
471: }
472:
473: sub insert_functionplotelements {
474: return '
475: <functionplotelements>
476: <spline />
477: </functionplotelements>';
478: }
479:
480: sub insert_numericalresponse {
481: return '
482: <numericalresponse answer="">
483: <responseparam type="tolerance" default="5%" name="tol" description="Numerical Tolerance" />
484: <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />
485: <textline />
486: <hintgroup>
487: <startouttext /><endouttext />
488: </hintgroup>
489: </numericalresponse>';
490: }
491:
492: sub insert_externalresponse {
493: return '
494: <externalresponse url="" answer="" answerdisplay="" form="">
495: <textfield spellcheck="none" />
496: </externalresponse>';
497: }
498:
499: sub insert_customresponse {
500: return '
501: <customresponse>
502: <answer type="loncapa/perl">
503: </answer>
504: <textline />
505: <hintgroup>
506: <startouttext /><endouttext />
507: </hintgroup>
508: </customresponse>';
509: }
510:
511: sub insert_customresponse_answer {
512: return '
513: <answer type="loncapa/perl">
514: </answer>
515: ';
516: }
517:
518: sub insert_customhint {
519: return '
520: <customhint>
521: <answer type="loncapa/perl">
522: </answer>
523: </customhint>';
524: }
525:
526: sub insert_customhint_answer {
527: return '
528: <answer type="loncapa/perl">
529: </answer>
530: ';
531: }
532:
533: sub insert_mathresponse {
534: return '
535: <mathresponse>
536: <answer>
537: </answer>
538: <textline />
539: <hintgroup>
540: <startouttext />
541: <endouttext />
542: </hintgroup>
543: </mathresponse>';
544: }
545:
546: sub insert_mathresponse_answer {
547: return '
548: <answer>
549: </answer>
550: ';
551: }
552:
553: sub insert_mathhint {
554: return '
555: <mathhint>
556: <answer>
557: </answer>
558: </mathhint>';
559: }
560:
561: sub insert_mathhint_answer {
562: return '
563: <answer>
564: </answer>
565: ';
566: }
567:
568: sub insert_stringresponse {
569: return '
570: <stringresponse answer="" type="">
571: <textline />
572: <hintgroup>
573: <startouttext /><endouttext />
574: </hintgroup>
575: </stringresponse>';
576: }
577:
578: sub insert_essayresponse {
579: return '
580: <essayresponse>
581: <textfield></textfield>
582: </essayresponse>';
583: }
584:
585: sub insert_imageresponse {
586: return '
587: <imageresponse max="1">
588: <foilgroup>
589: <foil>
590: </foil>
591: </foilgroup>
592: <hintgroup>
593: <startouttext /><endouttext />
594: </hintgroup>
595: </imageresponse>';
596: }
597:
598: sub insert_optionresponse {
599: return '
600: <optionresponse max="10">
601: <foilgroup options="">
602: <foil>
603: <startouttext /><endouttext />
604: </foil>
605: </foilgroup>
606: <hintgroup>
607: <startouttext /><endouttext />
608: </hintgroup>
609: </optionresponse>';
610: }
611:
612: sub insert_organicresponse {
613: return '
614: <organicresponse>
615: <textline />
616: <hintgroup>
617: <startouttext /><endouttext />
618: </hintgroup>
619: </organicresponse>';
620: }
621:
622: sub insert_organicstructure {
623: return '
624: <organicstructure />
625: ';
626: }
627:
628: sub insert_radiobuttonresponse {
629: return '
630: <radiobuttonresponse max="10">
631: <foilgroup>
632: <foil>
633: <startouttext /><endouttext />
634: </foil>
635: </foilgroup>
636: <hintgroup>
637: <startouttext /><endouttext />
638: </hintgroup>
639: </radiobuttonresponse>';
640: }
641:
642: sub insert_reactionresponse {
643: return '
644: <reactionresponse>
645: <textline />
646: <hintgroup>
647: <startouttext /><endouttext />
648: </hintgroup>
649: </reactionresponse>';
650: }
651:
652: sub insert_rankresponse {
653: return '
654: <rankresponse max="10">
655: <foilgroup options="">
656: <foil>
657: <startouttext /><endouttext />
658: </foil>
659: </foilgroup>
660: <hintgroup>
661: <startouttext /><endouttext />
662: </hintgroup>
663: </rankresponse>';
664: }
665:
666: sub insert_matchresponse {
667: return '
668: <matchresponse max="10">
669: <foilgroup options="">
670: <itemgroup>
671: <item>
672: <startouttext /><endouttext />
673: </item>
674: </itemgroup>
675: <foil>
676: <startouttext /><endouttext />
677: </foil>
678: </foilgroup>
679: <hintgroup>
680: <startouttext /><endouttext />
681: </hintgroup>
682: </matchresponse>';
683: }
684:
685: sub insert_startpartmarker { return '<startpartmarker />'; }
686: sub insert_endpartmarker { return '<endpartmarker />'; }
687:
688: sub insert_displayduedate { return '<displayduedate />'; }
689: sub insert_displaytitle { return '<displaytitle />'; }
690: sub insert_hintpart {
691: return '
692: <hintpart on="default">
693: <startouttext/><endouttext />
694: </hintpart>';
695: }
696:
697: sub insert_hintgroup {
698: return '
699: <hintgroup>
700: <startouttext /><endouttext />
701: </hintgroup>';
702: }
703:
704: sub insert_numericalhint {
705: return '
706: <numericalhint>
707: </numericalhint>';
708: }
709:
710: sub insert_reactionhint {
711: return '
712: <reactionhint>
713: </reactionhint>';
714: }
715:
716: sub insert_organichint {
717: return '
718: <organichint>
719: </organichint>';
720: }
721:
722: sub insert_stringhint {
723: return '
724: <stringhint>
725: </stringhint>';
726: }
727:
728: sub insert_formulahint {
729: return '
730: <formulahint>
731: </formulahint>';
732: }
733:
734: sub insert_radiobuttonhint {
735: return '
736: <radiobuttonhint>
737: </radiobuttonhint>';
738: }
739:
740: sub insert_optionhint {
741: return '
742: <optionhint>
743: </optionhint>';
744: }
745:
746: sub insert_startouttext {
747: return "<startouttext /><endouttext />";
748: }
749:
750: sub insert_script {
751: return "\n<script type=\"loncapa/perl\"></script>";
752: }
753:
754: sub js_change_detection {
755: my $unsaved=&mt("There are unsaved changes");
756: return (<<SCRIPT);
757: <script type="text/javascript">
758: // <![CDATA[
759: var clean = true;
760: var is_submit = false;
761: var still_ask = false;
762: function compareForm(event_) {
763: if (!event_ && window.event) {
764: event_ = window.event;
765: }
766: if ((!is_submit || (is_submit && still_ask)) && !clean) {
767: still_ask = false;
768: is_submit = false;
769: event_.returnValue = "$unsaved";
770: return "$unsaved";
771: }
772: }
773: function unClean() {
774: clean=false;
775: }
776: window.onbeforeunload = compareForm;
777: // ]]>
778: </script>
779: SCRIPT
780: }
781:
782: sub form_change_detection {
783: return ' onsubmit="is_submit=true;" ';
784: }
785:
786: sub element_change_detection {
787: return ' onchange="unClean();" ';
788: }
789:
790: sub submit_ask_anyway {
791: my ($extra_action) = @_;
792: my $resource = &Apache::loncommon::escape_single($env{'request.ambiguous'});
793: return ' onclick="saveScrollPosition(\''.$resource.'\');still_ask=true;'.$extra_action.';" ';
794: }
795:
796: sub submit_dont_ask {
797: my ($extra_action) = @_;
798: my $resource = &Apache::loncommon::escape_single($env{'request.ambiguous'});
799: return ' onclick="saveScrollPosition(\''.$resource.'\');is_submit=true;'.$extra_action.';" ';
800: }
801:
802: sub js_update_linknum {
803: return (<<SCRIPT);
804: <script type="text/javascript">
805: // <![CDATA[
806: function updateNumber(name,index,caller,textprompt) {
807: var pickitem = document.getElementById(name+'_'+index);
808: var picknumtext = document.getElementById(name+'_numtext_'+index);
809: if (pickitem.checked) {
810: var showval = '';
811: if (pickitem.value != 'nochoice') {
812: showval = pickitem.value;
813: }
814: var picknum=prompt(textprompt,showval);
815: if (picknum == '' || picknum == null) {
816: if (caller == 'check') {
817: pickitem.checked=false;
818: pickitem.value='nochoice';
819: }
820: } else {
821: picknum.toString();
822: var regexdigit=/^\\d+\$/;
823: if (regexdigit.test(picknum)) {
824: pickitem.value = picknum;
825: picknumtext.innerHTML = ' <a href="javascript:updateNumber(\\''+name+'\\',\\''+index+'\\',\\'link\\',\\''+textprompt+'\\');">'+picknum+'</a>';
826: } else {
827: if (caller == 'check') {
828: pickitem.checked=false;
829: pickitem.value='nochoice';
830: }
831: return;
832: }
833: }
834: } else {
835: pickitem.value = '';
836: picknumtext.innerHTML = '';
837: }
838: }
839:
840: // ]]>
841: </script>
842: SCRIPT
843:
844: }
845:
846: sub textarea_sizes {
847: my ($data)=@_;
848: my $count=0;
849: my $maxlength=-1;
850: foreach (split ("\n", $$data)) {
851: $count+=int(length($_)/79);
852: $count++;
853: if (length($_) > $maxlength) { $maxlength = length($_); }
854: }
855: my $rows = $count;
856: my $cols = $maxlength;
857: return ($rows,$cols);
858: }
859:
860: sub editline {
861: my ($tag,$data,$description,$size)=@_;
862: $data=&HTML::Entities::encode($data,'<>&"');
863: if ($description) { $description=$description."<br />"; }
864: my $change_code = &element_change_detection();
865: my $result = <<"END";
866: $description
867: <input type="text" name="homework_edit_$Apache::lonxml::curdepth"
868: value="$data" size="$size" $change_code />
869: END
870: return $result;
871: }
872:
873: sub editfield {
874: my ($tag,$data,$description,$minwidth,$minheight,$usehtmlarea)=@_;
875:
876: my ($rows,$cols)=&textarea_sizes(\$data);
877: my $textareaclass;
878:
879: if (&Apache::lonhtmlcommon::htmlareabrowser() && $usehtmlarea) {
880: $rows+=7; # make room for HTMLarea
881: $minheight+=7; # make room for HTMLarea
882: $textareaclass = ' class="LC_richDefaultOff"';
883: }
884: if ($cols > 80) { $cols = 80; }
885: if ($cols < $minwidth ) { $cols = $minwidth; }
886: if ($rows < $minheight) { $rows = $minheight; }
887: if ($description) { $description='<br />'.&mt($description).'<br />'; }
888:
889: # remove typesetting whitespace from between data and the end tag
890: # to make the edit look prettier
891: $data =~ s/\n?[ \t]*$//;
892:
893: return $description."\n".'<textarea style="width:99%" rows="'.$rows.
894: '" cols="'.$cols.'" name="homework_edit_'.
895: $Apache::lonxml::curdepth.'" id="homework_edit_'.
896: $Apache::lonxml::curdepth.'" '.&element_change_detection().
897: $textareaclass.'>'.
898: &HTML::Entities::encode($data,'<>&"').'</textarea>'.
899: ($usehtmlarea?'<br />'.&Apache::lonhtmlcommon::spelllink('lonhomework',
900: 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
901: }
902:
903: sub modifiedfield {
904: my ($endtag,$parser) = @_;
905: my $result;
906: $result=$env{"form.homework_edit_$Apache::lonxml::curdepth"};
907: my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
908: # textareas throw away intial \n
909: if ($bodytext=~/^\n/) {
910: $result="\n".$result;
911: }
912: # if there is typesetting whitespace from between the data and the end tag
913: # restore to keep the source looking pretty
914: if ($bodytext =~ /(\n?[ \t]*)$/) {
915: $result .= $1;
916: }
917: return $result;
918: }
919:
920: # Returns a 1 if the token has been modified and you should rebuild the tag
921: # side-effects, will modify the $token if new values are found
922: sub get_new_args {
923: my ($token,$parstack,$safeeval,@args)=@_;
924: my $rebuild=0;
925: foreach my $arg (@args) {
926: #just want the string that it was set to
927: my $value=$token->[2]->{$arg};
928: my $element=&html_element_name($arg);
929: my $newvalue=$env{"form.$element"};
930: &Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
931: if (defined($newvalue) && $value ne $newvalue) {
932: if (ref($newvalue) eq 'ARRAY') {
933: $token->[2]->{$arg}=join(',',@$newvalue);
934: } else {
935: $token->[2]->{$arg}=$newvalue;
936: }
937: $rebuild=1;
938: # add new attributes to the of the attribute seq
939: if (!grep { $arg eq $_ } (@{ $token->[3] })) {
940: push(@{ $token->[3] },$arg);
941: }
942: } elsif (!defined($newvalue) && defined($value)) {
943: delete($token->[2]->{$arg});
944: $rebuild=1;
945: }
946: }
947: return $rebuild;
948: }
949:
950: # looks for /> on start tags
951: sub rebuild_tag {
952: my ($token) = @_;
953: my $result;
954: if ($token->[0] eq 'S') {
955: $result = '<'.$token->[1];
956: foreach my $attribute (@{ $token->[3] }) {
957: my $value = $token->[2]{$attribute};
958: next if ($value eq '');
959: $value =~s/^\s+|\s+$//g;
960: $value =~s/\"//g;
961: &Apache::lonxml::debug("setting :$attribute: to :$value:");
962: $result.=' '.$attribute.'="'.$value.'"';
963: }
964: if ($token->[4] =~ m:/>$:) {
965: $result.=' />';
966: } else {
967: $result.='>';
968: }
969: } elsif ( $token->[0] eq 'E' ) {
970: $result = '</'.$token->[1].'>';
971: }
972: return $result;
973: }
974:
975: sub html_element_name {
976: my ($name) = @_;
977: return $name.'_'.$Apache::lonxml::curdepth;
978: }
979:
980: sub hidden_arg {
981: my ($name,$token) = @_;
982: my $result;
983: my $arg=$token->[2]{$name};
984: $result='<input name="'.&html_element_name($name).
985: '" type="hidden" value="'.$arg.'" />';
986: return $result;
987: }
988:
989: sub checked_arg {
990: my ($description,$name,$list,$token,$onclick,$useid) = @_;
991: my $result;
992: my $optionlist="";
993: my $allselected=$token->[2]{$name};
994: $result=&mt($description);
995: foreach my $option (@$list) {
996: my ($value,$text);
997: if ( ref($option) eq 'ARRAY') {
998: $value='value="'.$$option[0].'"';
999: $text=&mt($$option[1]);
1000: $option=$$option[0];
1001: } else {
1002: $text=&mt($option);
1003: $value='value="'.$option.'"';
1004: }
1005: $result.=' <span class="LC_edit_opt"><label><input type="checkbox" '.$value.' name="'.
1006: &html_element_name($name).'"';
1007: foreach my $selected (split(/,/,$allselected)) {
1008: if ( $selected eq $option ) {
1009: $result.=' checked="checked" ';
1010: last;
1011: }
1012: }
1013: if ($useid) {
1014: $result .= ' id="'.&html_element_name($name).'" ';
1015: }
1016: $result.=&element_change_detection().$onclick.' />'.$text.'</label></span>'."\n";
1017: }
1018: return $result;
1019: }
1020:
1021: sub text_arg {
1022: my ($description,$name,$token,$size, $class) = @_;
1023: my $result;
1024: if (!defined $size) { $size=20; }
1025: my $arg=$token->[2]{$name};
1026: $result=&mt($description).' <input name="'.&html_element_name($name).
1027: '" type="text" value="'.$arg.'" size="'.$size.'" ';
1028: if (defined $class) {
1029: $result .= 'class="' . $class . '" ';
1030: }
1031: $result .= &element_change_detection().'/>';
1032: return ' <span class="LC_edit_opt">'.$result.'</span>';
1033: }
1034:
1035: sub select_arg {
1036: my ($description,$name,$list,$token) = @_;
1037: my $result;
1038: my $optionlist="";
1039: my $selected=$token->[2]{$name};
1040: if (ref($list) eq 'ARRAY') {
1041: foreach my $option (@{$list}) {
1042: my ($text,$value);
1043: if (ref($option) eq 'ARRAY') {
1044: $value='value="'.&HTML::Entities::encode($option->[0]).'"';
1045: $text=$option->[1];
1046: $option=$option->[0];
1047: } else {
1048: $text=$option;
1049: $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
1050: }
1051: if ( $selected eq $option ) {
1052: $optionlist.="<option $value selected=\"selected\">".&mt($text)."</option>\n";
1053: } else {
1054: $optionlist.="<option $value >".&mt($text)."</option>\n";
1055: }
1056: }
1057: }
1058: $result.=' <span class="LC_edit_opt">'.&mt($description).' <select name="'.
1059: &html_element_name($name).'" '.&element_change_detection().' >
1060: '.$optionlist.'
1061: </select></span>';
1062: return $result;
1063: }
1064:
1065: sub select_or_text_arg {
1066: my ($description,$name,$list,$token,$size) = @_;
1067: my $result;
1068: my $optionlist="";
1069: my $found=0;
1070: my $selected=$token->[2]{$name};
1071: if (ref($list) eq 'ARRAY') {
1072: foreach my $option (@{$list}) {
1073: my ($text,$value);
1074: if (ref($option) eq 'ARRAY') {
1075: $value='value="'.&HTML::Entities::encode($option->[0]).'"';
1076: $text=$option->[1];
1077: $option=$option->[0];
1078: } else {
1079: $text=$option;
1080: $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
1081: }
1082: if ( $selected eq $option ) {
1083: $optionlist.="<option $value selected=\"selected\">$text</option>\n";
1084: $found=1;
1085: } else {
1086: $optionlist.="<option $value>$text</option>\n";
1087: }
1088: }
1089: }
1090: $optionlist.="<option value=\"TYPEDINVALUE\"".
1091: ((!$found)?' selected="selected"':'').
1092: ">".&mt('Type-in value')."</option>\n";
1093: #
1094: my $change_code=&element_change_detection();
1095: my $element=&html_element_name($name);
1096: my $selectelement='select_list_'.$element;
1097: my $typeinelement='type_in_'.$element;
1098: my $typeinvalue=($found?'':$selected);
1099: #
1100: my $hiddenvalue='this.form.'.$element.'.value';
1101: my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
1102: my $selectedvalue='this.form.'.$selectelement.
1103: '.options['.$selectedindex.'].value';
1104: my $typedinvalue='this.form.'.$typeinelement.'.value';
1105: my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
1106: $description=&mt($description);
1107: #
1108: return (<<ENDSELECTORTYPE);
1109: <span class="LC_edit_opt">
1110: $description
1111: <select name="$selectelement"
1112: onchange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }" >
1113: $optionlist
1114: </select>
1115: <input type="text" size="$size" name="$typeinelement"
1116: value="$typeinvalue"
1117: onchange="$hiddenvalue=$typedinvalue;"
1118: onfocus="$selectedindex=$selecttypeinindex-1;" />
1119: <input type="hidden" name="$element" value="$selected" $change_code />
1120: </span>
1121: ENDSELECTORTYPE
1122: }
1123:
1124: #----------------------------------------------------- image coordinates
1125: # single image coordinates, x, y
1126: sub entercoords {
1127: my ($idx,$idy,$mode,$width,$height) = @_;
1128: unless ($Apache::edit::bgimgsrc) { return ''; }
1129: if ($idx) { $idx.='_'; }
1130: if ($idy) { $idy.='_'; }
1131: my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1132: my $form = 'lonhomework';
1133: my $element;
1134: if (! defined($mode) || $mode eq 'attribute') {
1135: $element = &escape("$Apache::lonxml::curdepth");
1136: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1137: $element = &escape('homework_edit_'.
1138: $Apache::lonxml::curdepth);
1139: }
1140: my $id=$Apache::lonxml::curdepth;
1141: my %data=("imagechoice.$id.type" =>'point',
1142: "imagechoice.$id.formname" =>$form,
1143: "imagechoice.$id.formx" =>"$idx$element",
1144: "imagechoice.$id.formy" =>"$idy$element",
1145: "imagechoice.$id.file" =>$bgfile,
1146: "imagechoice.$id.formcoord" =>$element);
1147: if ($height) {
1148: $data{"imagechoice.$id.formheight"}=$height.'_'.
1149: $Apache::edit::bgimgsrccurdepth;
1150: }
1151: if ($width) {
1152: $data{"imagechoice.$id.formwidth"}=$width.'_'.
1153: $Apache::edit::bgimgsrccurdepth;
1154: }
1155: &Apache::lonnet::appenv(\%data);
1156: my $text="Click Coordinates";
1157: my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1158: return $result;
1159: }
1160:
1161: # coordinates (x1,y1)-(x2,y2)...
1162: # mode can be either box, or polygon
1163: sub entercoord {
1164: my ($idx,$mode,$width,$height,$type) = @_;
1165: unless ($Apache::edit::bgimgsrc) { return ''; }
1166: my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1167: my $form = 'lonhomework';
1168: my $element;
1169: if (! defined($mode) || $mode eq 'attribute') {
1170: $element = &escape("$idx\_$Apache::lonxml::curdepth");
1171: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1172: $element = &escape('homework_edit_'.
1173: $Apache::lonxml::curdepth);
1174: }
1175: my $id=$Apache::lonxml::curdepth;
1176: my %data=("imagechoice.$id.type" =>$type,
1177: "imagechoice.$id.formname" =>$form,
1178: "imagechoice.$id.file" =>$bgfile,
1179: "imagechoice.$id.formcoord" =>$element);
1180: if ($height) {
1181: $data{"imagechoice.$id.formheight"}=$height.'_'.
1182: $Apache::edit::bgimgsrccurdepth;
1183: }
1184: if ($width) {
1185: $data{"imagechoice.$id.formwidth"}=$width.'_'.
1186: $Apache::edit::bgimgsrccurdepth;
1187: }
1188: &Apache::lonnet::appenv(\%data);
1189: my $text="Enter Coordinates";
1190: if ($type eq 'polygon') { $text='Create Polygon Data'; }
1191: my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1192: return $result;
1193: }
1194:
1195: sub deletecoorddata {
1196: &Apache::lonnet::delenv('imagechoice.');
1197: }
1198:
1199: #----------------------------------------------------- browse and search links
1200: sub browse_or_search {
1201: my ($id,$mode,$titleid,$only,$crsonly,$usesearch) = @_;
1202: my $output;
1203: my ($cnum,$cdom) = &Apache::loncommon::crsauthor_url();
1204: if ($cnum) {
1205: my $form = 'lonhomework';
1206: my ($element,$bretitleelement,$srchtitleelement);
1207: $element = &get_element($id,$mode);
1208: my %lt = &Apache::lonlocal::texthash (
1209: uacf => 'Use a course file',
1210: uanf => 'Use a new file',
1211: impo => 'Import',
1212: sear => 'Search',
1213: sefi => 'Select File',
1214: upfi => 'Upload File',
1215: dire => 'Directory',
1216: news => 'New sub-directory',
1217: empd => 'No suitable resources found',
1218: );
1219: my ($importcrsres,$uploadfile,$allonly);
1220: if ($only) {
1221: $allonly = $only;
1222: if ($crsonly) {
1223: $allonly .= ','.$crsonly;
1224: }
1225: } elsif ($crsonly) {
1226: $allonly = $crsonly;
1227: }
1228: my ($numdirs,$pickfile) =
1229: &Apache::loncommon::import_crsauthor_form('coursepath_'.$element,'coursefile_'.$element,undef,$allonly,$element);
1230: $importcrsres=(<<CRSRES);
1231: <fieldset id="importcrsresform_$element" style="display:inline;">
1232: <legend>$lt{'uacf'}</legend>
1233: <div id="hascrsres_$element" style="padding:0;clear:both;margin:0;border:0;display:none;">
1234: <p>
1235: $pickfile
1236: </p><p>
1237: <input type="button" name="crsres_$element" value="$lt{'sefi'}" onclick="updateCrsFile(this.form,'$element');" />
1238: </p>
1239: </div>
1240: <div id="nocrsres_$element" style="padding:0;clear:both;margin:0;border:0display:none;">
1241: <p>
1242: $lt{'empd'}
1243: </p>
1244: </div>
1245: </fieldset>
1246: CRSRES
1247: my %subdirs;
1248: my $toppath="/priv/$cdom/$cnum";
1249: my $exclude = &Apache::lonnet::priv_exclude();
1250: &Apache::lonnet::recursedirs(1,1,'',$exclude,'',0,$toppath,'',\%subdirs);
1251: my $numcrsdirs = keys(%subdirs);
1252: my $pickdir = $lt{'dire'}.'<select name="crsauthorpath_'.$element.'">'."\n".
1253: '<option value="/">/</option>'."\n";
1254: if ($numcrsdirs) {
1255: foreach my $key (sort { lc($a) cmp lc($b) } (keys(%subdirs))) {
1256: $pickdir .= '<option value="'.$key.'">'.$key.'</option>'."\n";
1257: }
1258: }
1259: $pickdir .= '</select><br />';
1260: my $uploadfile =(<<CRSUPL);
1261: <fieldset id="uploadcrsresform_$element" style="display:inline;">
1262: <legend>$lt{'uanf'}</legend>
1263: <p>
1264: $pickdir
1265: <span class="LC_nobreak">$lt{'news'}?
1266: <label><input type="radio" name="newsubdir_$element" value="0" onclick="toggleNewsubdir(this.form,'$element');" checked="checked" />No</label>
1267:
1268: <label><input type="radio" name="newsubdir_$element" value="1" onclick="toggleNewsubdir(this.form,'$element');" />Yes</label>
1269: </span><span id="newsubdir_$element"></span>
1270: <input type="hidden" name="newsubdirname_$element" id="newsubdirname_$element" value="" autocomplete="off" />
1271: </p>
1272: <input type="file" id="uploadcrsres_$element" name="uploadcrsres_$element" size="40" />
1273: <input type="hidden" id="crsuploadto_$element" name="filename_$element" value="$ENV{'REQUEST_URI'}" />
1274: <input type="submit" id="crsupload_$element" class="LC_uploadcrsres" value="$lt{'upfi'}" />
1275: <iframe id="crsupload_target_$element" name="crsupload_target_$element" src="" style="width:0px;height:0px;border:0px"></iframe>
1276: </fieldset>
1277: CRSUPL
1278: if ($titleid) {
1279: $bretitleelement=",'$only','','".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1280: $srchtitleelement=",'".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1281: } else {
1282: $bretitleelement=",'$only'";
1283: }
1284: $output = '<a href="javascript:toggleChooser(document.'.$form.",'$element'".');">'.
1285: &mt('Choose File').'</a>'.
1286: '<div id="chooser_'.$element.'" style="display:none" class="LC_left_float">'.
1287: '<fieldset><legend>'.&mt('Choose File').'</legend>'.
1288: '<label><input type="radio" name="chooser_'.$element.'" value="crsres" onclick="toggleCrsFile(this.form,'."'$element'".')" />'.$lt{'uacf'}.'</label> '.
1289: '<label><input type="radio" name="chooser_'.$element.'" value="upload" onclick="toggleCrsUpload(this.form,'."'$element'".')" />'.$lt{'uanf'}.'</label> '.
1290: '<label><input type="radio" name="chooser_'.$element.'" value="import" onclick="toggleResImport(this.form,'."'$element'".');openbrowser('."'$form','$element'$bretitleelement)".'" />'.$lt{'impo'}.'</label>';
1291: if ($usesearch) {
1292: $output .= ' <label><input type="radio" name="chooser_'.$element.'" value="search" onclick="opensearcher('."'$form','$element'$srchtitleelement".')" />'.$lt{'sear'}.'</label>';
1293: }
1294: $output .= '<div id="chooser_'.$element.'_crsres" style="display:none">'.
1295: $importcrsres.
1296: '</div>'.
1297: '<div id="chooser_'.$element.'_upload" style="display:none">'.
1298: $uploadfile.
1299: '</div>'.
1300: '</fieldset></div>';
1301:
1302: } else {
1303: $output = &browse($id, $mode, $titleid, $only);
1304: if ($usesearch) {
1305: $output .= ' '.&search($id, $mode, $titleid);
1306: }
1307: }
1308: return $output;
1309: }
1310:
1311: #----------------------------------------------------- browse
1312: sub browse {
1313: # insert a link to call up the filesystem browser (lonindexer)
1314: my ($id, $mode, $titleid, $only) = @_;
1315: my %lt = &Apache::lonlocal::texthash (
1316: se => 'Select',
1317: );
1318: my $form = 'lonhomework';
1319: my $element = &get_element($id,$mode);
1320: my $titleelement;
1321: if ($titleid) {
1322: $titleelement=",'$only','','".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1323: } else {
1324: $titleelement=",'$only'";
1325: }
1326: return <<"ENDBUTTON";
1327: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>$lt{'se'}</a>
1328: ENDBUTTON
1329: }
1330:
1331: #----------------------------------------------------- search
1332: sub search {
1333: # insert a link to call up the filesystem browser (lonindexer)
1334: my ($id, $mode, $titleid) = @_;
1335: my $form = 'lonhomework';
1336: my $element = &get_element($id,$mode);
1337: my $titleelement;
1338: if ($titleid) {
1339: $titleelement=",'".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
1340: }
1341: my $linktext = &mt('Search');
1342: my $result = <<"ENDBUTTON";
1343: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>$linktext</a>
1344: ENDBUTTON
1345: return $result;
1346: }
1347:
1348: sub get_element {
1349: my ($id,$mode) = @_;
1350: my $element;
1351: if (! defined($mode) || $mode eq 'attribute') {
1352: $element = &escape("$id\_$Apache::lonxml::curdepth");
1353: } elsif ($mode eq 'textnode') { # for data between <tag> ... </tag>
1354: $element = &escape('homework_edit_'.
1355: $Apache::lonxml::curdepth);
1356: }
1357: return $element;
1358: }
1359:
1360: 1;
1361: __END__
1362:
1363:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>