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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # edit mode helpers
1.25      albertel    3: #
1.30    ! matthew     4: # $Id: edit.pm,v 1.29 2002/03/06 20:15:35 matthew 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.1       albertel   28: # 3/20 Guy
1.27      matthew    29: # 01/10/02 Matthew
1.29      matthew    30: # 03/06/02 Matthew
1.1       albertel   31: package Apache::edit; 
                     32: 
                     33: use strict;
                     34: use Apache::lonnet;
                     35: 
1.10      albertel   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: 
1.1       albertel   46: sub tag_start {
1.9       albertel   47:   my ($target,$token,$description) = @_;
1.1       albertel   48:   my $result='';
1.5       albertel   49:   if ($target eq "edit") {
1.4       albertel   50:     my $tag=$token->[1];
1.22      albertel   51:     if (!$description) {
                     52:       $description=&Apache::lonxml::description($token);
                     53:       if (!$description) { $description="<$tag>"; }
                     54:     }
1.10      albertel   55:     $result.= &start_table($token)."<tr><td>$description</td>
1.14      albertel   56: <td>Delete".
1.8       albertel   57:   &deletelist($target,$token)
                     58:   ."</td>
1.4       albertel   59: <td>".
1.27      matthew    60:     &insertlist($target,$token).&end_row().&start_spanning_row();
                     61: #<td>". 
1.22      albertel   62: #  &movebuttons($target,$token).
                     63: #    "</tr><tr><td colspan=\"3\">\n";
1.4       albertel   64:   }
1.1       albertel   65:   return $result;
                     66: }
                     67: 
                     68: sub tag_end {
1.9       albertel   69:   my ($target,$token,$description) = @_;
1.1       albertel   70:   my $result='';
1.4       albertel   71:   if ($target eq 'edit') {
                     72:     my $tag=$token->[1];
1.9       albertel   73:     if (!defined($description)) {
1.14      albertel   74:       $result.="</td></tr><tr><td>&lt;/$tag&gt;</td><td colspan=\"2\">&nbsp;</td>";
1.9       albertel   75:     } else {
1.14      albertel   76:       if ($description ne '') { $result.="</td></tr><tr><td>$description</td><td colspan=\"2\">&nbsp;</td>"; }
1.9       albertel   77:     }
1.12      albertel   78:     $result.="</tr>".&end_table()."\n";
1.4       albertel   79:   }
                     80:   return $result;
                     81: }
1.1       albertel   82: 
1.10      albertel   83: sub start_table {
                     84:   my ($token)=@_;
                     85:   my $tag = $token->[1];
                     86:   my $tagnum;
                     87:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
1.23      albertel   88:     my $testtag=$namespace.'::'.$tag;
1.10      albertel   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++;
1.12      albertel   98:   my $result="<table bgcolor=\"$color\" width=\"100%\" border=\"5\">";
1.10      albertel   99:   return $result;
                    100: }
                    101: 
                    102: sub end_table {
                    103:   $Apache::edit::colordepth--;
                    104:   my $result="</table>";
                    105:   return $result;
                    106: }
                    107: 
1.27      matthew   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: 
1.22      albertel  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: 
1.8       albertel  121: sub deletelist {
                    122:   my ($target,$token) = @_;
                    123:   my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
1.14      albertel  124: <option></option>
                    125: <option>Yes</option>
1.8       albertel  126: </select>";
                    127:   return $result;
                    128: }
                    129: 
1.14      albertel  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: 
1.7       albertel  153: sub get_insert_list {
1.6       albertel  154:   my ($token) = @_;
                    155:   my $result='';
1.7       albertel  156:   my @tagnums= ();
                    157:   #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
1.6       albertel  158:   if ($Apache::lonxml::insertlist{"$token->[1].which"}) {
1.7       albertel  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"} });
1.6       albertel  164:     }
                    165:   }
1.7       albertel  166:   if (@tagnums) {
                    167:     foreach my $tagnum (@tagnums) {
                    168:       $result.='<option value="'.$tagnum.'">'.$Apache::lonxml::insertlist{"$tagnum.description"}."</option>\n";
1.5       albertel  169:     }
                    170:     if ($result) { $result='<option selected="on"></option>'.$result; }
                    171:   }
                    172:   return $result;
                    173: }
                    174: 
1.4       albertel  175: sub insertlist {
1.8       albertel  176:   my ($target,$token) = @_;
1.4       albertel  177:   my $result;
                    178:   if ($target eq 'edit') {
1.5       albertel  179:     my $optionlist= &get_insert_list($token);
                    180:     if ($optionlist) {
                    181:       $result = "Insert:
1.4       albertel  182: <select name=\"insert_$Apache::lonxml::curdepth\">
1.5       albertel  183: $optionlist
1.4       albertel  184: </select>"
1.11      albertel  185:     } else {
                    186:       $result="&nbsp;";
1.6       albertel  187:     }
                    188:   }
                    189:   return $result;
                    190: }
                    191: 
1.7       albertel  192: sub handle_insert {
1.15      albertel  193:   if ($ENV{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
1.6       albertel  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"};
1.7       albertel  199:     my $namespace;
                    200:     if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
1.6       albertel  201:     $result.="\n<$newtag>\n</$newtag>";
                    202:   } else {
1.15      albertel  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.");
1.5       albertel  211:     }
                    212:   }
                    213:   return $result;
1.16      albertel  214: }
                    215: 
                    216: sub insert_responseparam {
                    217:   return '
                    218:     <responseparam />';
1.5       albertel  219: }
                    220: 
