File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.112: download - view: text, annotated - select for diffs
Wed Oct 3 00:24:48 2007 UTC (16 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_2_6_0, version_2_5_99_1, version_2_5_99_0, HEAD
- tighten up the edit display some

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

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