if ($this_meld != '') { $lab_subject[$chem_event_id] = $chem_event; } } } /** * if we have candidate events * iterate the candidate array, find the baseline date and flag */ if (count($lab_subject) > 0) { /** * fetch the baseline date and set baseline / flag pair */ $baseline_date = ''; $baseline_flag = ''; $this_data = get_baseline_date($lab_subject, 'chem_im', $rfstdtc); /** * if the nearest date is prior or equal to rfstdtc, it's a baseline date */ d($lab_subject); d($this_data); d($rfstdtc); if ($this_data['chem_im_lbdtc'] != '' && $this_data['chem_im_lbdtc'] <= $rfstdtc) { $baseline_date = $this_data['chem_im_lbdtc']; $baseline_flag = 'Y'; /** * Baseline date belongs in Baseline event */ update_field_compare($subject_id, $project_id, $initial_event, $baseline_date, $event['meld_im_supplb_lbdtbl'], "meld_im_supplb_lbdtbl", $debug); d($baseline_date); /**
foreach ($fields AS $field) { foreach ($subject AS $lab_event_id => $lab_event) { if ($lab_event[$fields['lborres']] != '' || $lab_event[$fields['hcvdtct']] != '' || $lab_event[$fields['im_lborres']] != '' || $lab_event[$fields['im_hcvdtct']] != '') { $lab_subject[$lab_event_id][$field] = $lab_event[$field]; } } } d($lab_subject); /** * if we have candidate events */ if (count($lab_subject) > 0) { /** * fetch the baseline date and set baseline / flag pair */ $this_data = get_baseline_date($lab_subject, $form_prefix, $rfstdtc); d($form_prefix, $lab_prefix, $this_data); /** * ABSTRACTED * if the nearest date is prior or equal to rfstdtc, it's a baseline date */ $baseline_date = ''; $this_baseline_flag = ''; if ($this_data[$form_prefix . '_lbdtc'] != '' && $this_data[$form_prefix . '_lbdtc'] <= $rfstdtc) { $baseline_date = $this_data[$form_prefix . '_lbdtc']; $this_baseline_flag = 'Y'; } else { /** * Now reset all other flags that have changed */ foreach ($data[$subject_id] AS $flag_event_id => $flag_event) {
/** * @param $prefix string * @param $lab_fields array 'Must include _supplb_lbdtbl, _lbblfl, _lborres or _lbstresn without prefix' * @param $date_field array 'Date field for this form / fields' * @param $debug * UNDER CONSTRUCTION */ function derive_baseline_from_fields($prefix, $lab_fields, $date_field, $debug) { $project = new Project(); $initial_event = $project->firstEventId; $lab_fields = array_merge($date_field, $lab_fields); if ($debug) { /*show_var($lab_fields);*/ } /** * find baselines for each lab test */ $data = REDCap::getData('array', '', $lab_fields); foreach ($data AS $subjid => $subject) { $lab_subject = array(); if ($debug) { show_var($subjid, 'FIELD SUBJID', 'blue'); } /** * get refstdtc for this subject */ $tx_start_fields = array("dm_rfstdtc"); $tx_start_data = REDCap::getData('array', $subjid, $tx_start_fields); $rfstdtc = $tx_start_data[$subjid][$initial_event]['dm_rfstdtc']; if ($debug) { show_var($subject, 'BEFORE', 'red'); } /** * if treatment has started */ if (isset($rfstdtc) || $rfstdtc != '') { /** * iterate the lab events for this prefix */ foreach ($subject AS $lab_event_id => $lab_event) { /** * if we have a value for the orres, then add to candidate events array */ if ($lab_event[$prefix . "_lborres"] != '') { $lab_subject[$lab_event_id] = $subject[$lab_event_id]; } } if ($debug) { show_var($lab_subject, "AFTER {$prefix}", 'green'); } /** * if we have candidate events */ if (count($lab_subject) > 0) { /** * fetch the baseline date and set baseline / flag pair */ $baseline_date = ''; $this_data = get_baseline_date($lab_subject, $fragment, $rfstdtc); /** * if the nearest date is prior or equal to rfstdtc, it's a baseline date */ if ($debug) { show_var($lab_subject, 'FIELD SUBJECT DATA', 'blue'); show_var($this_data, 'FIELD BASELINE DATA', 'red'); show_var($rfstdtc, 'TX start', 'red'); } if ($this_data[$fragment . '_lbdtc'] != '' && $this_data[$fragment . '_lbdtc'] <= $rfstdtc) { $baseline_date = $this_data[$fragment . '_lbdtc']; /** * Baseline date belongs in Baseline event */ update_field_compare($subjid, $project_id, $initial_event, $baseline_date, get_single_field($subjid, $project_id, $initial_event, $prefix . "_supplb_lbdtbl", null), $prefix . "_supplb_lbdtbl", $debug); if ($debug) { show_var($baseline_date, 'FIELD BASELINE DATE', 'red'); } /** * Now reset all other flags that have changed */ $flag_reset_data = REDCap::getData('array', $subjid, $prefix . "_lbblfl"); $this_baseline_flag = get_single_field($subjid, $project_id, $this_data['event_id'], $prefix . "_lbblfl", null); foreach ($flag_reset_data AS $reset) { if ($debug) { show_var($reset, 'FIELD RESET FLAGS', 'red'); } foreach ($reset AS $reset_event_id => $reset_event) { foreach ($reset_event as $reset_field => $reset_val) { if ($reset_event_id != $this_data['event_id']) { update_field_compare($subjid, $project_id, $reset_event_id, '', $reset_val, $prefix . "_lbblfl", $debug); } } } } /** * Baseline flag belongs in the event where the date occurs */ if ($baseline_date != '') { update_field_compare($subjid, $project_id, $this_data['event_id'], 'Y', $this_baseline_flag, $prefix . "_lbblfl", $debug); } } } } } }