$fields = array('dm_usubjid', 'ae_aestdtc', 'ae_aedecod', 'ae_aeser');
$data = REDCap::getData('array', $subjects, $fields);
d($data);


foreach ($data AS $subject_id => $subject) {
	/**
	 * SUBJECT-LEVEL vars
	 */
	$history_array = array();
	$dm_usubjid = $subject[$first_event_id]['dm_usubjid'];
	/**
	 * MAIN EVENT LOOP
	 */
	foreach ($subject AS $event_id => $event) {
		foreach ($event AS $key => $value) {
			if ($key != 'dm_usubjid') {
				$history_array[$key][] = Form::getDataHistoryLog($subject_id, $event_id, $key);
				/**
				 * do stuff
				 */
			}
		}
	}
	d($history_array);
}
if ($debug) {
	$timer['main_end'] = microtime(true);
	$init_time = benchmark_timing($timer);
	echo $init_time;
}
	</form>
<?php
if (isset($_POST['field_name'])) {
	$timer['main_start'] = microtime(true);
	/**
	 * WORKING SET OF STUDY_IDs
	 */
	$timer['start_main'] = microtime(true);
//$empty_result = db_query("SELECT * FROM redcap_data WHERE project_id = '$project_id' AND value = '' ORDER BY abs(record) ASC");
	/**
	 * exclude dm_subjid from query, as RED-I writes orphan records to this field, and we don't want to screw that up, do we?
	 */
	$empty_result = db_query("SELECT * FROM redcap_data WHERE project_id = '$project_id' AND field_name = '{$_POST['field_name']}' ORDER BY abs(record) ASC");
	if ($empty_result) {
		while ($empty_row = db_fetch_assoc($empty_result)) {
			$history = Form::getDataHistoryLog($empty_row['record'], $empty_row['event_id'], $empty_row['field_name']);
			$all_fields_hidden = $my_branching_logic->allFieldsHidden($empty_row['record'], $empty_row['event_id'], array($empty_row['field_name']));
			if ($debug) {
				/**
				 * pick apart the hidden field logic - something's not right with fib_lbtest logic. ?'null'?
				 */
				$fieldsDependent = getDependentFields(array($empty_row['field_name']), false, true);
				$unique_event_name = $project->getUniqueEventNames($empty_row['event_id']);
				$record_data = Records::getSingleRecordData($empty_row['record'], array_merge($fieldsDependent, array($empty_row['field_name'])));
				$logic = $project->metadata[$empty_row['field_name']]['branching_logic'];
				if ($longitudinal) {
					$logic = LogicTester::logicPrependEventName($logic, $unique_event_name);
				}
				if (LogicTester::isValid($logic)) {
					$displayField = LogicTester::apply($logic, $record_data);
					$displayField = $displayField ? false : true;
if ($debug) {
	$timer['main_start'] = microtime(true);
}
$fields = array('dm_usubjid');
$data = REDCap::getData('array', $subjects, $fields);
foreach ($data AS $subject_id => $subject) {
	d($subject_id);
	/**
	 * SUBJECT-LEVEL vars
	 */
	$data_row = array();
	/**
	 * MAIN EVENT LOOP
	 */
	foreach ($subject AS $event_id => $event) {
		d(Form::getDataHistoryLog($subject_id, $event_id, 'cm_suppcm_atcname'));
		foreach ($event AS $key => $value) {
			$data_row[$Proj->metadata[$key]['element_label']] = $value;
		}
	}
	$row_csv = implode(',', $data_row) . "\n";
	$table_csv .= $row_csv;
}
$headers = implode(',', array_keys($data_row)) . "\n";
d($headers);
d($table_csv);
if (!$debug) {
	create_download($lang, $app_title, $userid, $headers, $user_rights, $table_csv, '', $parent_chkd_flds, $project_id, $export_filename, $debug);
} else {
	$timer['main_end'] = microtime(true);
	$init_time = benchmark_timing($timer);