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

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

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