File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.32: download - view: text, annotated - select for diffs
Fri Mar 22 20:05:19 2002 UTC (22 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changed textfield to the more descriptive editline
- updated all references to textfield to use edit line
- update <import> <scriptlib> <parserlib> to use the new editline
- editline, and editfield now protect entities, should preserve &nbsp; and " much better

    1: # The LearningOnline Network with CAPA 
    2: # edit mode helpers
    3: #
    4: # $Id: edit.pm,v 1.32 2002/03/22 20:05:19 albertel Exp $
    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: #
   28: # 3/20 Guy
   29: # 01/10/02 Matthew
   30: # 03/06/02 Matthew
   31: package Apache::edit; 
   32: 
   33: use strict;
   34: use Apache::lonnet();
   35: use HTML::Entities();
   36: 
   37: # Global Vars
   38: # default list of colors to use in editing
   39: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
   40: # depth of nesting of edit
   41: $Apache::edit::colordepth=0;
   42: 
   43: sub initialize_edit {
   44:   $Apache::edit::colordepth=0;
   45: }
   46: 
   47: sub tag_start {
   48:   my ($target,$token,$description) = @_;
   49:   my $result='';
   50:   if ($target eq "edit") {
   51:     my $tag=$token->[1];
   52:     if (!$description) {
   53:       $description=&Apache::lonxml::description($token);
   54:       if (!$description) { $description="&lt;$tag&gt;"; }
   55:     }
   56:     $result.= &start_table($token)."<tr><td>$description</td>
   57: <td>Delete".
   58:   &deletelist($target,$token)
   59:   ."</td>
   60: <td>".
   61:     &insertlist($target,$token).&end_row().&start_spanning_row();
   62: #<td>". 
   63: #  &movebuttons($target,$token).
   64: #    "</tr><tr><td colspan=\"3\">\n";
   65:   }
   66:   return $result;
   67: }
   68: 
   69: sub tag_end {
   70:   my ($target,$token,$description) = @_;
   71:   my $result='';
   72:   if ($target eq 'edit') {
   73:     my $tag=$token->[1];
   74:     if (!defined($description)) {
   75:       $result.="</td></tr><tr><td>&lt;/$tag&gt;</td><td colspan=\"2\">&nbsp;</td>";
   76:     } else {
   77:       if ($description ne '') { $result.="</td></tr><tr><td>$description</td><td colspan=\"2\">&nbsp;</td>"; }
   78:     }
   79:     $result.="</tr>".&end_table()."\n";
   80:   }
   81:   return $result;
   82: }
   83: 
   84: sub start_table {
   85:   my ($token)=@_;
   86:   my $tag = $token->[1];
   87:   my $tagnum;
   88:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
   89:     my $testtag=$namespace.'::'.$tag;
   90:     $tagnum=$Apache::lonxml::insertlist{"$testtag.num"};
   91:     if (defined($tagnum)) { last; }
   92:   }
   93:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
   94:   my $color = $Apache::lonxml::insertlist{"$tagnum.color"};
   95:   if (!defined($color)) {
   96:     $color = $Apache::edit::colorlist[$Apache::edit::colordepth];
   97:   }
   98:   $Apache::edit::colordepth++;
   99:   my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"5\">";
  100:   return $result;
  101: }
  102: 
  103: sub end_table {
  104:   $Apache::edit::colordepth--;
  105:   my $result="</table>";
  106:   return $result;
  107: }
  108: 
  109: sub start_spanning_row { return '<tr><td colspan="3">';}
  110: sub start_row          { return '<tr><td>';            }
  111: sub end_row            { return '</td></tr>';          }
  112: 
  113: sub movebuttons {
  114:   my ($target,$token) = @_;
  115:   my $result='<input type="submit" name="moveup.'.
  116:     $Apache::lonxml::curdepth.'" value="Move Up" />';
  117:   $result.='<input type="submit" name="movedown.'.
  118:     $Apache::lonxml::curdepth.'" value="Move Down" />';
  119:   return $result;
  120: }
  121: 
  122: sub deletelist {
  123:   my ($target,$token) = @_;
  124:   my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
  125: <option></option>
  126: <option>Yes</option>
  127: </select>";
  128:   return $result;
  129: }
  130: 
  131: sub handle_delete {
  132:   if (!$ENV{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
  133:   my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  134:   my $result=0;
  135:   if ($space) {
  136:     my $sub1="$space\:\:delete_$token->[1]";
  137:     {
  138:       no strict 'refs';
  139:       if (defined &$sub1) {
  140: 	$result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
  141:       }
  142:     }
  143:   }
  144:   if (!$result) {
  145:     my $endtag='/'.$token->[1];
  146:     my $bodytext=&Apache::lonxml::get_all_text($endtag,$$parser[$#$parser]);
  147:     $$parser['-1']->get_token();
  148:     &Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
  149:     &Apache::lonxml::end_tag($tagstack,$parstack,$token);
  150:   }
  151:   return 1;
  152: }
  153: 
  154: sub get_insert_list {
  155:   my ($token) = @_;
  156:   my $result='';
  157:   my @tagnums= ();
  158:   #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
  159:   if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
  160:     push (@tagnums, @{ $Apache::lonxml::insertlist{"$token->[1].which"} });
  161:   }
  162:   foreach my $namespace (@Apache::lonxml::namespace) {
  163:     if ($Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"}) {
  164:       push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"} });
  165:     }
  166:   }
  167:   if (@tagnums) {
  168:     foreach my $tagnum (@tagnums) {
  169:       $result.='<option value="'.$tagnum.'">'.$Apache::lonxml::insertlist{"$tagnum.description"}."</option>\n";
  170:     }
  171:     if ($result) { $result='<option selected="on"></option>'.$result; }
  172:   }
  173:   return $result;
  174: }
  175: 
  176: sub insertlist {
  177:   my ($target,$token) = @_;
  178:   my $result;
  179:   if ($target eq 'edit') {
  180:     my $optionlist= &get_insert_list($token);
  181:     if ($optionlist) {
  182:       $result = "Insert:
  183: <select name=\"insert_$Apache::lonxml::curdepth\">
  184: $optionlist
  185: </select>"
  186:     } else {
  187:       $result="&nbsp;";
  188:     }
  189:   }
  190:   return $result;
  191: }
  192: 
  193: sub handle_insert {
  194:   if ($ENV{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
  195:   my $result;
  196:   my $tagnum = $ENV{"form.insert_$Apache::lonxml::curdepth"};
  197:   my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
  198:   if ($func eq 'default') {
  199:     my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
  200:     my $namespace;
  201:     if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
  202:     $result.="\n<$newtag>\n</$newtag>";
  203:   } else {
  204:     if (defined(&$func)) {
  205:       {
  206: 	no strict 'refs';
  207: 	$result.=&$func();
  208:       }
  209:     } else {
  210:       my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
  211:       &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined.");
  212:     }
  213:   }
  214:   return $result;
  215: }
  216: 
  217: sub insert_responseparam {
  218:   return '
  219:     <responseparam />';
  220: }
  221: 
  222: sub insert_formularesponse {
  223:   return '
  224: <formularesponse answer="" samples="">
  225:     <textline />
  226:     <hintgroup>
  227:     </hintgroup>
  228: </formularesponse>';
  229: }
  230: 
  231: sub insert_numericalresponse {
  232:   return '
  233: <numericalresponse answer="">
  234:     <textline />
  235:     <hintgroup>
  236:     </hintgroup>
  237: </numericalresponse>';
  238: }
  239: 
  240: sub insert_stringresponse {
  241:   return '
  242: <stringresponse answer="" type="">
  243:     <textline />
  244:     <hintgroup>
  245:     </hintgroup>
  246: </stringresponse>';
  247: }
  248: 
  249: sub insert_optionresponse {
  250:   return '
  251: <optionresponse max="10">
  252:     <foilgroup options="">
  253:     </foilgroup>
  254:     <hintgroup>
  255:     </hintgroup>
  256: </optionresponse>';
  257: }
  258: 
  259: sub insert_radiobuttonresponse {
  260:   return '
  261: <radiobuttonresponse max="10">
  262:     <foilgroup>
  263:     </foilgroup>
  264:     <hintgroup>
  265:     </hintgroup>
  266: </radiobuttonresponse>';
  267: }
  268: 
  269: sub insert_displayduedate { return '<displayduedate />'; }
  270: sub insert_displaytitle   { return '<displaytitle />'; }
  271: sub insert_hintpart {
  272:   return '
  273: <hintpart on="default">
  274:     <startouttext/>
  275:     <endouttext />
  276: </hintpart>';
  277: }
  278: 
  279: sub insert_numericalhint {
  280:   return '
  281: <numericalhint>
  282: </numericalhint>';
  283: }
  284: 
  285: sub insert_startouttext {
  286:   return "<startouttext />\n<endouttext />";
  287: }
  288: 
  289: sub insert_script {
  290:   return "\n<script type=\"loncapa/perl\">\n</script>";
  291: }
  292: 
  293: sub textarea_sizes {
  294:   my ($data)=@_;
  295:   my $count=0;
  296:   my $maxlength=-1;
  297:   foreach (split ("\n", $$data)) { $count++;
  298: 	if (length($_) > $maxlength) { $maxlength = length($_); }
  299:       }
  300:   my $rows = $count;
  301:   my $cols = $maxlength;
  302:   return ($rows,$cols);
  303: }
  304: 
  305: sub editline {
  306:     my ($tag,$data,$description,$size)=@_;
  307:     $data=&HTML::Entities::encode($data);
  308:     if ($description) { $description="<br />".$description."<br />"; }
  309:     my $result = <<"END";
  310: $description
  311: <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
  312:        value="$data" size="$size" />
  313: END
  314:     return $result;
  315: }
  316: 
  317: sub editfield {
  318:   my ($tag,$data,$description,$minwidth,$minheight)=@_;
  319: 
  320:   my ($rows,$cols)=&textarea_sizes(\$data);
  321:   if ($cols > 80) { $cols = 80; }
  322:   if ($cols < $minwidth ) { $cols = $minwidth; }
  323:   if ($rows < $minheight) { $rows = $minheight; }
  324:   if ($description) { $description="<br />".$description."<br />"; }
  325:   return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea rows="'.$rows.
  326:     '" cols="'.$cols.'" name="homework_edit_'.$Apache::lonxml::curdepth.'">'.
  327:       &HTML::Entities::encode($data).'</textarea>'."\n";
  328: }
  329: 
  330: sub modifiedfield {
  331:   my ($token) = @_;
  332:   my $result;
  333: #  foreach my $envkey (sort keys %ENV) {
  334: #    &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
  335: #  }
  336: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
  337: #  &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
  338:   $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
  339:   return $result;
  340: }
  341: 
  342: # Returns a 1 if the token has been modified and you should rebuild the tag
  343: # side-effects, will modify the $token if new values are found
  344: sub get_new_args {
  345:   my ($token,$parstack,$safeeval,@args)=@_;
  346:   my $rebuild=0;
  347:   foreach my $arg (@args) {
  348:     #just want the string that it was set to
  349:     my $value=$token->[2]->{$arg};
  350:     my $newvalue=$ENV{"form.$Apache::lonxml::curdepth.$arg"};
  351:     &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:");
  352:     if ($value ne $newvalue) {
  353:       $token->[2]->{$arg}=$newvalue;
  354:       $rebuild=1;
  355:     }
  356:   }
  357:   return $rebuild;
  358: }
  359: 
  360: # looks for /> on start tags
  361: sub rebuild_tag {
  362:   my ($token) = @_;
  363:   my $result;
  364:   if ($token->[0] eq 'S') {
  365:     $result = '<'.$token->[1];
  366:     while (my ($key,$val)= each(%{$token->[2]})) {
  367:       $val=~s:^\s+|\s+$::g;
  368:       $val=~s:"::g; #"
  369:       &Apache::lonxml::debug("setting :$key: to  :$val:");
  370:       $result.=' '.$key.'="'.$val.'"';
  371:     }
  372:     if ($token->[4] =~ m:/>$:) {
  373:       $result.=' />';
  374:     } else {
  375:       $result.='>';
  376:     }
  377:   } elsif ( $token->[0] eq 'E' ) {
  378:     $result = '</'.$token->[1].'>';
  379:   }
  380:   return $result;
  381: }
  382: 
  383: sub text_arg {
  384:   my ($description,$name,$token,$size) = @_;
  385:   my $result;
  386:   if (!defined $size) { $size=20; }
  387:   my $arg=$token->[2]{$name};
  388:   $result=$description.'<input name="'."$Apache::lonxml::curdepth.$name".
  389:     '" type="text" value="'.$arg.'" size="'.$size.'" />';
  390:   return $result;
  391: }
  392: 
  393: sub select_arg {
  394:   my ($description,$name,$list,$token) = @_;
  395:   my $result;
  396:   my $optionlist="";
  397:   my $selected=$token->[2]{$name};
  398:   foreach my $option (@$list) {
  399:     if ( $selected eq $option ) {
  400:       $optionlist.="<option selected=\"on\">$option</option>\n";
  401:     } else {
  402:       $optionlist.="<option>$option</option>\n";
  403:     }
  404:   }
  405:   $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
  406:        '.$optionlist.'
  407:       </select>';
  408:   return $result;
  409: }
  410: 
  411: sub select_or_text_arg {
  412:   my ($description,$name,$list,$token,$size) = @_;
  413:   my $result;
  414:   my $optionlist="";
  415:   my $found=0;
  416:   my $selected=$token->[2]{$name};
  417:   foreach my $option (@$list) {
  418:     if ( $selected eq $option ) {
  419:       $optionlist.="<option selected=\"on\">$option</option>\n";
  420:       $found=1;
  421:     } else {
  422:       $optionlist.="<option>$option</option>\n";
  423:     }
  424:   }
  425:   $optionlist.="<option value=\"TYPEDINVALUE\">Type in value</option>\n";
  426:   if ($found) {
  427:     $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
  428:        '.$optionlist.'
  429:       </select>';
  430:   } else {
  431:     $result.=&text_arg($description,$name,$token,$size);
  432:   }
  433:   return $result;
  434: }
  435: 
  436: #----------------------------------------------------- browse
  437: sub browse {
  438:     # insert a link to call up the filesystem browser (lonindexer)
  439:     $_ = shift;
  440:     my $form    = 'lonhomework';
  441:     my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
  442:     my $result = <<"ENDBUTTON";
  443: <a href=\"javascript:openbrowser('$form','$element')\"\>Browse</a>
  444: ENDBUTTON
  445:     return $result;
  446: }
  447: 
  448: #----------------------------------------------------- browse
  449: sub search {
  450:     # insert a link to call up the filesystem browser (lonindexer)
  451:     $_ = shift;
  452:     my $form    = 'lonhomework';
  453:     my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
  454:     my $result = <<"ENDBUTTON";
  455: <a href=\"javascript:opensearcher('$form','$element')\"\>Search</a>
  456: ENDBUTTON
  457:     return $result;
  458: }
  459: 
  460: 
  461: 1;
  462: __END__
  463: 
  464: =head1 NAME
  465: 
  466: Apache::edit - edit mode helpers
  467: 
  468: =head1 SYNOPSIS
  469: 
  470: Invoked by many homework and xml related modules.
  471: 
  472:  &Apache::edit::SUBROUTINENAME(ARGUMENTS);
  473: 
  474: =head1 INTRODUCTION
  475: 
  476: This module outputs HTML syntax helpful for the rendering of edit
  477: mode interfaces.
  478: 
  479: This is part of the LearningOnline Network with CAPA project
  480: described at http://www.lon-capa.org.
  481: 
  482: =head1 HANDLER SUBROUTINE
  483: 
  484: There is no handler subroutine.
  485: 
  486: =head1 OTHER SUBROUTINES
  487: 
  488: =over 4
  489: 
  490: =item *
  491: 
  492: initialize_edit() : initialize edit (set colordepth to zero)
  493: 
  494: =item *
  495: 
  496: tag_start($target,$token,$description) : provide deletion and insertion lists
  497: for the manipulation of a start tag; return a scalar string
  498: 
  499: =item *
  500: 
  501: tag_end($target,$token,$description) : ending syntax corresponding to
  502: &tag_start. return a scalar string.
  503: 
  504: =item *
  505: 
  506: start_table($token) : start table; update colordepth; return scalar string.
  507: 
  508: =item *
  509: 
  510: end_table() : reduce color depth; end table; return scalar string
  511: 
  512: =item *
  513: 
  514: start_spanning_row() : start a new table row spanning the 'edit' environment.
  515: 
  516: =item *
  517: 
  518: start_row() : start a new table row and element. 
  519: 
  520: =item *
  521: 
  522: end_row() : end current table element and row.
  523: 
  524: =item *
  525: 
  526: movebuttons($target,$token) : move-up and move-down buttons; return scalar
  527: string
  528: 
  529: =item *
  530: 
  531: deletelist($target,$token) : provide a yes option in an HTML select element;
  532: return scalar string
  533: 
  534: =item *
  535: 
  536: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
  537: $style) : respond to a user delete request by passing relevant stack
  538: and array information to various rendering functions; return a scalar string
  539: 
  540: =item *
  541: 
  542: get_insert_list($token) : provide an insertion list based on possibilities
  543: from lonxml; return a scalar string
  544: 
  545: =item *
  546: 
  547: insertlist($target,$token) : api that uses get_insert_list;
  548: return a scalar string
  549: 
  550: =item *
  551: 
  552: handleinsert($token) : provide an insertion list based on possibilities
  553: from lonxml; return a scalar string
  554: 
  555: =item *
  556: 
  557: get_insert_list($token) : provide an insertion list based on possibilities
  558: from lonxml; return a scalar string
  559: 
  560: =item *
  561: browse($elementname) : provide a link which will open up the filesystem
  562: browser (lonindexer) and, once a file is selected, place the result in
  563: the form element $elementname.
  564: 
  565: =item *
  566: search($elementname) : provide a link which will open up the filesystem
  567: searcher (lonsearchcat) and, once a file is selected, place the result in
  568: the form element $elementname.
  569: 
  570: = item *
  571: editline(tag,data,description,size): Provide a <input type="text" ../> for
  572: single-line text entry.  This is to be used for text enclosed by tags, not
  573: arguements/parameters associated with a tag.
  574: 
  575: =back
  576: 
  577: incomplete...
  578: 
  579: =cut

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