File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.143: download - view: text, annotated - select for diffs
Thu Apr 25 17:58:32 2013 UTC (11 years ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Improved spacing in colorful editor:
- More and consistent spacing between options
- Minimize missunderstanding to which element a help icon belongs to
- Less needed total window width in editor

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

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