remove deprecated studygroup calculation function

This commit is contained in:
2016-06-23 18:28:39 +10:00
parent 4adc2b4ee5
commit e69ebc95bd
2 changed files with 22 additions and 353 deletions

View File

@@ -549,16 +549,6 @@ function booking_menu() {
'type' => MENU_CALLBACK 'type' => MENU_CALLBACK
); );
/*
$items['admin/booking/studygroups/calculate'] = array(
'title' => 'Calculate Study Groups',
'description' => 'Calculate Study Group memberships',
'page callback' => 'drupal_get_form',
'page arguments' => array('booking_studygroups_calculate'),
'access arguments' => array('edit study groups'),
'type' => MENU_LOCAL_ACTION,
);
*/
$items['admin/booking/studygroups/%/update'] = array( $items['admin/booking/studygroups/%/update'] = array(
'title' => 'Update Study Groups', 'title' => 'Update Study Groups',
'description' => 'Calculate updated Study Group memberships', 'description' => 'Calculate updated Study Group memberships',

View File

@@ -551,7 +551,8 @@ function booking_studygroups_edit_form_submit($form, &$form_state) {
db_update('booking_studygroup_mapping') db_update('booking_studygroup_mapping')
->fields(array( ->fields(array(
'booking_session_id' => $value, 'booking_session_id' => $value,
'booking_studygroup_role' => $person_groups[$key]->booking_studygroup_role, 'booking_studygroup_role' => $person_groups[$key]->booking_studygroup_role,
'booking_session_manually_allocated' => 'Y',
)) ))
->condition('sid', $person_groups[$key]->sid) ->condition('sid', $person_groups[$key]->sid)
->execute(); ->execute();
@@ -580,6 +581,7 @@ function booking_studygroups_edit_form_submit($form, &$form_state) {
'booking_studygroup_id' => $key, 'booking_studygroup_id' => $key,
'booking_session_id' => $value, 'booking_session_id' => $value,
'booking_studygroup_role' => 0, 'booking_studygroup_role' => 0,
'booking_session_manually_allocated' => 'Y',
)) ))
->execute(); ->execute();
//check if there is a readings group colour to update //check if there is a readings group colour to update
@@ -594,318 +596,6 @@ function booking_studygroups_edit_form_submit($form, &$form_state) {
} //end checkbox loop } //end checkbox loop
} }
/**
* Function for defining the number of study group sessions
* Note: This is hard-coded for now in the install file
*/
/**
* Function for calculating who belongs to which study group
*/
function booking_studygroups_calculate() {
global $event;
//master attendee list
$attendees = array();
//temporary working copy of attendee list
$working_list = array();
//create an array to keep track of the number of people in each session for each group
$session_count = array();
//delete from booking_studygroup_mapping;
//alter table booking_studygroup_mapping AUTO_INCREMENT=1;
//TODO: consider using a lock as per https://api.drupal.org/api/drupal/includes!lock.inc/group/lock/7
//select all the study groups for this event id
$studygroups_query = db_query("SELECT * FROM {booking_studygroup_list} WHERE booking_eventid = :eid", array(':eid' => $event->eid));
$studygroups = $studygroups_query->fetchAllAssoc('sid');
//calculate the max number of attendees in a group
$firstgroup = reset($studygroups);
$limit = variable_get('booking_regn_limit','500');
//add an extra one to the maximum size, to cater for some larger groups when the number of people doesn't divide evenly
$max_people = (int) ($limit / $firstgroup->booking_num_group_sessions) + 2;
//select all the attendees booked in
$query = db_query("SELECT * FROM {booking_person} WHERE booking_eventid = :eid",
array(':eid' => $event->eid));
$attendees = $query->fetchAllAssoc('nid');
//calculate the ratios of males to females, and various age groups
$statistics = _booking_generate_statistics($attendees);
$gender_ratio = ($statistics['males'] / $statistics['females']) * ($max_people / 2);
//store them in a nice easy to access array
$maximums = array(
'male' => ceil($gender_ratio),
'female' => ceil($max_people - $gender_ratio),
'under20' => floor(($statistics['under20'] / $limit) * ($max_people - 2)),
'20to25' => floor(($statistics['20to25'] / $limit) * ($max_people - 2)),
'over25' => floor(($statistics['over25'] / $limit) * ($max_people - 2)),
);
drupal_set_message(t('Aiming for !males males, !females females in group. Made up of !20s under 20, !low20s between 20 and 25, and !high20s over 25. Total count in group is !max.',
array('!20s' => $maximums['under20'], '!low20s' => $maximums['20to25'], '!high20s' => $maximums['over25'],
'!males' => $maximums['male'], '!females' => $maximums['female'], '!max' => $max_people)
));
//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 = $group_mapping_query->fetchAllAssoc('sid');
//iterate over the attendee associative array and add some fields
foreach ($attendees as $person)
{
//flag that indicates processed or not
$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
$person->session = 0;
$person->booking_studygroup_role = 0;
$person->is_leader = 'N';
$person->is_helper = 'N';
$person->is_reserveleader = 'N';
}
//watchdog('booking', "<pre>Attendee list:\n@info</pre>", array('@info' => print_r( $attendees, true)));
//watchdog('booking', "Attendee list: @info", array('@info' => var_export($attendees, TRUE)));
//process each study group (eg Monday Tuesday Wednesday etc)
foreach ($studygroups as $group)
{
drupal_set_message(t('Processing study group !group with !sessions sessions.',
array('!group' => $group->booking_studygroup_descrip, '!sessions' => $group->booking_num_group_sessions)));
//create a temporary copy of the attendee list to work with for this study group
$working_list = array();
$working_list = _booking_shuffle_assoc(_booking_clone_array($attendees));
//set up the iterator
$obj = new ArrayObject( $working_list );
$it = $obj->getIterator();
//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++)
{
$session_count[$i] = array(
'total' => 0,
'male' => 0,
'female' => 0,
'under20' => 0,
'20to25' => 0,
'over25' => 0,
);
}
// $session_count[$i] = 0;
//search for the leaders and helpers for this study group
foreach ($group_mapping as $person) {
//if the study group id matches the group we're currently looking at, and they have a role defined
if ($person->booking_studygroup_id == $group->sid && $person->booking_studygroup_role > 0) {
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]['total'])
));
//mark this position as being used
$age = _booking_get_age_years($working_list[$person->booking_node_id]->booking_dob);
_booking_assign_attendee_group($person->booking_node_id, $person->booking_session_id, 'male', $age, $working_list, $session_count);
//get any potential spouse or boyfriend/girlfriend
$spouse_id = $working_list[$person->booking_node_id]->booking_partner_id;
$bf_gf_id = $working_list[$person->booking_node_id]->booking_bf_gf_nid;
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]['total'])
));
//allocate the spouse to the same session
$age = _booking_get_age_years($working_list[$spouse_id]->booking_dob);
_booking_assign_attendee_group($spouse_id, $person->booking_session_id, 'female', $age, $working_list, $session_count);
}
elseif ($bf_gf_id > 0) {
drupal_set_message(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'])
));
//allocate the boyfriend/girlfriend to the same session
$age = _booking_get_age_years($working_list[$bf_gf_id]->booking_dob);
$gender = $working_list[$bf_gf_id]->booking_gender == 'M' ? 'male' : 'female';
_booking_assign_attendee_group($bf_gf_id, $person->booking_session_id, $gender, $age, $working_list, $session_count);
}
} //end current group check
} //end searching for leaders and helpers
//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
$it->rewind();
//watchdog('booking', "Attendee list: @info", array('@info' => var_export($attendees, TRUE)));
//watchdog('booking', "Attendee list working copy: @info", array('@info' => var_export($working_list, TRUE)));
//watchdog('booking', "Attendee list distribution: @info", array('@info' => var_export($session_count, TRUE)));
//initialise our counters
$session_id = 1;
$rewound = FALSE;
$i = 1;
//iterate over the attendee list
while ($it->valid()) {
//cycle the session counter if we already reached the end
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) {
//generate stats about this attendee
$gender = $current->booking_gender == 'M' ? 'male' : 'female';
$age = _booking_get_age_years($current->booking_dob);
if ($age < 20)
$age_type = 'under20';
elseif($age >= 20 && $age < 25)
$age_type = '20to25';
else
$age_type = 'over25';
//make sure this person is going to fit in with our calculated gender ratios
_booking_loop_carefully($session_count, $gender, $i, $maximums[$gender], $group->booking_num_group_sessions, 3);
//make sure this person is going to fit in with our calculated age ratios
_booking_loop_carefully($session_count, $age_type, $i, $maximums[$age_type], $group->booking_num_group_sessions, 3);
//check for maximum group size
_booking_loop_carefully($session_count, 'total', $i, $max_people, $group->booking_num_group_sessions, 3);
drupal_set_message(t('Assigning person with id !id with gender !gender and age group !age to session !session (currently with !num people).',
array('!id' => $it->key(), '!session' => $i, '!num' => $session_count[$i]['total'], '!gender' => $gender, '!age' => $age_type )
));
$partner_id = $current->booking_partner_id;
$bf_gf_id = $current->booking_bf_gf_nid;
_booking_assign_attendee_group($it->key(), $i, $gender, $age, $working_list, $session_count);
//check if the attendee was married
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' => $i, '!spouse' => $current->booking_partner_id, '!num' => $session_count[$i]['total'])));
_booking_assign_attendee_group($partner_id, $i, $working_list[$partner_id]->booking_gender == 'M' ? 'male' : 'female',
_booking_get_age_years($working_list[$partner_id]->booking_dob), $working_list, $session_count);
}
//Check for boyfriend/girlfriend
if ($bf_gf_id> 0)
{
//add the spouse to the same session and mark as processed in the temporary attendee list
drupal_set_message(t('Assigning bf/gf (id !spouse) of id !id to session !session (currently with !num people).',
array('!id' => $it->key(), '!session' => $i, '!spouse' => $current->booking_bf_gf_nid, '!num' => $session_count[$i]['total'])));
_booking_assign_attendee_group($bf_gf_id, $i, $working_list[$bf_gf_id]->booking_gender == 'M' ? 'male' : 'female',
_booking_get_age_years($working_list[$bf_gf_id]->booking_dob), $working_list, $session_count);
}
} //end processed check
//move to the next unprocessed attendee in the list
while ($it->valid() && $it->current()->processed == 1)
{
//drupal_set_message(t("Skipping attendee ID !id, already processed.", array('!id' => $it->key())));
$it->next();
}
//move to the next session
$i++;
} //finished looping through attendees for this study group
//iterate over the attendee list and write to the database the session they're assigned to
//use the multi-insert query type at https://drupal.org/node/310079
$insert_query = db_insert('booking_studygroup_mapping')->fields(array('booking_eventid', 'booking_node_id',
'booking_studygroup_id', 'booking_session_id', 'booking_studygroup_role'));
foreach($working_list as $person) {
//watchdog('booking', "<pre>Working list person:\n@info</pre>", array('@info' => print_r( $person, true)));
//check to update existing records rather than inserting new one
// if already in $group_mapping then just run an update query here
$found = FALSE;
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) {
$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',
// array('!id' => $person->nid, '!sid' => $mapping->sid, '!session' => $person->session)));
//update the entry
/*
db_update('booking_studygroup_mapping')
->fields(array(
'booking_session_id' => $person->session,
'booking_is_leader' => $person->is_leader,
'booking_is_helper' => $person->is_helper,
'booking_studygroup_role' => $person->booking_studygroup_role,
))
->condition('sid', $mapping->sid)
->execute();
break;
*/
}
}
//if we didn't find an existing record, add it to the list to insert
if (! $found && $person->session <> 0)
{
//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)));
$record = array(
'booking_eventid' => $event->eid,
'booking_node_id' => $person->nid,
'booking_studygroup_id' => $group->sid,
'booking_session_id' => $person->session,
//'booking_is_leader' => $person->is_leader,
//'booking_is_helper' => $person->is_helper,
//'booking_is_reserveleader' => $person->is_reserveleader,
'booking_studygroup_role' => $person->booking_studygroup_role,
);
$insert_query->values($record);
}
}
$insert_query->execute();
//watchdog('booking', "<pre>Session statistics list:\n@info</pre>", array('@info' => print_r( $session_count, true)));
//clear the arrays we've been using for this iteration
unset($session_count);
unset($working_list);
drupal_set_message(t('Finished processing study group !group.', array('!group' => $group->booking_studygroup_descrip)));
} //finished processing study groups
//watchdog('booking', "Attendee list final version: @info", array('@info' => var_export($attendees, TRUE)));
}
/** /**
* Function for calculating who belongs to which study group * Function for calculating who belongs to which study group
*/ */
@@ -924,7 +614,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
$update_messages = array(); $update_messages = array();
$prefix = ""; $prefix = "";
$intro_text = ""; $intro_text = "";
$reading_group_id = variable_get('booking_readinggroup_id','7'); //$reading_group_id = variable_get('booking_readinggroup_id','7');
//store our calculated updates in this array which will then get serialized and added to the form for processing //store our calculated updates in this array which will then get serialized and added to the form for processing
$updates_to_confirm = array(); $updates_to_confirm = array();
@@ -947,8 +637,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
$group_mapping = $group_mapping_query->fetchAllAssoc('sid'); $group_mapping = $group_mapping_query->fetchAllAssoc('sid');
//throw an error if we got no groups //throw an error if we got no groups
if (! $studygroups) if (! $studygroups) {
{
drupal_set_message("Error: Unable to find group with corresponding ID '" . $sid . "'.", 'error', FALSE); drupal_set_message("Error: Unable to find group with corresponding ID '" . $sid . "'.", 'error', FALSE);
drupal_goto('admin/booking/studygroups'); drupal_goto('admin/booking/studygroups');
return ""; return "";
@@ -992,26 +681,24 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
_booking_studygroup_cleanup(); _booking_studygroup_cleanup();
//iterate over the attendee associative array and add some fields //iterate over the attendee associative array and add some fields
foreach ($attendees as $person) foreach ($attendees as $person) {
{
//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->booking_studygroup_role = 0; $person->booking_studygroup_role = 0;
//make sure we skip attendees that do not have the status of "booked in"
if ($person->booking_status <> 1) {
$person->processed = 1;
}
} }
//watchdog('booking', "<pre>Attendee list:\n@info</pre>", array('@info' => print_r( $attendees, true))); //watchdog('booking', "<pre>Attendee list:\n@info</pre>", array('@info' => print_r( $attendees, true)));
//watchdog('booking', "Attendee list: @info", array('@info' => var_export($attendees, TRUE))); //watchdog('booking', "Attendee list: @info", array('@info' => var_export($attendees, TRUE)));
//process each study group (eg Monday Tuesday Wednesday etc) //process each study group (eg Monday Tuesday Wednesday etc)
foreach ($studygroups as $group) foreach ($studygroups as $group) {
{
$prefix .= t('<h2>Processing study group !group with !sessions sessions</h2>', $prefix .= t('<h2>Processing study group !group with !sessions sessions</h2>',
array('!group' => $group->booking_studygroup_descrip, '!sessions' => $group->booking_num_group_sessions)); array('!group' => $group->booking_studygroup_descrip, '!sessions' => $group->booking_num_group_sessions));
$prefix .= t('<p>Aiming for !males males, !females females in group. Made up of !20s under 20, !low20s between 20 and 25, and !high20s over 25. Total count in group is !max.</p>', $prefix .= t('<p>Aiming for !males males, !females females in group. Made up of !20s under 20, !low20s between 20 and 25, and !high20s over 25. Total count in group is !max.</p>',
@@ -1028,8 +715,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
$it = $obj->getIterator(); $it = $obj->getIterator();
//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] = array( $session_count[$i] = array(
'total' => 0, 'total' => 0,
'male' => 0, 'male' => 0,
@@ -1040,6 +726,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
); );
} }
//@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 //search for the leaders helpers, spouses and boyfriend/girlfriend for this study group to pre-allocate
foreach ($group_mapping as $person) { foreach ($group_mapping as $person) {
//dereference a bunch of fields we'll be using //dereference a bunch of fields we'll be using
@@ -1071,8 +758,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
_booking_assign_attendee_group($person->booking_node_id, $session_id, $gender, $age, $working_list, $session_count, $calculation_messages); _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 //make sure the leader/helper's partner gets updated now, otherwise they could still end up in different groups
if ($spouse_id > 0) if ($spouse_id > 0) {
{
//also mark their spouse as allocated to this group //also mark their spouse as allocated to this group
$calculation_messages[] = t('Spouse with id !id assigned to session !session (currently with !num people).', $calculation_messages[] = t('Spouse with id !id assigned to session !session (currently with !num people).',
array('!id' => $spouse_id, '!session' => $person->booking_session_id, array('!id' => $spouse_id, '!session' => $person->booking_session_id,
@@ -1082,8 +768,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
$spouse_gender = $working_list[$spouse_id]->booking_gender == 'M' ? 'male' : 'female'; $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); _booking_assign_attendee_group($spouse_id, $session_id, $spouse_gender, $age, $working_list, $session_count, $calculation_messages);
} }
elseif ($bf_gf_id > 0) elseif ($bf_gf_id > 0) {
{
//also mark their boyfriend/girlfriend as allocated to this group //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).', $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, array('!id' => $bf_gf_id, '!session' => $person->booking_session_id,
@@ -1095,13 +780,10 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
} }
} }
//anyone else already assigned to this group previously //anyone else already assigned to this group previously
elseif ($working_list[$person->booking_node_id]->processed == 0) elseif ($working_list[$person->booking_node_id]->processed == 0) {
{ if ($spouse_id > 0) {
if ($spouse_id > 0)
{
//if the spouse has already been processed due to being a leader or helper, use that session //if the spouse has already been processed due to being a leader or helper, use that session
if ($working_list[$spouse_id]->processed == 1) if ($working_list[$spouse_id]->processed == 1) {
{
$session_id = $working_list[$spouse_id]->session; $session_id = $working_list[$spouse_id]->session;
} }
@@ -1122,11 +804,9 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
$spouse_gender = $working_list[$spouse_id]->booking_gender == 'M' ? 'male' : 'female'; $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); _booking_assign_attendee_group($spouse_id, $session_id, $spouse_gender, $age, $working_list, $session_count, $calculation_messages);
} }
elseif ($bf_gf_id > 0) elseif ($bf_gf_id > 0) {
{
//if the bf/gf has already been processed due to being a leader or helper, use that session //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) if ($working_list[$bf_gf_id]->processed == 1) {
{
$session_id = $working_list[$bf_gf_id]->session; $session_id = $working_list[$bf_gf_id]->session;
} }
@@ -1148,8 +828,7 @@ function booking_studygroups_update_form($node, &$form_state, $sid) {
_booking_assign_attendee_group($bf_gf_id, $session_id, $bfgf_gender, $age, $working_list, $session_count, $calculation_messages); _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 //for anyone else, just record what session they're currently in
else else {
{
$working_list[$person->booking_node_id]->session = $session_id; $working_list[$person->booking_node_id]->session = $session_id;
} }
} //end looking at people with spouse or bf/gf } //end looking at people with spouse or bf/gf