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

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

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