File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.105: download - view: text, annotated - select for diffs
Sun Feb 18 02:00:59 2007 UTC (17 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: version_2_3_99_0, HEAD
- work wth Tasks
- strip and restore leading and trailing whitespace from textarea editors
- Nesting multiple of the same tags did't work well

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>