From 8d33ec2d2f8a300c6c95a007c21e2229ed257073 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Sun, 19 Jan 2014 00:40:05 +1100 Subject: [PATCH] Added randomisation to study group calculations --- booking.studygroups.inc | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/booking.studygroups.inc b/booking.studygroups.inc index e6902a0..af318ac 100644 --- a/booking.studygroups.inc +++ b/booking.studygroups.inc @@ -201,6 +201,19 @@ function clone_array($copied_array) { }, $copied_array); } +//taken from http://stackoverflow.com/questions/4102777/php-random-shuffle-array-maintaining-key-value +function shuffle_assoc($list) { + if (!is_array($list)) return $list; + + $keys = array_keys($list); + shuffle($keys); + $random = array(); + foreach ($keys as $key) { + $random[$key] = $list[$key]; + } + return $random; +} + /** * Function for calculating who belongs to which study group */ @@ -242,7 +255,7 @@ function booking_studygroups_calculate() { //create a temporary copy of the attendee list to work with for this study group $working_list = array(); - $working_list = clone_array($attendees); + $working_list = shuffle_assoc(clone_array($attendees)); //set up the iterator $obj = new ArrayObject( $working_list ); $it = $obj->getIterator(); @@ -296,7 +309,7 @@ function booking_studygroups_calculate() { //watchdog('booking', 'Attendee before leader check has id !id.', array('!id' => $it->key())); //check if this attendee is a leader - if ($current->processed == 0 && $current->booking_available_lead > 0) + if ($current->processed == 0 && $current->booking_available_lead > 0 && $leader_found == FALSE) { drupal_set_message(t('Found available leader with id !id for session !session.', array('!id' => $it->key(), '!session' => $session_id))); @@ -322,7 +335,7 @@ function booking_studygroups_calculate() { } } //check if this attendee is a helper - elseif ($current->processed == 0 && $current->booking_available_help > 0) + elseif ($current->processed == 0 && $current->booking_available_help > 0 && $helper_found == FALSE) { drupal_set_message(t('Found available helper with id !id for session !session.', array('!id' => $it->key(), '!session' => $session_id))); @@ -408,8 +421,9 @@ function booking_studygroups_calculate() { } - //move to the next attendee in the list - $it->next(); + //move to the next unprocessed attendee in the list + while ($it->valid() && $it->current()->processed == 1) + $it->next(); } //finished looping through session list