File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.71: download - view: text, annotated - select for diffs
Wed Nov 5 21:53:48 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Minimal internationalization

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

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