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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # edit mode helpers
1.25      albertel    3: #
1.85    ! www         4: # $Id: edit.pm,v 1.84 2004/06/08 01:32:01 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="">
                    288:     <textline />
                    289:     <hintgroup>
1.78      www       290:     <startouttext /><endouttext />
1.24      albertel  291:     </hintgroup>
                    292: </formularesponse>';
                    293: }
                    294: 
1.15      albertel  295: sub insert_numericalresponse {
1.63      albertel  296:     return '
1.15      albertel  297: <numericalresponse answer="">
                    298:     <textline />
                    299:     <hintgroup>
1.78      www       300:     <startouttext /><endouttext />
1.15      albertel  301:     </hintgroup>
                    302: </numericalresponse>';
                    303: }
                    304: 
1.18      albertel  305: sub insert_stringresponse {
1.63      albertel  306:     return '
1.18      albertel  307: <stringresponse answer="" type="">
                    308:     <textline />
                    309:     <hintgroup>
1.78      www       310:     <startouttext /><endouttext />
1.18      albertel  311:     </hintgroup>
                    312: </stringresponse>';
1.36      albertel  313: }
                    314: 
                    315: sub insert_essayresponse {
1.63      albertel  316:     return '
1.36      albertel  317: <essayresponse>
                    318:     <textfield></textfield>
                    319: </essayresponse>';
1.54      albertel  320: }
                    321: 
                    322: sub insert_imageresponse {
1.63      albertel  323:     return '
1.54      albertel  324: <imageresponse max="1">
                    325:     <foilgroup>
                    326:     </foilgroup>
                    327:     <hintgroup>
1.78      www       328:     <startouttext /><endouttext />
1.54      albertel  329:     </hintgroup>
                    330: </imageresponse>';
1.18      albertel  331: }
                    332: 
1.7       albertel  333: sub insert_optionresponse {
1.63      albertel  334:     return '
1.7       albertel  335: <optionresponse max="10">
                    336:     <foilgroup options="">
                    337:     </foilgroup>
1.14      albertel  338:     <hintgroup>
1.78      www       339:     <startouttext /><endouttext />
1.14      albertel  340:     </hintgroup>
1.7       albertel  341: </optionresponse>';
1.1       albertel  342: }
                    343: 
1.72      albertel  344: sub insert_organicresponse {
                    345:     return '
                    346: <organicresponse>
                    347:     <textline />
                    348:     <hintgroup>
1.78      www       349:     <startouttext /><endouttext />
1.72      albertel  350:     </hintgroup>
                    351: </organicresponse>';
                    352: }
                    353: 
                    354: sub insert_organicstructure {
                    355:     return '
                    356: <organicstructure />
                    357: ';
                    358: }
                    359: 
1.23      albertel  360: sub insert_radiobuttonresponse {
1.63      albertel  361:     return '
1.23      albertel  362: <radiobuttonresponse max="10">
                    363:     <foilgroup>
                    364:     </foilgroup>
                    365:     <hintgroup>
1.78      www       366:     <startouttext /><endouttext />
1.23      albertel  367:     </hintgroup>
                    368: </radiobuttonresponse>';
1.72      albertel  369: }
                    370: 
                    371: sub insert_reactionresponse {
                    372:     return '
                    373: <reactionresponse>
                    374:     <textline />
                    375:     <hintgroup>
1.78      www       376:     <startouttext /><endouttext />
1.72      albertel  377:     </hintgroup>
                    378: </reactionresponse>';
1.43      albertel  379: }
                    380: 
                    381: sub insert_rankresponse {
1.63      albertel  382:     return '
1.43      albertel  383: <rankresponse max="10">
                    384:     <foilgroup options="">
                    385:     </foilgroup>
                    386:     <hintgroup>
1.78      www       387:     <startouttext /><endouttext />
1.43      albertel  388:     </hintgroup>
                    389: </rankresponse>';
1.23      albertel  390: }
                    391: 
