From bb5dc7322ea04d6bf8c7ddc3b2c9b8575c004e40 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Wed, 2 Oct 2013 15:29:25 +1000 Subject: [PATCH] Renamed function _split_date, added average age calculation to summary report --- booking.helper.inc | 21 ++++++++++++---- booking.register.inc | 6 +---- booking.reports.inc | 60 +++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 33 deletions(-) diff --git a/booking.helper.inc b/booking.helper.inc index 4f0ea1c..6e5a066 100644 --- a/booking.helper.inc +++ b/booking.helper.inc @@ -185,7 +185,7 @@ function _datetime_array_to_ts($date) function _date_to_ts($date) { - $date_split = _split_date($date); + $date_split = _booking_split_date($date); date_default_timezone_set(TIMEZONE); $tz = new DateTimeZone(TIMEZONE); @@ -204,7 +204,7 @@ function _date_to_ts($date) { } function _datetime_to_ts($date) { - $date_split = _split_date($date); + $date_split = _booking_split_date($date); date_default_timezone_set(TIMEZONE); $tz = new DateTimeZone(TIMEZONE); @@ -228,7 +228,7 @@ function _datetime_to_ts($date) { * @param $date in format YYYY-MM-DD * @return array containing Year, Month, Day */ -function _split_date($date) { +function _booking_split_date($date) { $pattern = '/^(\d{4})-(\d{2})-(\d{2})(\s(\d{2})\:(\d{2}))?/'; if (preg_match($pattern, $date, $matches)) { return $matches; @@ -267,9 +267,20 @@ function _booking_convert_ts($timestamp) return $date; } + +/** + * Helper function to ensure timestamp is converted to correct local time + */ +function _booking_max_dob_ts() +{ + //calculate DOB limit as defined by configuration + $max_dob_matches = _booking_split_date(variable_get('booking_max_dob','0')); + return mktime(12, 0, 0, $max_dob_matches[2], $max_dob_matches[3], $max_dob_matches[1]); +} + function _date_range_to_string($date_start, $date_end) { - $start = _split_date($date_start); - $end = _split_date($date_end); + $start = _booking_split_date($date_start); + $end = _booking_split_date($date_end); $final_string = ''; //check for correctly formatted dates diff --git a/booking.register.inc b/booking.register.inc index 175e586..e7d6615 100644 --- a/booking.register.inc +++ b/booking.register.inc @@ -796,12 +796,8 @@ function _booking_validate($node, &$form_state) { if (strlen(trim($email)) > 0 && !_valid_email_address($email)) form_set_error('booking_email', t('You must enter a valid e-mail address. Please ensure you typed your email address correctly.')); - //calculate DOB limit as defined by configuration - $max_dob_matches = _split_date(variable_get('booking_max_dob','0')); - - $max_dob_check= mktime(12, 0, 0, $max_dob_matches[2], $max_dob_matches[3], $max_dob_matches[1]); //if DOB on form is more recent than DOB limit, display validation error - if ($dob_check > $max_dob_check) + if ($dob_check > _booking_max_dob_ts()) form_set_error('booking_dob', t('Unfortunately you are too young to attend !event.', array('!event' => variable_get('booking_event_name','this event')))); diff --git a/booking.reports.inc b/booking.reports.inc index a83445f..29e0679 100644 --- a/booking.reports.inc +++ b/booking.reports.inc @@ -6,8 +6,9 @@ */ function booking_report_summary() { global $event; - $text = ""; + //$text = ""; $output = ""; + $non_australia_count = 0; $bookedin_counter = 0; $notpaid_counter = 0; $waiting_counter = 0; @@ -16,19 +17,22 @@ function booking_report_summary() { $baptised_count = 0; $married_count = 0; $total_paid = 0; + $dob_total = 0; + $person_count = 0; + $average = 0; + $average_age = ""; + //$year = 60 * 60 * 24 * 365.25; $stats_attributes = array('style' => 'max-width:30%'); $header = array('Id', 'Name', 'Email', 'Payment To Date', 'Total Payment Required'); - $rows = array(); - - $result = db_query("SELECT * FROM {booking_person} p WHERE p.booking_event_id = :eid", - array(':eid' => $event->eid)); - - //do some analysis about the people booked in - //first the summary of states + $rows = array(); $state_header = array('State', 'Count'); $state_rows = array(); - $non_australia_count = 0; + $ecclesia_heaeder = array('State','Ecclesia', 'Count'); + $ecclesia_rows = array(); + + //do some analysis about the people booked in + //first the summary of states $query = db_select('booking_person', 'p') ->fields('p', array('booking_state', 'booking_country')) ->condition('p.booking_event_id', $event->eid, '='); @@ -43,16 +47,19 @@ function booking_report_summary() { $non_australia_count += $state->state_count; } //non australian states - $state_rows[] = array('International',$non_australia_count); + $state_rows[] = array('International', $non_australia_count); //general stats $query = db_select('booking_person', 'p') - ->fields('p', array('booking_baptised', 'booking_married', 'booking_gender')) + ->fields('p', array('booking_baptised', 'booking_married', 'booking_gender', 'booking_dob')) ->condition('p.booking_event_id', $event->eid, '='); $general_stats = $query->execute(); foreach ($general_stats as $person) { + $dob_total += $person->booking_dob; + $person_count++; + if ($person->booking_gender == 'M') $male_count++; else @@ -64,10 +71,13 @@ function booking_report_summary() { if ($person->booking_married == 'Y') $married_count++; } + + //calculate the average age timestamp + $average = $dob_total / $person_count; + //convert that to years + $average_age = floor((time() - $average) / (60 * 60 * 24 * 365.25)); //bookings by ecclesia - $ecclesia_heaeder = array('State','Ecclesia', 'Count'); - $ecclesia_rows = array(); $query = db_select('booking_person', 'p') ->fields('p', array('booking_ecclesia','booking_state')) ->condition('p.booking_event_id', $event->eid, '='); @@ -82,6 +92,8 @@ function booking_report_summary() { } //more detailed summary + $result = db_query("SELECT * FROM {booking_person} p WHERE p.booking_event_id = :eid", + array(':eid' => $event->eid)); foreach ($result as $person) { $rows[] = array( l(t('!id', array('!id' => $person->nid)), t('node/!id', array('!id' => $person->nid))), @@ -101,17 +113,19 @@ function booking_report_summary() { } //output everything - $output .= t("

There are !boys males and !girls females registered. Of these, !baptised are baptised and !married are married.

", - array('!boys' => $male_count, '!girls' => $female_count, '!baptised' => $baptised_count, '!married' => $married_count)); - $output .= t("

Bookings by state

"); - $output .= theme('table', array('header' => $state_header, 'rows' => $state_rows, 'attributes' => $stats_attributes)); - $output .= t("

Bookings by ecclesia

"); - $output .= theme('table', array('header' => $ecclesia_heaeder, 'rows' => $ecclesia_rows, 'attributes' => $stats_attributes)); - $output .= t("

The following table presents a summary of payments that have been made for !event.

", array('!event' => $event->booking_eventname)); - $output .= theme('table', array('header' => $header, 'rows' => $rows)); - $output .= t("

Total of !bookedin registrations currently booked in, !waiting on waiting list, and !notpaid haven't paid.

", + $output .= t("

There are !boys males and !girls females registered. Of these, !baptised are baptised and !married are married. The average age is !average years.", + array('!boys' => $male_count, '!girls' => $female_count, '!baptised' => $baptised_count, '!married' => $married_count + '!average' => $average_age)); + $output .= t("Total of !bookedin registrations currently booked in, !waiting on waiting list, and !notpaid haven't paid.
", array('!bookedin' => $bookedin_counter, '!waiting' => $waiting_counter, '!notpaid' => $notpaid_counter)); - $output .= t("

Total amount paid: $!paid

", array('!paid' => $total_paid)); + $output .= t("Total amount paid: $!paid

", array('!paid' => $total_paid)); + $output .= t("

Bookings by state

"); + $output .= theme('table', array('header' => $state_header, 'rows' => $state_rows, 'attributes' => $stats_attributes)); + $output .= t("

Bookings by ecclesia

"); + $output .= theme('table', array('header' => $ecclesia_heaeder, 'rows' => $ecclesia_rows, 'attributes' => $stats_attributes)); + $output .= t("

Summary of attendees for !event.

", array('!event' => $event->booking_eventname)); + $output .= theme('table', array('header' => $header, 'rows' => $rows)); + return $output; }