1.24      albertel  221: sub insert_formularesponse {
                    222:   return '
                    223: <formularesponse answer="" samples="">
                    224:     <textline />
                    225:     <hintgroup>
                    226:     </hintgroup>
                    227: </formularesponse>';
                    228: }
                    229: 
1.15      albertel  230: sub insert_numericalresponse {
                    231:   return '
                    232: <numericalresponse answer="">
                    233:     <textline />
                    234:     <hintgroup>
                    235:     </hintgroup>
                    236: </numericalresponse>';
                    237: }
                    238: 
1.18      albertel  239: sub insert_stringresponse {
                    240:   return '
                    241: <stringresponse answer="" type="">
                    242:     <textline />
                    243:     <hintgroup>
                    244:     </hintgroup>
                    245: </stringresponse>';
                    246: }
                    247: 
1.7       albertel  248: sub insert_optionresponse {
                    249:   return '
                    250: <optionresponse max="10">
                    251:     <foilgroup options="">
                    252:     </foilgroup>
1.14      albertel  253:     <hintgroup>
                    254:     </hintgroup>
1.7       albertel  255: </optionresponse>';
1.1       albertel  256: }
                    257: 
1.23      albertel  258: sub insert_radiobuttonresponse {
                    259:   return '
                    260: <radiobuttonresponse max="10">
                    261:     <foilgroup>
                    262:     </foilgroup>
                    263:     <hintgroup>
                    264:     </hintgroup>
                    265: </radiobuttonresponse>';
                    266: }
                    267: 
1.21      albertel  268: sub insert_displayduedate { return '<displayduedate />'; }
                    269: sub insert_displaytitle   { return '<displaytitle />'; }
1.22      albertel  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: }
1.21      albertel  283: 
1.23      albertel  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: 
1.25      albertel  292: sub textarea_sizes {
                    293:   my ($data)=@_;
                    294:   my $count=0;
                    295:   my $maxlength=-1;
1.26      harris41  296:   foreach (split ("\n", $$data)) { $count++;
1.25      albertel  297: 	if (length($_) > $maxlength) { $maxlength = length($_); }
1.26      harris41  298:       }
1.25      albertel  299:   my $rows = $count;
                    300:   my $cols = $maxlength;
                    301:   return ($rows,$cols);
                    302: }
                    303: 
1.2       albertel  304: sub editfield {
1.5       albertel  305:   my ($tag,$data,$description,$minwidth,$minheight)=@_;
1.22      albertel  306: 
1.25      albertel  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";
1.2       albertel  313: }
                    314: 
                    315: sub modifiedfield {
                    316:   my ($token) = @_;
1.3       albertel  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;
1.2       albertel  325: }
                    326: 
1.15      albertel  327: # Returns a 1 if the token has been modified and you should rebuild the tag
1.12      albertel  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) {
1.20      albertel  333:     #just want the string that it was set to
                    334:     my $value=$token->[2]->{$arg};
1.12      albertel  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: 
1.15      albertel  345: # looks for /> on start tags
1.12      albertel  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]})) {
1.20      albertel  352:       $val=~s:^\s+|\s+$::g;
1.17      albertel  353:       $val=~s:"::g; #"
1.12      albertel  354:       &Apache::lonxml::debug("setting :$key: to  :$val:");
                    355:       $result.=' '.$key.'="'.$val.'"';
                    356:     }
1.15      albertel  357:     if ($token->[4] =~ m:/>$:) {
                    358:       $result.=' />';
                    359:     } else {
                    360:       $result.='>';
                    361:     }
1.12      albertel  362:   } elsif ( $token->[0] eq 'E' ) {
                    363:     $result = '</'.$token->[1].'>';
                    364:   }
                    365:   return $result;
                    366: }
1.13      albertel  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.'
1.27      matthew   392:       </select>';
1.13      albertel  393:   return $result;
                    394: }
                    395: 
1.19      albertel  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.'
1.27      matthew   414:       </select>';
1.19      albertel  415:   } else {
                    416:     $result.=&text_arg($description,$name,$token,$size);
                    417:   }
                    418:   return $result;
                    419: }
1.29      matthew   420: 
                    421: #----------------------------------------------------- browse
                    422: sub browse {
                    423:     # insert a link to call up the filesystem browser (lonindexer)
1.30    ! matthew   424:     $_ = shift;
1.29      matthew   425:     my $form    = 'lonhomework';
1.30    ! matthew   426:     my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
1.29      matthew   427:     my $result = <<"ENDBUTTON";
                    428: <a href=\"javascript:openbrowser('$form','$element')\"\>Browse</a>
                    429: ENDBUTTON
                    430:     return $result;
                    431: }
                    432: 
1.30    ! matthew   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: 
1.1       albertel  446: 1;
                    447: __END__
1.26      harris41  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
1.27      matthew   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.
1.26      harris41  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
1.29      matthew   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
1.30    ! matthew   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
1.29      matthew   553: the form element $elementname.
1.26      harris41  554: 
                    555: =back
                    556: 
                    557: incomplete...
                    558: 
                    559: =cut

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