File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.135: download - view: text, annotated - select for diffs
Tue Aug 9 16:12:21 2011 UTC (12 years, 8 months ago) by bisitz
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, HEAD
Improve edit.pm 1.91:
Make textareas in the colorful editor fit in the colored tables, not overlap.
(tested with Win 7 Pro. FF 4.0.1, Win 7 Pro. IE 9, Ubuntu 10.04 FF 3.6.18, Ubuntu 10.04 Opera 10.63)

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

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