File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.84.2.1: download - view: text, annotated - select for diffs
Thu Aug 19 21:03:23 2004 UTC (19 years, 8 months ago) by albertel
Branches: version_1_2_X
CVS tags: version_1_2_1, version_1_2_0, version_1_1_99_5
Diff to branchpoint 1.84: preferred, unified
 backport 1.86

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

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