1.44      albertel  392: sub insert_matchresponse {
1.63      albertel  393:     return '
1.44      albertel  394: <matchresponse max="10">
                    395:     <foilgroup options="">
                    396:       <itemgroup>
                    397:       </itemgroup>
                    398:     </foilgroup>
                    399:     <hintgroup>
1.78      www       400:     <startouttext /><endouttext />
1.44      albertel  401:     </hintgroup>
                    402: </matchresponse>';
                    403: }
                    404: 
1.21      albertel  405: sub insert_displayduedate { return '<displayduedate />'; }
                    406: sub insert_displaytitle   { return '<displaytitle />'; }
1.22      albertel  407: sub insert_hintpart {
1.63      albertel  408:     return '
1.22      albertel  409: <hintpart on="default">
                    410:     <startouttext/>
                    411:     <endouttext />
                    412: </hintpart>';
1.67      albertel  413: }
                    414: 
                    415: sub insert_hintgroup {
                    416:   return '
                    417: <hintgroup>
1.78      www       418:     <startouttext /><endouttext />
1.67      albertel  419: </hintgroup>';
1.22      albertel  420: }
                    421: 
                    422: sub insert_numericalhint {
1.63      albertel  423:     return '
1.22      albertel  424: <numericalhint>
                    425: </numericalhint>';
1.46      albertel  426: }
                    427: 
                    428: sub insert_stringhint {
1.63      albertel  429:     return '
1.46      albertel  430: <stringhint>
                    431: </stringhint>';
                    432: }
                    433: 
                    434: sub insert_formulahint {
1.63      albertel  435:     return '
1.46      albertel  436: <formulahint>
                    437: </formulahint>';
1.37      albertel  438: }
                    439: 
                    440: sub insert_radiobuttonhint {
1.63      albertel  441:     return '
1.37      albertel  442: <radiobuttonhint>
                    443: </radiobuttonhint>';
1.50      albertel  444: }
                    445: 
                    446: sub insert_optionhint {
1.63      albertel  447:     return '
1.50      albertel  448: <optionhint>
                    449: </optionhint>';
1.22      albertel  450: }
1.21      albertel  451: 
1.23      albertel  452: sub insert_startouttext {
1.78      www       453:     return "<startouttext /><endouttext />";
1.23      albertel  454: }
                    455: 
                    456: sub insert_script {
1.78      www       457:     return "\n<script type=\"loncapa/perl\"></script>";
1.23      albertel  458: }
                    459: 
1.25      albertel  460: sub textarea_sizes {
1.63      albertel  461:     my ($data)=@_;
                    462:     my $count=0;
                    463:     my $maxlength=-1;
                    464:     foreach (split ("\n", $$data)) {
                    465: 	$count+=int(length($_)/79);
                    466: 	$count++;
                    467: 	if (length($_) > $maxlength) { $maxlength = length($_); }
                    468:     }
                    469:     my $rows = $count;
                    470:     my $cols = $maxlength;
                    471:     return ($rows,$cols);
1.25      albertel  472: }
                    473: 
1.32      albertel  474: sub editline {
1.31      matthew   475:     my ($tag,$data,$description,$size)=@_;
1.81      albertel  476:     $data=&HTML::Entities::encode($data,'<>&"');
1.31      matthew   477:     if ($description) { $description="<br />".$description."<br />"; }
                    478:     my $result = <<"END";
                    479: $description
                    480: <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
                    481:        value="$data" size="$size" />
                    482: END
                    483:     return $result;
                    484: }
                    485: 
