File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.94: download - view: text, annotated - select for diffs
Tue Aug 30 15:36:07 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: version_2_0_X, version_2_0_2, version_2_0_1, HEAD
- BUG#4163, IE inserts were being flushed right (the div align="right
" was bleeding through for some stupid resaon)

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

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