Annotation of loncom/homework/edit.pm, revision 1.98

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # edit mode helpers
1.25      albertel    3: #
1.98    ! albertel    4: # $Id: edit.pm,v 1.97 2005/12/01 18:46:31 albertel Exp $
1.25      albertel    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: #
1.82      www        28: 
1.1       albertel   29: package Apache::edit; 
                     30: 
                     31: use strict;
1.92      albertel   32: use Apache::lonnet;
1.32      albertel   33: use HTML::Entities();
1.71      www        34: use Apache::lonlocal;
1.1       albertel   35: 
1.10      albertel   36: # Global Vars
                     37: # default list of colors to use in editing
                     38: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
                     39: # depth of nesting of edit
                     40: $Apache::edit::colordepth=0;
1.38      www        41: @Apache::edit::inserttag=();
1.49      www        42: # image-type responses: active background image and curdepth at definition
                     43: $Apache::edit::bgimgsrc='';
                     44: $Apache::edit::bgimgsrccurdepth='';
1.10      albertel   45: 
                     46: sub initialize_edit {
1.63      albertel   47:     $Apache::edit::colordepth=0;
                     48:     @Apache::edit::inserttag=();
1.10      albertel   49: }
                     50: 
1.1       albertel   51: sub tag_start {
1.63      albertel   52:     my ($target,$token,$description) = @_;
                     53:     my $result='';
                     54:     if ($target eq "edit") {
                     55: 	my $tag=$token->[1];
                     56: 	if (!$description) {
                     57: 	    $description=&Apache::lonxml::description($token);
                     58: 	    if (!$description) { $description="<$tag>"; }
                     59: 	}
                     60: 	$result.= &start_table($token)."<tr><td>$description</td>
                     61:                       <td>Delete".
                     62: 		      &deletelist($target,$token)
                     63: 		      ."</td>
                     64:                        <td>".
                     65: 		       &insertlist($target,$token);
1.27      matthew    66: #<td>". 
1.22      albertel   67: #  &movebuttons($target,$token).
                     68: #    "</tr><tr><td colspan=\"3\">\n";
1.66      matthew    69: 	my @help = Apache::lonxml::helpinfo($token);
1.63      albertel   70: 	if ($help[0]) {
1.88      albertel   71: 	    $result .= '</td><td align="right" valign="top">' .
                     72: 		Apache::loncommon::help_open_topic(@help);
                     73: 	} else { $result .= "</td><td>&nbsp;"; }
1.63      albertel   74: 	$result .= &end_row().&start_spanning_row();
                     75:     }
                     76:     return $result;
1.1       albertel   77: }
                     78: 
                     79: sub tag_end {
1.63      albertel   80:     my ($target,$token,$description) = @_;
                     81:     my $result='';
                     82:     if ($target eq 'edit') {
                     83: 	$result.="</td></tr>".&end_table()."\n";
                     84:     }
                     85:     return $result;
1.4       albertel   86: }
1.1       albertel   87: 
1.10      albertel   88: sub start_table {
1.63      albertel   89:     my ($token)=@_;
                     90:     my $tag = $token->[1];
                     91:     my $tagnum;
                     92:     foreach my $namespace (reverse @Apache::lonxml::namespace) {
                     93: 	my $testtag=$namespace.'::'.$tag;
                     94: 	$tagnum=$Apache::lonxml::insertlist{"$testtag.num"};
1.95      albertel   95: 	&Apache::lonxml::debug(" $testtag ");
1.63      albertel   96: 	if (defined($tagnum)) { last; }
                     97:     }
                     98:     if (!defined ($tagnum)) {$tagnum=$Apache::lonxml::insertlist{"$tag.num"};}
                     99:     my $color = $Apache::lonxml::insertlist{"$tagnum.color"};
1.95      albertel  100:     &Apache::lonxml::debug(" $tagnum -- $color");
1.63      albertel  101:     if (!defined($color)) {
                    102: 	$color = $Apache::edit::colorlist[$Apache::edit::colordepth];
                    103:     }
                    104:     $Apache::edit::colordepth++;
                    105:     push(@Apache::edit::inserttag,$token->[1]);
                    106:     my $result='<div align="right">';
                    107:     $result.='<table bgcolor="'.$color.'" width="97%" border="0" cellspacing="5" cellpadding="3">';
                    108:     return $result;
1.10      albertel  109: }
                    110: 
                    111: sub end_table {
1.63      albertel  112:     $Apache::edit::colordepth--;
                    113:     my $result='</table></div>';
1.94      albertel  114:     $result.='<div align="left"><table><tr><td>';
1.63      albertel  115: 
                    116:     my ($tagname,$closingtag);
                    117:     if (defined($Apache::edit::inserttag[-2])) {
                    118: 	$tagname=$Apache::edit::inserttag[-2];
                    119:     } else {$tagname='problem';}
                    120:     if (defined($Apache::edit::inserttag[-1])) {
                    121: 	$closingtag=$Apache::edit::inserttag[-1];
                    122:     }
                    123:     $result.=&innerinsertlist('edit',$tagname,$closingtag).
1.94      albertel  124: 	"</td></tr></table></div>";
1.63      albertel  125:     pop(@Apache::edit::inserttag);
                    126:     return $result;
1.10      albertel  127: }
                    128: 
1.58      bowersj2  129: sub start_spanning_row { return '<tr><td colspan="4" bgcolor="#DDDDDD">';}
1.41      www       130: sub start_row          { return '<tr><td bgcolor="#DDDDDD">';            }
1.27      matthew   131: sub end_row            { return '</td></tr>';          }
                    132: 
