From 8d8f0803383eea01e24dfea40cf30f83687c6391 Mon Sep 17 00:00:00 2001
From: Nathan Coad
Date: Fri, 19 Jun 2015 11:53:01 +1000
Subject: [PATCH] More analytics for bookings by state
---
booking.reports.inc | 76 ++++++++++++++++++++++++++++++++++-----------
1 file changed, 58 insertions(+), 18 deletions(-)
diff --git a/booking.reports.inc b/booking.reports.inc
index 0d7803b..5daf1cf 100644
--- a/booking.reports.inc
+++ b/booking.reports.inc
@@ -59,36 +59,58 @@ function booking_report_summary() {
$header[] = array('data' => t('Welfare Required?'), 'field' => 'booking_welfare_required');
$header[] = array('data' => t('Committee?'), 'field' => 'booking_committee_member');
$rows = array();
- $state_header = array('State', 'Count');
+
+ //the state summary table
+ $state_header = array('State', 'Count', 'Males', 'Male Average Age', 'Females', 'Female Average Age', 'Overall Average Age', 'Baptised');
$state_rows = array();
- $ecclesia_header = array('State','Ecclesia', 'Count');
- $ecclesia_rows = array();
+ $state_statistics = 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'));
-
+ //include people either booked in or on the waiting list, that belong to the current event id
$db_or = db_or();
$db_or->condition('p.booking_status', 1, '=');
$db_or->condition('p.booking_status', 2, '=');
$db_and = db_and()->condition($db_or)->condition('p.booking_event_id', $event->eid, '=');
$query->condition($db_and);
-
$query->groupBy('p.booking_state');
$query->addExpression('COUNT(p.booking_state)', 'state_count');
$query->orderBy('state_count', 'DESC');
$state_stats = $query->execute();
+ //TODO: add some info to a hash so we can calculate age info and generate the actual rows after the more detailed summary loop
+ //include count of guys, girls, minimum, maximum and average ages
foreach ($state_stats as $state) {
- if (strcmp($state->booking_country,'Australia') == 0 )
- $state_rows[] = array($state->booking_state, $state->state_count);
- else
+ if (strcmp($state->booking_country,'Australia') == 0 ) {
+ //$state_rows[] = array($state->booking_state, $state->state_count);
+ //store the total count for this state
+ $state_statistics[$state->booking_state]->total_count = $state->state_count;
+ $state_statistics[$state->booking_state]->male_count = 0;
+ $state_statistics[$state->booking_state]->male_avg = 0;
+ $state_statistics[$state->booking_state]->female_count = 0;
+ $state_statistics[$state->booking_state]->female_avg = 0;
+ $state_statistics[$state->booking_state]->total_avg = 0;
+ $state_statistics[$state->booking_state]->baptised_count = 0;
+ } else {
$non_australia_count += $state->state_count;
+ }
}
//non australian states
- $state_rows[] = array('International', $non_australia_count);
-
+ //$state_rows[] = array('International', $non_australia_count);
+ $state_statistics['International']->total_count = $non_australia_count;
+ $state_statistics['International']->male_count = 0;
+ $state_statistics['International']->male_avg = 0;
+ $state_statistics['International']->female_count = 0;
+ $state_statistics['International']->female_avg = 0;
+ $state_statistics['International']->total_avg = 0;
+ $state_statistics['International']->baptised_count = 0;
+
+ //the ecclesia summary table
+ $ecclesia_header = array('State','Ecclesia', 'Count');
+ $ecclesia_rows = array();
+
//bookings by ecclesia
$query = db_select('booking_person', 'p')
->fields('p', array('booking_ecclesia','booking_state'))
@@ -168,7 +190,6 @@ function booking_report_summary() {
{
$total_refunds += $person->booking_refund_due;
}
-
//travel form completed?
if (! empty($person->tid))
@@ -200,29 +221,48 @@ function booking_report_summary() {
$person_count++;
+ $state = strcmp($person->booking_country,'Australia') == 0 ? $person->booking_state : 'International';
+
//general stats for booked in people
- if ($person->booking_status == 1)
- {
+ if ($person->booking_status == 1) {
+ //for overall average age
$dob_total += $person->booking_dob;
+ //store data for average ages per gender and state
if ($person->booking_gender == 'M')
{
$male_count++;
$male_dob_total += $person->booking_dob;
- }
- else
- {
+ $state_statistics[$state]->male_avg += $person->booking_dob;
+ $state_statistics[$state]->male_count++;
+ } else {
$female_count++;
$female_dob_total += $person->booking_dob;
+ $state_statistics[$state]->female_avg += $person->booking_dob;
+ $state_statistics[$state]->female_count++;
}
- if ($person->booking_baptised == 'Y')
+ if ($person->booking_baptised == 'Y') {
$baptised_count++;
+ $state_statistics[$state]->baptised_count++;
+ }
if ($person->booking_married == 'Y')
$married_count++;
}
}
+
+ //generate the table for state statistics
+ foreach ($state_statistics as $key => $value) {
+ //$state_rows[] = array($state, $state->total_count, $state->male_count, $state->female_count, 0);
+ $male_average_age = _booking_avg_age($value->male_avg, $value->male_count, $event->booking_event_start);
+ $female_average_age = _booking_avg_age($value->female_avg, $value->female_count, $event->booking_event_start);
+ $state_average_age = _booking_avg_age($value->female_avg + $value->male_avg, $value->female_count + $value->male_count, $event->booking_event_start);
+
+ $data = array($key, $value->total_count, $value->male_count, $male_average_age, $value->female_count, $female_average_age, $state_average_age, $value->baptised_count);
+ //watchdog('booking', "State statistics:\n@info
", array('@info' => print_r( $data, true)));
+ $state_rows[] = $data;
+ }
//output everything
$output .= t("General Statistics
");
@@ -246,7 +286,7 @@ function booking_report_summary() {
));
$output .= t("Total amount paid: $!paid, minus $!refunds in refunds.
", array('!paid' => $total_paid, '!refunds' => $total_refunds));
$output .= t("Bookings by state
");
- $output .= theme('table', array('header' => $state_header, 'rows' => $state_rows, 'attributes' => $stats_attributes));
+ $output .= theme('table', array('header' => $state_header, 'rows' => $state_rows));
$output .= t("Bookings by ecclesia
");
$output .= theme('table', array('header' => $ecclesia_header, 'rows' => $ecclesia_rows, 'attributes' => $stats_attributes));
$output .= t("Summary of attendees for !event.
", array('!event' => $event->booking_eventname));