From 8bf2de05da167090deaec013309c3fdc328daa14 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Tue, 3 Jun 2014 23:14:15 +1000 Subject: [PATCH] Extend capacity checking to mass room allocation form --- booking.rooms.inc | 63 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/booking.rooms.inc b/booking.rooms.inc index 905d87f..7dc099c 100644 --- a/booking.rooms.inc +++ b/booking.rooms.inc @@ -310,7 +310,12 @@ function booking_room_edit_form_validate($form, &$form_state) { } else { - if (! _booking_room_capacity_check($values['booking_room_location_id'], $values['booking_room_number'], $values['booking_room_bedtype'])) + //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(); + + + if (! _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.') @@ -349,7 +354,7 @@ function booking_room_edit_form_validate($form, &$form_state) { * 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_location_id, $room_number, $room_bedtype) { +function _booking_room_capacity_check($room_id, $room_bedtype, $room_definition_object = NULL) { global $event; $bed_inputs = array( @@ -361,14 +366,19 @@ function _booking_room_capacity_check($room_location_id, $room_number, $room_bed //make sure the value exists before validating it if (!empty($room_bedtype)) { - //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' => $room_location_id, ':num' => $room_number))->fetchObject(); + //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' => $details->rid, ':type' => $room_bedtype))->fetchObject(); + array(':eid' => $event->eid, ':rid' => $room_id, ':type' => $room_bedtype))->fetchObject(); $db_field = $bed_inputs[$room_bedtype]; $max_beds = $details->$db_field; @@ -376,7 +386,8 @@ function _booking_room_capacity_check($room_location_id, $room_number, $room_bed 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' => $room_location_id, '!room' => $room_number, '!type' => $room_bedtype, '!count' => $max_beds - $mappings->num)); + 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; } @@ -810,7 +821,7 @@ 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))); @@ -836,17 +847,33 @@ function booking_rooms_allocate_form_submit($form, &$form_state) { //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.', - array('!id' => $nid, '!room' => $room, '!type' => $type_id))); + + //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); + } + - $result = db_insert('booking_room_mapping') - ->fields(array( - 'booking_roomid' => $room, - 'booking_eventid' => $event->eid, - 'booking_nodeid' => $nid, - 'booking_room_bedtype' => $type_id, - )) - ->execute(); } //this person previously had a room mapping but to a different room elseif ((!empty($room_mapping[$nid])) && $room_mapping[$nid]->booking_roomid != $room)