--- loncom/interface/Attic/lonwizard.pm 2003/02/20 22:10:06 1.10 +++ loncom/interface/Attic/lonwizard.pm 2003/02/21 18:50:09 1.11 @@ -132,8 +132,7 @@ sub declareVars { # if there's a form in the env, use that instead my $envname = "form." . $element; - if (defined ($ENV{$envname})) - { + if (defined ($ENV{$envname})) { $self->{VARS}->{$element} = $ENV{$envname}; } @@ -470,8 +469,6 @@ These methods should be overridden in de =item B(): render returns a string of itself to be rendered to the screen, which the wizard will display. -=back - =cut package Apache::lonwizard::state; @@ -508,6 +505,40 @@ sub preprocess { return 1; } +=pod + +=item * B(formname, var_name): A service function that correctly handles resources with multiple selections, such as checkboxes. It delimits the selections with triple pipes and stores them in the given wizard variable. 'formname' is the name of the form element to process. + +=back + +=cut + +sub process_multiple_choices { + my $self = shift; + my $formname = shift; + my $var = shift; + my $wizard = $self->{WIZARD}; + + my $formvalue = $ENV{'form.' . $var}; + if ($formvalue) { + # Must extract values from $wizard->{DATA} directly, as there + # may be more then one. + my @values; + for my $formparam (split (/&/, $wizard->{DATA})) { + my ($name, $value) = split(/=/, $formparam); + if ($name ne $var) { + next; + } + $value =~ tr/+/ /; + $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; + push @values, $value; + } + $wizard->setVar($var, join('|||', @values)); + } + + return; +} + sub render { return "This is the empty state. If you can see this, it's a bug.\n" } @@ -1304,22 +1335,8 @@ sub postprocess { my $self = shift; my $wizard = $self->{WIZARD}; - my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'}; - if ($formvalue) { - # Must extract values from $wizard->{DATA} directly, as there - # may be more then one. - my @values; - for my $formparam (split (/&/, $wizard->{DATA})) { - my ($name, $value) = split(/=/, $formparam); - if ($name ne $self->{VAR_NAME} . '.forminput') { - next; - } - $value =~ tr/+/ /; - $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; - push @values, $value; - } - $wizard->setVar($self->{VAR_NAME}, join('|||', @values)); - } + $self->process_multiple_choices($self->{VAR_NAME}.'.forminput', + $self->{VAR_NAME}); # If nothing was selected... if (!$wizard->{VARS}->{$self->{VAR_NAME}}) { @@ -1576,24 +1593,11 @@ sub postprocess { my $self = shift; print $self->{NEXT_STATE}; my $wizard = $self->{WIZARD}; - my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'}; - if ($formvalue) { - # Must extract values from $wizard->{DATA} directly, as there - # may be more then one. - my @values; - for my $formparam (split (/&/, $wizard->{DATA})) { - my ($name, $value) = split(/=/, $formparam); - if ($name ne $self->{VAR_NAME} . '.forminput') { - next; - } - $value =~ tr/+/ /; - $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; - push @values, $value; - } - $wizard->setVar($self->{VAR_NAME}, join('|||', @values)); - $wizard->changeState($self->{NEXT_STATE}); - } else { + $self->process_multiple_choices($self->{VAR_NAME}.'.forminput', + $self->{VAR_NAME}); + + if (!$wizard->{VARS}->{$self->{VAR_NAME}}) { $self->{ERROR_MSG} = "Can't continue the wizard because you ". "must make a selection to continue."; }