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

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

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