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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # edit mode helpers
1.25      albertel    3: #
1.31    ! matthew     4: # $Id: edit.pm,v 1.30 2002/03/08 18:30:33 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.31    ! matthew   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: 
1.2       albertel  315: sub editfield {
1.5       albertel  316:   my ($tag,$data,$description,$minwidth,$minheight)=@_;
1.22      albertel  317: 
1.25      albertel  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";
1.2       albertel  324: }
                    325: 
                    326: sub modifiedfield {
                    327:   my ($token) = @_;
1.3       albertel  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;
1.2       albertel  336: }
                    337: 
1.15      albertel  338: # Returns a 1 if the token has been modified and you should rebuild the tag
1.12      albertel  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) {
1.20      albertel  344:     #just want the string that it was set to
                    345:     my $value=$token->[2]->{$arg};
1.12      albertel  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: 
1.15      albertel  356: # looks for /> on start tags
1.12      albertel  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]})) {
1.20      albertel  363:       $val=~s:^\s+|\s+$::g;
1.17      albertel  364:       $val=~s:"::g; #"
1.12      albertel  365:       &Apache::lonxml::debug("setting :$key: to  :$val:");
                    366:       $result.=' '.$key.'="'.$val.'"';
                    367:     }
1.15      albertel  368:     if ($token->[4] =~ m:/>$:) {
                    369:       $result.=' />';
                    370:     } else {
                    371:       $result.='>';
                    372:     }
1.12      albertel  373:   } elsif ( $token->[0] eq 'E' ) {
                    374:     $result = '</'.$token->[1].'>';
                    375:   }
                    376:   return $result;
                    377: }
1.13      albertel  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.'
1.27      matthew   403:       </select>';
1.13      albertel  404:   return $result;
                    405: }
                    406: 
1.19      albertel  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.'
1.27      matthew   425:       </select>';
1.19      albertel  426:   } else {
                    427:     $result.=&text_arg($description,$name,$token,$size);
                    428:   }
                    429:   return $result;
                    430: }
1.29      matthew   431: 
                    432: #----------------------------------------------------- browse
                    433: sub browse {
                    434:     # insert a link to call up the filesystem browser (lonindexer)
1.30      matthew   435:     $_ = shift;
1.29      matthew   436:     my $form    = 'lonhomework';
1.30      matthew   437:     my $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth.$_");
1.29      matthew   438:     my $result = <<"ENDBUTTON";
                    439: <a href=\"javascript:openbrowser('$form','$element')\"\>Browse</a>
                    440: ENDBUTTON
                    441:     return $result;
                    442: }
                    443: 
1.30      matthew   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: 
1.1       albertel  457: 1;
                    458: __END__
1.26      harris41  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
1.27      matthew   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.
1.26      harris41  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
1.29      matthew   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
1.30      matthew   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
1.29      matthew   564: the form element $elementname.
1.31    ! matthew   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.
1.26      harris41  570: 
                    571: =back
                    572: 
                    573: incomplete...
                    574: 
                    575: =cut

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