1.2       albertel  486: sub editfield {
1.82      www       487:     my ($tag,$data,$description,$minwidth,$minheight,$usehtmlarea)=@_;
1.22      albertel  488: 
1.63      albertel  489:     my ($rows,$cols)=&textarea_sizes(\$data);
1.84      www       490:     if (&Apache::lonhtmlcommon::htmlareabrowser() &&
                    491: 	!&Apache::lonhtmlcommon::htmlareablocked()) {
                    492: 	$rows+=7;      # make room for HTMLarea
                    493: 	$minheight+=7; # make room for HTMLarea
                    494:     }
1.63      albertel  495:     if ($cols > 80) { $cols = 80; }
                    496:     if ($cols < $minwidth ) { $cols = $minwidth; }
                    497:     if ($rows < $minheight) { $rows = $minheight; }
                    498:     if ($description) { $description="<br />".$description."<br />"; }
1.82      www       499:     if ($usehtmlarea) {
                    500: 	push @Apache::lonxml::htmlareafields,'homework_edit_'.
                    501: 	    $Apache::lonxml::curdepth;
                    502:     }
1.63      albertel  503:     return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea rows="'.$rows.
                    504: 	'" cols="'.$cols.'" name="homework_edit_'.
1.82      www       505: 	$Apache::lonxml::curdepth.'" id="homework_edit_'.
1.63      albertel  506: 	$Apache::lonxml::curdepth.'">'.
1.85    ! www       507: 	&HTML::Entities::encode($data,'<>&"').'</textarea>'.
        !           508: 	($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
        !           509: 			 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
1.2       albertel  510: }
                    511: 
                    512: sub modifiedfield {
1.70      albertel  513:     my ($endtag,$parser) = @_;
1.63      albertel  514:     my $result;
1.3       albertel  515: #  foreach my $envkey (sort keys %ENV) {
                    516: #    &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
                    517: #  }
                    518: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
                    519: #  &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
1.63      albertel  520:     $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
1.70      albertel  521:     my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
                    522:     # textareas throw away intial \n 
                    523:     if ($bodytext=~/^\n/) { $result="\n".$result; }
1.63      albertel  524:     return $result;
1.2       albertel  525: }
                    526: 
1.15      albertel  527: # Returns a 1 if the token has been modified and you should rebuild the tag
1.12      albertel  528: # side-effects, will modify the $token if new values are found
                    529: sub get_new_args {
1.61      albertel  530:     my ($token,$parstack,$safeeval,@args)=@_;
                    531:     my $rebuild=0;
                    532:     foreach my $arg (@args) {
1.63      albertel  533: 	#just want the string that it was set to
                    534: 	my $value=$token->[2]->{$arg};
                    535: 	my $element=&html_element_name($arg);
                    536: 	my $newvalue=$ENV{"form.$element"};
                    537: 	&Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
                    538: 	if (defined($newvalue) && $value ne $newvalue) {
                    539: 	    if (ref($newvalue) eq 'ARRAY') {
                    540: 		$token->[2]->{$arg}=join(',',@$newvalue);
                    541: 	    } else {
                    542: 		$token->[2]->{$arg}=$newvalue;
                    543: 	    }
1.79      albertel  544: 	    $rebuild=1;
                    545: 	} elsif (!defined($newvalue) && defined($value)) {
                    546: 	    delete($token->[2]->{$arg});
1.63      albertel  547: 	    $rebuild=1;
1.61      albertel  548: 	}
1.12      albertel  549:     }
1.63      albertel  550:     return $rebuild;
1.12      albertel  551: }
                    552: 
1.15      albertel  553: # looks for /> on start tags
1.12      albertel  554: sub rebuild_tag {
1.63      albertel  555:     my ($token) = @_;
                    556:     my $result;
                    557:     if ($token->[0] eq 'S') {
                    558: 	$result = '<'.$token->[1];
                    559: 	while (my ($key,$val)= each(%{$token->[2]})) {
                    560: 	    $val=~s:^\s+|\s+$::g;
                    561: 	    $val=~s:"::g; #"
                    562: 	    &Apache::lonxml::debug("setting :$key: to  :$val:");
                    563: 	    $result.=' '.$key.'="'.$val.'"';
                    564: 	}
                    565: 	if ($token->[4] =~ m:/>$:) {
                    566: 	    $result.=' />';
                    567: 	} else {
                    568: 	    $result.='>';
                    569: 	}
                    570:     } elsif ( $token->[0] eq 'E' ) {
                    571: 	$result = '</'.$token->[1].'>';
1.12      albertel  572:     }
1.63      albertel  573:     return $result;
1.12      albertel  574: }
1.13      albertel  575: 
1.47      matthew   576: sub html_element_name {
                    577:     my ($name) = @_;
1.48      albertel  578:     return $name.'_'.$Apache::lonxml::curdepth;
                    579: }
                    580: 
                    581: sub hidden_arg {
                    582:     my ($name,$token) = @_;
                    583:     my $result;
                    584:     my $arg=$token->[2]{$name};
                    585:     $result='<input name="'.&html_element_name($name).
                    586: 	'" type="hidden" value="'.$arg.'" />';
1.61      albertel  587:     return $result;
                    588: }
                    589: 
                    590: sub checked_arg {
                    591:     my ($description,$name,$list,$token) = @_;
                    592:     my $result;
                    593:     my $optionlist="";
                    594:     my $allselected=$token->[2]{$name};
1.71      www       595:     $result=&mt($description);
1.61      albertel  596:     foreach my $option (@$list) {
                    597: 	my ($value,$text);
                    598: 	if ( ref($option) eq 'ARRAY') {
                    599: 	    $value='value="'.$$option[0].'"';
                    600: 	    $text=$$option[1];
                    601: 	    $option=$$option[0];
                    602: 	} else {
                    603: 	    $text=$option;
                    604: 	    $value='value="'.$option.'"';
                    605: 	}
                    606: 	$result.="<nobr><input type='checkbox' $value name='".
                    607: 	    &html_element_name($name)."'";
                    608: 	foreach my $selected (split(/,/,$allselected)) {
                    609: 	    if ( $selected eq $option ) {
                    610: 		$result.=" checked='on' ";
                    611: 		last;
                    612: 	    }
                    613: 	}
                    614: 	$result.=" />$text</nobr>\n";
                    615:     }
1.48      albertel  616:     return $result;
1.47      matthew   617: }
                    618: 
1.13      albertel  619: sub text_arg {
1.63      albertel  620:     my ($description,$name,$token,$size) = @_;
                    621:     my $result;
                    622:     if (!defined $size) { $size=20; }
                    623:     my $arg=$token->[2]{$name};
1.71      www       624:     $result=&mt($description).'&nbsp;<input name="'.&html_element_name($name).
1.63      albertel  625: 	'" type="text" value="'.$arg.'" size="'.$size.'" />';
                    626:     return '<nobr>'.$result.'</nobr>';
1.13      albertel  627: }
                    628: 
                    629: sub select_arg {
1.39      albertel  630:     my ($description,$name,$list,$token) = @_;
                    631:     my $result;
                    632:     my $optionlist="";
                    633:     my $selected=$token->[2]{$name};
                    634:     foreach my $option (@$list) {
1.64      albertel  635: 	my ($text,$value);
1.39      albertel  636: 	if ( ref($option) eq 'ARRAY') {
                    637: 	    $value='value="'.$$option[0].'"';
1.64      albertel  638: 	    $text=$$option[1];
                    639: 	    $option=$$option[0];
1.39      albertel  640: 	} else {
1.64      albertel  641: 	    $text=$option;
1.39      albertel  642: 	    $value='value="'.$option.'"';
                    643: 	}
                    644: 	if ( $selected eq $option ) {
1.64      albertel  645: 	    $optionlist.="<option $value selected=\"on\">$text</option>\n";
1.39      albertel  646: 	} else {
1.64      albertel  647: 	    $optionlist.="<option $value >$text</option>\n";
1.39      albertel  648: 	}
1.13      albertel  649:     }
1.57      albertel  650:     $result.='<nobr>'.$description.'&nbsp;<select name="'.
                    651: 	&html_element_name($name).'">
1.13      albertel  652:        '.$optionlist.'
1.57      albertel  653:       </select></nobr>';
1.39      albertel  654:     return $result;
1.13      albertel  655: }
                    656: 
1.19      albertel  657: sub select_or_text_arg {
1.39      albertel  658:     my ($description,$name,$list,$token,$size) = @_;
                    659:     my $result;
                    660:     my $optionlist="";
                    661:     my $found=0;
                    662:     my $selected=$token->[2]{$name};
                    663:     foreach my $option (@$list) {
1.64      albertel  664: 	my ($text,$value);
1.39      albertel  665: 	if ( ref($option) eq 'ARRAY') {
                    666: 	    $value='value="'.$$option[0].'"';
1.64      albertel  667: 	    $text=$$option[1];
                    668: 	    $option=$$option[0];
1.39      albertel  669: 	} else {
1.64      albertel  670: 	    $text=$option;
1.39      albertel  671: 	    $value='value="'.$option.'"';
                    672: 	}
                    673: 	if ( $selected eq $option ) {
1.64      albertel  674: 	    $optionlist.="<option $value selected=\"on\">$text</option>\n";
1.39      albertel  675: 	    $found=1;
                    676: 	} else {
1.64      albertel  677: 	    $optionlist.="<option $value>$text</option>\n";
1.39      albertel  678: 	}
                    679:     }
1.60      www       680:     $optionlist.="<option value=\"TYPEDINVALUE\"".
                    681:  	((!$found)?' selected="on"':'').
1.73      www       682:  	">".&mt('Type-in value')."</option>\n";
1.60      www       683: #
                    684:     my $element=&html_element_name($name);
                    685:     my $selectelement='select_list_'.$element;
                    686:     my $typeinelement='type_in_'.$element;
                    687:     my $typeinvalue=($found?'':$selected);
                    688: #
                    689:     my $hiddenvalue='this.form.'.$element.'.value';
                    690:     my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
                    691:     my $selectedvalue='this.form.'.$selectelement.
                    692: 	     '.options['.$selectedindex.'].value';
                    693:     my $typedinvalue='this.form.'.$typeinelement.'.value';
                    694:     my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
1.71      www       695:     $description=&mt($description);
1.60      www       696: #
                    697:     return (<<ENDSELECTORTYPE);
                    698: <nobr>
                    699: $description
                    700: &nbsp;<select name="$selectelement"
1.74      albertel  701: onChange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }" >
1.60      www       702: $optionlist
                    703: </select>
                    704: <input type="text" size="$size" name="$typeinelement"
                    705:        value="$typeinvalue" 
                    706: onChange="$hiddenvalue=$typedinvalue;"
                    707: onFocus="$selectedindex=$selecttypeinindex-1;" />
                    708: <input type="hidden" name="$element" value="$selected" />
                    709: </nobr>
                    710: ENDSELECTORTYPE
1.19      albertel  711: }
1.29      matthew   712: 
1.49      www       713: #----------------------------------------------------- image coordinates
                    714: # single image coordinates, x, y 
                    715: sub entercoords {
1.80      albertel  716:     my ($idx,$idy,$mode,$width,$height) = @_;
1.49      www       717:     unless ($Apache::edit::bgimgsrc) { return ''; }
                    718:     if ($idx) { $idx.='_'; }
                    719:     if ($idy) { $idy.='_'; }
1.80      albertel  720:     my $bgfile=&Apache::lonnet::escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1.49      www       721:     my $form    = 'lonhomework';
                    722:     my $element;
                    723:     if (! defined($mode) || $mode eq 'attribute') {
                    724:         $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth");
                    725:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    726:         $element = &Apache::lonnet::escape('homework_edit_'.
                    727:                                            $Apache::lonxml::curdepth);
                    728:     }
1.80      albertel  729:     my $id=$Apache::lonxml::curdepth;
                    730:     my %data=("imagechoice.$id.type"      =>'point',
                    731: 	      "imagechoice.$id.formname"  =>$form,
                    732: 	      "imagechoice.$id.formx"     =>"$idx$element",
                    733: 	      "imagechoice.$id.formy"     =>"$idy$element",
                    734: 	      "imagechoice.$id.file"      =>$bgfile,
                    735: 	      "imagechoice.$id.formcoord" =>$element);
1.49      www       736:     if ($height) {
1.80      albertel  737: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
                    738: 	    $Apache::edit::bgimgsrccurdepth;
1.49      www       739:     }
                    740:     if ($width) {
1.80      albertel  741: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
                    742: 	    $Apache::edit::bgimgsrccurdepth;
1.49      www       743:     }
1.80      albertel  744:     &Apache::lonnet::appenv(%data);
                    745:     my $text="Click Coordinates";
                    746:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1.49      www       747:     return $result;
                    748: }
                    749: 
