File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.130: download - view: text, annotated - select for diffs
Sat Oct 9 22:31:49 2010 UTC (13 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Recognize previous tries
Work on colorful editor

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

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