File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.123.4.1: download - view: text, annotated - select for diffs
Sun Jan 31 00:47:41 2010 UTC (14 years, 3 months ago) by raeburn
Branches: version_2_9_X
CVS tags: version_2_9_1, version_2_9_0, version_2_8_99_1
Diff to branchpoint 1.123: preferred, unified
- Backport 1.125.

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

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