File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.145: download - view: text, annotated - select for diffs
Tue Jun 25 15:07:30 2013 UTC (10 years, 11 months ago) by kruse
Branches: MAIN
CVS tags: HEAD
Added "external reponse" to "insert" dropdown-list in colourful-editor

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

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