File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.131: download - view: text, annotated - select for diffs
Sun Oct 10 00:14:15 2010 UTC (13 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Colorful editor for the existing functionplotresponse tags.

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

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