Refactor studygroups calculations now that leaders/helpers are manually assigned

This commit is contained in:
2014-02-08 21:50:44 +11:00
parent 8856c037e0
commit 750eb292b6
2 changed files with 55 additions and 133 deletions

View File

@@ -192,7 +192,7 @@ function booking_admin() {
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Dietary Requirements Text Definition'), '#title' => t('Dietary Requirements Text Definition'),
'#default_value' => variable_get('booking_dietary_text_definition', ''), '#default_value' => variable_get('booking_dietary_text_definition', ''),
'#description' => 'Text to use if attendee cannot specify dietary requirements', '#description' => 'Text to use if attendee may not specify dietary requirements (as above).',
); );
return system_settings_form($form); return system_settings_form($form);

View File

@@ -56,6 +56,7 @@ function booking_available_leadhelp_select_form() {
'booking_baptised' => array('data' => t('Baptised?'), 'field' => 'booking_baptised'), 'booking_baptised' => array('data' => t('Baptised?'), 'field' => 'booking_baptised'),
'booking_age' => array('data' => t('Age (start of week)'), 'field' => 'booking_dob', 'sort' => 'asc'), 'booking_age' => array('data' => t('Age (start of week)'), 'field' => 'booking_dob', 'sort' => 'asc'),
'booking_state' => array('data' => t('State'), 'field' => 'booking_state'), 'booking_state' => array('data' => t('State'), 'field' => 'booking_state'),
'booking_status' => array('data' => t('Status'), 'field' => 'booking_status'),
//'booking_assign_leader_group' => array('data' => t('Assign leader group')), //'booking_assign_leader_group' => array('data' => t('Assign leader group')),
'booking_assign_leader_session' => array('data' => t('Leader Session')), 'booking_assign_leader_session' => array('data' => t('Leader Session')),
//'booking_assign_helper_group' => array('data' => t('Assign helper group')), //'booking_assign_helper_group' => array('data' => t('Assign helper group')),
@@ -101,6 +102,7 @@ function booking_available_leadhelp_select_form() {
'booking_baptised' => $data->booking_baptised == 'Y' ? 'Yes' : 'No', 'booking_baptised' => $data->booking_baptised == 'Y' ? 'Yes' : 'No',
'booking_age' => _booking_avg_age($data->booking_dob, 1, $event->booking_event_start), 'booking_age' => _booking_avg_age($data->booking_dob, 1, $event->booking_event_start),
'booking_state' => $data->booking_state, 'booking_state' => $data->booking_state,
'booking_status' => _booking_status_generate($data->booking_status),
//'booking_lead_sessions' => $data->booking_total_lead === NULL ? '0' : $data->booking_total_lead, //'booking_lead_sessions' => $data->booking_total_lead === NULL ? '0' : $data->booking_total_lead,
//'booking_available_lead' => $data->booking_available_lead === NULL ? '0' : $data->booking_available_lead, //'booking_available_lead' => $data->booking_available_lead === NULL ? '0' : $data->booking_available_lead,
//'booking_help_sessions' => $data->booking_total_help === NULL ? '0' : $data->booking_total_help, //'booking_help_sessions' => $data->booking_total_help === NULL ? '0' : $data->booking_total_help,
@@ -453,9 +455,13 @@ function booking_studygroups_calculate() {
$max_people = (int) ($limit / $firstgroup->booking_num_group_sessions) + 1; $max_people = (int) ($limit / $firstgroup->booking_num_group_sessions) + 1;
//select all the attendees booked in //select all the attendees booked in
/*
$query = db_query("SELECT p.nid, p.booking_partner_id, p.booking_event_id, p.booking_status, l.booking_total_lead, l.booking_available_lead, l.booking_total_help, l.booking_available_help FROM {booking_person} p left outer join {booking_leadhelp_list} l on p.nid = l.booking_node_id WHERE p.booking_event_id = :eid AND p.booking_status = 1", $query = db_query("SELECT p.nid, p.booking_partner_id, p.booking_event_id, p.booking_status, l.booking_total_lead, l.booking_available_lead, l.booking_total_help, l.booking_available_help FROM {booking_person} p left outer join {booking_leadhelp_list} l on p.nid = l.booking_node_id WHERE p.booking_event_id = :eid AND p.booking_status = 1",
array(':eid' => $event->eid)); array(':eid' => $event->eid));
$attendees = $query->fetchAllAssoc('nid'); */
$query = db_query("SELECT * FROM {booking_person} WHERE booking_event_id = :eid",
array(':eid' => $event->eid));
$attendees = $query->fetchAllAssoc('nid');
//select any entries already in the mapping table //select any entries already in the mapping table
$group_mapping_query = db_query("SELECT * FROM {booking_studygroup_mapping} WHERE booking_eventid = :eid", array(':eid' => $event->eid)); $group_mapping_query = db_query("SELECT * FROM {booking_studygroup_mapping} WHERE booking_eventid = :eid", array(':eid' => $event->eid));
@@ -466,15 +472,15 @@ function booking_studygroups_calculate() {
{ {
//flag that indicates processed or not //flag that indicates processed or not
$person->processed = 0; $person->processed = 0;
//make sure we skip attendees that do not have the status of "booked in"
if ($person->booking_status <> 1)
$person->processed = 1;
//field that indicates the session id the person is assigned to //field that indicates the session id the person is assigned to
$person->session = 0; $person->session = 0;
$person->is_leader = 0; $person->is_leader = 'N';
$person->is_helper = 0; $person->is_helper = 'N';
//convert NULLs into zero
$person->booking_total_lead = $person->booking_total_lead === NULL ? '0' : $person->booking_total_lead;
$person->booking_available_lead = $person->booking_available_lead === NULL ? '0' : $person->booking_available_lead;
$person->booking_total_help = $person->booking_total_help === NULL ? '0' : $person->booking_total_help;
$person->booking_available_help = $person->booking_available_help === NULL ? '0' : $person->booking_available_help;
} }
//watchdog('booking', "Attendee list: @info", array('@info' => var_export($attendees, TRUE))); //watchdog('booking', "Attendee list: @info", array('@info' => var_export($attendees, TRUE)));
@@ -495,6 +501,34 @@ function booking_studygroups_calculate() {
//clear the array keeping track of the number of people in each session for this group //clear the array keeping track of the number of people in each session for this group
for ($i = 1; $i <= $group->booking_num_group_sessions; $i++) for ($i = 1; $i <= $group->booking_num_group_sessions; $i++)
$session_count[$i] = 0; $session_count[$i] = 0;
//search for the leaders and helpers for this study group
foreach ($group_mapping as $person)
{
if ($person->booking_studygroup_id == $group->sid && ($person->booking_is_leader == 'Y' || $person->booking_is_helper == 'Y'))
{
drupal_set_message(t('Leader/helper with id !id assigned to session !session (currently with !num people).', array('!id' => $person->booking_node_id, '!session' => $person->booking_session_id, '!num' => $session_count[$person->booking_session_id])));
//mark a position in this session as being used
$session_count[$person->booking_session_id]++;
//mark this person as processed in the working list
$working_list[$person->booking_node_id]->processed = 1;
//search for a spouse
$spouse_id = $working_list[$person->booking_node_id]->booking_partner_id;
if ($spouse_id > 0)
{
drupal_set_message(t('Spouse with id !id assigned to session !session (currently with !num people).', array('!id' => $spouse_id, '!session' => $person->booking_session_id, '!num' => $session_count[$person->booking_session_id])));
//allocate the spouse to the same session
$working_list[$spouse_id]->session = $person->booking_session_id;
$working_list[$spouse_id]->processed = 1;
$session_count[$person->booking_session_id]++;
}
}
}
//watchdog('booking', "Attendee list working copy after leader/helper spouse processing: @info", array('@info' => var_export($working_list, TRUE)));
//watchdog('booking', "Attendee list session count after leader/helper spouse processing: @info", array('@info' => var_export($session_count, TRUE)));
//reset the iterator to starting position //reset the iterator to starting position
$it->rewind(); $it->rewind();
@@ -504,120 +538,7 @@ function booking_studygroups_calculate() {
$session_id = 1; $session_id = 1;
$rewound = FALSE; $rewound = FALSE;
$leader_found = FALSE;
$helper_found = FALSE;
while (true)
{
//if we're at the end of the attendee list, go back to the start
if (! $it->valid() )
{
if ($rewound == TRUE)
{
//watchdog('booking', "Already rewound once.");
drupal_set_message(t("Error: Reached end of attendee list before allocating all study group leaders/helpers for group !group.",
array('!group' => $group->booking_studygroup_descrip)), 'error', FALSE);
//we've already gone back to the start once, don't do it again
break;
}
else
{
watchdog('booking', "Rewinding to start of attendee list.");
$it->rewind();
$rewound = TRUE;
}
}
//check if we have reached the total number of sessions required
if ($session_id > $group->booking_num_group_sessions)
{
break;
}
//check if we can increment the session count
if ($leader_found == TRUE && $helper_found == TRUE)
{
$session_id++;
$leader_found = FALSE;
$helper_found = FALSE;
}
//get the current attendee element
$current = $it->current();
$key = $it->key();
//watchdog('booking', 'Attendee before leader check has id !id.', array('!id' => $it->key()));
//check if this attendee can be a leader
if ($leader_found == FALSE && $current->processed == 0 && $current->booking_available_lead > 0)
{
drupal_set_message(t('Found available leader with id !id for session !session (currently with !num people).',
array('!id' => $it->key(), '!session' => $session_id, '!num' => $session_count[$session_id])));
$leader_found = TRUE;
//assign leader to session and mark as processed in the temporary attendee list
$current->session = $session_id;
$current->processed = 1;
$current->is_leader = 1;
//keep track of the number of people in this study group session
$session_count[$session_id]++;
//decrement the number of available leading positions for this user in our master copy of the attendee list
$attendees[$key]->booking_available_lead = $attendees[$key]->booking_available_lead - 1;
$partner_id = $current->booking_partner_id;
//Check for spouse of leader, allocate to this group also
if ($partner_id > 0)
{
//add the spouse to the same session and mark as processed in the temporary attendee list
drupal_set_message(t('Assigning spouse (id !spouse) of id !id to session !session (currently with !num people).',
array('!id' => $it->key(), '!session' => $session_id, '!spouse' => $partner_id, '!num' => $session_count[$session_id])));
//$spouse = ;
$working_list[$partner_id]->session = $session_id;
$working_list[$partner_id]->processed = 1;
$session_count[$session_id]++;
}
}
//check if this attendee can be a helper
elseif ($helper_found == FALSE && $current->processed == 0 && $current->booking_available_help > 0)
{
drupal_set_message(t('Found available helper with id !id for session !session (currently with !num people).',
array('!id' => $it->key(), '!session' => $session_id, '!num' => $session_count[$session_id])));
$helper_found = TRUE;
//assign leader to session and mark as processed in the temporary attendee list
$current->session = $session_id;
$current->processed = 1;
$current->is_helper = 1;
$session_count[$session_id]++;
//decrement the number of available helping positions for this user in our master copy of the attendee list
$attendees[$key]->booking_available_help = $attendees[$key]->booking_available_help - 1;
$partner_id = $current->booking_partner_id;
//Check for spouse of helper, allocate to this group also
if ($partner_id > 0)
{
//add the spouse to the same session and mark as processed in the temporary attendee list
drupal_set_message(t('Assigning spouse (id !spouse) of id !id to session !session.',
array('!id' => $it->key(), '!session' => $session_id, '!spouse' => $current->booking_partner_id)));
$session_count[$session_id]++;
$working_list[$partner_id]->session = $session_id;
$working_list[$partner_id]->processed = 1;
}
}
//this attendee can be neither a leader nor a helper, go to the next attendee
else
{
$it->next();
}
}
//watchdog('booking', "Attendee list: @info", array('@info' => var_export($session_count, TRUE))); //watchdog('booking', "Attendee list: @info", array('@info' => var_export($session_count, TRUE)));
//reset the iterator to starting position of the attendee list //reset the iterator to starting position of the attendee list
@@ -659,7 +580,7 @@ function booking_studygroups_calculate() {
//make sure we don't get stuck in an infinite loop - only go past the end once //make sure we don't get stuck in an infinite loop - only go past the end once
if ($break_condition == true) if ($break_condition == true)
{ {
drupal_set_message("Ran out of sessions that aren't full to place attendees into."); drupal_set_message(t("Ran out of sessions that aren't full to place attendees into for '!id' group.", array('!id' => $group->booking_studygroup_descrip)), 'error', FALSE);
break; break;
} }
$break_condition = true; $break_condition = true;
@@ -672,7 +593,7 @@ function booking_studygroups_calculate() {
//assign this attendee to this session if unprocessed so far //assign this attendee to this session if unprocessed so far
if ($current->processed == 0) if ($current->processed == 0)
{ {
drupal_set_message(t('Assigning person with id !id to session !session (now with !num people).', array('!id' => $it->key(), '!session' => $i, '!num' => $session_count[$i]))); drupal_set_message(t('Assigning person with id !id to session !session (currently with !num people).', array('!id' => $it->key(), '!session' => $i, '!num' => $session_count[$i])));
$current->session = $i; $current->session = $i;
$current->processed = 1; $current->processed = 1;
$partner_id = $current->booking_partner_id; $partner_id = $current->booking_partner_id;
@@ -682,8 +603,8 @@ function booking_studygroups_calculate() {
if ($partner_id > 0) if ($partner_id > 0)
{ {
//add the spouse to the same session and mark as processed in the temporary attendee list //add the spouse to the same session and mark as processed in the temporary attendee list
drupal_set_message(t('Assigning spouse (id !spouse) of id !id to session !session.', drupal_set_message(t('Assigning spouse (id !spouse) of id !id to session !session (currently with !num people).',
array('!id' => $it->key(), '!session' => $i, '!spouse' => $current->booking_partner_id))); array('!id' => $it->key(), '!session' => $i, '!spouse' => $current->booking_partner_id, '!num' => $session_count[$i])));
$working_list[$partner_id]->session = $i; $working_list[$partner_id]->session = $i;
$working_list[$partner_id]->processed = 1; $working_list[$partner_id]->processed = 1;
@@ -722,10 +643,11 @@ function booking_studygroups_calculate() {
{ {
$found = TRUE; $found = TRUE;
drupal_set_message(t('Found existing study group session for user id !id. Running update query on table id !sid to set session id to !session', //drupal_set_message(t('Found existing study group session for user id !id. Running update query on table id !sid to set session id to !session',
array('!id' => $person->nid, '!sid' => $mapping->sid, '!session' => $person->session))); // array('!id' => $person->nid, '!sid' => $mapping->sid, '!session' => $person->session)));
//update the entry //update the entry
/*
db_update('booking_studygroup_mapping') db_update('booking_studygroup_mapping')
->fields(array( ->fields(array(
'booking_session_id' => $person->session, 'booking_session_id' => $person->session,
@@ -734,16 +656,16 @@ function booking_studygroups_calculate() {
)) ))
->condition('sid', $mapping->sid) ->condition('sid', $mapping->sid)
->execute(); ->execute();
break; break;
*/
} }
} }
//if we didn't find an existing record, add it to the list to insert //if we didn't find an existing record, add it to the list to insert
if (! $found) if (! $found && $person->session <> 0)
{ {
drupal_set_message(t('Found no existing study group session for user id !id. Adding to list of inserts.', //drupal_set_message(t('Found no existing study group session for user id !id. Adding to list of inserts.',
array('!id' => $person->nid, '!sid' => $mapping->sid, '!session' => $person->session))); // array('!id' => $person->nid, '!sid' => $mapping->sid, '!session' => $person->session)));
$record = array( $record = array(