Added code to import study group data from CSV files

This commit is contained in:
2016-01-17 22:50:10 +11:00
parent a283320925
commit 7bbdf57238
3 changed files with 252 additions and 14 deletions

View File

@@ -11,10 +11,10 @@ function booking_import_data_admin()
{ {
global $event; global $event;
$prefix = t("<p>Upload csv file containing data to import. Minimum fields present should be <strong>nid and booking_status</strong>, " . $prefix = t("<p>Upload csv file containing data to import. Minimum fields present should be <strong>nid</strong>, " .
" along with user-specified fields of <strong>!config</strong>. CSV Column names should match exactly (case sensitive).</p>", " along with user-specified fields of <strong>!config</strong>. CSV Column names should match exactly (case sensitive).</p>",
array('!config' => variable_get('booking_import_include_fields', ''))); array('!config' => variable_get('booking_import_include_fields', '')));
$prefix .= "<p>Note that only registration specific data like phone numbers, barcodes, etc can be updated. Study group data cannot be imported via this page.</p>" $prefix .= "<p>Note that only registration specific data like phone numbers, barcodes, etc can be updated, as well as study group membership and roles. Other data like room allocations cannot be uploaded at this time.</p>";
$form = array(); $form = array();
@@ -70,7 +70,7 @@ function booking_import_data_admin_submit($form, &$form_state)
$update_messages = array(); $update_messages = array();
$datetime_fields = array('booking_outflight_origin_ts', 'booking_outflight_destination_ts', 'booking_rtrnflight_origin_ts', $datetime_fields = array('booking_outflight_origin_ts', 'booking_outflight_destination_ts', 'booking_rtrnflight_origin_ts',
'booking_rtrnflight_destination_ts','booking_dob', 'booking_passport_expiry_date'); 'booking_rtrnflight_destination_ts','booking_dob', 'booking_passport_expiry_date');
$builtin_fields_to_import = array('nid', 'booking_status'); $builtin_fields_to_import = array('nid');
$custom_fields_to_import = explode(";", variable_get('booking_import_include_fields', '')); $custom_fields_to_import = explode(";", variable_get('booking_import_include_fields', ''));
$fields_to_import = array_merge($builtin_fields_to_import, $custom_fields_to_import); $fields_to_import = array_merge($builtin_fields_to_import, $custom_fields_to_import);
@@ -112,10 +112,13 @@ function booking_import_data_admin_submit($form, &$form_state)
//process the input data //process the input data
foreach ($array as $record) foreach ($array as $record)
{ {
$rows = 0;
//watchdog('booking', "<pre>Processing row data:\n@info</pre>", array('@info' => print_r( $record, true))); //watchdog('booking', "<pre>Processing row data:\n@info</pre>", array('@info' => print_r( $record, true)));
$update_counter++; $update_counter++;
$update_text = ""; $update_text = "";
$update_array = array(); $update_array = array();
$studygroups_update_array = array();
$studygroup_roles_update_array = array();
//do some error checking //do some error checking
foreach($fields_to_import as $field) foreach($fields_to_import as $field)
@@ -152,6 +155,18 @@ function booking_import_data_admin_submit($form, &$form_state)
//skip to the next record //skip to the next record
//continue 2; //continue 2;
} }
elseif (preg_match('/session(\d{1,2})_role/i', $field, $matches))
{
$session_id = $matches[1];
//drupal_set_message(t('Setting session id !field role to value: !info', array ('!field' => $session_id, '!info' => var_export($record[$field], TRUE))));
$studygroup_roles_update_array[$session_id] = $record[$field];
}
elseif (preg_match('/session(\d{1,2})$/i', $field, $matches))
{
$session_id = $matches[1];
//drupal_set_message(t('Setting session id !field group to value: !info', array ('!field' => $session_id, '!info' => var_export($record[$field], TRUE))));
$studygroups_update_array[$session_id] = $record[$field];
}
else else
{ {
$update_text .= " set '" . $field . "' to '" . $record[$field] . "'; "; $update_text .= " set '" . $field . "' to '" . $record[$field] . "'; ";
@@ -159,21 +174,28 @@ function booking_import_data_admin_submit($form, &$form_state)
} }
} }
drupal_set_message(t("Updating record !nid as follows: !update", array('!nid' => $record['nid'], '!update' => $update_text))); if (count($update_array) > 0)
{
$query = db_update('booking_person')
->fields($update_array)
->condition('nid', $record['nid']);
$query = db_update('booking_person') $rows = $query->execute();
->fields($update_array)
->condition('nid', $record['nid']); $update_messages[] = t("Update Query:<br />@info<br />Condition: @nid<br />Rows affected: @rows<br />Values: <br /><pre>@values</pre>", array('@info' => (string) $query, '@nid' => $record['nid'], '@rows' => $rows, '@values' => print_r( $update_array, true) ));
$rows = $query->execute(); }
//handle study group processing
$update_text .= _booking_import_studygroup_info($record['nid'], $studygroups_update_array, $studygroup_roles_update_array);
drupal_set_message(t("Updating record !nid as follows: !update", array('!nid' => $record['nid'], '!update' => $update_text)));
//$args = $query->getArguments(); //$args = $query->getArguments();
/* /*
watchdog('booking', "Update Query:<br />@info<br />Condition: @nid<br />Rows affected: @rows<br />Values: <br /><pre>@values</pre>", watchdog('booking', "Update Query:<br />@info<br />Condition: @nid<br />Rows affected: @rows<br />Values: <br /><pre>@values</pre>",
array('@info' => (string) $query, '@nid' => $record['nid'], '@rows' => $rows, '@values' => print_r( $update_array, true) )); array('@info' => (string) $query, '@nid' => $record['nid'], '@rows' => $rows, '@values' => print_r( $update_array, true) ));
*/ */
$update_messages[] = t("Update Query:<br />@info<br />Condition: @nid<br />Rows affected: @rows<br />Values: <br /><pre>@values</pre>", array('@info' => (string) $query, '@nid' => $record['nid'], '@rows' => $rows, '@values' => print_r( $update_array, true) ));
/* /*
$result_array[] = t('Setting payment for id !nid to $!price of total required $!total and status to !status', $result_array[] = t('Setting payment for id !nid to $!price of total required $!total and status to !status',
array('!nid' => $record['nid'], array('!nid' => $record['nid'],
@@ -230,5 +252,69 @@ function booking_import_data_admin_submit($form, &$form_state)
drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename))); drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
*/ */
}
/**
* Function for processing CSV import data for study groups
*/
function _booking_import_studygroup_info($nid, $studygroups_update_array, $studygroup_roles_update_array)
{
global $event;
$update_text = "";
$studygroup_roles = _booking_studygroup_role_lookup();
//update the study group mapping info if imported
foreach ($studygroup_roles_update_array as $key => $value)
{
//turn leader/helper/reserve text into the number used internally in the database
if (!is_numeric($value))
{
$role = array_search($value, $studygroup_roles);
}
else
{
$role = $value;
}
//confirm that the data is valid
if (is_numeric($key) && is_numeric($studygroups_update_array[$key]))
{
//check whether there is already a record to be updated or if it is a new record
$record_check = db_query("SELECT * FROM {booking_studygroup_mapping} WHERE booking_eventid = :eid and booking_node_id = :nid and booking_studygroup_id = :sid",
array(':eid' => $event->eid, ':nid' => $nid, ':sid' => $studygroups_update_array[$key]))->fetchObject();
//no existing record, so run a db_insert
if (! $record_check) {
$update_text .= t('Running DB insert for nid !nid, studygroup session id !id, for study group with ID !sid, for role !role;',
array ('!nid' => $nid, '!id' => $key, '!sid' => $studygroups_update_array[$key], '!role' => $role));
db_insert('booking_studygroup_mapping')
->fields(array(
'booking_eventid' => $event->eid,
'booking_node_id' => $nid,
'booking_studygroup_id' => $key,
'booking_session_id' => $studygroups_update_array[$key],
'booking_studygroup_role' => $role,
))
->execute();
}
//we found an existing record so just update it
else
{
$update_text .= t('Running DB update for nid !nid, studygroup session id !id, for study group with ID !sid, for role !role;',
array ('!nid' => $nid, '!id' => $key, '!sid' => $studygroups_update_array[$key], '!role' => $role));
db_update('booking_studygroup_mapping')
->fields(array (
'booking_session_id' => $studygroups_update_array[$key],
'booking_studygroup_role' => $role,
))
->condition('booking_node_id', $nid)
->condition('booking_studygroup_id', $key)
->execute();
}
}
}
return $update_text;
} }

