File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.103: download - view: text, annotated - select for diffs
Thu Jul 27 20:47:02 2006 UTC (17 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, HEAD
- trying to use less vertical space by text blocks

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

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