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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # edit mode helpers
1.25      albertel    3: #
1.57.2.4! albertel    4: # $Id: edit.pm,v 1.57.2.3 2003/10/15 21:19:46 albertel 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;
1.32      albertel   34: use Apache::lonnet();
                     35: use HTML::Entities();
1.1       albertel   36: 
1.10      albertel   37: # Global Vars
                     38: # default list of colors to use in editing
                     39: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
                     40: # depth of nesting of edit
                     41: $Apache::edit::colordepth=0;
1.38      www        42: @Apache::edit::inserttag=();
1.49      www        43: # image-type responses: active background image and curdepth at definition
                     44: $Apache::edit::bgimgsrc='';
                     45: $Apache::edit::bgimgsrccurdepth='';
1.10      albertel   46: 
                     47: sub initialize_edit {
                     48:   $Apache::edit::colordepth=0;
1.38      www        49:   @Apache::edit::inserttag=();
1.10      albertel   50: }
                     51: 
1.1       albertel   52: sub tag_start {
1.9       albertel   53:   my ($target,$token,$description) = @_;
1.1       albertel   54:   my $result='';
1.5       albertel   55:   if ($target eq "edit") {
1.4       albertel   56:     my $tag=$token->[1];
1.22      albertel   57:     if (!$description) {
                     58:       $description=&Apache::lonxml::description($token);
                     59:       if (!$description) { $description="<$tag>"; }
                     60:     }
1.10      albertel   61:     $result.= &start_table($token)."<tr><td>$description</td>
1.14      albertel   62: <td>Delete".
1.8       albertel   63:   &deletelist($target,$token)
                     64:   ."</td>
1.4       albertel   65: <td>".
1.27      matthew    66:     &insertlist($target,$token).&end_row().&start_spanning_row();
                     67: #<td>". 
1.22      albertel   68: #  &movebuttons($target,$token).
                     69: #    "</tr><tr><td colspan=\"3\">\n";
1.4       albertel   70:   }
1.1       albertel   71:   return $result;
                     72: }
                     73: 
                     74: sub tag_end {
1.9       albertel   75:   my ($target,$token,$description) = @_;
1.1       albertel   76:   my $result='';
1.4       albertel   77:   if ($target eq 'edit') {
1.55      albertel   78:     $result.="</td></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.38      www        98:   push(@Apache::edit::inserttag,$token->[1]);
1.40      albertel   99:   my $result='<div align="right">';
1.41      www       100:   $result.='<table bgcolor="'.$color.'" width="97%" border="0" cellspacing="5" cellpadding="3">';
1.10      albertel  101:   return $result;
                    102: }
                    103: 
                    104: sub end_table {
                    105:   $Apache::edit::colordepth--;
1.40      albertel  106:   my $result='</table></div>';
1.39      albertel  107:   $result.="<table><tr><td>";
                    108: 
                    109:   my ($tagname,$closingtag);
                    110:   if (defined($Apache::edit::inserttag[-2])) {
                    111:     $tagname=$Apache::edit::inserttag[-2];
                    112:   } else {$tagname='problem';}
                    113:   if (defined($Apache::edit::inserttag[-1])) {
                    114:     $closingtag=$Apache::edit::inserttag[-1];
                    115:   }
                    116:   $result.=&innerinsertlist('edit',$tagname,$closingtag).
                    117:     "</td></tr></table>";
1.38      www       118:   pop(@Apache::edit::inserttag);
1.10      albertel  119:   return $result;
                    120: }
                    121: 
1.41      www       122: sub start_spanning_row { return '<tr><td colspan="3" bgcolor="#DDDDDD">';}
                    123: sub start_row          { return '<tr><td bgcolor="#DDDDDD">';            }
1.27      matthew   124: sub end_row            { return '</td></tr>';          }
                    125: 
1.22      albertel  126: sub movebuttons {
                    127:   my ($target,$token) = @_;
                    128:   my $result='<input type="submit" name="moveup.'.
                    129:     $Apache::lonxml::curdepth.'" value="Move Up" />';
                    130:   $result.='<input type="submit" name="movedown.'.
                    131:     $Apache::lonxml::curdepth.'" value="Move Down" />';
                    132:   return $result;
                    133: }
                    134: 
1.8       albertel  135: sub deletelist {
                    136:   my ($target,$token) = @_;
                    137:   my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
1.14      albertel  138: <option></option>
                    139: <option>Yes</option>
1.8       albertel  140: </select>";
                    141:   return $result;
                    142: }
                    143: 
1.14      albertel  144: sub handle_delete {
                    145:   if (!$ENV{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
                    146:   my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    147:   my $result=0;
                    148:   if ($space) {
                    149:     my $sub1="$space\:\:delete_$token->[1]";
                    150:     {
                    151:       no strict 'refs';
                    152:       if (defined &$sub1) {
                    153: 	$result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
                    154:       }
                    155:     }
                    156:   }
                    157:   if (!$result) {
                    158:     my $endtag='/'.$token->[1];
1.45      albertel  159:     my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
1.14      albertel  160:     $$parser['-1']->get_token();
                    161:     &Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
                    162:     &Apache::lonxml::end_tag($tagstack,$parstack,$token);
                    163:   }
                    164:   return 1;
                    165: }
                    166: 
1.7       albertel  167: sub get_insert_list {
1.38      www       168:   my ($tagname) = @_;
1.6       albertel  169:   my $result='';
1.7       albertel  170:   my @tagnums= ();
                    171:   #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
1.38      www       172:   if ($Apache::lonxml::insertlist{"$tagname.which"}) {
                    173:     push (@tagnums, @{ $Apache::lonxml::insertlist{"$tagname.which"} });
1.7       albertel  174:   }
                    175:   foreach my $namespace (@Apache::lonxml::namespace) {
1.38      www       176:     if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) {
                    177:       push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} });
1.6       albertel  178:     }
                    179:   }
