File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.138: download - view: text, annotated - select for diffs
Tue Nov 22 02:13:37 2011 UTC (12 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Custom function plot rule

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

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