File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.136: download - view: text, annotated - select for diffs
Fri Nov 18 16:39:22 2011 UTC (12 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Work in progress: objects and vectors

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

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