File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.68: download - view: text, annotated - select for diffs
Fri Oct 24 21:09:24 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- needed for BUG#1473, the 'Select' link can now also get you the title of a resource back

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

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