[LON-CAPA-cvs] cvs: modules /raeburn/register Utilities.pm
raeburn
lon-capa-cvs-allow@mail.lon-capa.org
Mon, 14 Apr 2008 15:28:56 -0000
This is a MIME encoded message
--raeburn1208186936
Content-Type: text/plain
raeburn Mon Apr 14 11:28:56 2008 EDT
Added files:
/modules/raeburn/register Utilities.pm
Log:
- Date form elements and routines to retrieve values from these form elements.
--raeburn1208186936
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080414112856.txt"
Index: modules/raeburn/register/Utilities.pm
+++ modules/raeburn/register/Utilities.pm
##########################################################
#
# Generate date/time form elements and retrieve dates/times
# from them. Used by registrations.pm
#
# $Id: Utilities.pm,v 1.1 2008/04/14 15:28:56 raeburn Exp $
#
# Stuart P Raeburn
#
##########################################################
package Apache::LON::Utilities;
use strict;
use Time::Local;
use Time::HiRes;
=pod
=item &date_setter
&date_setter returns html and javascript for a compact date-setting form.
To retrieve values from it, use &get_date_from_form().
Inputs
=over 4
=item $dname
The name to prepend to the form elements.
The form elements defined will be dname_year, dname_month, dname_day,
dname_hour, dname_min, and dname_sec.
=item $currentvalue
The current setting for this time parameter. A unix format time
(time in seconds since the beginning of Jan 1st, 1970, GMT.
An undefined value is taken to indicate the value is the current time.
Also, to be explicit, a value of 'now' also indicates the current time.
=item $special
Additional html/javascript to be associated with each element in
the date_setter. See lonparmset for example usage.
=item $includeempty
=item $state
Specifies the initial state of the form elements. Either 'disabled' or empty.
Defaults to empty, which indiciates the form elements are not disabled.
=back
Bugs
The method used to restrict user input will fail in the year 2400.
=cut
sub date_setter {
my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
$no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
my $wasdefined=1;
if (! defined($state) || $state ne 'disabled') {
$state = '';
}
if (! defined($no_hh_mm_ss)) {
$no_hh_mm_ss = 0;
}
if ($currentvalue eq 'now') {
$currentvalue=time;
}
if ((!defined($currentvalue)) || ($currentvalue eq '')) {
$wasdefined=0;
if ($includeempty) {
$currentvalue = 0;
} else {
$currentvalue = time;
}
}
# other potentially useful values: wkday,yrday,is_daylight_savings
my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
if ($currentvalue) {
($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) =
localtime($currentvalue);
$year += 1900;
}
unless ($wasdefined) {
if (($defhour) || ($defmin) || ($defsec)) {
($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) =
localtime(time);
$year += 1900;
$sec=($defsec?$defsec:0);
$min=($defmin?$defmin:0);
$hour=($defhour?$defhour:0);
} elsif (!$includeempty) {
$sec=0;
$min=0;
$hour=0;
}
}
my $result = "\n<!-- $dname date setting form -->\n";
$result .= <<ENDJS;
<script type="text/javascript">
function $dname\_checkday() {
var day = document.$formname.$dname\_day.value;
var month = document.$formname.$dname\_month.value;
var year = document.$formname.$dname\_year.value;
var valid = true;
if (day < 1) {
document.$formname.$dname\_day.value = 1;
}
if (day > 31) {
document.$formname.$dname\_day.value = 31;
}
if ((month == 1) || (month == 3) || (month == 5) ||
(month == 7) || (month == 8) || (month == 10) ||
(month == 12)) {
if (day > 31) {
document.$formname.$dname\_day.value = 31;
day = 31;
}
} else if (month == 2 ) {
if ((year % 4 == 0) && (year % 100 != 0)) {
if (day > 29) {
document.$formname.$dname\_day.value = 29;
}
} else if (day > 29) {
document.$formname.$dname\_day.value = 28;
}
} else if (day > 30) {
document.$formname.$dname\_day.value = 30;
}
}
function $dname\_disable() {
document.$formname.$dname\_month.disabled=true;
document.$formname.$dname\_day.disabled=true;
document.$formname.$dname\_year.disabled=true;
document.$formname.$dname\_hour.disabled=true;
document.$formname.$dname\_minute.disabled=true;
document.$formname.$dname\_second.disabled=true;
}
function $dname\_enable() {
document.$formname.$dname\_month.disabled=false;
document.$formname.$dname\_day.disabled=false;
document.$formname.$dname\_year.disabled=false;
document.$formname.$dname\_hour.disabled=false;
document.$formname.$dname\_minute.disabled=false;
document.$formname.$dname\_second.disabled=false;
}
function $dname\_opencalendar() {
if (! document.$formname.$dname\_month.disabled) {
var calwin=window.open(
"/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
document.$formname.$dname\_month.value+"&year="+
document.$formname.$dname\_year.value,
"LONCAPAcal",
"height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
}
}
</script>
ENDJS
$result .= ' <span style="white-space: nowrap;">';
my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
# Month
my @Months = qw/January February March April May June July August September October November December/;
# Pad @Months with a bogus value to make indexing easier
unshift(@Months,'If you can read this an error occurred');
if ($includeempty) { $monthselector.="<option value=''></option>"; }
for(my $m = 1;$m <=$#Months;$m++) {
$monthselector .= qq{ <option value="$m" };
$monthselector .= "selected " if ($m-1 eq $month);
$monthselector .= '> '.$Months[$m].' </option>';
}
$monthselector.= ' </select>';
# Day
my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
# Year
my $yearselector = qq{<input type="year" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
#
my $hourselector = qq{<select name="$dname\_hour" $special $state >};
if ($includeempty) {
$hourselector.=qq{<option value=''></option>};
}
for (my $h = 0;$h<24;$h++) {
$hourselector .= qq{<option value="$h" };
$hourselector .= "selected " if (defined($hour) && $hour == $h);
$hourselector .= ">";
my $timest='';
if ($h == 0) {
$timest .= "12 am";
} elsif($h == 12) {
$timest .= "12 noon";
} elsif($h < 12) {
$timest .= "$h am";
} else {
$timest .= $h-12 ." pm";
}
$hourselector .= $timest." </option>\n";
}
$hourselector .= " </select>\n";
my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
my $cal_link;
if (!$nolink) {
$cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
}
#
if ($no_hh_mm_ss) {
$result .= "$monthselector $dayselector $yearselector ";
if (!$nolink) {
$result .= $cal_link.'Select Date</a>';
}
} else {
$result .= "$monthselector $dayselector $yearselector $hourselector ".$minuteselector.'m '.$secondselector.'s ';
if (!$nolink) {
$result .= $cal_link.'Select Date</a>';
}
}
$result .= "</span>\n<!-- end $dname date setting form -->\n";
return $result;
}
##############################################
##############################################
=pod
=item &get_date_from_form
get_date_from_form retrieves the date specified in an &date_setter form.
Inputs:
=over 4
=item $dname
The name passed to &datesetter, which prefixes the form elements.
=back
Returns: Unix time represented in the form.
=cut
##############################################
##############################################
sub get_date_from_form {
my ($params,$dname) = @_;
my ($sec,$min,$hour,$day,$month,$year);
#
if (ref($params) eq 'HASH') {
if (defined($params->{$dname.'_second'})) {
my $tmpsec = $params->{$dname.'_second'};
if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
$sec = $tmpsec;
}
if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
} else {
$sec = 0;
}
if (defined($params->{$dname.'_minute'})) {
my $tmpmin = $params->{$dname.'_minute'};
if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
$min = $tmpmin;
}
if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
} else {
$min = 0;
}
if (defined($params->{$dname.'_hour'})) {
my $tmphour = $params->{$dname.'_hour'};
if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
$hour = $tmphour;
}
} else {
$hour = 0;
}
if (defined($params->{$dname.'_day'})) {
my $tmpday = $params->{$dname.'_day'};
if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
$day = $tmpday;
}
}
if (defined($params->{$dname.'_month'})) {
my $tmpmonth = $params->{$dname.'_month'};
if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
$month = $tmpmonth - 1;
}
}
if (defined($params->{$dname.'_year'})) {
my $tmpyear = $params->{$dname.'_year'};
if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
$year = $tmpyear - 1900;
}
}
if (($year<70) || ($year>137)) { return undef; }
if (defined($sec) && defined($min) && defined($hour) &&
defined($day) && defined($month) && defined($year) &&
eval('&timelocal($sec,$min,$hour,$day,$month,$year)')) {
return &timelocal($sec,$min,$hour,$day,$month,$year);
} else {
return undef;
}
} else {
return undef;
}
}
1;
__END__
--raeburn1208186936--