Files
booking/booking.rooms.inc

1196 lines
40 KiB
PHP

<?php
/**
* @file
* Functions to handle room allocation and administration for the booking module
*/
/**
* Function for listing the room locations links to view each one and assign people to rooms
*/
function booking_room_view_summary() {
global $event;
$output = "";
$header = array('Location', 'Assign Attendees', 'View Rooms For Location');
$attributes = array('style' => 'max-width:45%');
$rows = array();
//get room definitions
$room_definitions = _booking_room_location_lookup();
//ditch the first element which is empty
array_shift($room_definitions);
$i = 1;
foreach ($room_definitions as $room) {
$rows[] = array(
$room,
l(t('Allocate Rooms'), t('admin/booking/rooms/!id/assign', array('!id' => $i))),
l(t('View Rooms'), t('admin/booking/rooms/!id/view', array('!id' => $i))),
);
$i++;
}
//output everything
$output .= t("<h3>!event Room Locations</h3>", array('!event' => $event->booking_eventname));
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
return $output;
}
/**
* Function for viewing a printable format of who belongs to which study group
*/
function booking_roomallocations_view_summary() {
global $event;
$rows = array();
$header = array(
'booking_firstname' => array('data' => t('First Name'), 'field' => 'p.booking_firstname'),
'booking_lastname' => array('data' => t('Last Name'), 'field' => 'p.booking_lastname', 'sort' => 'asc'),
'booking_gender' => array('data' => t('Gender'), 'field' => 'p.booking_gender'),
'booking_age' => array('data' => t('Age'), 'field' => 'p.booking_dob'),
'booking_married' => array('data' => t('Married?'), 'field' => 'p.booking_married'),
'booking_room_request' => array('data' => t('Requested Roommates'), 'field' => 'p.booking_room_mate1'),
'booking_roomlocation' => array('data' => t('Room Location'), 'field' => 'r.booking_room_location_id'),
'booking_room_num' => array('data' => t('Room Number'), 'field' => 'r.booking_room_number'),
'booking_room_bedtype' => array('data' => t('Bed Type'), 'field' => 'm.booking_room_bedtype'),
'booking_room_edit' => array('data' => t('Edit')),
);
$query = db_select('booking_person', 'p');
$query->leftJoin('booking_room_mapping', 'm', 'm.booking_nodeid = p.nid');
$query->leftJoin('booking_room_definition', 'r', 'r.rid = m.booking_roomid');
$db_and = db_and();
$db_and->condition('p.booking_event_id', $event->eid, '=');
$db_and->condition('p.booking_status', 1, '=');
$query->condition($db_and);
$query->fields('p')->fields('m')->fields('r');
$table_sort = $query->extend('TableSort')->orderbyHeader($header);
$result = $table_sort->execute();
foreach($result as $data)
{
$location_link = $data->booking_room_location_id > 0 ? _booking_room_location_lookup($data->booking_room_location_id) .
l(t(' (Edit)'), t('admin/booking/rooms/!id/assign', array('!id' => $data->booking_room_location_id))) : '';
$rows[] = array (
'data' => array(
$data->booking_firstname,
$data->booking_lastname,
$data->booking_gender == 'M' ? 'Male' : 'Female',
_booking_get_age_years($data->booking_dob),
$data->booking_married == 'Y' ? 'Yes' : 'No',
$data->booking_room_mate1,
$location_link,
$data->booking_room_number,
_booking_room_bedtype_lookup($data->booking_room_bedtype),
l(t('Change Room'), t('admin/booking/!id/edit-room', array('!id' => $data->nid))),
),
);
}
$prefix = t("<h2>Room Allocations</h2>");
$result = array (
'first_para' => array (
'#type' => 'markup',
'#markup' => $prefix,
),
'table' => array (
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('id' => 'sort-table'),
//'#sticky' => FALSE,
)
);
return $result;
}
/**
* Function for manually assigning room allocation for a person
* @see http://drupal.stackexchange.com/questions/10112/dynamic-select-list-in-the-form-dependent-dropdown
*/
function booking_room_edit_form($node, &$form_state, $nid) {
global $event;
$form = array();
$room_options = array();
//verify that $nid is a number
if (! preg_match('/^[0-9]+$/', $nid)) {
drupal_set_message("Error: Invalid registration ID '" . $nid . "' supplied. Unable to edit study group sessions.", 'error', FALSE);
drupal_goto('admin/booking/rooms');
return "";
}
//check that this person exists
$check_query = db_query("SELECT nid " .
"FROM {booking_person} " .
"WHERE nid = :nid",
array(':nid' => $nid))
->fetchObject();
//throw an error if they don't exist
if (! $check_query)
{
drupal_set_message("Error: Unable to find booking corresponding with registration ID '" . $nid . "'.", 'error', FALSE);
drupal_goto('admin/booking/rooms');
return "";
}
//person must exist in database, load all the bits
$person = node_load($nid);
$prefix = t("<p>Manually assign/update room allocation for !first !last.<br /><b>Note:&nbsp;Still under testing!</b></p>",
array('!first' => $person->booking_firstname, '!last' => $person->booking_lastname));
//***form starts here***
$selected_room_location = isset($form_state['values']['booking_room_location_id']) ?
$form_state['values']['booking_room_location_id'] : $person->booking_room_location_id;
//watchdog('booking', "<pre>Room Edit selected room location:\n@info</pre>", array('@info' => print_r( $selected_room_location, true)));
$selected_room_num = isset($form_state['values']['booking_room_number']) ?
$form_state['values']['booking_room_number'] : $person->booking_room_number;
//watchdog('booking', "<pre>Room Edit selected room number:\n@info</pre>", array('@info' => print_r( $selected_room_num, true)));
$form['booking_room_location_id'] = array(
'#type' => 'select',
'#title' => t('Room Location'),
'#options' => _booking_room_location_lookup(),
'#default_value' => $person->booking_room_location_id,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'booking_roomnum_wrapper',
'callback' => 'booking_roomnum_ajax_callback',
),
);
$form['booking_room_number'] = array(
'#type' => 'select',
'#title' => t('Room Number'),
//'#description' => t(''),
'#prefix' => '<div id="booking_roomnum_wrapper">',
'#suffix' => '</div>',
'#options' => _booking_get_roomedit_roomnum_options($selected_room_location),
'#default_value' => isset($form_state['values']['booking_room_number']) ? $form_state['values']['booking_room_number'] : $person->booking_room_number,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'booking_bedtype_wrapper',
'callback' => 'booking_bedtype_ajax_callback',
),
);
$form['booking_room_bedtype'] = array(
'#type' => 'select',
'#title' => t('Bed Type'),
'#prefix' => '<div id="booking_bedtype_wrapper">',
'#suffix' => '</div>',
'#options' => _booking_get_roomedit_bedtype_options($selected_room_location, $selected_room_num),
'#default_value' => $person->booking_room_bedtype,
);
//generate the render array
$form['personid'] = array(
'#type' => 'hidden',
'#value' => $nid,
);
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['remove'] = array (
'#type' => 'submit',
'#value' => t('Remove'),
);
return array (
'first_para' => array (
'#type' => 'markup',
'#markup' => $prefix,
),
'form' => $form,
);
}
/**
* Function to return the updated form element booking_room_number for booking_room_edit_form()
*/
function booking_roomnum_ajax_callback($form, $form_state) {
//watchdog('booking', "<pre>Room Edit ajax callback:\n@info</pre>", array('@info' => print_r( $form, true)));
return $form['form']['booking_room_number'];
}
/**
* Function to return the updated form element booking_room_bedtype for booking_room_edit_form()
*/
function booking_bedtype_ajax_callback($form, $form_state) {
//watchdog('booking', "<pre>Room Edit ajax callback:\n@info</pre>", array('@info' => print_r( $form, true)));
return $form['form']['booking_room_bedtype'];
}
/**
* Function to calculate appropriate range of room numbers for ajax enabled form booking_room_edit_form()
* @param $location_id - the room location id to look at
* @return array containing the room numbers for this location
*/
function _booking_get_roomedit_roomnum_options($selected) {
$room_options = array();
$room_query = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid",
array(':lid' => $selected));
foreach($room_query as $room)
{
$room_options[$room->booking_room_number] = $room->booking_room_number;
}
//watchdog('booking', "<pre>Room Number Options:\n@info</pre>", array('@info' => print_r( $room_options, true)));
return $room_options;
}
/**
* Function to calculate appropriate bed options for ajax enabled form booking_room_edit_form()
* @param $location_id - the room location id to look at
* @param $room_num - the room number to get the bed details from
* @return array containing the bed options for this specific room
*/
function _booking_get_roomedit_bedtype_options($location_id, $room_num) {
$bed_options = array();
$bed_options[] = "";
$details = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid AND booking_room_number = :num",
array(':lid' => $location_id, ':num' => $room_num))->fetchObject();
//check we got a response from the query
if ($details)
{
//go through the bed options
if ($details->booking_room_singlebeds > 0)
{
$bed_options[1] = "Single";
}
if ($details->booking_room_doublebeds > 0)
{
$bed_options[2] = "Double";
}
if ($details->booking_room_queenbeds > 0)
{
$bed_options[3] = "Queen";
}
}
//watchdog('booking', "<pre>Room Number Options:\n@info</pre>", array('@info' => print_r( $bed_options, true)));
return $bed_options;
}
/**
* Validate the submission to update allocated room and bed for a person
*/
function booking_room_edit_form_validate($form, &$form_state) {
//TODO: turn this into a generalised helper function, and use it for validating booking_rooms_allocate_form() also
global $event;
$values = $form_state['input'];
//watchdog('booking', "<pre>Room Number Form State:\n@info</pre>", array('@info' => print_r( $form_state['values'], true)));
//no need to validate if we're just removing the mapping
//op won't be defined in the form if it's just receiving the ajax callback, so check that is defined first
if (isset($form_state['values']['op']) && $form_state['values']['op'] == 'Remove')
{
return;
}
else
{
//get the specific room definition from the database
$details = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid AND booking_room_number = :num",
array(':lid' => $values['booking_room_location_id'], ':num' => $values['booking_room_number']))->fetchObject();
//perform a check to see if this person is already allocated to this bed
$check = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid AND booking_nodeid = :nid",
array(':eid' => $event->eid, ':nid' => $values['personid']))->fetchObject();
if ($check && ($check->booking_roomid == $details->rid && $check->booking_room_bedtype == $values['booking_room_bedtype']))
{
//drupal_set_message('Not validating room capacity because this person is already allocated this bed in this room.');
}
//there was no existing mapping for this person, so check that there's capacity in this room
elseif (! _booking_room_capacity_check($details->rid, $values['booking_room_bedtype'], $details))
{
form_set_error('booking_room_number',
t('Unfortunately there are no beds available of the type specified in the room.')
);
}
}
}
/**
* Validate there is available capacity for the specified room and bed type
* @return TRUE if there is sufficient capacity, otherwise FALSE
*/
function _booking_room_capacity_check($room_id, $room_bedtype, $room_definition_object = NULL) {
global $event;
$bed_inputs = array(
1 => 'booking_room_singlebeds',
2 => 'booking_room_doublebeds',
3 => 'booking_room_queenbeds',
);
//make sure the value exists before validating it
if (!empty($room_bedtype))
{
//if we already have the object available, don't query for it again
if ($room_definition_object == NULL)
{
$details = db_query("SELECT * FROM {booking_room_definition} WHERE rid = :rid",
array(':rid' => $room_id))->fetchObject();
}
else
$details = $room_definition_object;
//get all person-to-room mappings relating to this room and bed type
$mappings = db_query("SELECT count(*) as num FROM {booking_room_mapping} " .
"WHERE booking_eventid = :eid AND booking_roomid = :rid AND booking_room_bedtype = :type",
array(':eid' => $event->eid, ':rid' => $room_id, ':type' => $room_bedtype))->fetchObject();
$db_field = $bed_inputs[$room_bedtype];
$max_beds = $details->$db_field;
//if the beds are dual occupency, pretend there's twice as many of them
if ($room_bedtype == 2 || $room_bedtype == 3)
{
$max_beds = $max_beds * 2;
}
//check that there is sufficient capacity to allocate another person to this room and bed type
if ($mappings->num < $max_beds)
{
watchdog('booking','Sufficient capacity is available in location !id, room !room, with bed type !type. !count beds remaining of this type',
array('!id' => $details->booking_room_location_id, '!room' => $details->booking_room_number,
'!type' => _booking_room_bedtype_lookup($room_bedtype), '!count' => $max_beds - $mappings->num));
return true;
}
else
{
watchdog('booking',"Couldn't locate sufficient capacity in location !id, room !room, with bed type !type. !count beds remaining of this type",
array('!id' => $details->booking_room_location_id, '!room' => $details->booking_room_number,
'!type' => _booking_room_bedtype_lookup($room_bedtype), '!count' => $max_beds - $mappings->num));
}
}
return false;
}
/**
* Process the submission to update allocated room and bed for a person
*/
function booking_room_edit_form_submit($form, &$form_state) {
global $event;
$values = $form_state['input'];
//check if we should remove the room allocation
if ($form_state['values']['op'] == 'Remove')
{
$message = t("Removing id !nid from room number !number in location !location",
array('!nid' => $values['personid'], '!location' => $values['booking_room_location_id'],
'!number' => $values['booking_room_number'])
);
watchdog('booking', $message);
drupal_set_message($message, 'status', FALSE);
db_delete('booking_room_mapping')
->condition('booking_eventid', $event->eid)
->condition('booking_nodeid', $values['personid'])
->execute();
return;
}
//otherwise, continue with adding/updating the room allocation
else
{
$details = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid AND booking_room_number = :num",
array(':lid' => $values['booking_room_location_id'], ':num' => $values['booking_room_number']))->fetchObject();
$check = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid AND booking_nodeid = :nid",
array(':eid' => $event->eid, ':nid' => $values['personid']))->fetchObject();
//this person already exists in mapping table
if ($check)
{
$message = t("Updating person id !nid to room id !id with bed type !type.",
array('!nid' => $values['personid'], '!id' => $details->rid, '!type' => _booking_room_bedtype_lookup($values['booking_room_bedtype']),
'!number' => $values['booking_room_number'])
);
//there is an existing mapping to update
$result = db_update('booking_room_mapping')
->fields(array(
'booking_roomid' => $details->rid,
'booking_room_bedtype' => $values['booking_room_bedtype'],
))
->condition('mid', $check->mid)
//->condition('booking_eventid', $event->eid)
//->condition('booking_nodeid', $values['personid'])
->execute();
}
//create a new record in the mapping table
else
{
$message = t("Allocating person id !nid to room id !id with bed type !type.",
array('!nid' => $values['personid'], '!id' => $details->rid, '!type' => _booking_room_bedtype_lookup($values['booking_room_bedtype']),
'!number' => $values['booking_room_number'])
);
//create a new room mapping for this person
$result = db_insert('booking_room_mapping')
->fields(array(
'booking_roomid' => $details->rid,
'booking_eventid' => $event->eid,
'booking_nodeid' => $values['personid'],
'booking_room_bedtype' => $values['booking_room_bedtype'],
))
->execute();
}
watchdog('booking', $message);
drupal_set_message($message, 'status', FALSE);
} //end operation check
}
/**
* Function to display existing room definitions
*/
function booking_rooms_view_definitions() {
global $event;
$prefix = "<h2>Room Definitions</h2>";
//query for room definitions
$result = db_query("SELECT * from {booking_room_definition}");
$header = array (
'booking_room_location_description' => t('Room Location'),
'booking_room_number' => t('Room Number'),
'booking_room_singlebeds' => t('Number Single Beds'),
'booking_room_doublebeds' => t('Number Double Beds'),
'booking_room_queenbeds' => t('Number Queen Beds'),
'booking_room_ensuite' => t('Ensuite?'),
'booking_room_edit' => t('Edit Room'),
);
foreach($result as $data)
{
$rows[] = array (
_booking_room_location_lookup($data->booking_room_location_id),
$data->booking_room_number,
$data->booking_room_singlebeds,
$data->booking_room_doublebeds,
$data->booking_room_queenbeds,
$data->booking_room_ensuite == 'Y' ? 'Yes' : 'No',
l(t('Edit'), t('admin/config/booking/rooms/!id/edit', array('!id' => $data->rid))),
);
/*
$options[$data->rid] = array
(
'booking_room_location_description' => _booking_room_location_lookup($data->booking_room_location_id),
'booking_room_number' => $data->booking_room_number,
'booking_room_singlebeds' => $data->booking_room_singlebeds,
'booking_room_doublebeds' => $data->booking_room_doublebeds,
'booking_room_queenbeds' => $data->booking_room_queenbeds,
'booking_room_ensuite' => $data->booking_room_ensuite == 'Y' ? 'Yes' : 'No',
);
*/
}
$result = array (
'first_para' => array (
'#type' => 'markup',
'#markup' => $prefix,
),
'table' => array (
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
//'#attributes' => array('id' => 'sort-table'),
)
);
return $result;
}
/**
* Function to define the form for configuring room definitions and also display existing room definitions
*/
function booking_rooms_definition_form($node, &$form_state, $create, $room_id = 0) {
global $event;
$prefix = "";
$form = array();
$bedcount_options = array();
//TODO: update to be like booking_price_form()
if ($create == true)
{
$data = $node;
$prefix = t("<p>Create new room defintion.</p>");
//watchdog('booking', 'Creating new room definition: @info', array ('@info' => var_export($node, TRUE)));
}
else
{
//verify that $editid is a number
if (! preg_match('/^[0-9]+$/', $room_id)) {
drupal_set_message("Error: Invalid room ID supplied. Unable to update room definition.", 'error', FALSE);
drupal_goto('admin/config/booking/rooms');
return "";
}
//$data = $form_state['input'];
$data = db_query("SELECT * FROM {booking_room_definition} WHERE rid = :id",
array(':id' => $room_id))
->fetchObject();
$prefix = t("<p>Update the room defintions.</p>");
//add this to the form in a hidden field so we can update the right price
$form['booking_rid'] = array (
'#type' => 'hidden',
'#value' => $room_id,
);
//watchdog('booking', 'Editing existing room definition: @info', array ('@info' => var_export($data, TRUE)));
}
//create the options array for bed counts
for ($i = 0; $i <= 15; $i++)
$bedcount_options[$i] = $i;
//define the form for adding to the room definitions
if(!isset($form_state['storage']['confirm'])) {
$form[] = array (
'first_heading' => array (
'#type' => 'markup',
'#markup' => $prefix,
),
);
$form['booking_room_location_id'] = array(
'#type' => 'select',
'#title' => t('Choose room location'),
'#options' => _booking_room_location_lookup(),
'#default_value' => !empty($data->booking_room_location_id) ? $data->booking_room_location_id : ''
);
$form['booking_room_number'] = array (
'#type' => 'textfield',
'#title' => t('Specify room number'),
'#size' => 5,
'#maxlength' => 10,
'#default_value' => !empty($data->booking_room_number) ? $data->booking_room_number : '',
);
$form['booking_room_singlebeds'] = array (
'#type' => 'select',
'#title' => t('Specify number of single beds'),
'#options' => $bedcount_options,
'#default_value' => !empty($data->booking_room_singlebeds) ? $data->booking_room_singlebeds : '',
);
$form['booking_room_doublebeds'] = array (
'#type' => 'select',
'#title' => t('Specify number of double beds'),
'#options' => $bedcount_options,
'#default_value' => !empty($data->booking_room_doublebeds) ? $data->booking_room_doublebeds : '',
);
$form['booking_room_queenbeds'] = array (
'#type' => 'select',
'#title' => t('Specify number of queen beds'),
'#options' => $bedcount_options,
'#default_value' => !empty($data->booking_room_queenbeds) ? $data->booking_room_queenbeds : '',
);
$form['booking_room_ensuite'] = array(
'#type' => 'checkbox',
'#title' => t('Tick if this room has an ensuite'),
'#default_value' => (!empty($data->booking_room_ensuite) && $data->booking_room_ensuite == 'Y') ? 1 : 0,
);
if ($create == true)
{
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Add Room'),
);
} else {
$form['Update'] = array
(
'#type' => 'submit',
'#value' => t('Update Room'),
);
$form['Delete'] = array
(
'#type' => 'submit',
'#value' => t('Delete Room'),
);
}
return array (
'form' => $form,
);
}
else
{
return confirm_form($form, "Are you sure you wish to delete room definition with id " . $room_id . "?",
current_path(), NULL, "Delete Room");
}
}
/**
* Process the form for adding room definitions
*/
function booking_rooms_definition_form_submit($form, &$form_state) {
global $event;
$values = $form_state['input'];
$redirect_path = "admin/config/booking/rooms";
//watchdog('booking', "<pre>Room definition submission:\n@info</pre>", array('@info' => print_r( $form_state, true)));
//if we're deleting, add the confirmation to the form if it hasn't been defined yet
if($form_state['values']['op'] == 'Delete Room' && (!isset($form_state['storage']['confirm'])))
{
//watchdog('booking', "<pre>Room deletion confirmation being set:\n@info</pre>", array('@info' => print_r( $form_state, true)));
$form_state['storage']['confirm'] = TRUE;
$form_state['rebuild'] = TRUE;
}
elseif ($form_state['values']['op'] == 'Delete Room')
{
//delete the room
watchdog('booking', "Deleting room ID !rid", array('!rid' => $values['booking_rid']));
db_delete('booking_room_definition')
->condition('rid', $values['booking_rid'])
->execute();
drupal_set_message('Deleted room id ' . $values['booking_rid'] );
$form_state['redirect'] = $redirect_path;
}
elseif ($form_state['values']['op'] == 'Update Room')
{
$result = db_update('booking_room_definition')
->fields(array(
'booking_room_location_id' => $values['booking_room_location_id'],
'booking_room_number' => $values['booking_room_number'],
'booking_room_singlebeds' => $values['booking_room_singlebeds'],
'booking_room_doublebeds' => $values['booking_room_doublebeds'],
'booking_room_queenbeds' => $values['booking_room_queenbeds'],
'booking_room_ensuite' => $values['booking_room_ensuite'] == 1 ? 'Y' : 'N',
))
->condition('rid', $values['booking_rid'] )
->execute();
drupal_set_message('Updated room id ' . $values['booking_rid'] );
$form_state['redirect'] = $redirect_path;
}
elseif ($form_state['values']['op'] == 'Add Room')
{
db_insert('booking_room_definition')
->fields(array(
'booking_room_location_id' => $values['booking_room_location_id'],
'booking_room_number' => $values['booking_room_number'],
'booking_room_singlebeds' => $values['booking_room_singlebeds'],
'booking_room_doublebeds' => $values['booking_room_doublebeds'],
'booking_room_queenbeds' => $values['booking_room_queenbeds'],
'booking_room_ensuite' => $values['booking_room_ensuite'] == 1 ? 'Y' : 'N',
))
->execute();
drupal_set_message('Created new room definition');
$form_state['redirect'] = $redirect_path;
}
}
/**
* Function for allocating rooms
*/
function booking_rooms_allocate_form($node, &$form_state, $location_id) {
global $event;
$form = array();
$attendee_select = array();
$options = array();
$counter = 0;
//verify that $location_id is a number
if (! preg_match('/^[0-9]+$/', $location_id)) {
drupal_set_message("Error: Invalid room location ID '" . $location_id . "' supplied. Unable to allocate rooms.", 'error', FALSE);
drupal_goto('admin/booking/rooms');
return "";
}
//query for existing room allocations
$room_mapping_query = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid",
array(':eid' => $event->eid));
//$room_mapping = $room_mapping_query->fetchAll();
$room_mapping = $room_mapping_query->fetchAllAssoc('booking_nodeid');
//query for room definitions
$room_query = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid",
array(':lid' => $location_id));
//query for attendees
$query = db_query("SELECT nid, booking_firstname, booking_lastname, booking_gender, booking_dob, booking_partner_id " .
" FROM {booking_person} " .
" WHERE booking_event_id = :eid and booking_status=1 order by booking_lastname, booking_firstname",
array(':eid' => $event->eid));
//make a list of all attendees that are booked in
$attendee_select[] = '';
foreach($query as $row)
{
$married = $row->booking_partner_id > 0 ? ' *' : '';
$age = _booking_get_age_years($row->booking_dob);
$assigned_flag = empty($room_mapping[$row->nid]) ? '' : ' - ';
$attendee_select[$row->nid] = $assigned_flag . $row->booking_lastname . ', ' . $row->booking_firstname . ' ' . ' ['. $age . ' ' . $row->booking_gender . ']' . $married;
}
watchdog('booking', "<pre>Loading existing room allocations:\n@info</pre>", array('@info' => print_r( $room_mapping, true)));
//attach the custom css
$form['#attached']['css'] = array(
drupal_get_path('module', 'booking') . '/booking.css',
);
//define the header
$header = array (
'booking_room_location' => array('data' => t('Room Location'), 'field' => 'booking_room_location_id'),
'booking_room_number' => array('data' => t('Room Number')),
'booking_room_singlebed' => array('data' => t('Single Bed')),
'booking_room_doublebed_p1' => array('data' => t('Double Bed Person 1')),
'booking_room_doublebed_p2' => array('data' => t('Double Bed Person 2')),
'booking_room_queenbed_p1' => array('data' => t('Queen Bed Person 1')),
'booking_room_queenbed_p2' => array('data' => t('Queen Bed Person 2')),
);
$default_row = array();
$default_row['booking_room_location'] = "";
$default_row['booking_room_number'] = "";
$default_row['booking_room_singlebed'] = "";
$default_row['booking_room_doublebed_p1'] = "";
$default_row['booking_room_doublebed_p2'] = "";
$default_row['booking_room_queenbed_p1'] = "";
$default_row['booking_room_queenbed_p2'] = "";
foreach ($room_query as $data)
{
//load the existing bed mappings for this room
$existing_beds = array();
for ($i = 1; $i <= 3; $i++)
{
foreach ($room_mapping as $mapping)
{
//check that the room id in the mapping table matches the room that we're currently adding to the table
//and also the bed type matches the first dimension in the array
if ($mapping->booking_roomid == $data->rid && $mapping->booking_room_bedtype == $i)
{
$existing_beds[$i][] = $mapping->booking_nodeid;
}
}
}
//watchdog('booking', "<pre>Existing bed mappings:\n@info</pre>", array('@info' => print_r( $existing_beds, true)));
//create a row that contains just the room location and number
$row = _booking_clone_array($default_row);
$row['booking_room_location'] = _booking_room_location_lookup($data->booking_room_location_id);
$row['booking_room_number'] = $data->booking_room_number;
$row['#attributes'] = array('id' => array("new-group-row"));
$options[$counter++] = $row;
//create an additional row for each single bed
for ($i = 0; $i < $data->booking_room_singlebeds; $i++)
{
//retrieve the default value if one exists
$default = (!empty($existing_beds[1][$i])) ? $existing_beds[1][$i] : 0;
$row = _booking_clone_array($default_row);
$row['booking_room_singlebed'] = array('data' => array(
'#type' => 'select',
'#options' => $attendee_select,
'#name' => 'booking_room_singlebed[' . $data->rid . '][' . $i . ']',
'#value' => $default,
));
$options[$counter++] = $row;
}
//create an additional row for each double bed
//$j is our counter that increments twice as fast as $i to cater for both beds
$j = 0;
for ($i = 0; $i < $data->booking_room_doublebeds; $i++)
{
$row = _booking_clone_array($default_row);
$row['booking_room_doublebed_p1'] = array('data' => array(
'#type' => 'select',
'#options' => $attendee_select,
'#name' => 'booking_room_doublebed_p1[' . $data->rid . '][' . $i . ']',
'#value' => (!empty($existing_beds[2][$j])) ? $existing_beds[2][$j++] : 0,
));
$row['booking_room_doublebed_p2'] = array('data' => array(
'#type' => 'select',
'#options' => $attendee_select,
'#name' => 'booking_room_doublebed_p2[' . $data->rid . '][' . $i . ']',
'#value' => (!empty($existing_beds[2][$j])) ? $existing_beds[2][$j++] : 0,
));
$options[$counter++] = $row;
}
//create an additional row for each queen bed
//$j is our counter that increments twice as fast as $i to cater for both beds
$j = 0;
for ($i = 1; $i <= $data->booking_room_queenbeds; $i++)
{
$row = _booking_clone_array($default_row);
$default = (!empty($existing_beds[3][$j])) ? $existing_beds[3][$j++] : 0;
$row['booking_room_queenbed_p1'] = array('data' => array(
'#type' => 'select',
'#options' => $attendee_select,
'#name' => 'booking_room_queenbed_p1[' . $data->rid . '][' . $i . ']',
'#value' => $default,
));
//find the default for the second bed
$default = (!empty($existing_beds[3][$j])) ? $existing_beds[3][$j++] : 0;
$row['booking_room_queenbed_p2'] = array('data' => array(
'#type' => 'select',
'#options' => $attendee_select,
'#name' => 'booking_room_queenbed_p2[' . $data->rid . '][' . $i . ']',
'#value' => $default,
));
//add this row to the table
$options[$counter++] = $row;
}
}
$form['table'] = array (
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No rooms found for this room location id.'),
);
//so we can access the dropdown elements
$form['booking_room_singlebed'] = array( '#type' => 'value' );
$form['booking_room_queenbed_p1'] = array( '#type' => 'value' );
$form['booking_room_queenbed_p2'] = array( '#type' => 'value' );
$form['booking_room_doublebed_p1'] = array( '#type' => 'value' );
$form['booking_room_doublebed_p2'] = array( '#type' => 'value' );
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Submit'),
);
return array (
'form' => $form,
);
}
/**
* Process the submission for room assignment
*/
function booking_rooms_allocate_form_submit($form, &$form_state) {
global $event;
//query for existing room allocations
$room_mapping_query = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid", array(':eid' => $event->eid));
$room_mapping = $room_mapping_query->fetchAllAssoc('booking_nodeid');
$bed_inputs = array(
'booking_room_singlebed' => 1,
'booking_room_doublebed_p1' => 2,
'booking_room_doublebed_p2' => 2,
'booking_room_queenbed_p1' => 3,
'booking_room_queenbed_p2' => 3,
);
//watchdog('booking', "<pre>Room assignment submission:\n@info</pre>", array('@info' => print_r( $singlebed_ids, true)));
//go through the different bed types
foreach ($bed_inputs as $type => $type_id)
{
//watchdog('booking', "Bed type !type with id !id", array('!type' => $type, '!id' => $type_id));
//watchdog('booking', "<pre>Room assignment submission:\n@info</pre>", array('@info' => print_r( $form_state['values'][$type], true)));
//if this bed type wasn't defined in the form, skip it
if (empty($form_state['values'][$type]))
continue;
//go through each room
foreach($form_state['values'][$type] as $room => $value)
{
//go through each bed
foreach ($value as $index => $nid)
{
//if this is actually a person to process
if ($nid > 0)
{
//this person didn't previously have a room/bed mapping
if (empty($room_mapping[$nid]))
{
//Validate that there is capacity for the person to be allocated to this room
if (_booking_room_capacity_check($room, $type_id))
{
drupal_set_message(t('Assigning person id !id to a type !type bed in room id !room.',
array('!id' => $nid, '!room' => $room, '!type' => $type_id)));
$result = db_insert('booking_room_mapping')
->fields(array(
'booking_roomid' => $room,
'booking_eventid' => $event->eid,
'booking_nodeid' => $nid,
'booking_room_bedtype' => $type_id,
))
->execute();
}
else
{
drupal_set_message(
t('Insufficient capacity to assign person id !id to a type !type bed in room id !room.',
array('!id' => $nid, '!room' => $room, '!type' => $type_id)
), 'error'
);
//, 'error', FALSE);
}
}
//this person previously had a room mapping but to a different room
elseif ((!empty($room_mapping[$nid])) && $room_mapping[$nid]->booking_roomid != $room)
{
drupal_set_message(t('Changing person id !id from old room !oldroom to new room !room with type !type bed.',
array('!id' => $nid, '!room' => $room, '!type' => $type_id, '!oldroom' => $room_mapping[$nid]->booking_roomid)));
db_update('booking_room_mapping')
->fields(array(
'booking_roomid' => $room,
'booking_room_bedtype' => $type_id,
))
->condition('booking_eventid', $event->eid)
->condition('booking_nodeid', $nid)
->execute();
}
//this person previously had a room mapping but to a different bed type in the same room
elseif ((!empty($room_mapping[$nid])) && $room_mapping[$nid]->booking_room_bedtype != $type_id)
{
drupal_set_message(t('Changing person id !id in room !room to new bed type type !type .',
array('!id' => $nid, '!room' => $room, '!type' => $type_id, '!oldroom' => $room_mapping[$nid]->booking_roomid)));
db_update('booking_room_mapping')
->fields(array(
'booking_roomid' => $room,
'booking_room_bedtype' => $type_id,
))
->condition('booking_eventid', $event->eid)
->condition('booking_nodeid', $nid)
->execute();
}
else
{
//drupal_set_message(t('Person id !id already has some other room allocation.',
// array('!id' => $nid, '!room' => $room, '!type' => $type_id)));
}
} //valid node id check
} //each bed
} //each room
} //each bed type
}
/**
* Function for generating report of room allocations in a specific location
*/
function booking_rooms_view_form($node, &$form_state, $location_id) {
global $event;
$rows = array();
$form = array();
//verify that $location_id is a number
if (! preg_match('/^[0-9]+$/', $location_id)) {
drupal_set_message("Error: Invalid room location ID '" . $location_id . "' supplied. Unable to allocate rooms.",
'error', FALSE);
drupal_goto('admin/booking/rooms');
return "";
}
$prefix = t("<h2>Room Allocations for !room</h2>", array('!room' => _booking_room_location_lookup($location_id)));
//query for room definitions
$room_query = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid",
array(':lid' => $location_id));
//query for existing room allocations
$room_mapping_query = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid",
array(':eid' => $event->eid));
$room_mapping = $room_mapping_query->fetchAllAssoc('booking_nodeid');
//query for attendees
$attendees = db_query("SELECT nid, booking_firstname, booking_lastname, booking_gender, booking_dob, booking_partner_id " .
" FROM {booking_person} " .
" WHERE booking_event_id = :eid and booking_status=1 order by booking_lastname, booking_firstname",
array(':eid' => $event->eid))->fetchAllAssoc('nid');
//define the header
$header = array (
'booking_room_number' => array('data' => t('Room Number')),
'booking_room_singlebed' => array('data' => t('Single Bed')),
'booking_room_doublebed_p1' => array('data' => t('Double Bed Person 1')),
'booking_room_doublebed_p2' => array('data' => t('Double Bed Person 2')),
'booking_room_queenbed_p1' => array('data' => t('Queen Bed Person 1')),
'booking_room_queenbed_p2' => array('data' => t('Queen Bed Person 2')),
);
foreach ($room_query as $data)
{
//load the existing bed mappings for this room
$existing_beds = array();
for ($i = 1; $i <= 3; $i++)
{
foreach ($room_mapping as $mapping)
{
//check that the room id in the mapping table matches the room that we're currently adding to the table
//and also the bed type matches the first dimension in the array
if ($mapping->booking_roomid == $data->rid && $mapping->booking_room_bedtype == $i)
{
$existing_beds[$i][] = $mapping->booking_nodeid;
}
}
}
//create a row that contains just the room location and number
$rows[] = array (
'data' => array(
$data->booking_room_number,
"",
"",
"",
"",
"",
),
'id' => array("new-group-row"),
);
//create an additional row for each single bed
for ($i = 0; $i < $data->booking_room_singlebeds; $i++)
{
//retrieve the default value if one exists
$nid = (!empty($existing_beds[1][$i])) ? $existing_beds[1][$i] : 0;
$rows[] = array (
'data' => array(
"",
_booking_rooms_view_formatperson($attendees, $nid),
"",
"",
"",
"",
),
);
}
//create an additional row for each double bed
//$j is our counter that increments twice as fast as $i to cater for both beds
$j = 0;
for ($i = 0; $i < $data->booking_room_doublebeds; $i++)
{
$rows[] = array (
'data' => array(
"",
"",
_booking_rooms_view_formatperson($attendees, (!empty($existing_beds[2][$j])) ? $existing_beds[2][$j++] : 0),
_booking_rooms_view_formatperson($attendees, (!empty($existing_beds[2][$j])) ? $existing_beds[2][$j++] : 0),
"",
"",
),
);
}
//create an additional row for each queen bed
//$j is our counter that increments twice as fast as $i to cater for both beds
$j = 0;
for ($i = 1; $i <= $data->booking_room_queenbeds; $i++)
{
$rows[] = array (
'data' => array(
"",
"",
"",
"",
_booking_rooms_view_formatperson($attendees, (!empty($existing_beds[3][$j])) ? $existing_beds[3][$j++] : 0),
_booking_rooms_view_formatperson($attendees, (!empty($existing_beds[3][$j])) ? $existing_beds[3][$j++] : 0),
),
);
}
}
//watchdog('booking', "<pre>Room assignment report rows:\n@info</pre>", array('@info' => print_r( $rows, true)));
$result = array (
'#attached' => array (
'css' => array(drupal_get_path('module', 'booking') . '/booking.css')
),
'first_para' => array (
'#type' => 'markup',
'#markup' => $prefix,
),
'table' => array (
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('id' => 'sort-table'),
'#empty' => t('No room allocations found for this location.'),
)
);
return $result;
}
/**
* Function for generating a name relating to a node id
* @param $attendees - an associative array containing the possible attendees
* @param $nid - the node id of the person for which to return the formatted string
* @return string containing first name and last name relating to node id
*/
function _booking_rooms_view_formatperson(&$attendees, $nid) {
$output = "Empty";
if ($nid > 0 && !empty($attendees[$nid]))
{
$output = $attendees[$nid]->booking_firstname . " " . $attendees[$nid]->booking_lastname;
}
return $output;
}