File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.89: download - view: text, annotated - select for diffs
Wed Jan 5 21:20:13 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- bug 3793, throw in a single foil to start with...

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

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