Files
booking/booking.variety_form.inc
2018-05-15 12:55:42 +10:00

265 lines
9.7 KiB
PHP

<?php
/**
* @file
* User facing page for booking into a variety session
* NOTE: This feature is not complete
*/
/**
* Build the user-facing variety session registration form
*/
function booking_variety_regn_form($node, &$form_state)
{
global $event;
$form = array();
$data = $node;
// Query the variety timeslot table
$timeslot_query = db_select('booking_variety_timeslots', 'v');
$timeslot_query->condition('v.booking_eventid', $event->eid, '=')
->fields('v')
->orderBy('v.booking_variety_start');
$result = $timeslot_query->execute();
$form['wrapper'] = array(
'#prefix' => '<div id="booking_variety_form_wrapper">',
'#suffix' => '</div>'
);
$form['identity'] = array(
'#type' => 'fieldset',
'#title' => 'Select Person',
);
$form['identity']['booking_nid'] = array(
'#type' => 'textfield',
'#title' => t('Booking Number'),
'#description' => t('Please enter your booking number from your lanyard.'),
'#size' => 60,
'#required' => TRUE,
'#default_value' => !empty($data->booking_nid) ? $data->booking_nid : '',
'#ajax' => array(
'event' => 'change',
'wrapper' => 'booking_variety_form_wrapper',
'callback' => 'booking_variety_session_form_callback',
),
);
$form['identity']['booking_lastname'] = array(
'#type' => 'textfield',
'#title' => t('Surname'),
'#description' => t('Please enter your last name as it appeared when you registered.'),
'#size' => 60,
'#required' => TRUE,
'#default_value' => !empty($data->booking_lastname) ? $data->booking_lastname : '',
'#ajax' => array(
'event' => 'change',
'wrapper' => 'booking_variety_form_wrapper',
'callback' => 'booking_variety_session_form_callback',
),
);
$form['variety-sessions'] = array(
'#type' => 'fieldset',
'#title' => 'Select Variety Sessions',
//'#prefix' => '<div id="booking_variety_session_fieldset_wrapper">',
//'#suffix' => '</div>'
);
$form['identity']['booking_feedback_wrapper'] = array(
'#markup' => '<div id="booking_feedback_wrapper"></div>',
);
//for each entry in the variety timeslot table, create a new form select item
foreach($result as $timeslot) {
$fieldname = 'select-variety-' . $timeslot->tid;
//create the form element for this timeslot
$form['variety-sessions'][$fieldname] = array(
'#type' => 'select',
'#title' => t('Variety Session: ' . $timeslot->booking_variety_time_descrip),
'#required' => TRUE,
'#default_value' => isset($form_state['values'][$fieldname]) ? $form_state['values'][$fieldname] : 0,
'#options' => _booking_get_variety_timeslot_options($timeslot->tid),
//'#prefix' => '<div id="booking_variety_session_' . $timeslot->tid . '_wrapper">',
//'#suffix' => '</div>',
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return array (
'form' => $form,
);
}
/**
* Function to calculate available variety sessions for ajax enabled form booking_variety_regn_form()
* @param $timeslot_id - the timeslot ID to query
* @return array containing the variety sessions for specified timeslot that still have capacity
*/
function _booking_get_variety_timeslot_options($timeslot_id) {
$session_options = array();
$session_options[] = "--";
$session_query = db_query("SELECT * FROM {booking_variety_sessions} WHERE booking_variety_timeslot_id = :tid AND booking_variety_status = 1",
array(':tid' => $timeslot_id));
// Only add sessions that aren't full to the return result
foreach($session_query as $session) {
$available_spots = $session->booking_variety_maxsize - $session->booking_variety_regncount;
if ($available_spots > 0) {
$session_options[$session->vid] = $session->booking_variety_descrip . " [" . $available_spots . " spots]";
}
}
//watchdog('booking_debug', "<pre>Variety Session Options:\n@info</pre>", array('@info' => print_r( $session_options, true)));
return $session_options;
}
/**
* Callback function to rebuild the variety session fieldset
*/
function booking_variety_session_form_callback($form, &$form_state) {
$form_state['rebuild'] = TRUE;
//$form['form']['variety-sessions']['booking_feedback_wrapper']['#markup'] = '<div id="booking_feedback_wrapper">Test</div>';
//return $form['form']['variety-sessions'];
$form['form']['identity']['booking_feedback_wrapper']['#markup'] = '<div id="booking_feedback_wrapper">Test</div>';
return $form['form'];
}
/**
* Validate the submission for the user-facing variety session registration form
*/
function booking_variety_regn_form_validate($form, &$form_state) {
global $event;
$values = $form_state['input'];
//watchdog('booking_debug', 'booking_variety_regn_form_validate: <pre>@info</pre>', array('@info' => print_r( $form_state, true)));
// Don't run validation on ajax callback
if (isset($form_state['input']['_triggering_element_name'])) {
//watchdog('booking_debug', 'booking_variety_regn_form_validate: skipping due to ajax callback');
return;
}
// --- Check that the registration number is valid ---
//verify that user-entered data is a number
if (! preg_match('/^[0-9]+$/', $values['booking_nid'])) {
form_set_error('booking_nid', t('You have entered an invalid booking reference number.'));
}
// Perform lookup on barcode to make sure it matches someone attending the current event
$db_and = db_and();
$db_and->condition('p.booking_eventid', $event->eid, '=');
$db_and->condition('p.booking_status', 1, '=');
$db_and->condition('p.nid', $values['booking_nid'], '=');
$db_and->condition('p.booking_lastname', $values['booking_lastname'], '=');
$query = db_select('booking_person', 'p');
$query->condition($db_and)
->fields('p');
$person = $query->execute()
->fetchObject();
if (! $person) {
form_set_error('booking_nid', t('You have entered an invalid booking reference number or your surname does not match your registration details.'));
}
// --- Check that this person hasn't already registered for variety sessions
$prev_regn_query = db_select('booking_variety_regn', 'v')
->condition('v.booking_person_nid', $values['booking_nid'], '=')
->fields('v')
->execute();
if ($prev_regn_query->rowCount() > 0) {
form_set_error('booking_nid', t('You have already registered for variety sessions. Existing registration cannot be changed.'));
}
// --- Check there is still space available in the selected variety sessions ---
//get a list of timeslot IDs from matching form values
$variety_timeslot_ids = preg_filter('/^select-variety-(\d+)/', '$1', array_keys( $values ));
//query the sessions table
$sessions_query = db_query("SELECT * FROM {booking_variety_sessions} WHERE booking_eventid = :eid",
array(':eid' => $event->eid));
$sessions = $sessions_query->fetchAllAssoc('vid');
//watchdog('booking_debug', 'booking_variety_regn_form_validate sessions query: <pre>@info</pre>', array('@info' => print_r( $sessions, true)));
//check there is still room
foreach ($variety_timeslot_ids as $id) {
$selected_session_id = $values['select-variety-' . $id];
// Don't try and check availablity for a select element that is still on the default value
if ($selected_session_id == 0) {
form_set_error('select-variety-' . $id, t('You have not selected a variety session.'));
continue;
}
$session = $sessions[$selected_session_id];
if ($session->booking_variety_regncount >= $session->booking_variety_maxsize) {
//watchdog('booking_debug', 'No room in session @id : @count is less than @size', array(
// '@id' => $selected_session_id, '@count' => $session->booking_variety_regncount,
// '@size' => $session->booking_variety_maxsize
//));
$form_state['rebuild'] = TRUE;
$form_state['flag'] = 1;
drupal_set_message('You have selected a session that is now full. Please try again.', 'error', FALSE);
}
}
}
/**
* Process the submission for the user-facing variety session registration form
*/
function booking_variety_regn_form_submit($form, &$form_state) {
global $event;
$values = $form_state['input'];
watchdog('booking_debug', 'booking_variety_regn_form_submit: <pre>@info</pre>', array('@info' => print_r( $form_state, true)));
$booking_variety_ids = array();
//get a list of timeslot IDs from matching form values
$variety_timeslot_ids = preg_filter('/^select-variety-(\d+)/', '$1', array_keys( $values ));
//query the sessions table
$sessions_query = db_query("SELECT * FROM {booking_variety_sessions} WHERE booking_eventid = :eid",
array(':eid' => $event->eid));
$sessions = $sessions_query->fetchAllAssoc('vid');
foreach ($variety_timeslot_ids as $id) {
$selected_session_id = $values['select-variety-' . $id];
// Don't try and check availablity for a select element that is still on the default value
if ($selected_session_id == 0) {
continue;
}
//use an update query for the regncount field
//idea from https://api.drupal.org/comment/19374#comment-19374
db_update('booking_variety_sessions')
->expression('booking_variety_regncount', 'booking_variety_regncount + :count', array(':count' => 1))
->condition('vid', $selected_session_id)
->execute();
//store the selected variety sessions in an array of IDs
$booking_variety_ids[$id] = $selected_session_id;
}
//perform the insert to the booking_variety_regn table
db_insert('booking_variety_regn')
->fields(array(
'booking_variety_ids' => drupal_json_encode($booking_variety_ids),
'booking_person_nid' => $values['booking_nid'],
))
->execute();
drupal_set_message("Thanks for submitting your variety session registration.", $type = 'status');
$form_state['redirect'] = array('');
}