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

albertel lon-capa-cvs@mail.lon-capa.org
Tue, 11 Oct 2005 20:57:57 -0000


albertel		Tue Oct 11 16:57:57 2005 EDT

  Modified files:              
    /loncom/interface	lonhelper.pm 
  Log:
  - add the ability for a <date> to return an 'anytime'
  - allow <date> <defaultvalue> to actually work
  
  
Index: loncom/interface/lonhelper.pm
diff -u loncom/interface/lonhelper.pm:1.117 loncom/interface/lonhelper.pm:1.118
--- loncom/interface/lonhelper.pm:1.117	Tue Oct 11 11:44:30 2005
+++ loncom/interface/lonhelper.pm	Tue Oct 11 16:57:54 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # .helper XML handler to implement the LON-CAPA helper
 #
-# $Id: lonhelper.pm,v 1.117 2005/10/11 15:44:30 albertel Exp $
+# $Id: lonhelper.pm,v 1.118 2005/10/11 20:57:54 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1536,6 +1536,7 @@
     $paramHash->{'variable'} = $token->[2]{'variable'};
     $helper->declareVar($paramHash->{'variable'});
     $paramHash->{'hoursminutes'} = $token->[2]{'hoursminutes'};
+    $paramHash->{'anytime'} = $token->[2]{'anytime'};
 }
 
 sub end_date {
@@ -1554,9 +1555,19 @@
     my $var = $self->{'variable'};
 
     my $date;
-    
+
+    my $time=time;
+    my $anytime;
+
+    if (defined($self->{DEFAULT_VALUE})) {
+        my $valueFunc = eval($self->{DEFAULT_VALUE});
+        die('Error in default value code for variable ' . 
+            $self->{'variable'} . ', Perl said: ' . $@) if $@;
+        $time = &$valueFunc($helper, $self);
+	if (lc($time) eq 'anytime') { $time=time; $anytime=1; }
+    }
     # Default date: The current hour.
-    $date = localtime();
+    $date = localtime($time);
     $date->min(0);
 
     if (defined $self->{ERROR_MSG}) {
@@ -1644,7 +1655,13 @@
         }
         $result .= "</select>\n";
     }
-
+    if ($self->{'anytime'}) {
+	$result.="&nbsp;or&nbsp;<label><input type='checkbox' ";
+	if ($anytime) {
+	    $result.=' checked="checked" '
+	}
+	$result.="name='${var}anytime'/>".&mt('Anytime').'</label>'
+    }
     return $result;
 
 }
@@ -1652,41 +1669,44 @@
 sub postprocess {
     my $self = shift;
     my $var = $self->{'variable'};
-    my $month = $env{'form.' . $var . 'month'}; 
-    my $day = $env{'form.' . $var . 'day'}; 
-    my $year = $env{'form.' . $var . 'year'}; 
-    my $min = 0; 
-    my $hour = 0;
-    if ($self->{'hoursminutes'}) {
-        $min = $env{'form.' . $var . 'minute'};
-        $hour = $env{'form.' . $var . 'hour'};
-    }
+    if ($env{'form.' . $var . 'anytime'}) {
+	$helper->{VARS}->{$var} = undef;
+    } else {
+	my $month = $env{'form.' . $var . 'month'}; 
+	my $day = $env{'form.' . $var . 'day'}; 
+	my $year = $env{'form.' . $var . 'year'}; 
+	my $min = 0; 
+	my $hour = 0;
+	if ($self->{'hoursminutes'}) {
+	    $min = $env{'form.' . $var . 'minute'};
+	    $hour = $env{'form.' . $var . 'hour'};
+	}
+
+	my $chosenDate;
+	eval {$chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);};
+	my $error = $@;
+
+	# Check to make sure that the date was not automatically co-erced into a 
+	# valid date, as we want to flag that as an error
+	# This happens for "Feb. 31", for instance, which is coerced to March 2 or
+	# 3, depending on if it's a leap year
+	my $checkDate = localtime($chosenDate);
+	
+	if ($error || $checkDate->mon != $month || $checkDate->mday != $day ||
+	    $checkDate->year + 1900 != $year) {
+	    unless (Apache::lonlocal::current_language()== ~/^en/) {
+		$self->{ERROR_MSG} = &mt("Invalid date entry");
+		return 0;
+	    }
+	    # LOCALIZATION FIXME: Needs to be parameterized
+	    $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
+		. "date because it doesn't exist. Please enter a valid date.";
 
-    my $chosenDate;
-    eval {$chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);};
-    my $error = $@;
-
-    # Check to make sure that the date was not automatically co-erced into a 
-    # valid date, as we want to flag that as an error
-    # This happens for "Feb. 31", for instance, which is coerced to March 2 or
-    # 3, depending on if it's a leap year
-    my $checkDate = localtime($chosenDate);
-
-    if ($error || $checkDate->mon != $month || $checkDate->mday != $day ||
-        $checkDate->year + 1900 != $year) {
-	unless (Apache::lonlocal::current_language()== ~/^en/) {
-	    $self->{ERROR_MSG} = &mt("Invalid date entry");
 	    return 0;
 	}
-	# LOCALIZATION FIXME: Needs to be parameterized
-        $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
-            . "date because it doesn't exist. Please enter a valid date.";
-
-        return 0;
+	$helper->{VARS}->{$var} = $chosenDate;
     }
 
-    $helper->{VARS}->{$var} = $chosenDate;
-
     if (defined($self->{NEXTSTATE})) {
         $helper->changeState($self->{NEXTSTATE});
     }