1.77      albertel  750: # coordinates (x1,y1)-(x2,y2)...
                    751: # mode can be either box, or polygon
                    752: sub entercoord {
                    753:     my ($idx,$mode,$width,$height,$type) = @_;
1.49      www       754:     unless ($Apache::edit::bgimgsrc) { return ''; }
1.76      albertel  755:     my $bgfile=&Apache::lonnet::escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
1.75      albertel  756:     my $form    = 'lonhomework';
                    757:     my $element;
                    758:     if (! defined($mode) || $mode eq 'attribute') {
1.77      albertel  759:         $element = &Apache::lonnet::escape("$idx\_$Apache::lonxml::curdepth");
1.75      albertel  760:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    761:         $element = &Apache::lonnet::escape('homework_edit_'.
                    762:                                            $Apache::lonxml::curdepth);
                    763:     }
1.76      albertel  764:     my $id=$Apache::lonxml::curdepth;
1.77      albertel  765:     my %data=("imagechoice.$id.type"      =>$type,
1.76      albertel  766: 	      "imagechoice.$id.formname"  =>$form,
                    767: 	      "imagechoice.$id.file"      =>$bgfile,
                    768: 	      "imagechoice.$id.formcoord" =>$element);
1.75      albertel  769:     if ($height) {
1.76      albertel  770: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
                    771: 	    $Apache::edit::bgimgsrccurdepth;
1.75      albertel  772:     }
                    773:     if ($width) {
1.76      albertel  774: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
                    775: 	    $Apache::edit::bgimgsrccurdepth;
1.75      albertel  776:     }
                    777:     &Apache::lonnet::appenv(%data);
1.77      albertel  778:     my $text="Enter Coordinates";
                    779:     if ($type eq 'polygon') { $text='Create Polygon Data'; }
                    780:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
1.49      www       781:     return $result;
                    782: }
