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

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

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