Files
booking/booking.studygroup_leaders.inc

181 lines
5.7 KiB
PHP

<?php
/**
* @file
* Functions for calculating leaders and helpers for discussion groups
*/
/**
* Function for calculating and assigning leaders and helpers to active study group sessions
*/
function booking_studygroups_leaders_calculate() {
global $event;
$calculation_messages = array();
//get a randomly ordered list of people to work with
$people = _booking_shuffle_assoc(_booking_studygroups_retrieve_eligible_people());
//get details of the study groups
$groups = _booking_studygroups_retrieve_groups();
//loop through the list of people
foreach ($people as $person)
{
$age = _booking_get_age_years($person->booking_dob);
$calculation_messages[] = t('Processing eligible Leader/helper with id !id and age !age',
array('!id' => $person->nid, '!age' => $age)
);
foreach ($groups as $studygroup)
{
//watchdog('booking_debug', "<pre>Study Group Element\n@info</pre>", array('@info' => print_r( $studygroup, true)));
if ($age >= 22 && $studygroup->leader_nid == 0)
{
$studygroup->leader_nid = $person->nid;
$calculation_messages[] = t("Study Group Assignment as leader for nid !nid to group !descrip at session id !sid",
array('!nid' => $person->nid, '!descrip' => $studygroup->booking_studygroup_descrip, '!sid' => $studygroup->session_id, '@info' => print_r( $studygroup, true)));
break;
}
elseif ($age < 22 && $studygroup->helper_nid == 0)
{
$studygroup->helper_nid = $person->nid;
$calculation_messages[] = t("Study Group Assignment as helper for nid !nid to group !descrip at session id !sid",
array('!nid' => $person->nid, '!descrip' => $studygroup->booking_studygroup_descrip, '!sid' => $studygroup->session_id, '@info' => print_r( $studygroup, true)));
break;
}
}
//if someone is 22 or over, they're a leader
//$next_group_index = $groups[_booking_get_next_studygroup('leader_nid', $groups)];
//$groups[$next_group_index]->leader_nid = $person->nid;
//if someone is under 22, they're a helper
//left over people become a reserve leader
}
watchdog('booking', "<pre>Study Group Leader/Helper calculation\n" . implode("\n", $calculation_messages) . "</pre>");
//input the data to the database
}
function _booking_get_next_studygroup($type, $studygroups) {
$count = count($studygroups) - 1;
for ($i = 0; $i < $count; $i++)
{
$group = $studygroups[$i];
$leader = $group->leader_nid;
//watchdog('booking_debug', "<pre>Study Group Element at index $i:\n@info</pre>", array('@info' => print_r( $group, true)));
//watchdog('booking_debug', "<pre>Study Group Leader NID at index $i:\n@info</pre>", array('@info' => var_dump($group)));
if ($leader == 0)
{
return $i;
}
}
}
/**
* Function for
*/
function _booking_studygroups_retrieve_eligible_people() {
global $event;
$attendees = array();
//select males that are booked in
$query = db_select('booking_person', 'p');
$db_and = db_and();
$db_and->condition('p.booking_eventid', $event->eid, '=');
$db_and->condition('p.booking_gender', 'M', '=');
$db_and->condition('p.booking_status', 1, '=');
$query->condition($db_and);
$query->fields('p');
$bookedin_result = $query->execute();
//iterate over the attendee associative array and add some fields
foreach ($bookedin_result as $person)
{
$person->processed = 0;
$person->leading = array();
$person->helping = array();
$person->reserve_leading = array();
$attendees[] = $person;
}
//select males that are on the waiting list up to position number 100
$result2 = db_query('SELECT DISTINCT nid, booking_firstname, booking_lastname, booking_state, booking_readinggroup, booking_country, booking_status, booking_dob, booking_gender
FROM (
SELECT p.nid, p.booking_firstname, p.booking_lastname, p.booking_state, p.booking_country, p.booking_readinggroup, pay.booking_payment_date, p.booking_status, p.booking_gender, p.booking_dob
FROM {booking_person} p, {booking_payment} pay
WHERE booking_eventid = :eid and p.nid = pay.booking_person_nid and ( booking_status = 2 or booking_status = 4)
LIMIT 100
) AS booking
WHERE booking_gender = \'M\'
ORDER BY booking_status, booking_payment_date',
array(':eid' => $event->eid));
//iterate over the attendee associative array and add some fields
foreach ($result2 as $person)
{
$person->processed = 0;
$person->leading = array();
$person->helping = array();
$person->reserve_leading = array();
$attendees[] = $person;
}
//watchdog('booking', "<pre>Eligible Leaders/Helpers List:\n@info</pre>", array('@info' => print_r( $attendees, true)));
return $attendees;
}
/**
* Function for
*/
function _booking_studygroups_retrieve_groups() {
global $event;
$groups = array();
//select all the study groups for this event id
$studygroup = db_query("SELECT * FROM {booking_studygroup_list} WHERE booking_eventid = :eid", array(':eid' => $event->eid));
$group_mapping = $studygroup->fetchAllAssoc('sid');
//select the number of study group sessions
//assume they all have the same number of sessions
//$num_sessions = $studygroup->booking_num_group_sessions;
foreach ($group_mapping as $group)
{
//watchdog('booking_debug', "<pre>Study Group Element:\n@info</pre>", array('@info' => print_r( $group, true)));
for ($i = 1; $i <= 16; $i++)
{
$new_group = clone $group;
$new_group->session_id = $i;
$new_group->leader_nid = 0;
$new_group->helper_nid = 0;
$new_group->reserve_nid = 0;
$new_group->processed = 0;
$groups[] = $new_group;
}
}
//watchdog('booking', "<pre>Study Group Sessions:\n@info</pre>", array('@info' => print_r( $groups, true)));
return $groups;
}