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

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

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