File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.128: download - view: text, annotated - select for diffs
Sun Aug 8 02:00:50 2010 UTC (13 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_0_RC1, HEAD
- Replacement of user's environment.wysiwygeditor with link to switch to/from
  CKEditor 3 Richtext editor on textareas where needed.
- Rich text editor link available for Text/HTML block in colorful editor
- Eliminate duplicate Richtext link for textarea entry fields
- "Edit Math" button hidden when richtext editor enabled - use <span> instead
  of <div> for more compact display.
- Help icon for "Edit Math" hidden by switching img src from help.png to
  transparent gif. Additional arg to loncommon::help_open_topic() is
  imgid - id of img tag for icon associated with each "Edit Math" button.

    1: # The LearningOnline Network with CAPA 
    2: # edit mode helpers
    3: #
    4: # $Id: edit.pm,v 1.128 2010/08/08 02:00:50 raeburn 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_numericalresponse {
  401:     return '
  402: <numericalresponse answer="">
  403: <responseparam type="tolerance" default="5%" name="tol" description="Numerical Tolerance" />
  404: <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />
  405:     <textline />
  406:     <hintgroup>
  407:     <startouttext /><endouttext />
  408:     </hintgroup>
  409: </numericalresponse>';
  410: }
  411: 
  412: sub insert_customresponse {
  413:     return '
  414: <customresponse>
  415:     <answer type="loncapa/perl">
  416:     </answer>
  417:     <textline />
  418:     <hintgroup>
  419:     <startouttext /><endouttext />
  420:     </hintgroup>
  421: </customresponse>';
  422: }
  423: 
  424: sub insert_customresponse_answer {
  425:     return '
  426:     <answer type="loncapa/perl">
  427:     </answer>
  428: ';
  429: }
  430: 
  431: sub insert_customhint {
  432:     return '
  433:         <customhint>
  434:             <answer type="loncapa/perl">
  435:             </answer>
  436:         </customhint>';
  437: }
  438: 
  439: sub insert_customhint_answer {
  440:     return '
  441:             <answer type="loncapa/perl">
  442:             </answer>
  443: ';
  444: }
  445: 
  446: sub insert_mathresponse {
  447:     return '
  448: <mathresponse>
  449:     <answer>
  450:     </answer>
  451:     <textline />
  452:     <hintgroup>
  453:         <startouttext />
  454:         <endouttext />
  455:     </hintgroup>
  456: </mathresponse>';
  457: }
  458: 
  459: sub insert_mathresponse_answer {
  460:     return '
  461:     <answer>
  462:     </answer>
  463: ';
  464: }
  465: 
  466: sub insert_mathhint {
  467:     return '
  468:         <mathhint>
  469:             <answer>
  470:             </answer>
  471:         </mathhint>';
  472: }
  473: 
  474: sub insert_mathhint_answer {
  475:     return '
  476:             <answer>
  477:             </answer>
  478: ';
  479: }
  480: 
  481: sub insert_stringresponse {
  482:     return '
  483: <stringresponse answer="" type="">
  484:     <textline />
  485:     <hintgroup>
  486:     <startouttext /><endouttext />
  487:     </hintgroup>
  488: </stringresponse>';
  489: }
  490: 
  491: sub insert_essayresponse {
  492:     return '
  493: <essayresponse>
  494:     <textfield></textfield>
  495: </essayresponse>';
  496: }
  497: 
  498: sub insert_imageresponse {
  499:     return '
  500: <imageresponse max="1">
  501:     <foilgroup>
  502:       <foil>
  503:       </foil>
  504:     </foilgroup>
  505:     <hintgroup>
  506:     <startouttext /><endouttext />
  507:     </hintgroup>
  508: </imageresponse>';
  509: }
  510: 
  511: sub insert_optionresponse {
  512:     return '
  513: <optionresponse max="10">
  514:     <foilgroup options="">
  515:       <foil>
  516:          <startouttext /><endouttext />
  517:       </foil>
  518:     </foilgroup>
  519:     <hintgroup>
  520:     <startouttext /><endouttext />
  521:     </hintgroup>
  522: </optionresponse>';
  523: }
  524: 
  525: sub insert_organicresponse {
  526:     return '
  527: <organicresponse>
  528:     <textline />
  529:     <hintgroup>
  530:     <startouttext /><endouttext />
  531:     </hintgroup>
  532: </organicresponse>';
  533: }
  534: 
  535: sub insert_organicstructure {
  536:     return '
  537: <organicstructure />
  538: ';
  539: }
  540: 
  541: sub insert_radiobuttonresponse {
  542:     return '
  543: <radiobuttonresponse max="10">
  544:     <foilgroup>
  545:       <foil>
  546:          <startouttext /><endouttext />
  547:       </foil>
  548:     </foilgroup>
  549:     <hintgroup>
  550:     <startouttext /><endouttext />
  551:     </hintgroup>
  552: </radiobuttonresponse>';
  553: }
  554: 
  555: sub insert_reactionresponse {
  556:     return '
  557: <reactionresponse>
  558:     <textline />
  559:     <hintgroup>
  560:     <startouttext /><endouttext />
  561:     </hintgroup>
  562: </reactionresponse>';
  563: }
  564: 
  565: sub insert_rankresponse {
  566:     return '
  567: <rankresponse max="10">
  568:     <foilgroup options="">
  569:       <foil>
  570:          <startouttext /><endouttext />
  571:       </foil>
  572:     </foilgroup>
  573:     <hintgroup>
  574:     <startouttext /><endouttext />
  575:     </hintgroup>
  576: </rankresponse>';
  577: }
  578: 
  579: sub insert_matchresponse {
  580:     return '
  581: <matchresponse max="10">
  582:     <foilgroup options="">
  583:       <itemgroup>
  584:       </itemgroup>
  585:       <foil>
  586:          <startouttext /><endouttext />
  587:       </foil>
  588:     </foilgroup>
  589:     <hintgroup>
  590:     <startouttext /><endouttext />
  591:     </hintgroup>
  592: </matchresponse>';
  593: }
  594: 
  595: sub insert_startpartmarker { return '<startpartmarker />'; }
  596: sub insert_endpartmarker { return '<endpartmarker />'; }
  597: 
  598: sub insert_displayduedate { return '<displayduedate />'; }
  599: sub insert_displaytitle   { return '<displaytitle />'; }
  600: sub insert_hintpart {
  601:     return '
  602: <hintpart on="default">
  603:     <startouttext/><endouttext />
  604: </hintpart>';
  605: }
  606: 
  607: sub insert_hintgroup {
  608:   return '
  609: <hintgroup>
  610:     <startouttext /><endouttext />
  611: </hintgroup>';
  612: }
  613: 
  614: sub insert_numericalhint {
  615:     return '
  616: <numericalhint>
  617: </numericalhint>';
  618: }
  619: 
  620: sub insert_reactionhint {
  621:     return '
  622: <reactionhint>
  623: </reactionhint>';
  624: }
  625: 
  626: sub insert_organichint {
  627:     return '
  628: <organichint>
  629: </organichint>';
  630: }
  631: 
  632: sub insert_stringhint {
  633:     return '
  634: <stringhint>
  635: </stringhint>';
  636: }
  637: 
  638: sub insert_formulahint {
  639:     return '
  640: <formulahint>
  641: </formulahint>';
  642: }
  643: 
  644: sub insert_radiobuttonhint {
  645:     return '
  646: <radiobuttonhint>
  647: </radiobuttonhint>';
  648: }
  649: 
  650: sub insert_optionhint {
  651:     return '
  652: <optionhint>
  653: </optionhint>';
  654: }
  655: 
  656: sub insert_startouttext {
  657:     return "<startouttext /><endouttext />";
  658: }
  659: 
  660: sub insert_script {
  661:     return "\n<script type=\"loncapa/perl\"></script>";
  662: }
  663: 
  664: sub js_change_detection {
  665:     my $unsaved=&mt("There are unsaved changes");
  666:     return (<<SCRIPT);
  667: <script type="text/javascript">
  668: var clean = true;
  669: var is_submit = false;
  670: var still_ask = false;
  671: function compareForm(event_) {
  672:         if (!event_ && window.event) {
  673:           event_ = window.event;
  674:         }
  675: 	if ((!is_submit || (is_submit && still_ask)) && !clean) {
  676: 	    still_ask = false;
  677: 	    is_submit = false;
  678:             event_.returnValue = "$unsaved";
  679:             return "$unsaved";
  680:         }
  681: }
  682: function unClean() {
  683:      clean=false;
  684: }
  685: window.onbeforeunload = compareForm;
  686: </script>
  687: SCRIPT
  688: }
  689: 
  690: sub form_change_detection {
  691:     return ' onsubmit="is_submit=true;" ';
  692: }
  693: 
  694: sub element_change_detection {
  695:     return ' onchange="unClean();" ';
  696: }
  697: 
  698: sub submit_ask_anyway {
  699:     my ($extra_action) = @_;
  700:     return ' onclick="still_ask=true;'.$extra_action.'" ';
  701: }
  702: 
  703: sub textarea_sizes {
  704:     my ($data)=@_;
  705:     my $count=0;
  706:     my $maxlength=-1;
  707:     foreach (split ("\n", $$data)) {
  708: 	$count+=int(length($_)/79);
  709: 	$count++;
  710: 	if (length($_) > $maxlength) { $maxlength = length($_); }
  711:     }
  712:     my $rows = $count;
  713:     my $cols = $maxlength;
  714:     return ($rows,$cols);
  715: }
  716: 
  717: sub editline {
  718:     my ($tag,$data,$description,$size)=@_;
  719:     $data=&HTML::Entities::encode($data,'<>&"');
  720:     if ($description) { $description=$description."<br />"; }
  721:     my $change_code = &element_change_detection();
  722:     my $result = <<"END";
  723: $description
  724: <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
  725:        value="$data" size="$size" $change_code />
  726: END
  727:     return $result;
  728: }
  729: 
  730: sub editfield {
  731:     my ($tag,$data,$description,$minwidth,$minheight,$usehtmlarea)=@_;
  732: 
  733:     my ($rows,$cols)=&textarea_sizes(\$data);
  734:     my $textareaclass;
  735:  
  736:     if (&Apache::lonhtmlcommon::htmlareabrowser() && $usehtmlarea) { 
  737: 	$rows+=7;      # make room for HTMLarea
  738: 	$minheight+=7; # make room for HTMLarea
  739:         $textareaclass = ' class="LC_richDefaultOff"';
  740:     }
  741:     if ($cols > 80) { $cols = 80; }
  742:     if ($cols < $minwidth ) { $cols = $minwidth; }
  743:     if ($rows < $minheight) { $rows = $minheight; }
  744:     if ($description) { $description=$description."<br />"; }
  745: 
  746:     # remove typesetting whitespace from between data and the end tag
  747:     # to make the edit look prettier
  748:     $data =~ s/\n?[ \t]*$//;
  749: 
  750:     return $description."\n".'<textarea style="width:100%" rows="'.$rows.
  751: 	'" cols="'.$cols.'" name="homework_edit_'.
  752: 	$Apache::lonxml::curdepth.'" id="homework_edit_'.
  753: 	$Apache::lonxml::curdepth.'" '.&element_change_detection().
  754:         $textareaclass.'>'.
  755: 	&HTML::Entities::encode($data,'<>&"').'</textarea>'.
  756: 	($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
  757: 			 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
  758: }
  759: 
  760: sub modifiedfield {
  761:     my ($endtag,$parser) = @_;
  762:     my $result;
  763:     $result=$env{"form.homework_edit_$Apache::lonxml::curdepth"};
  764:     my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
  765:     # textareas throw away intial \n 
  766:     if ($bodytext=~/^\n/) {
  767: 	$result="\n".$result;
  768:     }
  769:     # if there is typesetting whitespace from between the data and the end tag
  770:     # restore to keep the source looking pretty
  771:     if ($bodytext =~ /(\n?[ \t]*)$/) {
  772: 	$result .= $1;
  773:     }
  774:     return $result;
  775: }
  776: 
  777: # Returns a 1 if the token has been modified and you should rebuild the tag
  778: # side-effects, will modify the $token if new values are found
  779: sub get_new_args {
  780:     my ($token,$parstack,$safeeval,@args)=@_;
  781:     my $rebuild=0;
  782:     foreach my $arg (@args) {
  783: 	#just want the string that it was set to
  784: 	my $value=$token->[2]->{$arg};
  785: 	my $element=&html_element_name($arg);
  786: 	my $newvalue=$env{"form.$element"};
  787: 	&Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
  788: 	if (defined($newvalue) && $value ne $newvalue) {
  789: 	    if (ref($newvalue) eq 'ARRAY') {
  790: 		$token->[2]->{$arg}=join(',',@$newvalue);
  791: 	    } else {
  792: 		$token->[2]->{$arg}=$newvalue;
  793: 	    }
  794: 	    $rebuild=1;
  795: 	    # add new attributes to the of the attribute seq
  796: 	    if (!grep { $arg eq $_ } (@{ $token->[3] })) {
  797: 		push(@{ $token->[3] },$arg);
  798: 	    }
  799: 	} elsif (!defined($newvalue) && defined($value)) {
  800: 	    delete($token->[2]->{$arg});
  801: 	    $rebuild=1;
  802: 	}
  803:     }
  804:     return $rebuild;
  805: }
  806: 
  807: # looks for /> on start tags
  808: sub rebuild_tag {
  809:     my ($token) = @_;
  810:     my $result;
  811:     if ($token->[0] eq 'S') {
  812: 	$result = '<'.$token->[1];
  813: 	foreach my $attribute (@{ $token->[3] }) {
  814: 	    my $value = $token->[2]{$attribute};
  815: 	    next if ($value eq '');
  816: 	    $value =~s/^\s+|\s+$//g;
  817: 	    $value =~s/\"//g;
  818: 	    &Apache::lonxml::debug("setting :$attribute: to  :$value:");
  819: 	    $result.=' '.$attribute.'="'.$value.'"';
  820: 	}
  821: 	if ($token->[4] =~ m:/>$:) {
  822: 	    $result.=' />';
  823: 	} else {
  824: 	    $result.='>';
  825: 	}
  826:     } elsif ( $token->[0] eq 'E' ) {
  827: 	$result = '</'.$token->[1].'>';
  828:     }
  829:     return $result;
  830: }
  831: 
  832: sub html_element_name {
  833:     my ($name) = @_;
  834:     return $name.'_'.$Apache::lonxml::curdepth;
  835: }
  836: 
  837: sub hidden_arg {
  838:     my ($name,$token) = @_;
  839:     my $result;
  840:     my $arg=$token->[2]{$name};
  841:     $result='<input name="'.&html_element_name($name).
  842: 	'" type="hidden" value="'.$arg.'" />';
  843:     return $result;
  844: }
  845: 
  846: sub checked_arg {
  847:     my ($description,$name,$list,$token) = @_;
  848:     my $result;
  849:     my $optionlist="";
  850:     my $allselected=$token->[2]{$name};
  851:     $result=&mt($description);
  852:     foreach my $option (@$list) {
  853: 	my ($value,$text);
  854: 	if ( ref($option) eq 'ARRAY') {
  855: 	    $value='value="'.$$option[0].'"';
  856: 	    $text=$$option[1];
  857: 	    $option=$$option[0];
  858: 	} else {
  859: 	    $text=$option;
  860: 	    $value='value="'.$option.'"';
  861: 	}
  862: 	$result.='<span class="LC_nobreak"><label><input type="checkbox" '.$value.' name="'.
  863: 	    &html_element_name($name).'"';
  864: 	foreach my $selected (split(/,/,$allselected)) {
  865: 	    if ( $selected eq $option ) {
  866: 		$result.=' checked="checked" ';
  867: 		last;
  868: 	    }
  869: 	}
  870: 	$result.=&element_change_detection().' />'.$text.'</label></span>'."\n";
  871:     }
  872:     return $result;
  873: }
  874: 
  875: sub text_arg {
  876:     my ($description,$name,$token,$size) = @_;
  877:     my $result;
  878:     if (!defined $size) { $size=20; }
  879:     my $arg=$token->[2]{$name};
  880:     $result=&mt($description).'&nbsp;<input name="'.&html_element_name($name).
  881: 	'" type="text" value="'.$arg.'" size="'.$size.'" '.
  882: 	&element_change_detection().'/>';
  883:     return '<span class="LC_nobreak">'.$result.'</span>';
  884: }
  885: 
  886: sub select_arg {
  887:     my ($description,$name,$list,$token) = @_;
  888:     my $result;
  889:     my $optionlist="";
  890:     my $selected=$token->[2]{$name};
  891:     if (ref($list) eq 'ARRAY') {
  892:         foreach my $option (@{$list}) {
  893: 	    my ($text,$value);
  894: 	    if (ref($option) eq 'ARRAY') {
  895: 	        $value='value="'.&HTML::Entities::encode($option->[0]).'"';
  896: 	        $text=$option->[1];
  897: 	        $option=$option->[0];
  898: 	    } else {
  899: 	        $text=$option;
  900: 	        $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
  901: 	    }
  902: 	    if ( $selected eq $option ) {
  903: 	        $optionlist.="<option $value selected=\"selected\">".&mt($text)."</option>\n";
  904: 	    } else {
  905: 	        $optionlist.="<option $value >".&mt($text)."</option>\n";
  906: 	    }
  907:         }
  908:     }
  909:     $result.='<span class="LC_nobreak">'.&mt($description).'&nbsp;<select name="'.
  910: 	&html_element_name($name).'" '.&element_change_detection().' >
  911:        '.$optionlist.'
  912:       </select></span>';
  913:     return $result;
  914: }
  915: 
  916: sub select_or_text_arg {
  917:     my ($description,$name,$list,$token,$size) = @_;
  918:     my $result;
  919:     my $optionlist="";
  920:     my $found=0;
  921:     my $selected=$token->[2]{$name};
  922:     if (ref($list) eq 'ARRAY') {
  923:         foreach my $option (@{$list}) {
  924: 	    my ($text,$value);
  925: 	    if (ref($option) eq 'ARRAY') {
  926: 	        $value='value="'.&HTML::Entities::encode($option->[0]).'"';
  927: 	        $text=$option->[1];
  928: 	        $option=$option->[0];
  929: 	    } else {
  930: 	        $text=$option;
  931: 	        $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
  932: 	    }
  933: 	    if ( $selected eq $option ) {
  934: 	        $optionlist.="<option $value selected=\"selected\">$text</option>\n";
  935: 	        $found=1;
  936: 	    } else {
  937: 	        $optionlist.="<option $value>$text</option>\n";
  938: 	    }
  939:         }
  940:     }
  941:     $optionlist.="<option value=\"TYPEDINVALUE\"".
  942:  	((!$found)?' selected="selected"':'').
  943:  	">".&mt('Type-in value')."</option>\n";
  944: #
  945:     my $change_code=&element_change_detection();
  946:     my $element=&html_element_name($name);
  947:     my $selectelement='select_list_'.$element;
  948:     my $typeinelement='type_in_'.$element;
  949:     my $typeinvalue=($found?'':$selected);
  950: #
  951:     my $hiddenvalue='this.form.'.$element.'.value';
  952:     my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
  953:     my $selectedvalue='this.form.'.$selectelement.
  954: 	     '.options['.$selectedindex.'].value';
  955:     my $typedinvalue='this.form.'.$typeinelement.'.value';
  956:     my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
  957:     $description=&mt($description);
  958: #
  959:     return (<<ENDSELECTORTYPE);
  960: <span class="LC_nobreak">
  961: $description
  962: &nbsp;<select name="$selectelement"
  963: onChange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }" >
  964: $optionlist
  965: </select>
  966: <input type="text" size="$size" name="$typeinelement"
  967:        value="$typeinvalue" 
  968: onChange="$hiddenvalue=$typedinvalue;"
  969: onFocus="$selectedindex=$selecttypeinindex-1;" />
  970: <input type="hidden" name="$element" value="$selected" $change_code />
  971: </span>
  972: ENDSELECTORTYPE
  973: }
  974: 
  975: #----------------------------------------------------- image coordinates
  976: # single image coordinates, x, y 
  977: sub entercoords {
  978:     my ($idx,$idy,$mode,$width,$height) = @_;
  979:     unless ($Apache::edit::bgimgsrc) { return ''; }
  980:     if ($idx) { $idx.='_'; }
  981:     if ($idy) { $idy.='_'; }
  982:     my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
  983:     my $form    = 'lonhomework';
  984:     my $element;
  985:     if (! defined($mode) || $mode eq 'attribute') {
  986:         $element = &escape("$Apache::lonxml::curdepth");
  987:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  988:         $element = &escape('homework_edit_'.
  989:                                            $Apache::lonxml::curdepth);
  990:     }
  991:     my $id=$Apache::lonxml::curdepth;
  992:     my %data=("imagechoice.$id.type"      =>'point',
  993: 	      "imagechoice.$id.formname"  =>$form,
  994: 	      "imagechoice.$id.formx"     =>"$idx$element",
  995: 	      "imagechoice.$id.formy"     =>"$idy$element",
  996: 	      "imagechoice.$id.file"      =>$bgfile,
  997: 	      "imagechoice.$id.formcoord" =>$element);
  998:     if ($height) {
  999: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
 1000: 	    $Apache::edit::bgimgsrccurdepth;
 1001:     }
 1002:     if ($width) {
 1003: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
 1004: 	    $Apache::edit::bgimgsrccurdepth;
 1005:     }
 1006:     &Apache::lonnet::appenv(\%data);
 1007:     my $text="Click Coordinates";
 1008:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
 1009:     return $result;
 1010: }
 1011: 
 1012: # coordinates (x1,y1)-(x2,y2)...
 1013: # mode can be either box, or polygon
 1014: sub entercoord {
 1015:     my ($idx,$mode,$width,$height,$type) = @_;
 1016:     unless ($Apache::edit::bgimgsrc) { return ''; }
 1017:     my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
 1018:     my $form    = 'lonhomework';
 1019:     my $element;
 1020:     if (! defined($mode) || $mode eq 'attribute') {
 1021:         $element = &escape("$idx\_$Apache::lonxml::curdepth");
 1022:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
 1023:         $element = &escape('homework_edit_'.
 1024:                                            $Apache::lonxml::curdepth);
 1025:     }
 1026:     my $id=$Apache::lonxml::curdepth;
 1027:     my %data=("imagechoice.$id.type"      =>$type,
 1028: 	      "imagechoice.$id.formname"  =>$form,
 1029: 	      "imagechoice.$id.file"      =>$bgfile,
 1030: 	      "imagechoice.$id.formcoord" =>$element);
 1031:     if ($height) {
 1032: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
 1033: 	    $Apache::edit::bgimgsrccurdepth;
 1034:     }
 1035:     if ($width) {
 1036: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
 1037: 	    $Apache::edit::bgimgsrccurdepth;
 1038:     }
 1039:     &Apache::lonnet::appenv(\%data);
 1040:     my $text="Enter Coordinates";
 1041:     if ($type eq 'polygon') { $text='Create Polygon Data'; }
 1042:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
 1043:     return $result;
 1044: }
 1045: 
 1046: sub deletecoorddata {
 1047:     &Apache::lonnet::delenv('imagechoice.');
 1048: }
 1049: 
 1050: #----------------------------------------------------- browse
 1051: sub browse {
 1052:     # insert a link to call up the filesystem browser (lonindexer)
 1053:     my ($id, $mode, $titleid, $only) = @_;
 1054:     my $form    = 'lonhomework';
 1055:     my $element;
 1056:     if (! defined($mode) || $mode eq 'attribute') {
 1057:         $element = &escape("$id\_$Apache::lonxml::curdepth");
 1058:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
 1059:         $element = &escape('homework_edit_'.
 1060:                                            $Apache::lonxml::curdepth);	
 1061:     }
 1062:     my $titleelement;
 1063:     if ($titleid) {
 1064: 	$titleelement=",'$only','','".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
 1065:     } else {
 1066:         $titleelement=",'$only'";
 1067:     }
 1068:     my $result = <<"ENDBUTTON";
 1069: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
 1070: ENDBUTTON
 1071:     return $result;
 1072: }
 1073: 
 1074: #----------------------------------------------------- browse
 1075: sub search {
 1076:     # insert a link to call up the filesystem browser (lonindexer)
 1077:     my ($id, $mode, $titleid) = @_;
 1078:     my $form    = 'lonhomework';
 1079:     my $element;
 1080:     if (! defined($mode) || $mode eq 'attribute') {
 1081:         $element = &escape("$id\_$Apache::lonxml::curdepth");
 1082:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
 1083:         $element = &escape('homework_edit_'.
 1084:                                            $Apache::lonxml::curdepth);
 1085:     }
 1086:     my $titleelement;
 1087:     if ($titleid) {
 1088: 	$titleelement=",'".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
 1089:     }
 1090:     my $result = <<"ENDBUTTON";
 1091: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>
 1092: ENDBUTTON
 1093:     return $result;
 1094: }
 1095: 
 1096: 
 1097: 1;
 1098: __END__
 1099: 
 1100: 

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