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