1.7       albertel  180:   if (@tagnums) {
1.33      albertel  181:     my %options;
1.7       albertel  182:     foreach my $tagnum (@tagnums) {
1.33      albertel  183:       my $descrip=$Apache::lonxml::insertlist{"$tagnum.description"};
                    184:       $options{$descrip} ="<option value=\"$tagnum\">".$descrip."</option>\n";
1.5       albertel  185:     }
1.33      albertel  186:     foreach my $option (sort(keys(%options))) { $result.=$options{$option}; }
1.5       albertel  187:     if ($result) { $result='<option selected="on"></option>'.$result; }
                    188:   }
                    189:   return $result;
                    190: }
                    191: 
1.4       albertel  192: sub insertlist {
1.8       albertel  193:   my ($target,$token) = @_;
1.38      www       194:   return &innerinsertlist($target,$token->[1]);
                    195: }
                    196: 
                    197: sub innerinsertlist {
                    198:   my ($target,$tagname,$closingtag) = @_;
1.4       albertel  199:   my $result;
1.38      www       200:   my $after='';
                    201:   if ($closingtag) {
                    202:      $after='_after_'.$closingtag; 
                    203:   }
1.4       albertel  204:   if ($target eq 'edit') {
1.38      www       205:     my $optionlist= &get_insert_list($tagname);
1.5       albertel  206:     if ($optionlist) {
                    207:       $result = "Insert:
1.38      www       208: <select name=\"insert$after\_$Apache::lonxml::curdepth\">
1.5       albertel  209: $optionlist
1.4       albertel  210: </select>"
1.11      albertel  211:     } else {
                    212:       $result="&nbsp;";
1.6       albertel  213:     }
                    214:   }
                    215:   return $result;
                    216: }
                    217: 
