diff --git a/booking.studygroups.inc b/booking.studygroups.inc index 363fa71..9597f40 100644 --- a/booking.studygroups.inc +++ b/booking.studygroups.inc @@ -576,7 +576,7 @@ function booking_studygroups_edit_form_submit($form, &$form_state) { db_insert('booking_studygroup_mapping') ->fields(array( - 'booking_eventid' => $event->eid, + 'booking_eventid' => $event->eid, 'booking_node_id' => $nid, 'booking_studygroup_id' => $key, 'booking_session_id' => $value, @@ -726,6 +726,8 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { ); } + _booking_studygroups_update_preallocate($group, $group_mapping, $working_list, $session_count, $calculation_messages); + /* //@todo move this foreach loop into a separate function //search for the leaders helpers, spouses and boyfriend/girlfriend for this study group to pre-allocate foreach ($group_mapping as $person) { @@ -834,6 +836,8 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { } //end looking at people with spouse or bf/gf } //end checking for matching study group id } //end searching for people to pre-allocate + + */ //watchdog('booking', "
Attendee list working copy after pre-processing:\n@info
", array('@info' => print_r( $working_list, true))); //watchdog('booking', "Attendee list working copy after leader/helper spouse processing: @info", array('@info' => var_export($working_list, TRUE))); @@ -852,23 +856,24 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { $i = 1; //iterate over the attendee list - while ( $it->valid() ) - { + while ( $it->valid() ) { //cycle the session counter if we already reached the end - if ($i > $group->booking_num_group_sessions) - $i = 1; - + if ($i > $group->booking_num_group_sessions) { + $i = 1; + } + //get the current attendee element $current = $it->current(); //assign this attendee to this session if unprocessed so far - if ($current->processed == 0) - { + if ($current->processed == 0) { //use their existing study group as a starting point, if already defined - if ($current->session > 0) + if ($current->session > 0) { $start = $current->session; - else + } + else { $start = $i; + } //generate stats about this attendee $gender = $current->booking_gender == 'M' ? 'male' : 'female'; @@ -897,8 +902,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { } //end processed check //move to the next unprocessed attendee in the list - while ($it->valid() && $it->current()->processed == 1) - { + while ($it->valid() && $it->current()->processed == 1) { //drupal_set_message(t("Skipping attendee ID !id, already processed.", array('!id' => $it->key()))); $it->next(); } @@ -909,24 +913,20 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { //now calculate the updates for the database - foreach($working_list as $person) - { + foreach($working_list as $person) { $found = FALSE; $message = ""; //iterate all the groups for each person - foreach ($group_mapping as $mapping) - { + foreach ($group_mapping as $mapping) { //check if we can find a study group session already for this user and this study group (eg Monday Tuesday or Wednesday) - if ($person->nid == $mapping->booking_node_id && $group->sid == $mapping->booking_studygroup_id) - { + if ($person->nid == $mapping->booking_node_id && $group->sid == $mapping->booking_studygroup_id) { //keep track of our success to avoid doing an insert $found = TRUE; //if the existing session is not the same as our newly calculated session, run an update query //if ($mapping->booking_session_id !== $person->session && $group->sid != $reading_group_id) - if ($mapping->booking_session_id !== $person->session && $group->booking_is_readinggroup == 'N') - { + if ($mapping->booking_session_id !== $person->session && $group->booking_is_readinggroup == 'N') { $message = t('Suggest changing user !id (!name) from session !old to !session. Role id is !role', array('!id' => $person->nid, '!old' => $mapping->booking_session_id, '!session' => $person->session, '!role' => $person->booking_studygroup_role, '!name' => $person->booking_firstname . " " . $person->booking_lastname)); @@ -940,8 +940,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { } //elseif ($mapping->booking_session_id !== $person->session && $group->sid == $reading_group_id) - elseif ($mapping->booking_session_id !== $person->session && $group->booking_is_readinggroup == 'Y') - { + elseif ($mapping->booking_session_id !== $person->session && $group->booking_is_readinggroup == 'Y') { $message = t('Suggest changing user !id (!name) from reading group session !old to !session. Role id is !role', array('!id' => $person->nid, '!old' => $mapping->booking_session_id, '!session' => $person->session, '!role' => $person->booking_studygroup_role, '!name' => $person->booking_firstname . " " . $person->booking_lastname)); @@ -961,8 +960,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { } //end looping through study group mappings //if we didn't find an existing record, add it to the list to insert - if (! $found && $person->session <> 0) - { + if (! $found && $person->session <> 0) { $message = t('Found no existing study group session for user id !id (!name). Adding to list of inserts for session !session.', array('!id' => $person->nid, '!sid' => $mapping->sid, '!session' => $person->session, '!name' => $person->booking_firstname . " " . $person->booking_lastname @@ -979,8 +977,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { } //log any update we made - if ($message !== "") - { + if ($message !== "") { $update_messages[] = $message; //watchdog('booking', $message); } @@ -998,21 +995,11 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { watchdog('booking', "
" . $final_message . "\n" . implode("\n", $update_messages) . "
"); //make sure to update team colours if necessary - if ($group->booking_is_readinggroup == 'Y') - { + if ($group->booking_is_readinggroup == 'Y') { booking_studygroups_process_colours(); } - } //finished processing study groups - /* - //make sure to update team colours if necessary - if ($sid == $reading_group_id) - { - booking_studygroups_process_colours(); - } - */ - //watchdog('booking', "Attendee list final version: @info", array('@info' => var_export($attendees, TRUE))); $form['log'] = array( @@ -1056,7 +1043,122 @@ function booking_studygroups_update_form($node, &$form_state, $sid) { ), 'form' => $form, ); - +} + +/** + * Process attendee list to mark attendees that should be placed based on certain criteria rather than assigned based on age/gender + * Criteria include leader/helper role, spouse, relationship, committee member + * @see booking_studygroups_update_form() + */ +function _booking_studygroups_update_preallocate($group, $group_mapping, &$session_count, &$working_list, &$calculation_messages) { + //search for the leaders helpers, spouses and boyfriend/girlfriend for this study group to pre-allocate + foreach ($group_mapping as $person) { + //dereference a bunch of fields we'll be using + $committee_flag = $working_list[$person->booking_node_id]->booking_committee_member; + $manually_allocated_flag = $person->booking_session_manually_allocated; + $spouse_id = $working_list[$person->booking_node_id]->booking_partner_id; + $bf_gf_id = $working_list[$person->booking_node_id]->booking_bf_gf_nid; + $gender = $working_list[$person->booking_node_id]->booking_gender == 'M' ? 'male' : 'female'; + $session_id = $person->booking_session_id; + + //if the study group id matches the group we're currently looking at + if ($person->booking_studygroup_id == $group->sid) { + //if they have a role defined + //or if they are a committee member + //or if the manually allocated flag is set + //then make sure they stay where they are + if (($person->booking_studygroup_role > 0) || + ($committee_flag == 'Y') || + ($manually_allocated_flag == 'Y')) { + + $calculation_messages[] = t('Leader/helper/committee/manual allocation 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]['total']) + ); + + //mark this position as being used + $working_list[$person->booking_node_id]->booking_studygroup_role = $person->booking_studygroup_role; + $age = _booking_get_age_years($working_list[$person->booking_node_id]->booking_dob); + _booking_assign_attendee_group($person->booking_node_id, $session_id, $gender, $age, $working_list, $session_count, $calculation_messages); + + //make sure the leader/helper's partner gets updated now, otherwise they could still end up in different groups + if ($spouse_id > 0) { + //also mark their spouse as allocated to this group + $calculation_messages[] = 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]['total']) + ); + $age = _booking_get_age_years($working_list[$spouse_id]->booking_dob); + $spouse_gender = $working_list[$spouse_id]->booking_gender == 'M' ? 'male' : 'female'; + _booking_assign_attendee_group($spouse_id, $session_id, $spouse_gender, $age, $working_list, $session_count, $calculation_messages); + } + elseif ($bf_gf_id > 0) { + //also mark their boyfriend/girlfriend as allocated to this group + $calculation_messages[] = t('BF/GF with id !id assigned to session !session (currently with !num people).', + array('!id' => $bf_gf_id, '!session' => $person->booking_session_id, + '!num' => $session_count[$person->booking_session_id]['total']) + ); + $age = _booking_get_age_years($working_list[$bf_gf_id]->booking_dob); + $bfgf_gender = $working_list[$bf_gf_id]->booking_gender == 'M' ? 'male' : 'female'; + _booking_assign_attendee_group($bf_gf_id, $session_id, $bfgf_gender, $age, $working_list, $session_count, $calculation_messages); + } + } + //anyone else already assigned to this group previously + elseif ($working_list[$person->booking_node_id]->processed == 0) { + if ($spouse_id > 0) { + //if the spouse has already been processed due to being a leader or helper, use that session + if ($working_list[$spouse_id]->processed == 1) { + $session_id = $working_list[$spouse_id]->session; + } + + //mark this person as allocated to this group + $calculation_messages[] = t('Married person 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]['total']) + ); + $age = _booking_get_age_years($working_list[$person->booking_node_id]->booking_dob); + _booking_assign_attendee_group($person->booking_node_id, $session_id, $gender, $age, $working_list, $session_count, $calculation_messages); + + //also mark their spouse as allocated to this group + $calculation_messages[] = 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]['total']) + ); + $age = _booking_get_age_years($working_list[$spouse_id]->booking_dob); + $spouse_gender = $working_list[$spouse_id]->booking_gender == 'M' ? 'male' : 'female'; + _booking_assign_attendee_group($spouse_id, $session_id, $spouse_gender, $age, $working_list, $session_count, $calculation_messages); + } + elseif ($bf_gf_id > 0) { + //if the bf/gf has already been processed due to being a leader or helper, use that session + if ($working_list[$bf_gf_id]->processed == 1) { + $session_id = $working_list[$bf_gf_id]->session; + } + + //mark this person as allocated to this group + $calculation_messages[] = t('Person with id !id in relationship 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]['total']) + ); + $age = _booking_get_age_years($working_list[$person->booking_node_id]->booking_dob); + _booking_assign_attendee_group($person->booking_node_id, $session_id, $gender, $age, $working_list, $session_count, $calculation_messages); + + //also mark their boyfriend/girlfriend as allocated to this group + $calculation_messages[] = t('BF/GF with id !id assigned to session !session (currently with !num people).', + array('!id' => $bf_gf_id, '!session' => $person->booking_session_id, + '!num' => $session_count[$person->booking_session_id]['total']) + ); + $age = _booking_get_age_years($working_list[$bf_gf_id]->booking_dob); + $bfgf_gender = $working_list[$bf_gf_id]->booking_gender == 'M' ? 'male' : 'female'; + _booking_assign_attendee_group($bf_gf_id, $session_id, $bfgf_gender, $age, $working_list, $session_count, $calculation_messages); + } + //for anyone else, just record what session they're currently in + else { + $working_list[$person->booking_node_id]->session = $session_id; + } + } //end looking at people with spouse or bf/gf + } //end checking for matching study group id + } //end searching for people to pre-allocate + } /**