From d5d45512c6e73aa8fee976d6f365f1119fc62bfa Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Tue, 28 Jun 2016 13:12:55 +1000 Subject: [PATCH] update room number field to be an integer --- booking.helper.inc | 37 +++++++++++++++++++++++++++++++++++++ booking.install | 12 +++++++++++- booking.reports.inc | 3 ++- booking.rooms_admin.inc | 12 ++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/booking.helper.inc b/booking.helper.inc index f8329bc..3634600 100644 --- a/booking.helper.inc +++ b/booking.helper.inc @@ -1657,6 +1657,43 @@ function _booking_room_email_summary($node) { return implode("\n", $rows); } +/** + * Helper function to create the mean, median, mode or average of an array + * @see http://www.phpsnips.com/45/Mean,-Median,-Mode,-Range-Of-An-Array + */ +function _booking_mmmr($array, $output = 'mean'){ + if(!is_array($array)) { + return FALSE; + } + else { + switch($output){ + case 'mean': + $count = count($array); + $sum = array_sum($array); + $total = $sum / $count; + break; + case 'median': + rsort($array); + $middle = round(count($array) / 2); + $total = $array[$middle-1]; + break; + case 'mode': + $v = array_count_values($array); + arsort($v); + foreach($v as $k => $v){$total = $k; break;} + break; + case 'range': + sort($array); + $sml = $array[0]; + rsort($array); + $lrg = $array[0]; + $total = $lrg - $sml; + break; + } + return $total; + } +} + /** * @brief Generates a Universally Unique IDentifier, version 4. * diff --git a/booking.install b/booking.install index 8440949..04d8eef 100644 --- a/booking.install +++ b/booking.install @@ -595,6 +595,16 @@ function booking_update_7238() { _booking_node_create_mysqlview(); } +/** +* Change room number field in room definitions to integer +*/ +function booking_update_7239() { + $spec = array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'disp-width' => '10', 'default' => 0); + db_change_field('booking_room_definition', 'booking_room_number', 'booking_room_number', $spec); + //update the view to match the new table definition + _booking_node_create_mysqlview(); +} + /** * Implementation of hook_install(). */ @@ -926,7 +936,7 @@ function booking_schema() { 'fields' => array( 'rid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => '10'), 'booking_room_location_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'disp-width' => '10', 'default' => 0), - 'booking_room_number' => array('type' => 'varchar', 'length' => '500', 'not null' => FALSE), + 'booking_room_number' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'disp-width' => '10', 'default' => 0), 'booking_room_singlebeds' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'disp-width' => '10', 'default' => 0), 'booking_room_doublebeds' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'disp-width' => '10', 'default' => 0), 'booking_room_queenbeds' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'disp-width' => '10', 'default' => 0), diff --git a/booking.reports.inc b/booking.reports.inc index f8443fa..565849f 100644 --- a/booking.reports.inc +++ b/booking.reports.inc @@ -279,7 +279,8 @@ function booking_report_summary() { )); $output .= t("

Ages

"); $output .= t("

The combined average age at the start of the week will be !average.
The male average age will be !maleaverage.
The female average age will be !femaleaverage.

", - array('!average' => _booking_avg_age($dob_total, $male_count + $female_count, $event->booking_event_start), '!maleaverage' => _booking_avg_age($male_dob_total, $male_count, $event->booking_event_start), + array('!average' => _booking_avg_age($dob_total, $male_count + $female_count, $event->booking_event_start), + '!maleaverage' => _booking_avg_age($male_dob_total, $male_count, $event->booking_event_start), '!femaleaverage' => _booking_avg_age($female_dob_total, $female_count, $event->booking_event_start) )); $output .= t("

Finances

"); diff --git a/booking.rooms_admin.inc b/booking.rooms_admin.inc index 428907e..725e247 100644 --- a/booking.rooms_admin.inc +++ b/booking.rooms_admin.inc @@ -425,6 +425,18 @@ function booking_rooms_definition_form($node, &$form_state, $create, $room_id = } } +/** + * Validate the form for adding room definitions + */ +function booking_rooms_definition_form_validate($form, $form_state) { + //make sure room number is numerical + $values = $form_state['input']; + + if (! is_numeric($values['booking_room_number'])) { + form_set_error('booking_room_number', t('Room number must be numeric. If you need to use a more descriptive label, please use the room label field.')); + } +} + /** * Process the form for adding room definitions */