View File

@@ -699,13 +699,14 @@ function booking_csv_report() {
if (in_array($key, $fields_to_skip)) if (in_array($key, $fields_to_skip))
continue; continue;
/*
//Replace headings for study group sessions and roles with the name of the study group rather than just sessionN and sessionN_role etc //Replace headings for study group sessions and roles with the name of the study group rather than just sessionN and sessionN_role etc
if (array_key_exists($key, $studygroup_descriptions)) if (array_key_exists($key, $studygroup_descriptions))
{ {
$header_array[] = $studygroup_descriptions[$key]; $header_array[] = $studygroup_descriptions[$key];
continue; continue;
} }
*/
$header_array[] = $key; $header_array[] = $key;
//add in a special column for a processed version of the date of birth //add in a special column for a processed version of the date of birth
if (strcmp($key,"booking_dob") == 0) if (strcmp($key,"booking_dob") == 0)

View File

@@ -8,9 +8,160 @@
/** /**
* Function for selecting who is available to lead or help sessions * Function for calculating and assigning leaders and helpers to active study group sessions
*/ */
function booking_studygroups_leaders_calculate($node, &$form_state, $group_id) { function booking_studygroups_leaders_calculate() {
global $event; 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)
);
//if someone is 22 or over, they're a leader
if ($age >= 22)
{
$next_group_index = $groups[_booking_get_next_studygroup('leader_nid', $groups)];
$calculation_messages[] = t('Assigning id !id to study group !group as a leader',
array('!id' => $person->nid, '!group' => $next_group_index)
);
$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_event_id', $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_event_id = :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 = -1;
$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;
} }