File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.79: download - view: text, annotated - select for diffs
Fri Feb 13 21:12:54 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- handle checked_args when nothing is checked.

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

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