1.7       albertel  218: sub handle_insert {
1.15      albertel  219:   if ($ENV{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
1.6       albertel  220:   my $result;
                    221:   my $tagnum = $ENV{"form.insert_$Apache::lonxml::curdepth"};
                    222:   my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
                    223:   if ($func eq 'default') {
                    224:     my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
1.7       albertel  225:     my $namespace;
                    226:     if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
1.6       albertel  227:     $result.="\n<$newtag>\n</$newtag>";
                    228:   } else {
1.15      albertel  229:     if (defined(&$func)) {
                    230:       {
                    231: 	no strict 'refs';
                    232: 	$result.=&$func();
                    233:       }
                    234:     } else {
                    235:       my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
                    236:       &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined.");
1.38      www       237:     }
                    238:   }
                    239:   return $result;
                    240: }
                    241: 
                    242: sub handle_insertafter {
                    243:   my $tagname=shift;
                    244:   if ($ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '')
                    245:      { return ''; }
                    246:   my $result;
                    247:   my $tagnum =$ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"};
                    248:   my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
                    249:   if ($func eq 'default') {
                    250:     my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
                    251:     my $namespace;
                    252:     if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
                    253:     $result.="\n<$newtag>\n</$newtag>";
                    254:   } else {
                    255:     if (defined(&$func)) {
                    256:       {
                    257: 	no strict 'refs';
                    258: 	$result.=&$func();
                    259:       }
                    260:     } else {
                    261:       my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
                    262:       &Apache::lonxml::error("Unable to insert (after) tag $newtag, $func was not defined. ($tagname $tagnum)");
1.5       albertel  263:     }
                    264:   }
                    265:   return $result;
1.16      albertel  266: }
                    267: 
                    268: sub insert_responseparam {
                    269:   return '
                    270:     <responseparam />';
1.5       albertel  271: }
                    272: 
1.24      albertel  273: sub insert_formularesponse {
                    274:   return '
                    275: <formularesponse answer="" samples="">
                    276:     <textline />
                    277:     <hintgroup>
                    278:     </hintgroup>
                    279: </formularesponse>';
                    280: }
                    281: 
1.15      albertel  282: sub insert_numericalresponse {
                    283:   return '
                    284: <numericalresponse answer="">
                    285:     <textline />
                    286:     <hintgroup>
                    287:     </hintgroup>
                    288: </numericalresponse>';
                    289: }
                    290: 
1.18      albertel  291: sub insert_stringresponse {
                    292:   return '
                    293: <stringresponse answer="" type="">
                    294:     <textline />
                    295:     <hintgroup>
                    296:     </hintgroup>
                    297: </stringresponse>';
1.36      albertel  298: }
                    299: 
                    300: sub insert_essayresponse {
                    301:   return '
                    302: <essayresponse>
                    303:     <textfield></textfield>
                    304: </essayresponse>';
1.54      albertel  305: }
                    306: 
                    307: sub insert_imageresponse {
                    308:   return '
                    309: <imageresponse max="1">
                    310:     <foilgroup>
                    311:     </foilgroup>
                    312:     <hintgroup>
                    313:     </hintgroup>
                    314: </imageresponse>';
1.18      albertel  315: }
                    316: 
1.7       albertel  317: sub insert_optionresponse {
                    318:   return '
                    319: <optionresponse max="10">
                    320:     <foilgroup options="">
                    321:     </foilgroup>
1.14      albertel  322:     <hintgroup>
                    323:     </hintgroup>
1.7       albertel  324: </optionresponse>';
1.1       albertel  325: }
                    326: 
1.23      albertel  327: sub insert_radiobuttonresponse {
                    328:   return '
                    329: <radiobuttonresponse max="10">
                    330:     <foilgroup>
                    331:     </foilgroup>
                    332:     <hintgroup>
                    333:     </hintgroup>
                    334: </radiobuttonresponse>';
1.43      albertel  335: }
                    336: 
                    337: sub insert_rankresponse {
                    338:   return '
                    339: <rankresponse max="10">
                    340:     <foilgroup options="">
                    341:     </foilgroup>
                    342:     <hintgroup>
                    343:     </hintgroup>
                    344: </rankresponse>';
1.23      albertel  345: }
                    346: 
1.44      albertel  347: sub insert_matchresponse {
                    348:   return '
                    349: <matchresponse max="10">
                    350:     <foilgroup options="">
                    351:       <itemgroup>
                    352:       </itemgroup>
                    353:     </foilgroup>
                    354:     <hintgroup>
                    355:     </hintgroup>
                    356: </matchresponse>';
                    357: }
                    358: 
1.21      albertel  359: sub insert_displayduedate { return '<displayduedate />'; }
                    360: sub insert_displaytitle   { return '<displaytitle />'; }
1.22      albertel  361: sub insert_hintpart {
                    362:   return '
                    363: <hintpart on="default">
                    364:     <startouttext/>
                    365:     <endouttext />
                    366: </hintpart>';
                    367: }
                    368: 
                    369: sub insert_numericalhint {
                    370:   return '
                    371: <numericalhint>
                    372: </numericalhint>';
1.46      albertel  373: }
                    374: 
                    375: sub insert_stringhint {
                    376:   return '
                    377: <stringhint>
                    378: </stringhint>';
                    379: }
                    380: 
                    381: sub insert_formulahint {
                    382:   return '
                    383: <formulahint>
                    384: </formulahint>';
1.37      albertel  385: }
                    386: 
                    387: sub insert_radiobuttonhint {
                    388:   return '
                    389: <radiobuttonhint>
                    390: </radiobuttonhint>';
1.50      albertel  391: }
                    392: 
                    393: sub insert_optionhint {
                    394:   return '
                    395: <optionhint>
                    396: </optionhint>';
1.22      albertel  397: }
1.21      albertel  398: 
1.23      albertel  399: sub insert_startouttext {
                    400:   return "<startouttext />\n<endouttext />";
                    401: }
                    402: 
                    403: sub insert_script {
                    404:   return "\n<script type=\"loncapa/perl\">\n</script>";
                    405: }
                    406: 
1.25      albertel  407: sub textarea_sizes {
                    408:   my ($data)=@_;
                    409:   my $count=0;
                    410:   my $maxlength=-1;
1.53      albertel  411:   foreach (split ("\n", $$data)) {
                    412:       $count+=int(length($_)/79);
                    413:       $count++;
                    414:       if (length($_) > $maxlength) { $maxlength = length($_); }
                    415:   }
1.25      albertel  416:   my $rows = $count;
                    417:   my $cols = $maxlength;
                    418:   return ($rows,$cols);
                    419: }
                    420: 
1.32      albertel  421: sub editline {
1.31      matthew   422:     my ($tag,$data,$description,$size)=@_;
1.32      albertel  423:     $data=&HTML::Entities::encode($data);
1.31      matthew   424:     if ($description) { $description="<br />".$description."<br />"; }
                    425:     my $result = <<"END";
                    426: $description
                    427: <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
                    428:        value="$data" size="$size" />
                    429: END
                    430:     return $result;
                    431: }
                    432: 
1.2       albertel  433: sub editfield {
1.5       albertel  434:   my ($tag,$data,$description,$minwidth,$minheight)=@_;
1.22      albertel  435: 
1.25      albertel  436:   my ($rows,$cols)=&textarea_sizes(\$data);
                    437:   if ($cols > 80) { $cols = 80; }
                    438:   if ($cols < $minwidth ) { $cols = $minwidth; }
                    439:   if ($rows < $minheight) { $rows = $minheight; }
                    440:   if ($description) { $description="<br />".$description."<br />"; }
1.32      albertel  441:   return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea rows="'.$rows.
                    442:     '" cols="'.$cols.'" name="homework_edit_'.$Apache::lonxml::curdepth.'">'.
                    443:       &HTML::Entities::encode($data).'</textarea>'."\n";
1.2       albertel  444: }
                    445: 
                    446: sub modifiedfield {
                    447:   my ($token) = @_;
1.3       albertel  448:   my $result;
                    449: #  foreach my $envkey (sort keys %ENV) {
                    450: #    &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
                    451: #  }
                    452: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
                    453: #  &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
                    454:   $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
                    455:   return $result;
1.2       albertel  456: }
                    457: 
1.15      albertel  458: # Returns a 1 if the token has been modified and you should rebuild the tag
1.12      albertel  459: # side-effects, will modify the $token if new values are found
                    460: sub get_new_args {
1.57.2.3  albertel  461:     my ($token,$parstack,$safeeval,@args)=@_;
                    462:     my $rebuild=0;
                    463:     foreach my $arg (@args) {
1.20      albertel  464:     #just want the string that it was set to
                    465:     my $value=$token->[2]->{$arg};
1.48      albertel  466:     my $element=&html_element_name($arg);
                    467:     my $newvalue=$ENV{"form.$element"};
1.12      albertel  468:     &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:");
1.52      albertel  469:     if (defined($newvalue) && $value ne $newvalue) {
1.57.2.3  albertel  470: 	if (ref($newvalue) eq 'ARRAY') {
                    471: 	    $token->[2]->{$arg}=join(',',@$newvalue);
                    472: 	} else {
                    473: 	    $token->[2]->{$arg}=$newvalue;
                    474: 	}
                    475: 	$rebuild=1;
1.12      albertel  476:     }
                    477:   }
                    478:   return $rebuild;
                    479: }
                    480: 
1.15      albertel  481: # looks for /> on start tags
1.12      albertel  482: sub rebuild_tag {
                    483:   my ($token) = @_;
                    484:   my $result;
                    485:   if ($token->[0] eq 'S') {
                    486:     $result = '<'.$token->[1];
                    487:     while (my ($key,$val)= each(%{$token->[2]})) {
1.20      albertel  488:       $val=~s:^\s+|\s+$::g;
1.17      albertel  489:       $val=~s:"::g; #"
1.12      albertel  490:       &Apache::lonxml::debug("setting :$key: to  :$val:");
                    491:       $result.=' '.$key.'="'.$val.'"';
                    492:     }
1.15      albertel  493:     if ($token->[4] =~ m:/>$:) {
                    494:       $result.=' />';
                    495:     } else {
                    496:       $result.='>';
                    497:     }
1.12      albertel  498:   } elsif ( $token->[0] eq 'E' ) {
                    499:     $result = '</'.$token->[1].'>';
                    500:   }
                    501:   return $result;
                    502: }
1.13      albertel  503: 
1.47      matthew   504: sub html_element_name {
                    505:     my ($name) = @_;
1.48      albertel  506:     return $name.'_'.$Apache::lonxml::curdepth;
                    507: }
                    508: 
                    509: sub hidden_arg {
                    510:     my ($name,$token) = @_;
                    511:     my $result;
                    512:     my $arg=$token->[2]{$name};
                    513:     $result='<input name="'.&html_element_name($name).
                    514: 	'" type="hidden" value="'.$arg.'" />';
1.57.2.3  albertel  515:     return $result;
                    516: }
                    517: 
                    518: sub checked_arg {
                    519:     my ($description,$name,$list,$token) = @_;
                    520:     my $result;
                    521:     my $optionlist="";
                    522:     my $allselected=$token->[2]{$name};
                    523:     $result=$description;
                    524:     foreach my $option (@$list) {
                    525: 	my ($value,$text);
                    526: 	if ( ref($option) eq 'ARRAY') {
                    527: 	    $value='value="'.$$option[0].'"';
                    528: 	    $text=$$option[1];
                    529: 	    $option=$$option[0];
                    530: 	} else {
                    531: 	    $text=$option;
                    532: 	    $value='value="'.$option.'"';
                    533: 	}
                    534: 	$result.="<nobr><input type='checkbox' $value name='".
                    535: 	    &html_element_name($name)."'";
                    536: 	foreach my $selected (split(/,/,$allselected)) {
                    537: 	    if ( $selected eq $option ) {
                    538: 		$result.=" checked='on' ";
                    539: 		last;
                    540: 	    }
                    541: 	}
                    542: 	$result.=" />$text</nobr>\n";
                    543:     }
1.48      albertel  544:     return $result;
1.47      matthew   545: }
                    546: 
1.13      albertel  547: sub text_arg {
                    548:   my ($description,$name,$token,$size) = @_;
                    549:   my $result;
                    550:   if (!defined $size) { $size=20; }
                    551:   my $arg=$token->[2]{$name};
1.47      matthew   552:   $result=$description.'&nbsp;<input name="'.&html_element_name($name).
1.13      albertel  553:     '" type="text" value="'.$arg.'" size="'.$size.'" />';
1.57      albertel  554:   return '<nobr>'.$result.'</nobr>';
1.13      albertel  555: }
                    556: 
                    557: sub select_arg {
1.39      albertel  558:     my ($description,$name,$list,$token) = @_;
                    559:     my $result;
                    560:     my $optionlist="";
                    561:     my $selected=$token->[2]{$name};
                    562:     foreach my $option (@$list) {
1.57.2.1  albertel  563: 	my ($text,$value);
1.39      albertel  564: 	if ( ref($option) eq 'ARRAY') {
                    565: 	    $value='value="'.$$option[0].'"';
1.57.2.1  albertel  566: 	    $text=$$option[1];
                    567: 	    $option=$$option[0];
1.39      albertel  568: 	} else {
1.57.2.1  albertel  569: 	    $text=$option;
1.39      albertel  570: 	    $value='value="'.$option.'"';
                    571: 	}
                    572: 	if ( $selected eq $option ) {
1.57.2.1  albertel  573: 	    $optionlist.="<option $value selected=\"on\">$text</option>\n";
1.39      albertel  574: 	} else {
1.57.2.1  albertel  575: 	    $optionlist.="<option $value >$text</option>\n";
1.39      albertel  576: 	}
1.13      albertel  577:     }
1.57      albertel  578:     $result.='<nobr>'.$description.'&nbsp;<select name="'.
                    579: 	&html_element_name($name).'">
1.13      albertel  580:        '.$optionlist.'
1.57      albertel  581:       </select></nobr>';
1.39      albertel  582:     return $result;
1.13      albertel  583: }
                    584: 
1.19      albertel  585: sub select_or_text_arg {
1.39      albertel  586:     my ($description,$name,$list,$token,$size) = @_;
                    587:     my $result;
                    588:     my $optionlist="";
                    589:     my $found=0;
                    590:     my $selected=$token->[2]{$name};
                    591:     foreach my $option (@$list) {
1.57.2.1  albertel  592: 	my ($text,$value);
1.39      albertel  593: 	if ( ref($option) eq 'ARRAY') {
                    594: 	    $value='value="'.$$option[0].'"';
1.57.2.1  albertel  595: 	    $text=$$option[1];
                    596: 	    $option=$$option[0];
1.39      albertel  597: 	} else {
1.57.2.1  albertel  598: 	    $text=$option;
1.39      albertel  599: 	    $value='value="'.$option.'"';
                    600: 	}
                    601: 	if ( $selected eq $option ) {
1.57.2.1  albertel  602: 	    $optionlist.="<option $value selected=\"on\">$text</option>\n";
1.39      albertel  603: 	    $found=1;
                    604: 	} else {
1.57.2.1  albertel  605: 	    $optionlist.="<option $value>$text</option>\n";
1.39      albertel  606: 	}
                    607:     }
                    608:     $optionlist.="<option value=\"TYPEDINVALUE\">Type in value</option>\n";
                    609:     if (($found) || (!$selected)) {
1.47      matthew   610: 	$result.=$description.'&nbsp;<select name="'.&html_element_name($name)
                    611:             .'">
1.39      albertel  612:        '.$optionlist.'
                    613:       </select>';
1.19      albertel  614:     } else {
1.39      albertel  615: 	$result.=&text_arg($description,$name,$token,$size);
1.19      albertel  616:     }
1.57      albertel  617:     return '<nobr>'.$result.'</nobr>';
1.19      albertel  618: }
1.29      matthew   619: 
1.49      www       620: #----------------------------------------------------- image coordinates
                    621: # single image coordinates, x, y 
                    622: sub entercoords {
                    623:     my ($idx,,$idy,$mode,$width,$height) = @_;
                    624:     unless ($Apache::edit::bgimgsrc) { return ''; }
                    625:     if ($idx) { $idx.='_'; }
                    626:     if ($idy) { $idy.='_'; }
                    627:     my $bgfile=&Apache::lonnet::escape($Apache::edit::bgimgsrc);
                    628:     my $form    = 'lonhomework';
                    629:     my $element;
                    630:     if (! defined($mode) || $mode eq 'attribute') {
                    631:         $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth");
                    632:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    633:         $element = &Apache::lonnet::escape('homework_edit_'.
                    634:                                            $Apache::lonxml::curdepth);
                    635:     }
                    636:     my $formheight='';
                    637:     if ($height) {
                    638: 	$formheight='&formheight='.$height.'_'.$Apache::edit::bgimgsrccurdepth;
                    639:     }
                    640:     my $formwidth='';
                    641:     if ($width) {
                    642: 	$formwidth='&formwidth='.$width.'_'.$Apache::edit::bgimgsrccurdepth;
                    643:     }
                    644:     my $result = <<"ENDBUTTON";
                    645: <a href="/cgi-bin/imagechoice.pl?formname=$form&file=$bgfile&formx=$idx$element&formy=$idy$element$formheight$formwidth"
                    646: target="imagechoice">Click Coordinates</a>
                    647: ENDBUTTON
                    648:     return $result;
                    649: }
                    650: 
                    651: # coordinate pair (x1,y1)-(x2,y2)
                    652: sub entercoordpair {
                    653:     my ($id,$mode,$width,$height) = @_;
                    654:     unless ($Apache::edit::bgimgsrc) { return ''; }
                    655:     my $bgfile=&Apache::lonnet::escape($Apache::edit::bgimgsrc);
                    656:     my $form    = 'lonhomework';
                    657:     my $element;
                    658:     if (! defined($mode) || $mode eq 'attribute') {
                    659:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
                    660:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    661:         $element = &Apache::lonnet::escape('homework_edit_'.
                    662:                                            $Apache::lonxml::curdepth);
                    663:     }
                    664:     my $formheight='';
                    665:     if ($height) {
                    666: 	$formheight='&formheight='.$height.'_'.$Apache::edit::bgimgsrccurdepth;
                    667:     }
                    668:     my $formwidth='';
                    669:     if ($width) {
                    670: 	$formwidth='&formwidth='.$width.'_'.$Apache::edit::bgimgsrccurdepth;
                    671:     }
                    672:     my $result = <<"ENDBUTTON";
1.51      www       673: <a href="/cgi-bin/imagechoice.pl?mode=pair&formname=$form&file=$bgfile$formheight$formwidth&formcoord=$element"
1.49      www       674: target="imagechoice">Click Coordinate Pair</a>
                    675: ENDBUTTON
                    676:     return $result;
                    677: }
1.29      matthew   678: #----------------------------------------------------- browse
                    679: sub browse {
                    680:     # insert a link to call up the filesystem browser (lonindexer)
1.42      matthew   681:     my ($id, $mode) = @_;
1.29      matthew   682:     my $form    = 'lonhomework';
1.42      matthew   683:     my $element;
                    684:     if (! defined($mode) || $mode eq 'attribute') {
1.49      www       685:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
1.42      matthew   686:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    687:         $element = &Apache::lonnet::escape('homework_edit_'.
                    688:                                            $Apache::lonxml::curdepth);
                    689:     }
1.29      matthew   690:     my $result = <<"ENDBUTTON";
1.56      www       691: <a href=\"javascript:openbrowser('$form','$element')\"\>Select</a>
1.29      matthew   692: ENDBUTTON
                    693:     return $result;
                    694: }
                    695: 
1.30      matthew   696: #----------------------------------------------------- browse
                    697: sub search {
                    698:     # insert a link to call up the filesystem browser (lonindexer)
1.49      www       699:     my ($id, $mode) = @_;
1.30      matthew   700:     my $form    = 'lonhomework';
1.49      www       701:     my $element;
                    702:     if (! defined($mode) || $mode eq 'attribute') {
                    703:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
                    704:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
                    705:         $element = &Apache::lonnet::escape('homework_edit_'.
                    706:                                            $Apache::lonxml::curdepth);
                    707:     }
1.30      matthew   708:     my $result = <<"ENDBUTTON";
                    709: <a href=\"javascript:opensearcher('$form','$element')\"\>Search</a>
                    710: ENDBUTTON
                    711:     return $result;
                    712: }
                    713: 
                    714: 
1.1       albertel  715: 1;
                    716: __END__
1.26      harris41  717: 
                    718: =head1 NAME
                    719: 
                    720: Apache::edit - edit mode helpers
                    721: 
                    722: =head1 SYNOPSIS
                    723: 
                    724: Invoked by many homework and xml related modules.
                    725: 
                    726:  &Apache::edit::SUBROUTINENAME(ARGUMENTS);
                    727: 
                    728: =head1 INTRODUCTION
                    729: 
                    730: This module outputs HTML syntax helpful for the rendering of edit
                    731: mode interfaces.
                    732: 
                    733: This is part of the LearningOnline Network with CAPA project
                    734: described at http://www.lon-capa.org.
                    735: 
                    736: =head1 HANDLER SUBROUTINE
                    737: 
                    738: There is no handler subroutine.
                    739: 
                    740: =head1 OTHER SUBROUTINES
                    741: 
                    742: =over 4
                    743: 
                    744: =item *
                    745: 
                    746: initialize_edit() : initialize edit (set colordepth to zero)
                    747: 
                    748: =item *
                    749: 
                    750: tag_start($target,$token,$description) : provide deletion and insertion lists
                    751: for the manipulation of a start tag; return a scalar string
                    752: 
                    753: =item *
                    754: 
                    755: tag_end($target,$token,$description) : ending syntax corresponding to
                    756: &tag_start. return a scalar string.
                    757: 
                    758: =item *
                    759: 
                    760: start_table($token) : start table; update colordepth; return scalar string.
                    761: 
                    762: =item *
                    763: 
                    764: end_table() : reduce color depth; end table; return scalar string
1.27      matthew   765: 
                    766: =item *
                    767: 
                    768: start_spanning_row() : start a new table row spanning the 'edit' environment.
                    769: 
                    770: =item *
                    771: 
                    772: start_row() : start a new table row and element. 
                    773: 
                    774: =item *
                    775: 
                    776: end_row() : end current table element and row.
1.26      harris41  777: 
                    778: =item *
                    779: 
                    780: movebuttons($target,$token) : move-up and move-down buttons; return scalar
                    781: string
                    782: 
                    783: =item *
                    784: 
                    785: deletelist($target,$token) : provide a yes option in an HTML select element;
                    786: return scalar string
                    787: 
                    788: =item *
                    789: 
                    790: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
                    791: $style) : respond to a user delete request by passing relevant stack
                    792: and array information to various rendering functions; return a scalar string
                    793: 
                    794: =item *
                    795: 
                    796: get_insert_list($token) : provide an insertion list based on possibilities
                    797: from lonxml; return a scalar string
                    798: 
                    799: =item *
                    800: 
                    801: insertlist($target,$token) : api that uses get_insert_list;
                    802: return a scalar string
                    803: 
                    804: =item *
                    805: 
                    806: handleinsert($token) : provide an insertion list based on possibilities
                    807: from lonxml; return a scalar string
                    808: 
                    809: =item *
                    810: 
                    811: get_insert_list($token) : provide an insertion list based on possibilities
                    812: from lonxml; return a scalar string
1.29      matthew   813: 
                    814: =item *
                    815: browse($elementname) : provide a link which will open up the filesystem
                    816: browser (lonindexer) and, once a file is selected, place the result in
1.30      matthew   817: the form element $elementname.
                    818: 
                    819: =item *
                    820: search($elementname) : provide a link which will open up the filesystem
                    821: searcher (lonsearchcat) and, once a file is selected, place the result in
1.29      matthew   822: the form element $elementname.
1.31      matthew   823: 
1.34      harris41  824: =item *
1.32      albertel  825: editline(tag,data,description,size): Provide a <input type="text" ../> for
1.31      matthew   826: single-line text entry.  This is to be used for text enclosed by tags, not
                    827: arguements/parameters associated with a tag.
1.26      harris41  828: 
                    829: =back
                    830: 
                    831: incomplete...
                    832: 
                    833: =cut

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