File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.129: download - view: text, annotated - select for diffs
Mon Sep 27 22:15:32 2010 UTC (13 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Saving work on functionplotresponse with GeoGebra

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

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