File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.31: download - view: text, annotated - select for diffs
Fri Mar 22 14:45:11 2002 UTC (22 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added textfield() as an alternative to editfield().  textfield serves up an
<input type="text" .. /> form instead of a <textarea> form.  This means we
can limit the user to a single line of text within a <tag> </tag> pair.

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

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