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

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

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