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

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

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