1.22      albertel  133: sub movebuttons {
1.63      albertel  134:     my ($target,$token) = @_;
                    135:     my $result='<input type="submit" name="moveup.'.
                    136: 	$Apache::lonxml::curdepth.'" value="Move Up" />';
                    137:     $result.='<input type="submit" name="movedown.'.
                    138: 	$Apache::lonxml::curdepth.'" value="Move Down" />';
                    139:     return $result;
1.22      albertel  140: }
                    141: 
1.8       albertel  142: sub deletelist {
1.63      albertel  143:     my ($target,$token) = @_;
                    144:     my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
1.14      albertel  145: <option></option>
                    146: <option>Yes</option>
1.8       albertel  147: </select>";
1.63      albertel  148:     return $result;
1.8       albertel  149: }
                    150: 
1.14      albertel  151: sub handle_delete {
1.92      albertel  152:     if (!$env{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
1.63      albertel  153:     my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    154:     my $result=0;
                    155:     if ($space) {
                    156: 	my $sub1="$space\:\:delete_$token->[1]";
                    157: 	{
                    158: 	    no strict 'refs';
                    159: 	    if (defined &$sub1) {
                    160: 		$result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
                    161: 	    }
                    162: 	}
                    163:     }
                    164:     if (!$result) {
                    165: 	my $endtag='/'.$token->[1];
1.97      albertel  166: 	my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser,$style);
1.63      albertel  167: 	$$parser['-1']->get_token();
                    168: 	&Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
                    169: 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
                    170:     }
                    171:     return 1;
1.14      albertel  172: }
                    173: 
1.7       albertel  174: sub get_insert_list {
1.63      albertel  175:     my ($tagname) = @_;
                    176:     my $result='';
                    177:     my @tagnums= ();
                    178:     #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
                    179:     if ($Apache::lonxml::insertlist{"$tagname.which"}) {
                    180: 	push (@tagnums, @{ $Apache::lonxml::insertlist{"$tagname.which"} });
                    181:     }
                    182:     foreach my $namespace (@Apache::lonxml::namespace) {
                    183: 	if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) {
                    184: 	    push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} });
                    185: 	}
                    186:     }
                    187:     if (@tagnums) {
                    188: 	my %options;
                    189: 	foreach my $tagnum (@tagnums) {
                    190: 	    my $descrip=$Apache::lonxml::insertlist{"$tagnum.description"};
                    191: 	    $options{$descrip} ="<option value=\"$tagnum\">".
                    192: 		$descrip."</option>\n";
                    193: 	}
                    194: 	foreach my $option (sort(keys(%options))) {$result.=$options{$option};}
1.88      albertel  195: 	if ($result) { $result='<option selected="selected"></option>'.$result; }
1.63      albertel  196:     }
                    197:     return $result;
1.5       albertel  198: }
                    199: 
1.4       albertel  200: sub insertlist {
1.63      albertel  201:     my ($target,$token) = @_;
                    202:     return &innerinsertlist($target,$token->[1]);
1.38      www       203: }
                    204: 
                    205: sub innerinsertlist {
1.63      albertel  206:     my ($target,$tagname,$closingtag) = @_;
                    207:     my $result;
                    208:     my $after='';
                    209:     if ($closingtag) {
                    210: 	$after='_after_'.$closingtag; 
                    211:     }
                    212:     if ($target eq 'edit') {
                    213: 	my $optionlist= &get_insert_list($tagname);
                    214: 	if ($optionlist) {
                    215: 	    $result = "Insert:
                    216:             <select name=\"insert$after\_$Apache::lonxml::curdepth\">
                    217:                   $optionlist
                    218:             </select>"
                    219: 	} else {
                    220: 	    $result="&nbsp;";
                    221: 	}
1.6       albertel  222:     }
1.63      albertel  223:     return $result;
1.6       albertel  224: }
                    225: 
