File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.30: download - view: text, annotated - select for diffs
Fri Mar 8 18:30:33 2002 UTC (22 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added search(), which inserts a link to the search page.  Fixed hard-coded
information in browse().

    1: # The LearningOnline Network with CAPA 
    2: # edit mode helpers
    3: #
    4: # $Id: edit.pm,v 1.30 2002/03/08 18:30:33 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="<$tag>"; }
   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 editfield {
  305:   my ($tag,$data,$description,$minwidth,$minheight)=@_;
  306: 
  307:   my ($rows,$cols)=&textarea_sizes(\$data);
  308:   if ($cols > 80) { $cols = 80; }
  309:   if ($cols < $minwidth ) { $cols = $minwidth; }
  310:   if ($rows < $minheight) { $rows = $minheight; }
  311:   if ($description) { $description="<br />".$description."<br />"; }
  312:   return "$description\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$rows\" cols=\"$cols\" name=\"homework_edit_".$Apache::lonxml::curdepth."\">$data</textarea>\n";
  313: }
  314: 
  315: sub modifiedfield {
  316:   my ($token) = @_;
  317:   my $result;
  318: #  foreach my $envkey (sort keys %ENV) {
  319: #    &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
  320: #  }
  321: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
  322: #  &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
  323:   $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
  324:   return $result;
  325: }
  326: 
  327: # Returns a 1 if the token has been modified and you should rebuild the tag
  328: # side-effects, will modify the $token if new values are found
  329: sub get_new_args {
  330:   my ($token,$parstack,$safeeval,@args)=@_;
  331:   my $rebuild=0;
  332:   foreach my $arg (@args) {
  333:     #just want the string that it was set to
  334:     my $value=$token->[2]->{$arg};
  335:     my $newvalue=$ENV{"form.$Apache::lonxml::curdepth.$arg"};
  336:     &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:");
  337:     if ($value ne $newvalue) {
  338:       $token->[2]->{$arg}=$newvalue;
  339:       $rebuild=1;
  340:     }
  341:   }
  342:   return $rebuild;
  343: }
  344: 
  345: # looks for /> on start tags
  346: sub rebuild_tag {
  347:   my ($token) = @_;
  348:   my $result;
  349:   if ($token->[0] eq 'S') {
  350:     $result = '<'.$token->[1];
  351:     while (my ($key,$val)= each(%{$token->[2]})) {
  352:       $val=~s:^\s+|\s+$::g;
  353:       $val=~s:"::g; #"
  354:       &Apache::lonxml::debug("setting :$key: to  :$val:");
  355:       $result.=' '.$key.'="'.$val.'"';
  356:     }
  357:     if ($token->[4] =~ m:/>$:) {
  358:       $result.=' />';
  359:     } else {
  360:       $result.='>';
  361:     }
  362:   } elsif ( $token->[0] eq 'E' ) {
  363:     $result = '</'.$token->[1].'>';
  364:   }
  365:   return $result;
  366: }
  367: 
  368: sub text_arg {
  369:   my ($description,$name,$token,$size) = @_;
  370:   my $result;
  371:   if (!defined $size) { $size=20; }
  372:   my $arg=$token->[2]{$name};
  373:   $result=$description.'<input name="'."$Apache::lonxml::curdepth.$name".
  374:     '" type="text" value="'.$arg.'" size="'.$size.'" />';
  375:   return $result;
  376: }
  377: 
  378: sub select_arg {
  379:   my ($description,$name,$list,$token) = @_;
  380:   my $result;
  381:   my $optionlist="";
  382:   my $selected=$token->[2]{$name};
  383:   foreach my $option (@$list) {
  384:     if ( $selected eq $option ) {
  385:       $optionlist.="<option selected=\"on\">$option</option>\n";
  386:     } else {
  387:       $optionlist.="<option>$option</option>\n";
  388:     }
  389:   }
  390:   $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
  391:        '.$optionlist.'
  392:       </select>';
  393:   return $result;
  394: }
  395: 
  396: sub select_or_text_arg {
  397:   my ($description,$name,$list,$token,$size) = @_;
  398:   my $result;
  399:   my $optionlist="";
  400:   my $found=0;
  401:   my $selected=$token->[2]{$name};
  402:   foreach my $option (@$list) {
  403:     if ( $selected eq $option ) {
  404:       $optionlist.="<option selected=\"on\">$option</option>\n";
  405:       $found=1;
  406:     } else {
  407:       $optionlist.="<option>$option</option>\n";
  408:     }
  409:   }
  410:   $optionlist.="<option value=\"TYPEDINVALUE\">Type in value</option>\n";
  411:   if ($found) {
  412:     $result.=$description.'<select name="'."$Apache::lonxml::curdepth.$name".'">
  413:        '.$optionlist.'
  414:       </select>';
  415:   } else {
  416:     $result.=&text_arg($description,$name,$token,$size);
  417:   }
  418:   return $result;
  419: }
  420: 
  421: #----------------------------------------------------- browse
  422: sub browse {
  423:     # insert a link to call up the filesystem browser (lonindexer)
  424:     $_ = shift;
  425:     my $form    = 'lonhomework';
  426:     my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
  427:     my $result = <<"ENDBUTTON";
  428: <a href=\"javascript:openbrowser('$form','$element')\"\>Browse</a>
  429: ENDBUTTON
  430:     return $result;
  431: }
  432: 
  433: #----------------------------------------------------- browse
  434: sub search {
  435:     # insert a link to call up the filesystem browser (lonindexer)
  436:     $_ = shift;
  437:     my $form    = 'lonhomework';
  438:     my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
  439:     my $result = <<"ENDBUTTON";
  440: <a href=\"javascript:opensearcher('$form','$element')\"\>Search</a>
  441: ENDBUTTON
  442:     return $result;
  443: }
  444: 
  445: 
  446: 1;
  447: __END__
  448: 
  449: =head1 NAME
  450: 
  451: Apache::edit - edit mode helpers
  452: 
  453: =head1 SYNOPSIS
  454: 
  455: Invoked by many homework and xml related modules.
  456: 
  457:  &Apache::edit::SUBROUTINENAME(ARGUMENTS);
  458: 
  459: =head1 INTRODUCTION
  460: 
  461: This module outputs HTML syntax helpful for the rendering of edit
  462: mode interfaces.
  463: 
  464: This is part of the LearningOnline Network with CAPA project
  465: described at http://www.lon-capa.org.
  466: 
  467: =head1 HANDLER SUBROUTINE
  468: 
  469: There is no handler subroutine.
  470: 
  471: =head1 OTHER SUBROUTINES
  472: 
  473: =over 4
  474: 
  475: =item *
  476: 
  477: initialize_edit() : initialize edit (set colordepth to zero)
  478: 
  479: =item *
  480: 
  481: tag_start($target,$token,$description) : provide deletion and insertion lists
  482: for the manipulation of a start tag; return a scalar string
  483: 
  484: =item *
  485: 
  486: tag_end($target,$token,$description) : ending syntax corresponding to
  487: &tag_start. return a scalar string.
  488: 
  489: =item *
  490: 
  491: start_table($token) : start table; update colordepth; return scalar string.
  492: 
  493: =item *
  494: 
  495: end_table() : reduce color depth; end table; return scalar string
  496: 
  497: =item *
  498: 
  499: start_spanning_row() : start a new table row spanning the 'edit' environment.
  500: 
  501: =item *
  502: 
  503: start_row() : start a new table row and element. 
  504: 
  505: =item *
  506: 
  507: end_row() : end current table element and row.
  508: 
  509: =item *
  510: 
  511: movebuttons($target,$token) : move-up and move-down buttons; return scalar
  512: string
  513: 
  514: =item *
  515: 
  516: deletelist($target,$token) : provide a yes option in an HTML select element;
  517: return scalar string
  518: 
  519: =item *
  520: 
  521: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
  522: $style) : respond to a user delete request by passing relevant stack
  523: and array information to various rendering functions; return a scalar string
  524: 
  525: =item *
  526: 
  527: get_insert_list($token) : provide an insertion list based on possibilities
  528: from lonxml; return a scalar string
  529: 
  530: =item *
  531: 
  532: insertlist($target,$token) : api that uses get_insert_list;
  533: return a scalar string
  534: 
  535: =item *
  536: 
  537: handleinsert($token) : provide an insertion list based on possibilities
  538: from lonxml; 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: browse($elementname) : provide a link which will open up the filesystem
  547: browser (lonindexer) and, once a file is selected, place the result in
  548: the form element $elementname.
  549: 
  550: =item *
  551: search($elementname) : provide a link which will open up the filesystem
  552: searcher (lonsearchcat) and, once a file is selected, place the result in
  553: the form element $elementname.
  554: 
  555: =back
  556: 
  557: incomplete...
  558: 
  559: =cut

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