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