1.7       albertel  226: sub handle_insert {
1.92      albertel  227:     if ($env{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
1.63      albertel  228:     my $result;
1.92      albertel  229:     my $tagnum = $env{"form.insert_$Apache::lonxml::curdepth"};
1.63      albertel  230:     my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
                    231:     if ($func eq 'default') {
                    232: 	my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
                    233: 	my $namespace;
                    234: 	if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
                    235: 	$result.="\n<$newtag>\n</$newtag>";
1.15      albertel  236:     } else {
1.63      albertel  237: 	if (defined(&$func)) {
                    238: 	    {
                    239: 		no strict 'refs';
                    240: 		$result.=&$func();
                    241: 	    }
                    242: 	} else {
                    243: 	    my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
1.91      albertel  244: 	    &Apache::lonxml::error("Unable to insert tag ".$Apache::lonxml::curdepth." ($tagnum) $newtag, func was not defined.");
1.63      albertel  245: 	}
1.38      www       246:     }
1.63      albertel  247:     return $result;
1.38      www       248: }
                    249: 
                    250: sub handle_insertafter {
1.63      albertel  251:     my $tagname=shift;
1.92      albertel  252:     if ($env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '')
1.63      albertel  253:     { return ''; }
                    254:     my $result;
1.92      albertel  255:     my $tagnum =$env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"};
1.63      albertel  256:     my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
                    257:     if ($func eq 'default') {
                    258: 	my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
                    259: 	my $namespace;
                    260: 	if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
                    261: 	$result.="\n<$newtag>\n</$newtag>";
1.38      www       262:     } else {
1.63      albertel  263: 	if (defined(&$func)) {
                    264: 	    {
                    265: 		no strict 'refs';
                    266: 		$result.=&$func();
                    267: 	    }
                    268: 	} else {
                    269: 	    my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
                    270: 	    &Apache::lonxml::error("Unable to insert (after) tag $newtag, $func was not defined. ($tagname $tagnum)");
                    271: 	}
1.5       albertel  272:     }
1.63      albertel  273:     return $result;
1.69      albertel  274: }
                    275: 
                    276: sub insert_img {
                    277:     return '
                    278:     <img />';
1.16      albertel  279: }
                    280: 
                    281: sub insert_responseparam {
1.63      albertel  282:     return '
1.16      albertel  283:     <responseparam />';
1.5       albertel  284: }
                    285: 
1.87      albertel  286: sub insert_parameter {
                    287:     return '
                    288:     <parameter />';
                    289: }
                    290: 
1.24      albertel  291: sub insert_formularesponse {
1.63      albertel  292:     return '
1.24      albertel  293: <formularesponse answer="" samples="">
1.86      albertel  294:     <responseparam description="Numerical Tolerance" type="tolerance" default="0.00001" name="tol" />
                    295:     <textline size="25"/>
1.24      albertel  296:     <hintgroup>
1.78      www       297:     <startouttext /><endouttext />
1.24      albertel  298:     </hintgroup>
                    299: </formularesponse>';
                    300: }
                    301: 
1.15      albertel  302: sub insert_numericalresponse {
1.63      albertel  303:     return '
1.15      albertel  304: <numericalresponse answer="">
1.90      www       305: <responseparam type="tolerance" default="5%" name="tol" description="Numerical Tolerance" />
                    306: <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />
1.15      albertel  307:     <textline />
                    308:     <hintgroup>
1.78      www       309:     <startouttext /><endouttext />
1.15      albertel  310:     </hintgroup>
                    311: </numericalresponse>';
                    312: }
                    313: 
1.96      albertel  314: sub insert_customresponse {
1.95      albertel  315:     return '
1.96      albertel  316: <customresponse>
1.95      albertel  317:     <answer type="loncapa/perl">
                    318:     </answer>
                    319:     <textline />
                    320:     <hintgroup>
                    321:     <startouttext /><endouttext />
                    322:     </hintgroup>
1.96      albertel  323: </customresponse>';
1.95      albertel  324: }
                    325: 
1.96      albertel  326: sub insert_customresponse_answer {
1.95      albertel  327:     return '
                    328:     <answer type="loncapa/perl">
                    329:     </answer>
                    330: ';
                    331: }
                    332: 
1.18      albertel  333: sub insert_stringresponse {
1.63      albertel  334:     return '
1.18      albertel  335: <stringresponse answer="" type="">
                    336:     <textline />
                    337:     <hintgroup>
1.78      www       338:     <startouttext /><endouttext />
1.18      albertel  339:     </hintgroup>
                    340: </stringresponse>';
1.36      albertel  341: }
                    342: 
                    343: sub insert_essayresponse {
1.63      albertel  344:     return '
1.36      albertel  345: <essayresponse>
                    346:     <textfield></textfield>
                    347: </essayresponse>';
1.54      albertel  348: }
                    349: 
                    350: sub insert_imageresponse {
1.63      albertel  351:     return '
1.54      albertel  352: <imageresponse max="1">
                    353:     <foilgroup>
1.89      albertel  354:       <foil>
                    355:       </foil>
1.54      albertel  356:     </foilgroup>
                    357:     <hintgroup>
1.78      www       358:     <startouttext /><endouttext />
1.54      albertel  359:     </hintgroup>
                    360: </imageresponse>';
1.18      albertel  361: }
                    362: 
1.7       albertel  363: sub insert_optionresponse {
1.63      albertel  364:     return '
1.7       albertel  365: <optionresponse max="10">
                    366:     <foilgroup options="">
1.89      albertel  367:       <foil>
                    368:          <startouttext /><endouttext />
                    369:       </foil>
1.7       albertel  370:     </foilgroup>
1.14      albertel  371:     <hintgroup>
1.78      www       372:     <startouttext /><endouttext />
1.14      albertel  373:     </hintgroup>
1.7       albertel  374: </optionresponse>';
1.1       albertel  375: }
                    376: 
1.72      albertel  377: sub insert_organicresponse {
                    378:     return '
                    379: <organicresponse>
                    380:     <textline />
                    381:     <hintgroup>
1.78      www       382:     <startouttext /><endouttext />
1.72      albertel  383:     </hintgroup>
                    384: </organicresponse>';
                    385: }
                    386: 
                    387: sub insert_organicstructure {
                    388:     return '
                    389: <organicstructure />
                    390: ';
                    391: }
                    392: 
1.23      albertel  393: sub insert_radiobuttonresponse {
1.63      albertel  394:     return '
1.23      albertel  395: <radiobuttonresponse max="10">
                    396:     <foilgroup>
1.89      albertel  397:       <foil>
                    398:          <startouttext /><endouttext />
                    399:       </foil>
1.23      albertel  400:     </foilgroup>
                    401:     <hintgroup>
1.78      www       402:     <startouttext /><endouttext />
1.23      albertel  403:     </hintgroup>
                    404: </radiobuttonresponse>';
1.72      albertel  405: }
                    406: 
                    407: sub insert_reactionresponse {
                    408:     return '
                    409: <reactionresponse>
                    410:     <textline />
                    411:     <hintgroup>
1.78      www       412:     <startouttext /><endouttext />
1.72      albertel  413:     </hintgroup>
                    414: </reactionresponse>';
1.43      albertel  415: }
                    416: 
                    417: sub insert_rankresponse {
1.63      albertel  418:     return '
1.43      albertel  419: <rankresponse max="10">
                    420:     <foilgroup options="">
1.89      albertel  421:       <foil>
                    422:          <startouttext /><endouttext />
                    423:       </foil>
1.43      albertel  424:     </foilgroup>
                    425:     <hintgroup>
1.78      www       426:     <startouttext /><endouttext />
1.43      albertel  427:     </hintgroup>
                    428: </rankresponse>';
1.23      albertel  429: }
                    430: 
1.44      albertel  431: sub insert_matchresponse {
1.63      albertel  432:     return '
1.44      albertel  433: <matchresponse max="10">
                    434:     <foilgroup options="">
                    435:       <itemgroup>
                    436:       </itemgroup>
1.89      albertel  437:       <foil>
                    438:          <startouttext /><endouttext />
                    439:       </foil>
1.44      albertel  440:     </foilgroup>
                    441:     <hintgroup>
1.78      www       442:     <startouttext /><endouttext />
1.44      albertel  443:     </hintgroup>
                    444: </matchresponse>';
                    445: }
                    446: 
1.21      albertel  447: sub insert_displayduedate { return '<displayduedate />'; }
                    448: sub insert_displaytitle   { return '<displaytitle />'; }
1.22      albertel  449: sub insert_hintpart {
1.63      albertel  450:     return '
1.22      albertel  451: <hintpart on="default">
1.90      www       452:     <startouttext/><endouttext />
1.22      albertel  453: </hintpart>';
1.67      albertel  454: }
                    455: 
                    456: sub insert_hintgroup {
                    457:   return '
                    458: <hintgroup>
1.78      www       459:     <startouttext /><endouttext />
1.67      albertel  460: </hintgroup>';
1.22      albertel  461: }
                    462: 
                    463: sub insert_numericalhint {
1.63      albertel  464:     return '
1.22      albertel  465: <numericalhint>
                    466: </numericalhint>';
1.46      albertel  467: }
                    468: 
                    469: sub insert_stringhint {
1.63      albertel  470:     return '
1.46      albertel  471: <stringhint>
                    472: </stringhint>';
                    473: }
                    474: 
                    475: sub insert_formulahint {
1.63      albertel  476:     return '
1.46      albertel  477: <formulahint>
                    478: </formulahint>';
1.37      albertel  479: }
                    480: 
                    481: sub insert_radiobuttonhint {
1.63      albertel  482:     return '
1.37      albertel  483: <radiobuttonhint>
                    484: </radiobuttonhint>';
1.50      albertel  485: }
                    486: 
                    487: sub insert_optionhint {
1.63      albertel  488:     return '
1.50      albertel  489: <optionhint>
                    490: </optionhint>';
1.22      albertel  491: }
1.21      albertel  492: 
1.23      albertel  493: sub insert_startouttext {
1.78      www       494:     return "<startouttext /><endouttext />";
1.23      albertel  495: }
                    496: 
                    497: sub insert_script {
1.78      www       498:     return "\n<script type=\"loncapa/perl\"></script>";
1.23      albertel  499: }
                    500: 
1.98    ! albertel  501: sub js_change_detection {
        !           502:     my $unsaved=&mt("There are unsaved changes");
        !           503:     return (<<SCRIPT);
        !           504: <script type="text/javascript">
        !           505: var clean = true;
        !           506: var is_submit = false;
        !           507: function compareForm(event_) {
        !           508:         if (!event_ && window.event) {
        !           509:           event_ = window.event;
        !           510:         }
        !           511: 	if (!is_submit && !clean) {
        !           512:             event_.returnValue = "$unsaved";
        !           513:             return "$unsaved";
        !           514:         }
        !           515: }
        !           516: function unClean() {
        !           517:      clean=false;
        !           518: }
        !           519: window.onbeforeunload = compareForm;
        !           520: </script>
        !           521: SCRIPT
        !           522: }
        !           523: 
        !           524: sub form_change_detection {
        !           525:     return ' onsubmit="is_submit=true;" ';
        !           526: }
        !           527: 
        !           528: sub element_change_detection {
        !           529:     return ' onchange="unClean();" ';
        !           530: }
        !           531: 
1.25      albertel  532: sub textarea_sizes {
1.63      albertel  533:     my ($data)=@_;
                    534:     my $count=0;
                    535:     my $maxlength=-1;
                    536:     foreach (split ("\n", $$data)) {
                    537: 	$count+=int(length($_)/79);
                    538: 	$count++;
                    539: 	if (length($_) > $maxlength) { $maxlength = length($_); }
                    540:     }
                    541:     my $rows = $count;
                    542:     my $cols = $maxlength;
                    543:     return ($rows,$cols);
1.25      albertel  544: }
                    545: 
1.32      albertel  546: sub editline {
1.31      matthew   547:     my ($tag,$data,$description,$size)=@_;
1.81      albertel  548:     $data=&HTML::Entities::encode($data,'<>&"');
1.31      matthew   549:     if ($description) { $description="<br />".$description."<br />"; }
1.98    ! albertel  550:     my $change_code = &element_change_detection();
1.31      matthew   551:     my $result = <<"END";
                    552: $description
                    553: <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
1.98    ! albertel  554:        value="$data" size="$size" $change_code />
1.31      matthew   555: END
                    556:     return $result;
                    557: }
                    558: 
1.2       albertel  559: sub editfield {
1.82      www       560:     my ($tag,$data,$description,$minwidth,$minheight,$usehtmlarea)=@_;
1.22      albertel  561: 
1.63      albertel  562:     my ($rows,$cols)=&textarea_sizes(\$data);
1.84      www       563:     if (&Apache::lonhtmlcommon::htmlareabrowser() &&
                    564: 	!&Apache::lonhtmlcommon::htmlareablocked()) {
                    565: 	$rows+=7;      # make room for HTMLarea
                    566: 	$minheight+=7; # make room for HTMLarea
                    567:     }
1.63      albertel  568:     if ($cols > 80) { $cols = 80; }
                    569:     if ($cols < $minwidth ) { $cols = $minwidth; }
                    570:     if ($rows < $minheight) { $rows = $minheight; }
                    571:     if ($description) { $description="<br />".$description."<br />"; }
1.82      www       572:     if ($usehtmlarea) {
                    573: 	push @Apache::lonxml::htmlareafields,'homework_edit_'.
                    574: 	    $Apache::lonxml::curdepth;
                    575:     }
1.91      albertel  576:     return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea style="width:100%" rows="'.$rows.
1.63      albertel  577: 	'" cols="'.$cols.'" name="homework_edit_'.
1.82      www       578: 	$Apache::lonxml::curdepth.'" id="homework_edit_'.
1.98    ! albertel  579: 	$Apache::lonxml::curdepth.'" '.&element_change_detection().'>'.
1.85      www       580: 	&HTML::Entities::encode($data,'<>&"').'</textarea>'.
                    581: 	($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
                    582: 			 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
1.2       albertel  583: }
                    584: 
                    585: sub modifiedfield {
1.70      albertel  586:     my ($endtag,$parser) = @_;
1.63      albertel  587:     my $result;
1.92      albertel  588: #  foreach my $envkey (sort keys %env) {
                    589: #    &Apache::lonxml::debug("$envkey ---- $env{$envkey}");
1.3       albertel  590: #  }
                    591: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
1.92      albertel  592: #  &Apache::lonxml::debug($env{"form.homework_edit_$Apache::lonxml::curdepth"});
                    593:     $result=$env{"form.homework_edit_$Apache::lonxml::curdepth"};
1.70      albertel  594:     my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
                    595:     # textareas throw away intial \n 
                    596:     if ($bodytext=~/^\n/) { $result="\n".$result; }
1.63      albertel  597:     return $result;
1.2       albertel  598: }
                    599: 
1.15      albertel  600: # Returns a 1 if the token has been modified and you should rebuild the tag
1.12      albertel  601: # side-effects, will modify the $token if new values are found
                    602: sub get_new_args {
1.61      albertel  603:     my ($token,$parstack,$safeeval,@args)=@_;
                    604:     my $rebuild=0;
                    605:     foreach my $arg (@args) {
1.63      albertel  606: 	#just want the string that it was set to
                    607: 	my $value=$token->[2]->{$arg};
                    608: 	my $element=&html_element_name($arg);
1.92      albertel  609: 	my $newvalue=$env{"form.$element"};
1.63      albertel  610: 	&Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
                    611: 	if (defined($newvalue) && $value ne $newvalue) {
                    612: 	    if (ref($newvalue) eq 'ARRAY') {
                    613: 		$token->[2]->{$arg}=join(',',@$newvalue);
                    614: 	    } else {
                    615: 		$token->[2]->{$arg}=$newvalue;
                    616: 	    }
1.79      albertel  617: 	    $rebuild=1;
                    618: 	} elsif (!defined($newvalue) && defined($value)) {
                    619: 	    delete($token->[2]->{$arg});
1.63      albertel  620: 	    $rebuild=1;
1.61      albertel  621: 	}
1.12      albertel  622:     }
1.63      albertel  623:     return $rebuild;
1.12      albertel  624: }
                    625: 
1.15      albertel  626: # looks for /> on start tags
1.12      albertel  627: sub rebuild_tag {
1.63      albertel  628:     my ($token) = @_;
                    629:     my $result;
                    630:     if ($token->[0] eq 'S') {
                    631: 	$result = '<'.$token->[1];
                    632: 	while (my ($key,$val)= each(%{$token->[2]})) {
                    633: 	    $val=~s:^\s+|\s+$::g;
                    634: 	    $val=~s:"::g; #"
                    635: 	    &Apache::lonxml::debug("setting :$key: to  :$val:");
                    636: 	    $result.=' '.$key.'="'.$val.'"';
                    637: 	}
                    638: 	if ($token->[4] =~ m:/>$:) {
                    639: 	    $result.=' />';
                    640: 	} else {
                    641: 	    $result.='>';
                    642: 	}
                    643:     } elsif ( $token->[0] eq 'E' ) {
                    644: 	$result = '</'.$token->[1].'>';
1.12      albertel  645:     }
1.63      albertel  646:     return $result;
1.12      albertel  647: }
1.13      albertel  648: 
1.47      matthew   649: sub html_element_name {
                    650:     my ($name) = @_;
1.48      albertel  651:     return $name.'_'.$Apache::lonxml::curdepth;
                    652: }
                    653: 
                    654: sub hidden_arg {
                    655:     my ($name,$token) = @_;
                    656:     my $result;
                    657:     my $arg=$token->[2]{$name};
                    658:     $result='<input name="'.&html_element_name($name).
                    659: 	'" type="hidden" value="'.$arg.'" />';
1.61      albertel  660:     return $result;
                    661: }
                    662: 
                    663: sub checked_arg {
                    664:     my ($description,$name,$list,$token) = @_;
                    665:     my $result;
                    666:     my $optionlist="";
                    667:     my $allselected=$token->[2]{$name};
1.71      www       668:     $result=&mt($description);
1.61      albertel  669:     foreach my $option (@$list) {
                    670: 	my ($value,$text);
                    671: 	if ( ref($option) eq 'ARRAY') {
                    672: 	    $value='value="'.$$option[0].'"';
                    673: 	    $text=$$option[1];
                    674: 	    $option=$$option[0];
                    675: 	} else {
                    676: 	    $text=$option;
                    677: 	    $value='value="'.$option.'"';
                    678: 	}
                    679: 	$result.="<nobr><input type='checkbox' $value name='".
                    680: 	    &html_element_name($name)."'";
                    681: 	foreach my $selected (split(/,/,$allselected)) {
                    682: 	    if ( $selected eq $option ) {
1.88      albertel  683: 		$result.=" checked='checked' ";
1.61      albertel  684: 		last;
                    685: 	    }
                    686: 	}
1.98    ! albertel  687: 	$result.=&element_change_detection()." />$text</nobr>\n";
1.61      albertel  688:     }
1.48      albertel  689:     return $result;
1.47      matthew   690: }
                    691: 
1.13      albertel  692: sub text_arg {
1.63      albertel  693:     my ($description,$name,$token,$size) = @_;
                    694:     my $result;
                    695:     if (!defined $size) { $size=20; }
                    696:     my $arg=$token->[2]{$name};
1.71      www       697:     $result=&mt($description).'&nbsp;<input name="'.&html_element_name($name).
1.98    ! albertel  698: 	'" type="text" value="'.$arg.'" size="'.$size.'" '.
        !           699: 	&element_change_detection().'/>';
1.63      albertel  700:     return '<nobr>'.$result.'</nobr>';
1.13      albertel  701: }
                    702: 
                    703: sub select_arg {
1.39      albertel  704:     my ($description,$name,$list,$token) = @_;
                    705:     my $result;
                    706:     my $optionlist="";
                    707:     my $selected=$token->[2]{$name};
                    708:     foreach my $option (@$list) {
1.64      albertel  709: 	my ($text,$value);
1.39      albertel  710: 	if ( ref($option) eq 'ARRAY') {
1.93      albertel  711: 	    $value='value="'.&HTML::Entities::encode($$option[0]).'"';
1.64      albertel  712: 	    $text=$$option[1];
                    713: 	    $option=$$option[0];
1.39      albertel  714: 	} else {
1.64      albertel  715: 	    $text=$option;
1.93      albertel  716: 	    $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
1.39      albertel  717: 	}
                    718: 	if ( $selected eq $option ) {
1.88      albertel  719: 	    $optionlist.="<option $value selected=\"selected\">$text</option>\n";
1.39      albertel  720: 	} else {
1.64      albertel  721: 	    $optionlist.="<option $value >$text</option>\n";
1.39      albertel  722: 	}
1.13      albertel  723:     }
1.57      albertel  724:     $result.='<nobr>'.$description.'&nbsp;<select name="'.
1.98    ! albertel  725: 	&html_element_name($name).'" '.&element_change_detection().' >
1.13      albertel  726:        '.$optionlist.'
1.57      albertel  727:       </select></nobr>';
1.39      albertel  728:     return $result;
1.13      albertel  729: }
                    730: 
1.19      albertel  731: sub select_or_text_arg {
1.39      albertel  732:     my ($description,$name,$list,$token,$size) = @_;
                    733:     my $result;
                    734:     my $optionlist="";
                    735:     my $found=0;
                    736:     my $selected=$token->[2]{$name};
                    737:     foreach my $option (@$list) {
1.64      albertel  738: 	my ($text,$value);
1.39      albertel  739: 	if ( ref($option) eq 'ARRAY') {
1.93      albertel  740: 	    $value='value="'.&HTML::Entities::encode($$option[0]).'"';
1.64      albertel  741: 	    $text=$$option[1];
                    742: 	    $option=$$option[0];
1.39      albertel  743: 	} else {
1.64      albertel  744: 	    $text=$option;
1.93      albertel  745: 	    $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
1.39      albertel  746: 	}
                    747: 	if ( $selected eq $option ) {
1.88      albertel  748: 	    $optionlist.="<option $value selected=\"selected\">$text</option>\n";
1.39      albertel  749: 	    $found=1;
                    750: 	} else {
1.64      albertel  751: 	    $optionlist.="<option $value>$text</option>\n";
1.39      albertel  752: 	}
                    753:     }
1.60      www       754:     $optionlist.="<option value=\"TYPEDINVALUE\"".
1.88      albertel  755:  	((!$found)?' selected="selected"':'').
1.73      www       756:  	">".&mt('Type-in value')."</option>\n";
1.60      www       757: #
1.98    ! albertel  758:     my $change_code=&element_change_detection();
1.60      www       759:     my $element=&html_element_name($name);
                    760:     my $selectelement='select_list_'.$element;
                    761:     my $typeinelement='type_in_'.$element;
                    762:     my $typeinvalue=($found?'':$selected);
                    763: #
                    764:     my $hiddenvalue='this.form.'.$element.'.value';
                    765:     my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
                    766:     my $selectedvalue='this.form.'.$selectelement.
                    767: 	     '.options['.$selectedindex.'].value';
                    768:     my $typedinvalue='this.form.'.$typeinelement.'.value';
                    769:     my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
1.71      www       770:     $description=&mt($description);
1.60      www       771: #
                    772:     return (<<ENDSELECTORTYPE);
                    773: <nobr>
                    774: $description
                    775: &nbsp;<select name="$selectelement"
1.74      albertel  776: onChange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }" >
1.60      www       777: $optionlist
                    778: </select>
                    779: <input type="text" size="$size" name="$typeinelement"
                    780:        value="$typeinvalue" 
                    781: onChange="$hiddenvalue=$typedinvalue;"
                    782: onFocus="$selectedindex=$selecttypeinindex-1;" />
1.98    ! albertel  783: <input type="hidden" name="$element" value="$selected" $change_code />
1.60      www       784: </nobr>
                    785: ENDSELECTORTYPE
1.19      albertel  786: }
1.29      matthew   787: 
1.49      www       788: #----------------------------------------------------- image coordinates
                    789: # single image coordinates, x, y 
                    790: sub entercoords {
1.80      albertel  791:     my ($idx,$idy,$mode,$width,$height) = @_;
1.49      www       792:     unless ($Apache::edit::bgimgsrc) { return ''; }
                    793:     if ($idx) { $idx.='_'; }
                    794:     if ($idy) { $idy.='_'; }
1.80      albertel  795:     my $bgfile=&Apache::lonnet::escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1.49      www       796:     my $form    = 'lonhomework';
                    797:     my $element;
                    798:     if (! defined($mode) || $mode eq 'attribute') {
                    799:         $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth");
                    800:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    801:         $element = &Apache::lonnet::escape('homework_edit_'.
                    802:                                            $Apache::lonxml::curdepth);
                    803:     }
1.80      albertel  804:     my $id=$Apache::lonxml::curdepth;
                    805:     my %data=("imagechoice.$id.type"      =>'point',
                    806: 	      "imagechoice.$id.formname"  =>$form,
                    807: 	      "imagechoice.$id.formx"     =>"$idx$element",
                    808: 	      "imagechoice.$id.formy"     =>"$idy$element",
                    809: 	      "imagechoice.$id.file"      =>$bgfile,
                    810: 	      "imagechoice.$id.formcoord" =>$element);
1.49      www       811:     if ($height) {
1.80      albertel  812: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
                    813: 	    $Apache::edit::bgimgsrccurdepth;
1.49      www       814:     }
                    815:     if ($width) {
1.80      albertel  816: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
                    817: 	    $Apache::edit::bgimgsrccurdepth;
1.49      www       818:     }
1.80      albertel  819:     &Apache::lonnet::appenv(%data);
                    820:     my $text="Click Coordinates";
                    821:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1.49      www       822:     return $result;
                    823: }
                    824: 
1.77      albertel  825: # coordinates (x1,y1)-(x2,y2)...
                    826: # mode can be either box, or polygon
                    827: sub entercoord {
                    828:     my ($idx,$mode,$width,$height,$type) = @_;
1.49      www       829:     unless ($Apache::edit::bgimgsrc) { return ''; }
1.76      albertel  830:     my $bgfile=&Apache::lonnet::escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1.75      albertel  831:     my $form    = 'lonhomework';
                    832:     my $element;
                    833:     if (! defined($mode) || $mode eq 'attribute') {
1.77      albertel  834:         $element = &Apache::lonnet::escape("$idx\_$Apache::lonxml::curdepth");
1.75      albertel  835:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    836:         $element = &Apache::lonnet::escape('homework_edit_'.
                    837:                                            $Apache::lonxml::curdepth);
                    838:     }
1.76      albertel  839:     my $id=$Apache::lonxml::curdepth;
1.77      albertel  840:     my %data=("imagechoice.$id.type"      =>$type,
1.76      albertel  841: 	      "imagechoice.$id.formname"  =>$form,
                    842: 	      "imagechoice.$id.file"      =>$bgfile,
                    843: 	      "imagechoice.$id.formcoord" =>$element);
1.75      albertel  844:     if ($height) {
1.76      albertel  845: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
                    846: 	    $Apache::edit::bgimgsrccurdepth;
1.75      albertel  847:     }
                    848:     if ($width) {
1.76      albertel  849: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
                    850: 	    $Apache::edit::bgimgsrccurdepth;
1.75      albertel  851:     }
                    852:     &Apache::lonnet::appenv(%data);
1.77      albertel  853:     my $text="Enter Coordinates";
                    854:     if ($type eq 'polygon') { $text='Create Polygon Data'; }
                    855:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1.49      www       856:     return $result;
                    857: }
1.76      albertel  858: 
                    859: sub deletecoorddata {
                    860:     &Apache::lonnet::delenv("imagechoice\\.");
                    861: }
                    862: 
1.29      matthew   863: #----------------------------------------------------- browse
                    864: sub browse {
                    865:     # insert a link to call up the filesystem browser (lonindexer)
1.68      albertel  866:     my ($id, $mode, $titleid) = @_;
1.29      matthew   867:     my $form    = 'lonhomework';
1.42      matthew   868:     my $element;
                    869:     if (! defined($mode) || $mode eq 'attribute') {
1.49      www       870:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
1.42      matthew   871:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    872:         $element = &Apache::lonnet::escape('homework_edit_'.
1.68      albertel  873:                                            $Apache::lonxml::curdepth);	
                    874:     }
                    875:     my $titleelement;
                    876:     if ($titleid) {
                    877: 	$titleelement=",'','','".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
1.42      matthew   878:     }
1.29      matthew   879:     my $result = <<"ENDBUTTON";
1.68      albertel  880: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
1.29      matthew   881: ENDBUTTON
                    882:     return $result;
                    883: }
                    884: 
1.30      matthew   885: #----------------------------------------------------- browse
                    886: sub search {
                    887:     # insert a link to call up the filesystem browser (lonindexer)
1.68      albertel  888:     my ($id, $mode, $titleid) = @_;
1.30      matthew   889:     my $form    = 'lonhomework';
1.49      www       890:     my $element;
                    891:     if (! defined($mode) || $mode eq 'attribute') {
                    892:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
                    893:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    894:         $element = &Apache::lonnet::escape('homework_edit_'.
                    895:                                            $Apache::lonxml::curdepth);
                    896:     }
1.68      albertel  897:     my $titleelement;
                    898:     if ($titleid) {
                    899: 	$titleelement=",'".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
                    900:     }
1.30      matthew   901:     my $result = <<"ENDBUTTON";
1.68      albertel  902: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>
1.30      matthew   903: ENDBUTTON
                    904:     return $result;
                    905: }
                    906: 
                    907: 
1.1       albertel  908: 1;
                    909: __END__
1.26      harris41  910: 
                    911: =head1 NAME
                    912: 
                    913: Apache::edit - edit mode helpers
                    914: 
                    915: =head1 SYNOPSIS
                    916: 
                    917: Invoked by many homework and xml related modules.
                    918: 
                    919:  &Apache::edit::SUBROUTINENAME(ARGUMENTS);
                    920: 
                    921: =head1 INTRODUCTION
                    922: 
                    923: This module outputs HTML syntax helpful for the rendering of edit
                    924: mode interfaces.
                    925: 
                    926: This is part of the LearningOnline Network with CAPA project
                    927: described at http://www.lon-capa.org.
                    928: 
                    929: =head1 HANDLER SUBROUTINE
                    930: 
                    931: There is no handler subroutine.
                    932: 
                    933: =head1 OTHER SUBROUTINES
                    934: 
                    935: =over 4
                    936: 
                    937: =item *
                    938: 
                    939: initialize_edit() : initialize edit (set colordepth to zero)
                    940: 
                    941: =item *
                    942: 
                    943: tag_start($target,$token,$description) : provide deletion and insertion lists
                    944: for the manipulation of a start tag; return a scalar string
                    945: 
                    946: =item *
                    947: 
                    948: tag_end($target,$token,$description) : ending syntax corresponding to
                    949: &tag_start. return a scalar string.
                    950: 
                    951: =item *
                    952: 
                    953: start_table($token) : start table; update colordepth; return scalar string.
                    954: 
                    955: =item *
                    956: 
                    957: end_table() : reduce color depth; end table; return scalar string
1.27      matthew   958: 
                    959: =item *
                    960: 
                    961: start_spanning_row() : start a new table row spanning the 'edit' environment.
                    962: 
                    963: =item *
                    964: 
                    965: start_row() : start a new table row and element. 
                    966: 
                    967: =item *
                    968: 
                    969: end_row() : end current table element and row.
1.26      harris41  970: 
                    971: =item *
                    972: 
                    973: movebuttons($target,$token) : move-up and move-down buttons; return scalar
                    974: string
                    975: 
                    976: =item *
                    977: 
                    978: deletelist($target,$token) : provide a yes option in an HTML select element;
                    979: return scalar string
                    980: 
                    981: =item *
                    982: 
                    983: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
                    984: $style) : respond to a user delete request by passing relevant stack
                    985: and array information to various rendering functions; return a scalar string
                    986: 
                    987: =item *
                    988: 
                    989: get_insert_list($token) : provide an insertion list based on possibilities
                    990: from lonxml; return a scalar string
                    991: 
                    992: =item *
                    993: 
                    994: insertlist($target,$token) : api that uses get_insert_list;
                    995: return a scalar string
                    996: 
                    997: =item *
                    998: 
                    999: handleinsert($token) : provide an insertion list based on possibilities
                   1000: from lonxml; return a scalar string
                   1001: 
                   1002: =item *
                   1003: 
                   1004: get_insert_list($token) : provide an insertion list based on possibilities
                   1005: from lonxml; return a scalar string
1.29      matthew  1006: 
                   1007: =item *
                   1008: browse($elementname) : provide a link which will open up the filesystem
                   1009: browser (lonindexer) and, once a file is selected, place the result in
1.30      matthew  1010: the form element $elementname.
                   1011: 
                   1012: =item *
                   1013: search($elementname) : provide a link which will open up the filesystem
                   1014: searcher (lonsearchcat) and, once a file is selected, place the result in
1.29      matthew  1015: the form element $elementname.
1.31      matthew  1016: 
1.34      harris41 1017: =item *
1.32      albertel 1018: editline(tag,data,description,size): Provide a <input type="text" ../> for
1.31      matthew  1019: single-line text entry.  This is to be used for text enclosed by tags, not
                   1020: arguements/parameters associated with a tag.
1.26      harris41 1021: 
                   1022: =back
                   1023: 
                   1024: incomplete...
                   1025: 
                   1026: =cut

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