Diff for /loncom/interface/lonhelper.pm between versions 1.41 and 1.42

version 1.41, 2003/08/07 17:26:44 version 1.42, 2003/08/13 14:49:58
Line 1142  sub end_choice { Line 1142  sub end_choice {
 }  }
   
 sub render {  sub render {
     # START HERE: Replace this with correct choices code.  
     my $self = shift;      my $self = shift;
     my $var = $self->{'variable'};      my $var = $self->{'variable'};
     my $buttons = '';      my $buttons = '';
Line 1261  sub postprocess { Line 1260  sub postprocess {
     }      }
   
     if (defined($self->{NEXTSTATE})) {      if (defined($self->{NEXTSTATE})) {
           $helper->changeState($self->{NEXTSTATE});
       }
       
       foreach my $choice (@{$self->{CHOICES}}) {
           if ($choice->[1] eq $chosenValue) {
               if (defined($choice->[2])) {
                   $helper->changeState($choice->[2]);
               }
           }
       }
       return 1;
   }
   1;
   
   package Apache::lonhelper::dropdown;
   
   =pod
   
   =head2 Element: dropdown
   
   A drop-down provides a drop-down box instead of a radio button
   box. Because most people do not know how to use a multi-select
   drop-down box, that option is not allowed. Otherwise, the arguments
   are the same as "choices", except "allowempty" is also meaningless.
   
   <dropdown> takes an attribute "variable" to control which helper variable
   the result is stored in.
   
   B<SUB-TAGS>
   
   <choice>, which acts just as it does in the "choices" element.
   
   =back
   
   =cut
   
   no strict;
   @ISA = ("Apache::lonhelper::element");
   use strict;
   
   BEGIN {
       &Apache::lonhelper::register('Apache::lonhelper::dropdown',
                                 ('dropdown'));
   }
   
   sub new {
       my $ref = Apache::lonhelper::element->new();
       bless($ref);
   }
   
   # CONSTRUCTION: Construct the message element from the XML
   sub start_dropdown {
       my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   
       if ($target ne 'helper') {
           return '';
       }
   
       # Need to initialize the choices list, so everything can assume it exists
       $paramHash->{'variable'} = $token->[2]{'variable'} if (!defined($paramHash->{'variable'}));
       $helper->declareVar($paramHash->{'variable'});
       $paramHash->{CHOICES} = [];
       return '';
   }
   
   sub end_dropdown {
       my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   
       if ($target ne 'helper') {
           return '';
       }
       Apache::lonhelper::dropdown->new();
       return '';
   }
   
   sub render {
       my $self = shift;
       my $var = $self->{'variable'};
       my $result = '';
   
       if (defined $self->{ERROR_MSG}) {
           $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
       }
   
       my %checkedChoices;
       my $checkedChoicesFunc;
   
       if (defined($self->{DEFAULT_VALUE})) {
           $checkedChoicesFunc = eval ($self->{DEFAULT_VALUE});
           die 'Error in default value code for variable ' . 
               $self->{'variable'} . ', Perl said: ' . $@ if $@;
       } else {
           $checkedChoicesFunc = sub { return ''; };
       }
   
       # single choice
       my $selectedChoice = &$checkedChoicesFunc($helper, $self);
       
       my $foundChoice = 0;
       
       # check that the choice is in the list of choices.
       for my $choice (@{$self->{CHOICES}}) {
    if ($choice->[1] eq $selectedChoice) {
       $checkedChoices{$choice->[1]} = 1;
       $foundChoice = 1;
    }
       }
       
       # If we couldn't find the choice, pick the first one 
       if (!$foundChoice) {
    $checkedChoices{$self->{CHOICES}->[0]->[1]} = 1;
       }
   
       $result .= "<select name='${var}.forminput'>\n";
       foreach my $choice (@{$self->{CHOICES}}) {
           $result .= "<option value='" . 
               HTML::Entities::encode($choice->[1]) 
               . "'";
           if ($checkedChoices{$choice->[1]}) {
               $result .= " selected";
           }
           my $choiceLabel = $choice->[0];
           if ($choice->[4]) {  # if we need to evaluate this choice
               $choiceLabel = "sub { my $helper = shift; my $state = shift;" .
                   $choiceLabel . "}";
               $choiceLabel = eval($choiceLabel);
               $choiceLabel = &$choiceLabel($helper, $self);
           }
           $result .= ">" . $choiceLabel . "\n";
       }
   
       return $result;
   }
   
   # If a NEXTSTATE was given or a nextstate for this choice was
   # given, switch to it
   sub postprocess {
       my $self = shift;
       my $chosenValue = $ENV{'form.' . $self->{'variable'} . '.forminput'};
   
       if (!defined($chosenValue) && !$self->{'allowempty'}) {
           $self->{ERROR_MSG} = "You must choose one or more choices to" .
               " continue.";
           return 0;
       }
   
       if (defined($self->{NEXTSTATE})) {
         $helper->changeState($self->{NEXTSTATE});          $helper->changeState($self->{NEXTSTATE});
     }      }
           

Removed from v.1.41  
changed lines
  Added in v.1.42


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