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

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

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