diff --git a/booking.studygroups.inc b/booking.studygroups.inc index 0579c3c..dbc2b60 100644 --- a/booking.studygroups.inc +++ b/booking.studygroups.inc @@ -252,7 +252,8 @@ function booking_studygroups_calculate() { $session_count = array(); //select any entries already in the mapping table - $group_mapping = 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)); + $group_mapping = $group_mapping_query->fetchAllAssoc('sid'); //iterate over the attendee associative array and add some fields foreach ($attendees as $person) @@ -353,7 +354,7 @@ function booking_studygroups_calculate() { $current->is_leader = 1; //keep track of the number of people in this study group session - //$session_count[$session_id] = $session_count[$session_id] + 1; + $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; @@ -370,14 +371,8 @@ function booking_studygroups_calculate() { //$spouse = ; $working_list[$partner_id]->session = $session_id; $working_list[$partner_id]->processed = 1; - //add two people to this session - $session_count[$session_id] = $session_count[$session_id] + 2; - } - else - { - //only add one person to this session - $session_count[$session_id] = $session_count[$session_id] + 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) @@ -391,7 +386,7 @@ function booking_studygroups_calculate() { $current->session = $session_id; $current->processed = 1; $current->is_helper = 1; - //$session_count[$session_id] = $session_count[$session_id] + 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; @@ -405,16 +400,12 @@ function booking_studygroups_calculate() { 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] = $session_count[$session_id] + 2; + $session_count[$session_id]++; $working_list[$partner_id]->session = $session_id; $working_list[$partner_id]->processed = 1; - } - else - { - $session_count[$session_id] = $session_count[$session_id] + 1; } } - //otherwise go to the next one + //this attendee can be neither a leader nor a helper, go to the next attendee else { $it->next(); @@ -423,7 +414,7 @@ function booking_studygroups_calculate() { //watchdog('booking', "Attendee list: @info", array('@info' => var_export($session_count, TRUE))); - //reset the iterator to starting position + //reset the iterator to starting position of the attendee list $it->rewind(); //set our counter @@ -488,6 +479,7 @@ function booking_studygroups_calculate() { $current->session = $i; $current->processed = 1; $partner_id = $current->booking_partner_id; + $session_count[$i]++; //check if the attendee was married if ($partner_id > 0) @@ -498,13 +490,8 @@ function booking_studygroups_calculate() { $working_list[$partner_id]->session = $i; $working_list[$partner_id]->processed = 1; - $session_count[$i] = $session_count[$i] + 2; + $session_count[$i]++; } - else - { - $session_count[$i] = $session_count[$i] + 1; - } - } //move to the next unprocessed attendee in the list @@ -530,15 +517,48 @@ function booking_studygroups_calculate() { //TODO: a check to update existing records rather than inserting new one // if already in $group_mapping then just run an update query here - $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, - ); - $insert_query->values($record); + $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, + )) + ->condition('sid', $mapping->sid) + ->execute(); + + break; + } + } + + //if we didn't find an existing record, add it to the list to insert + if (! $found) + { + 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, + ); + $insert_query->values($record); + } } $insert_query->execute();