1.76      albertel  783: 
                    784: sub deletecoorddata {
                    785:     &Apache::lonnet::delenv("imagechoice\\.");
                    786: }
                    787: 
1.29      matthew   788: #----------------------------------------------------- browse
                    789: sub browse {
                    790:     # insert a link to call up the filesystem browser (lonindexer)
1.68      albertel  791:     my ($id, $mode, $titleid) = @_;
1.29      matthew   792:     my $form    = 'lonhomework';
1.42      matthew   793:     my $element;
                    794:     if (! defined($mode) || $mode eq 'attribute') {
1.49      www       795:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
1.42      matthew   796:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    797:         $element = &Apache::lonnet::escape('homework_edit_'.
1.68      albertel  798:                                            $Apache::lonxml::curdepth);	
                    799:     }
                    800:     my $titleelement;
                    801:     if ($titleid) {
                    802: 	$titleelement=",'','','".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
1.42      matthew   803:     }
1.29      matthew   804:     my $result = <<"ENDBUTTON";
1.68      albertel  805: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
1.29      matthew   806: ENDBUTTON
                    807:     return $result;
                    808: }
                    809: 
1.30      matthew   810: #----------------------------------------------------- browse
                    811: sub search {
                    812:     # insert a link to call up the filesystem browser (lonindexer)
1.68      albertel  813:     my ($id, $mode, $titleid) = @_;
1.30      matthew   814:     my $form    = 'lonhomework';
1.49      www       815:     my $element;
                    816:     if (! defined($mode) || $mode eq 'attribute') {
                    817:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
                    818:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    819:         $element = &Apache::lonnet::escape('homework_edit_'.
                    820:                                            $Apache::lonxml::curdepth);
                    821:     }
1.68      albertel  822:     my $titleelement;
                    823:     if ($titleid) {
                    824: 	$titleelement=",'".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
                    825:     }
1.30      matthew   826:     my $result = <<"ENDBUTTON";
1.68      albertel  827: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>
1.30      matthew   828: ENDBUTTON
                    829:     return $result;
                    830: }
                    831: 
                    832: 
