[LON-CAPA-cvs] cvs: loncom /interface lonwizard.pm

bowersj2 lon-capa-cvs@mail.lon-capa.org
Fri, 21 Feb 2003 18:50:09 -0000


bowersj2		Fri Feb 21 13:50:09 2003 EDT

  Modified files:              
    /loncom/interface	lonwizard.pm 
  Log:
  Because, like, the Number One rule of coding is "Duplication is the
  Spawn of the Devil." 
  
  Down with copy/paste coding! Forward the revolution! ;-)
  
  
  
Index: loncom/interface/lonwizard.pm
diff -u loncom/interface/lonwizard.pm:1.10 loncom/interface/lonwizard.pm:1.11
--- loncom/interface/lonwizard.pm:1.10	Thu Feb 20 17:10:06 2003
+++ loncom/interface/lonwizard.pm	Fri Feb 21 13:50:09 2003
@@ -132,8 +132,7 @@
 
 	# 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 @@
 
 =item B<render>(): 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 @@
     return 1;
 }
 
+=pod
+
+=item * B<process_multiple_choices>(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 @@
     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 @@
     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.";
     }