From 7bbdf572382775396841b4b0400571b0f3ee91f3 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Sun, 17 Jan 2016 22:50:10 +1100 Subject: [PATCH] Added code to import study group data from CSV files --- booking.import_data.inc | 108 ++++++++++++++++++++--- booking.reports.inc | 3 +- booking.studygroup_leaders.inc | 155 ++++++++++++++++++++++++++++++++- 3 files changed, 252 insertions(+), 14 deletions(-) diff --git a/booking.import_data.inc b/booking.import_data.inc index b17f595..45e45ef 100644 --- a/booking.import_data.inc +++ b/booking.import_data.inc @@ -11,10 +11,10 @@ function booking_import_data_admin() { global $event; - $prefix = t("

Upload csv file containing data to import. Minimum fields present should be nid and booking_status, " . + $prefix = t("

Upload csv file containing data to import. Minimum fields present should be nid, " . " along with user-specified fields of !config. CSV Column names should match exactly (case sensitive).

", array('!config' => variable_get('booking_import_include_fields', ''))); - $prefix .= "

Note that only registration specific data like phone numbers, barcodes, etc can be updated. Study group data cannot be imported via this page.

" + $prefix .= "

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.

"; $form = array(); @@ -70,7 +70,7 @@ function booking_import_data_admin_submit($form, &$form_state) $update_messages = array(); $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'); - $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', '')); $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 foreach ($array as $record) { + $rows = 0; //watchdog('booking', "
Processing row data:\n@info
", array('@info' => print_r( $record, true))); $update_counter++; $update_text = ""; $update_array = array(); + $studygroups_update_array = array(); + $studygroup_roles_update_array = array(); //do some error checking foreach($fields_to_import as $field) @@ -152,6 +155,18 @@ function booking_import_data_admin_submit($form, &$form_state) //skip to the next record //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 { $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') - ->fields($update_array) - ->condition('nid', $record['nid']); - $rows = $query->execute(); + $rows = $query->execute(); + + $update_messages[] = t("Update Query:
@info
Condition: @nid
Rows affected: @rows
Values:
@values
", array('@info' => (string) $query, '@nid' => $record['nid'], '@rows' => $rows, '@values' => print_r( $update_array, true) )); + } + + //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(); /* watchdog('booking', "Update Query:
@info
Condition: @nid
Rows affected: @rows
Values:
@values
", array('@info' => (string) $query, '@nid' => $record['nid'], '@rows' => $rows, '@values' => print_r( $update_array, true) )); */ - - $update_messages[] = t("Update Query:
@info
Condition: @nid
Rows affected: @rows
Values:
@values
", 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', 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))); */ +} +/** + * 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; } \ No newline at end of file diff --git a/booking.reports.inc b/booking.reports.inc index 25c4e41..d5fbb14 100644 --- a/booking.reports.inc +++ b/booking.reports.inc @@ -699,13 +699,14 @@ function booking_csv_report() { if (in_array($key, $fields_to_skip)) continue; + /* //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)) { $header_array[] = $studygroup_descriptions[$key]; continue; } - + */ $header_array[] = $key; //add in a special column for a processed version of the date of birth if (strcmp($key,"booking_dob") == 0) diff --git a/booking.studygroup_leaders.inc b/booking.studygroup_leaders.inc index 3d820cd..a699327 100644 --- a/booking.studygroup_leaders.inc +++ b/booking.studygroup_leaders.inc @@ -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; + $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', "
Study Group Leader/Helper calculation\n" . implode("\n", $calculation_messages) . "
"); + + //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', "
Study Group Element at index $i:\n@info
", array('@info' => print_r( $group, true))); + //watchdog('booking_debug', "
Study Group Leader NID at index $i:\n@info
", 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', "
Eligible Leaders/Helpers List:\n@info
", 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', "
Study Group Element:\n@info
", 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', "
Study Group Sessions:\n@info
", array('@info' => print_r( $groups, true))); + return $groups; + } \ No newline at end of file