1.1       albertel  833: 1;
                    834: __END__
1.26      harris41  835: 
                    836: =head1 NAME
                    837: 
                    838: Apache::edit - edit mode helpers
                    839: 
                    840: =head1 SYNOPSIS
                    841: 
                    842: Invoked by many homework and xml related modules.
                    843: 
                    844:  &Apache::edit::SUBROUTINENAME(ARGUMENTS);
                    845: 
                    846: =head1 INTRODUCTION
                    847: 
                    848: This module outputs HTML syntax helpful for the rendering of edit
                    849: mode interfaces.
                    850: 
                    851: This is part of the LearningOnline Network with CAPA project
                    852: described at http://www.lon-capa.org.
                    853: 
                    854: =head1 HANDLER SUBROUTINE
                    855: 
                    856: There is no handler subroutine.
                    857: 
                    858: =head1 OTHER SUBROUTINES
                    859: 
                    860: =over 4
                    861: 
                    862: =item *
                    863: 
                    864: initialize_edit() : initialize edit (set colordepth to zero)
                    865: 
                    866: =item *
                    867: 
                    868: tag_start($target,$token,$description) : provide deletion and insertion lists
                    869: for the manipulation of a start tag; return a scalar string
                    870: 
                    871: =item *
                    872: 
                    873: tag_end($target,$token,$description) : ending syntax corresponding to
                    874: &tag_start. return a scalar string.
                    875: 
                    876: =item *
                    877: 
                    878: start_table($token) : start table; update colordepth; return scalar string.
                    879: 
                    880: =item *
                    881: 
                    882: end_table() : reduce color depth; end table; return scalar string
