Added code to import study group data from CSV files
This commit is contained in:
@@ -11,10 +11,10 @@ function booking_import_data_admin()
|
||||
{
|
||||
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>",
|
||||
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();
|
||||
|
||||
@@ -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', "<pre>Processing row data:\n@info</pre>", 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:<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) ));
|
||||
}
|
||||
|
||||
//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:<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) ));
|
||||
*/
|
||||
|
||||
$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',
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user