File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.87: download - view: text, annotated - select for diffs
Thu Sep 30 21:47:21 2004 UTC (19 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- bug #3512

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

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