diff --git a/booking.helper.inc b/booking.helper.inc index 936525c..e5d0aff 100644 --- a/booking.helper.inc +++ b/booking.helper.inc @@ -1141,10 +1141,8 @@ function _booking_studygroup_email_summary($node) { //check that this study group session has been defined for this attendee if (!empty($node->$sessionid)) { - $rows[] = t($studygroups[$i]->booking_studygroup_descrip . " session number:\t" . $node->$sessionid); - $rows[] = t("Role:\t" . _booking_studygroup_role_lookup($node->$roleid)); - } - + $rows[] = t($studygroups[$i]->booking_studygroup_descrip . ": Group " . $node->$sessionid . ", " . _booking_studygroup_role_lookup($node->$roleid)); + } } } @@ -1167,12 +1165,12 @@ function _booking_room_email_summary($node) { //check that this attendee has had a room allocated if (! empty($node->rid)) { - $rows[] = t("Room Location\t" . _booking_room_location_lookup($node->booking_room_location_id)); - $rows[] = t("Room Number\t" . $node->booking_room_number); - $rows[] = t("Bed Type\t" . _booking_room_bedtype_lookup($node->booking_room_bedtype)); + $rows[] = t("Room Location: " . _booking_room_location_lookup($node->booking_room_location_id)); + $rows[] = t("Room Number: " . $node->booking_room_number); + $rows[] = t("Bed Type: " . _booking_room_bedtype_lookup($node->booking_room_bedtype)); } else - $rows[] = t("No room currently allocated."); + $rows[] = t("\tNo room currently allocated."); } foreach ($rows as $key => $value) diff --git a/booking.module b/booking.module index 341e483..f9cbee8 100644 --- a/booking.module +++ b/booking.module @@ -425,6 +425,15 @@ function booking_menu() { 'page arguments' => array('booking_rooms_view_form', 3), 'access arguments' => array('view room allocations'), ); + + $items['admin/booking/%/edit-room'] = array( + 'title' => 'Edit Room Allocation', + 'description' => 'Manually Update Room Allocation', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('booking_room_edit_form', 2), + 'access arguments' => array('edit room allocations'), + 'type' => MENU_CALLBACK, + ); } //Configure prices diff --git a/booking.register.inc b/booking.register.inc index b8436d9..1b03f50 100644 --- a/booking.register.inc +++ b/booking.register.inc @@ -1659,16 +1659,19 @@ function booking_view($node, $view_mode) { } $rows[] = array(t('Temporary UUID:'), $node->booking_tempid); - //if room allocation is defined, display that - if (! empty($node->rid)) - { + //display room allocation data if enabled + if (variable_get('booking_enable_roomallocations', 0) == 1) + { + $room_heading = t("

Room Allocation

!link

", + array('!link' => l(t('Edit Room Allocation'), t('admin/booking/!id/edit-room', array('!id' => $node->nid))) + )); $room_rows = array(); $room_rows[] = array(t('Room Location'), _booking_room_location_lookup($node->booking_room_location_id)); $room_rows[] = array(t('Room Number'), $node->booking_room_number); $room_rows[] = array(t('Bed Type'), _booking_room_bedtype_lookup($node->booking_room_bedtype)); $node->content['room-heading'] = array( - '#markup' => "

Room Details

", + '#markup' => $room_heading, '#weight' => 2, ); diff --git a/booking.rooms.inc b/booking.rooms.inc index fc2546c..7371370 100644 --- a/booking.rooms.inc +++ b/booking.rooms.inc @@ -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("

Manually assign/update room allocation for !first !last.
Note: Still under testing!

", + 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', "
Room Edit selected room location:\n@info
", 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', "
Room Edit selected room number:\n@info
", 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' => '
', + '#suffix' => '
', + '#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' => '
', + '#suffix' => '
', + '#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', "
Room Edit ajax callback:\n@info
", 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', "
Room Edit ajax callback:\n@info
", 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', "
Room Number Options:\n@info
", 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', "
Room Number Options:\n@info
", 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', "
Max beds of type !type for this room:\n@info
", + 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', "
Room assignment submission:\n@info
", 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 .',