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

bowersj2 lon-capa-cvs@mail.lon-capa.org
Wed, 13 Aug 2003 14:49:58 -0000


bowersj2		Wed Aug 13 10:49:58 2003 EDT

  Modified files:              
    /loncom/interface	lonhelper.pm 
  Log:
  Add a "dropdown" choice to the helper routines, for use on the print 
  screens.
  
  
Index: loncom/interface/lonhelper.pm
diff -u loncom/interface/lonhelper.pm:1.41 loncom/interface/lonhelper.pm:1.42
--- loncom/interface/lonhelper.pm:1.41	Thu Aug  7 13:26:44 2003
+++ loncom/interface/lonhelper.pm	Wed Aug 13 10:49:58 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # .helper XML handler to implement the LON-CAPA helper
 #
-# $Id: lonhelper.pm,v 1.41 2003/08/07 17:26:44 bowersj2 Exp $
+# $Id: lonhelper.pm,v 1.42 2003/08/13 14:49:58 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1142,7 +1142,6 @@
 }
 
 sub render {
-    # START HERE: Replace this with correct choices code.
     my $self = shift;
     my $var = $self->{'variable'};
     my $buttons = '';
@@ -1258,6 +1257,153 @@
 
     if (ref($chosenValue)) {
         $helper->{VARS}->{$self->{'variable'}} = join('|||', @$chosenValue);
+    }
+
+    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})) {