1.27      matthew   883: 
                    884: =item *
                    885: 
                    886: start_spanning_row() : start a new table row spanning the 'edit' environment.
                    887: 
                    888: =item *
                    889: 
                    890: start_row() : start a new table row and element. 
                    891: 
                    892: =item *
                    893: 
                    894: end_row() : end current table element and row.
1.26      harris41  895: 
                    896: =item *
                    897: 
                    898: movebuttons($target,$token) : move-up and move-down buttons; return scalar
                    899: string
                    900: 
                    901: =item *
                    902: 
                    903: deletelist($target,$token) : provide a yes option in an HTML select element;
                    904: return scalar string
                    905: 
                    906: =item *
                    907: 
                    908: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
                    909: $style) : respond to a user delete request by passing relevant stack
                    910: and array information to various rendering functions; return a scalar string
                    911: 
                    912: =item *
                    913: 
                    914: get_insert_list($token) : provide an insertion list based on possibilities
                    915: from lonxml; return a scalar string
                    916: 
                    917: =item *
                    918: 
                    919: insertlist($target,$token) : api that uses get_insert_list;
                    920: return a scalar string
                    921: 
                    922: =item *
                    923: 
                    924: handleinsert($token) : provide an insertion list based on possibilities
                    925: from lonxml; return a scalar string
                    926: 
                    927: =item *
                    928: 
                    929: get_insert_list($token) : provide an insertion list based on possibilities
                    930: from lonxml; return a scalar string
1.29      matthew   931: 
                    932: =item *
                    933: browse($elementname) : provide a link which will open up the filesystem
                    934: browser (lonindexer) and, once a file is selected, place the result in
1.30      matthew   935: the form element $elementname.
                    936: 
                    937: =item *
                    938: search($elementname) : provide a link which will open up the filesystem
                    939: searcher (lonsearchcat) and, once a file is selected, place the result in
1.29      matthew   940: the form element $elementname.
1.31      matthew   941: 
1.34      harris41  942: =item *
1.32      albertel  943: editline(tag,data,description,size): Provide a <input type="text" ../> for
1.31      matthew   944: single-line text entry.  This is to be used for text enclosed by tags, not
                    945: arguements/parameters associated with a tag.
1.26      harris41  946: 
                    947: =back
                    948: 
                    949: incomplete...
                    950: 
                    951: =cut

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