File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.88: download - view: text, annotated - select for diffs
Tue Oct 26 15:06:58 2004 UTC (19 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_99_1, version_1_2_99_0, HEAD
- xhtml cleanup

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

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