Added ajax function to individually manage room allocation
This commit is contained in:
@@ -105,6 +105,256 @@ function booking_roomallocations_view_summary() {
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for manually assigning study group sessions for a person
|
||||
*/
|
||||
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: 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'),
|
||||
);
|
||||
|
||||
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();
|
||||
|
||||
//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) {
|
||||
global $event;
|
||||
$values = $form_state['input'];
|
||||
$bed_inputs = array(
|
||||
1 => 'booking_room_singlebeds',
|
||||
2 => 'booking_room_doublebeds',
|
||||
3 => 'booking_room_queenbeds',
|
||||
);
|
||||
|
||||
//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();
|
||||
|
||||
//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' => $details->rid, ':type' => $values['booking_room_bedtype']))->fetchObject();
|
||||
|
||||
watchdog('booking', "<pre>Max beds of type !type for this room:\n@info</pre>",
|
||||
array('!type' => $bed_inputs[$values['booking_room_bedtype']], '@info' => print_r( $mappings, true)));
|
||||
|
||||
//make sure the value exists before validating it
|
||||
if (!empty($values['booking_room_bedtype']))
|
||||
{
|
||||
$db_field = $bed_inputs[$values['booking_room_bedtype']];
|
||||
//check that there is sufficient capacity to allocate another person to this room and bed type
|
||||
if ($mappings->num >= $details->$db_field)
|
||||
{
|
||||
form_set_error('booking_room_number',
|
||||
t('Unfortunately there are no beds available of the type specified in the room.')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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'];
|
||||
|
||||
$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();
|
||||
|
||||
if ($check)
|
||||
{
|
||||
//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('booking_eventid', $event->eid)
|
||||
->condition('booking_nodeid', $values['personid'])
|
||||
->execute();
|
||||
drupal_set_message("Successfully updated " . $result . " row(s) in the room allocation table.", $type = 'status');
|
||||
}
|
||||
else
|
||||
{
|
||||
//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();
|
||||
drupal_set_message("Successfully added " . $result . " row(s) to the room allocation table.", $type = 'status');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to define the form for configuring room definitions and also display existing room definitions
|
||||
@@ -451,6 +701,8 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
'booking_room_queenbed_p2' => 3,
|
||||
);
|
||||
|
||||
//TODO: Validate that there is capacioty for the person to be allocated to this room
|
||||
|
||||
//watchdog('booking', "<pre>Room assignment submission:\n@info</pre>", array('@info' => print_r( $singlebed_ids, true)));
|
||||
|
||||
//go through the different bed types
|
||||
@@ -472,6 +724,7 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
//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]))
|
||||
{
|
||||
drupal_set_message(t('Assigning person id !id to a type !type bed in room id !room.',
|
||||
@@ -486,6 +739,7 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
))
|
||||
->execute();
|
||||
}
|
||||
//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.',
|
||||
@@ -501,6 +755,7 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
->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 .',
|
||||
|
Reference in New Issue
Block a user