* project metadata
 */
$project = new Project();
$first_event_id = $project->firstEventId;
$plugin_title = "Derive INDCMODF, INDCOD and INDCSYS for all CMINDC";
/**
 * plugin
 */
echo "<h3>$plugin_title</h3>";
/**
 * MAIN
 */
if ($debug) {
	$timer['main_start'] = microtime(true);
}
$fields = array('cm_cmindc', 'cm_oth_cmindc', 'cm_suppcm_indcmodf', 'cm_suppcm_indcod', 'cm_suppcm_indcsys');
$data = REDCap::getData('array', $subjects, $fields);
foreach ($data AS $subject_id => $subject) {
    foreach ($subject AS $event_id => $event) {
	    if ($event['cm_cmindc'] != '') {
		    code_llt($project_id, $subject_id, $event_id, fix_case($event['cm_cmindc']), fix_case($event['cm_oth_cmindc']), $event['cm_suppcm_indcmodf'], 'cm_suppcm_indcmodf', $debug, $recode_llt);
		    code_pt($project_id, $subject_id, $event_id, get_single_field($subject_id, $project_id, $event_id, "cm_suppcm_indcmodf", ''), $event["cm_suppcm_indcod"], "cm_suppcm_indcod", $debug, $recode_pt);
		    code_bodsys($project_id, $subject_id, $event_id, get_single_field($subject_id, $project_id, $event_id, "cm_suppcm_indcod", ''), $event["cm_suppcm_indcsys"], "cm_suppcm_indcsys", $debug, $recode_soc);
	    }
    }
}
if ($debug) {
	$timer['main_end'] = microtime(true);
	$init_time = benchmark_timing($timer);
	echo $init_time;
}
    foreach ($subject AS $event_id => $event) {
        /**
         * AE_AEDECOD
         */
        code_pt($project_id, $subject_id, $event_id, fix_case($event['ae_aemodify']), $event['ae_aedecod'], 'ae_aedecod', $debug, $recode_pt);
        if ($debug) {
            error_log("INFO (TESTING): Coded AE_AEDECOD {$event['ae_aedecod']}: subject=$subject_id, event=$event_id for AEMODIFY {$event['ae_aemodify']}");
        }
        /**
         * EOT_AEDECOD
         */
        code_pt($project_id, $subject_id, $event_id, fix_case($event['eot_aemodify']), $event['eot_aedecod'], 'eot_aedecod', $debug, $recode_pt);
        if ($debug) {
            error_log("INFO (TESTING): Coded EOT_AEDECOD {$event['eot_aedecod']}: subject=$subject_id, event=$event_id for AEMODIFY {$event['eot_aemodify']}");
        }
        /**
         * TXDRUG_AEDECOD
         */
        foreach ($tx_prefixes as $tx_prefix) {
            code_pt($project_id, $subject_id, $event_id, fix_case($event[$tx_prefix . '_aemodify']), $event[$tx_prefix . '_aedecod'], $tx_prefix . '_aedecod', $debug, $recode_pt);
            if ($debug) {
                error_log("INFO (TESTING): Coded " . strtoupper($tx_prefix) . "_AEDECOD {$event[$tx_prefix . '_aedecod']}: subject=$subject_id, event=$event_id for AEMODIFY {$event[$tx_prefix . '_aemodify']}");
            }
        }
    }
}
if ($debug) {
	$timer['main_end'] = microtime(true);
	$init_time = benchmark_timing($timer);
	echo $init_time;
}
	/**
	 * @param $record
	 * @param $redcap_event_name
	 * @param $instrument
	 * @param $debug
	 */
	public static function code_terms($record, $redcap_event_name, $instrument, $debug)
	{
		global $Proj, $project_id, $tx_fragment_labels;
		$this_event_id = $Proj->getEventIdUsingUniqueEventName($redcap_event_name);
		switch ($instrument) {
			case 'ae_coding':
				$recode_llt = false;
				$recode_pt = true;
				$recode_soc = true;
				$prefix = 'ae';
				/**
				 * AE_AEMODIFY
				 */
				$fields = array("ae_aeterm", "ae_oth_aeterm", "ae_aemodify");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id]['ae_aeterm']), fix_case($data[$record][$this_event_id]['ae_oth_aeterm']), $data[$record][$this_event_id]['ae_aemodify'], 'ae_aemodify', $debug, $recode_llt);
				if ($debug) {
					error_log("DEBUG: Coded AE_AEMODIFY {$data[$record][$this_event_id]['ae_aemodify']}: subject=$record, event=$this_event_id for AE {$data[$record][$this_event_id]['ae_aeterm']} - {$data[$record][$this_event_id]['ae_oth_aeterm']}");
				}
				/**
				 * PREFIX_AEDECOD
				 * uses $tx_prefixes preset array
				 */
				$fields = array($prefix . "_aemodify", $prefix . "_aedecod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_pt($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_aemodify"], $data[$record][$this_event_id][$prefix . "_aedecod"], $prefix . "_aedecod", $debug, $recode_pt);
				if ($debug) {
					error_log("DEBUG: Coded " . strtoupper($prefix) . "_AEDECOD {$data[$record][$this_event_id][$prefix . '_aedecod']}: subject=$record, event=$this_event_id for AEMODIFY {$data[$record][$this_event_id][$prefix . '_aemodify']}");
				}
				/**
				 * PREFIX_AEBODSYS
				 * uses $tx_prefixes preset array
				 */
				$fields = array($prefix . "_aedecod", $prefix . "_aebodsys");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_aedecod"], $data[$record][$this_event_id][$prefix . "_aebodsys"], $prefix . "_aebodsys", $debug, $recode_soc);
				if ($debug) {
					error_log("DEBUG: Coded SOC: subject=$record, event=$this_event_id for AE {$data[$record][$this_event_id][$prefix . "_aedecod"]}");
				}
				unset($data);
				break;
			/**
			 * ADVERSE EVENTS
			 * ACTION: auto-code AE
			 */
			case 'adverse_events':
				$recode_llt = true;
				$recode_pt = true;
				$recode_soc = true;
				/**
				 * AE_AEDECOD
				 */
				$fields = array("ae_aeterm", "ae_oth_aeterm", "ae_aemodify");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id]['ae_aeterm']), fix_case($data[$record][$this_event_id]['ae_oth_aeterm']), $data[$record][$this_event_id]['ae_aemodify'], 'ae_aemodify', $debug, $recode_llt);
				if ($debug) {
					error_log("DEBUG: Coded AE_AEMODIFY {$data[$record][$this_event_id]['ae_aemodify']}: subject=$record, event=$this_event_id for AE {$data[$record][$this_event_id]['ae_aeterm']} - {$data[$record][$this_event_id]['ae_oth_aeterm']}");
				}
				/**
				 * AE_AEDECOD
				 */
				$fields = array("ae_aemodify", "ae_aedecod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_pt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id]['ae_aemodify']), $data[$record][$this_event_id]['ae_aedecod'], 'ae_aedecod', $debug, $recode_pt);
				if ($debug) {
					error_log("DEBUG: Coded AE_AEDECOD {$data[$record][$this_event_id]['ae_aedecod']}: subject=$record, event=$this_event_id for AE {$data[$record][$this_event_id]['ae_aemodify']}");
				}
				/**
				 * AE_AEBODSYS
				 */
				$fields = array("ae_aedecod", "ae_aebodsys");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id]['ae_aedecod'], $data[$record][$this_event_id]['ae_aebodsys'], 'ae_aebodsys', $debug, $recode_soc);
				if ($debug) {
					error_log("DEBUG: Coded SOC: subject=$record, event=$this_event_id for AE {$data[$record][$this_event_id]['ae_aedecod']}");
				}
				unset($data);
				break;
			/**
			 * MEDICAL HISTORY
			 * ACTION: auto-code MH
			 */
			case 'key_medical_history':
				$recode_llt = false;
				$recode_pt = true;
				$recode_soc = true;
				$mh_prefixes = array('othpsy', 'othca');
				/**
				 * MH_MHMODIFY
				 */
				foreach ($mh_prefixes AS $prefix) {
					$fields = array($prefix . "_oth_mhterm", $prefix . "_mhmodify");
					$data = REDCap::getData('array', $record, $fields, $this_event_id);
					code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id][$prefix . "_oth_mhterm"]), '', $data[$record][$this_event_id][$prefix . "_mhmodify"], $prefix . "_mhmodify", $debug, $recode_llt);
					if ($debug) {
						error_log("DEBUG: Coded " . strtoupper($prefix) . "_MHMODIFY {$data[$record][$this_event_id][$prefix . "_mhmodify"]}: subject=$record, event=$this_event_id for MH {$data[$record][$this_event_id][$prefix . "_oth_mhterm"]}");
					}
					/**
					 * PREFIX_MHDECOD
					 * uses $mh_prefixes preset array
					 */
					$fields = array($prefix . "_mhmodify", $prefix . "_mhdecod");
					$data = REDCap::getData('array', $record, $fields, $this_event_id);
					code_pt($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_mhmodify"], $data[$record][$this_event_id][$prefix . "_mhdecod"], $prefix . "_mhdecod", $debug, $recode_pt);
					if ($debug) {
						error_log("DEBUG: Coded " . strtoupper($prefix) . "_MHDECOD {$data[$record][$this_event_id][$prefix . '_mhdecod']}: subject=$record, event=$this_event_id for MHMODIFY {$data[$record][$this_event_id][$prefix . '_mhmodify']}");
					}
					/**
					 * PREFIX_mhBODSYS
					 * uses $mh_prefixes preset array
					 */
					$fields = array($prefix . "_mhdecod", $prefix . "_mhbodsys");
					$data = REDCap::getData('array', $record, $fields, $this_event_id);
					code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_mhdecod"], $data[$record][$this_event_id][$prefix . "_mhbodsys"], $prefix . "_mhbodsys", $debug, $recode_soc);
					if ($debug) {
						error_log("DEBUG: Coded " . strtoupper($prefix) . "_MHBODSYS {$data[$record][$this_event_id][$prefix . "_mhbodsys"]}: subject=$record, event=$this_event_id for MHDECOD {$data[$record][$this_event_id][$prefix . "_mhdecod"]}");
					}
				}
				unset($data);
				break;
			/**
			 * EOT
			 */
			case 'early_discontinuation_eot':
				$recode_llt = true;
				$recode_pt = true;
				$recode_soc = true;
				/**
				 * EOT_AEDECOD
				 */
				$data = REDCap::getData('array', $record, array("eot_suppds_ncmpae", "eot_oth_suppds_ncmpae", "eot_aemodify", "eot_dsterm"), $this_event_id);
				$ptdata = REDCap::getData('array', $record, array("eot_aemodify", "eot_aedecod"), $this_event_id);
				$soc_data = REDCap::getData('array', $record, array("eot_aedecod", "eot_aebodsys"), $this_event_id);
				foreach ($data[$record][$this_event_id] AS $event) {
					if ($event['eot_dsterm'] == 'ADVERSE_EVENT') {
						code_llt($project_id, $record, $this_event_id, fix_case($event['eot_suppds_ncmpae']), fix_case($event['eot_oth_suppds_ncmpae']), $event['eot_aemodify'], 'eot_aemodify', $debug, $recode_llt);
						if ($debug) {
							error_log("INFO (TESTING EOT): Coded EOT_AEMODIFY {$event['eot_aemodify']}: subject=$record, event=$this_event_id for AE {$event['eot_suppds_ncmpae']} - {$event['eot_oth_suppds_ncmpae']}");
						}
						/**
						 * AE_AEDECOD
						 */
						foreach ($ptdata[$record][$this_event_id] AS $ptevent) {
							code_pt($project_id, $record, $this_event_id, fix_case($ptevent['eot_aemodify']), $ptevent['eot_aedecod'], 'eot_aedecod', $debug, $recode_pt);
							if ($debug) {
								error_log("DEBUG: Coded EOT_AEDECOD {$ptevent['eot_aedecod']}: subject=$record, event=$this_event_id for AEMODIFY {$ptevent['eot_aemodify']}");
							}
						}
						/**
						 * EOT_AEBODSYS
						 */
						foreach ($soc_data[$record][$this_event_id] AS $soc_event) {
							code_bodsys($project_id, $record, $this_event_id, $soc_event['eot_aedecod'], $soc_event['eot_aebodsys'], 'eot_aebodsys', $debug, $recode_soc);
							if ($debug) {
								error_log("DEBUG: Coded SOC: subject=$record, event=$this_event_id for AE {$soc_event['eot_aedecod']}");
							}
						}
					}
				}
				unset($data);
				unset($ptdata);
				unset($soc_data);
				break;
			/**
			 * TX stop AEs
			 */
			case 'ribavirin_administration':
			case 'harvoni_administration':
			case 'ombitasvir_paritaprevir':
			case 'dasabuvir':
			case 'zepatier_administration':
				$recode_llt = true;
				$recode_pt = true;
				$recode_soc = true;
				$tx_prefix = array_search(substr($instrument, 0, strpos($instrument, '_')), $tx_fragment_labels);
				/**
				 * AE_AEMODIFY
				 */
				$fields = array($tx_prefix . '_suppcm_cmncmpae', $tx_prefix . '_oth_suppcm_cmncmpae', $tx_prefix . '_aemodify');
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				foreach ($data[$record][$this_event_id] AS $event) {
					code_llt($project_id, $record, $this_event_id, fix_case($event[$tx_prefix . '_suppcm_cmncmpae']), fix_case($event[$tx_prefix . '_oth_suppcm_cmncmpae']), $event[$tx_prefix . '_aemodify'], $tx_prefix . '_aemodify', $debug, $recode_llt);
					if ($debug) {
						error_log("DEBUG: Coded AE_AEMODIFY {$event[$tx_prefix . '_aemodify']}: subject=$record, event=$this_event_id for AE {$event[$tx_prefix . '_suppcm_cmncmpae']} - {$event[$tx_prefix . '_oth_suppcm_cmncmpae']}");
					}
				}
				/**
				 * AE_AEDECOD
				 */
				$fields = array($tx_prefix . '_aemodify', $tx_prefix . "_aedecod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				foreach ($data[$record][$this_event_id] AS $event) {
					code_pt($project_id, $record, $this_event_id, fix_case($event[$tx_prefix . '_aemodify']), $event[$tx_prefix . '_aedecod'], $tx_prefix . '_aedecod', $debug, $recode_pt);
					if ($debug) {
						error_log("DEBUG: Coded AE_AEDECOD {$event[$tx_prefix . '_aedecod']}: subject=$record, event=$this_event_id for AE {$event[$tx_prefix . '_aemodify']}");
					}
				}
				/**
				 * AE_AEBODSYS
				 */
				$fields = array($tx_prefix . '_aedecod', $tx_prefix . '_aebodsys');
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				foreach ($data[$record][$this_event_id] AS $event) {
					code_bodsys($project_id, $record, $this_event_id, $event[$tx_prefix . '_aedecod'], $event[$tx_prefix . '_aebodsys'], $tx_prefix . '_aebodsys', $debug, $recode_soc);
					if ($debug) {
						error_log("DEBUG: Coded SOC: subject=$record, event=$this_event_id for AE {$event[$tx_prefix . '_aedecod']}");
					}
				}
				unset($data);
				break;
			/**
			 * CONMEDS
			 * ACTION: auto-code CONMEDS
			 */
			case 'conmeds':
				/**
				 * CM_CMDECOD
				 */
				$recode_cm = true;
				$recode_llt = true;
				$recode_pt = true;
				$recode_soc = true;
				$recode_atc = true;
				$fields = array("cm_cmtrt", "cm_cmdecod", "cm_cmindc", "cm_oth_cmindc", "cm_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_cm($project_id, $record, $this_event_id, $data[$record][$this_event_id], $debug, $recode_cm);
				/**
				 * cm_suppcm_mktstat
				 * PRESCRIPTION or OTC
				 */
				$fields = array("cm_cmdecod", "cm_suppcm_mktstat");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				foreach ($data as $subject_id => $subject) {
					foreach ($subject as $event_id => $event) {
						if (isset($event['cm_cmdecod']) && $event['cm_cmdecod'] != '') {
							update_field_compare($subject_id, $project_id, $event_id, get_conmed_mktg_status($event['cm_cmdecod']), $event['cm_suppcm_mktstat'], 'cm_suppcm_mktstat', $debug);
							if ($debug) {
								error_log("DEBUG: $subject_id Marketing Status = " . get_conmed_mktg_status($event['cm_cmdecod']));
							}
						}
					}
				}
				/**
				 * CM_SUPPCM_INDCOD
				 */
				$fields = array("cm_cmindc", "cm_oth_cmindc", "cm_suppcm_indcmodf");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				/**
				 * re-code all nutritional support to nutritional supplement
				 */
				if ($data[$record][$this_event_id]['cm_oth_cmindc'] == 'Nutritional support') {
					$data[$record][$this_event_id]['cm_oth_cmindc'] = 'Nutritional supplement';
				}
				code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id]['cm_cmindc']), fix_case($data[$record][$this_event_id]['cm_oth_cmindc']), $data[$record][$this_event_id]['cm_suppcm_indcmodf'], 'cm_suppcm_indcmodf', $debug, $recode_llt);
				if ($debug) {
					error_log("DEBUG: Coded INDC LLT: {} subject=$record, event=$this_event_id for INDICATION {$data[$record][$this_event_id]['cm_cmindc']}");
				}
				/**
				 * CM_SUPPCM_INDCOD
				 */
				$fields = array("cm_suppcm_indcmodf", "cm_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_pt($project_id, $record, $this_event_id, $data[$record][$this_event_id]['cm_suppcm_indcmodf'], $data[$record][$this_event_id]['cm_suppcm_indcod'], 'cm_suppcm_indcod', $debug, $recode_pt);
				if ($debug) {
					error_log("DEBUG: Coded INDC PT: subject=$record, event=$this_event_id for INDICATION {$data[$record][$this_event_id]['cm_suppcm_indcod']}");
				}
				/**
				 * CM_SUPPCM_INDCSYS
				 */
				$fields = array("cm_suppcm_indcod", "cm_suppcm_indcsys");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id]['cm_suppcm_indcod'], $data[$record][$this_event_id]['cm_suppcm_indcsys'], 'cm_suppcm_indcsys', $debug, $recode_soc);
				if ($debug) {
					error_log("DEBUG: Coded INDCSYS: subject=$record, event=$this_event_id for INDC {$data[$record][$this_event_id]['cm_suppcm_indcod']}");
				}
				/**
				 * CM_SUPPCM_ATCNAME
				 * CM_SUPPCM_ATC2NAME
				 */
				$fields = array("cm_cmdecod", "cm_suppcm_atcname", "cm_suppcm_atc2name");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_atc($project_id, $record, $this_event_id, $data[$record][$this_event_id]['cm_cmdecod'], $data[$record][$this_event_id]['cm_suppcm_atcname'], $data[$record][$this_event_id]['cm_suppcm_atc2name'], $debug, $recode_atc);
				if ($debug) {
					error_log("DEBUG: Coded ATCs: subject=$record, event=$this_event_id for CONMED {$data[$record][$this_event_id]['cm_cmdecod']}");
				}
				break;

			case 'transfusions':
				$recode_cm = true;
				$recode_llt = true;
				$recode_soc = true;
				$recode_atc = true;
				/**
				 * XFSN_CMDECOD
				 */
				$fields = array("xfsn_cmtrt", "xfsn_cmdecod", "xfsn_cmindc", "xfsn_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_cm($project_id, $record, $this_event_id, $data[$record][$this_event_id], $debug, $recode_cm);
				/**
				 * XFSN_SUPPCM_INDCOD
				 */
				$fields = array("xfsn_cmindc", "xfsn_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id]['xfsn_cmindc']), fix_case($data[$record][$this_event_id]['xfsn_oth_cmindc']), $data[$record][$this_event_id]['xfsn_suppcm_indcod'], 'xfsn_suppcm_indcod', $debug, $recode_llt);
				if ($debug) {
					error_log("DEBUG: Coded XFSN INDC: subject=$record, event=$this_event_id for CONMED {$data[$record][$this_event_id]['xfsn_cmdecod']}");
				}
				/**
				 * XFSN_SUPPCM_INDCSYS
				 */
				$fields = array("xfsn_suppcm_indcod", "xfsn_suppcm_indcsys");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id]['xfsn_suppcm_indcod'], $data[$record][$this_event_id]['xfsn_suppcm_indcsys'], 'xfsn_suppcm_indcsys', $debug, $recode_soc);
				if ($debug) {
					error_log("DEBUG: Coded XFSN INDCSYS: subject=$record, event=$this_event_id for INDC {$data[$record][$this_event_id]['xfsn_suppcm_indcod']}");
				}
				/**
				 * XFSN_SUPPCM_ATCNAME
				 * XFSN_SUPPCM_ATC2NAME
				 */
				$fields = array("xfsn_cmdecod", "xfsn_suppcm_atcname", "xfsn_suppcm_atc2name");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_atc_xfsn($project_id, $record, $this_event_id, $data[$record][$this_event_id]['xfsn_cmdecod'], $data[$record][$this_event_id]['xfsn_suppcm_atcname'], $data[$record][$this_event_id]['xfsn_suppcm_atc2name'], $debug, $recode_atc);
				if ($debug) {
					error_log("DEBUG: Coded XFSN ATCs: subject=$record, event=$this_event_id for CONMED {$data[$record][$this_event_id]['xfsn_cmdecod']}");
				}
				unset($data);
				break;

			case 'mh_coding':
				$recode_llt = false;
				$recode_pt = true;
				$recode_soc = true;
				$mh_prefixes = array('othpsy', 'othca');
				/**
				 * MH_MHMODIFY
				 */
				foreach ($mh_prefixes AS $prefix) {
					$fields = array($prefix . "_oth_mhterm", $prefix . "_mhmodify");
					$data = REDCap::getData('array', $record, $fields, $this_event_id);
					code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id][$prefix . "_oth_mhterm"]), '', $data[$record][$this_event_id][$prefix . "_mhmodify"], $prefix . "_mhmodify", $debug, $recode_llt);
					if ($debug) {
						error_log("DEBUG: Coded " . strtoupper($prefix) . "_MHMODIFY {$data[$record][$this_event_id][$prefix . "_mhmodify"]}: subject=$record, event=$this_event_id for MH {$data[$record][$this_event_id][$prefix . "_oth_mhterm"]}");
					}
					/**
					 * PREFIX_MHDECOD
					 * uses $mh_prefixes preset array
					 */
					$fields = array($prefix . "_mhmodify", $prefix . "_mhdecod");
					$data = REDCap::getData('array', $record, $fields, $this_event_id);
					code_pt($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_mhmodify"], $data[$record][$this_event_id][$prefix . "_mhdecod"], $prefix . "_mhdecod", $debug, $recode_pt);
					if ($debug) {
						error_log("DEBUG: Coded " . strtoupper($prefix) . "_MHDECOD {$data[$record][$this_event_id][$prefix . '_mhdecod']}: subject=$record, event=$this_event_id for MHMODIFY {$data[$record][$this_event_id][$prefix . '_mhmodify']}");
					}
					/**
					 * PREFIX_mhBODSYS
					 * uses $mh_prefixes preset array
					 */
					$fields = array($prefix . "_mhdecod", $prefix . "_mhbodsys");
					$data = REDCap::getData('array', $record, $fields, $this_event_id);
					code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_mhdecod"], $data[$record][$this_event_id][$prefix . "_mhbodsys"], $prefix . "_mhbodsys", $debug, $recode_soc);
					if ($debug) {
						error_log("DEBUG: Coded  " . strtoupper($prefix) . "_MHBODSYS {$data[$record][$this_event_id][$prefix . "_mhbodsys"]}: subject=$record, event=$this_event_id for MHDECOD {$data[$record][$this_event_id][$prefix . "_mhdecod"]}");
					}
				}
				unset($data);
				break;

			case 'cm_coding':
				$recode_llt = false;
				$recode_pt = true;
				$recode_soc = true;
				$recode_atc = false;
				$recode_cm = true;
				/**
				 * CM_CMDECOD
				 */
				$fields = array("cm_cmtrt", "cm_cmdecod", "cm_cmindc", "cm_oth_cmindc", "cm_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_cm($project_id, $record, $this_event_id, $data[$record][$this_event_id], $debug, $recode_cm);
				if ($debug) {
					error_log("DEBUG: Coded CONMED: subject=$record, event=$this_event_id for CMTRT {$data[$record][$this_event_id]['cm_cmtrt']}");
				}
				/**
				 * cm_suppcm_mktstat
				 * PRESCRIPTION or OTC
				 */
				$fields = array("cm_cmdecod", "cm_suppcm_mktstat");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				foreach ($data as $subject_id => $subject) {
					foreach ($subject as $event_id => $event) {
						if (isset($event['cm_cmdecod']) && $event['cm_cmdecod'] != '') {
							update_field_compare($subject_id, $project_id, $event_id, get_conmed_mktg_status($event['cm_cmdecod']), $event['cm_suppcm_mktstat'], 'cm_suppcm_mktstat', $debug);
							if ($debug) {
								error_log("DEBUG: $subject_id Marketing Status = " . get_conmed_mktg_status($event['cm_cmdecod']));
							}
						}
					}
				}
				/**
				 * CM_SUPPCM_INDCOD
				 */
				$fields = array("cm_cmindc", "cm_oth_cmindc", "cm_suppcm_indcmodf");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				/**
				 * re-code all nutritional support to nutritional supplement
				 */
				if ($data[$record][$this_event_id]['cm_oth_cmindc'] == 'Nutritional support') {
					$data[$record][$this_event_id]['cm_oth_cmindc'] = 'Nutritional supplement';
				}
				code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id]['cm_cmindc']), fix_case($data[$record][$this_event_id]['cm_oth_cmindc']), $data[$record][$this_event_id]['cm_suppcm_indcmodf'], 'cm_suppcm_indcmodf', $debug, $recode_llt);
				if ($debug) {
					error_log("DEBUG: Coded INDC LLT: {} subject=$record, event=$this_event_id for INDICATION {$data[$record][$this_event_id]['cm_cmindc']}");
				}
				/**
				 * CM_SUPPCM_INDCOD
				 */
				$fields = array("cm_suppcm_indcmodf", "cm_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_pt($project_id, $record, $this_event_id, $data[$record][$this_event_id]['cm_suppcm_indcmodf'], $data[$record][$this_event_id]['cm_suppcm_indcod'], 'cm_suppcm_indcod', $debug, $recode_pt);
				if ($debug) {
					error_log("DEBUG: Coded INDC PT: subject=$record, event=$this_event_id for INDICATION {$data[$record][$this_event_id]['cm_suppcm_indcod']}");
				}
				/**
				 * CM_SUPPCM_INDCSYS
				 */
				$fields = array("cm_suppcm_indcod", "cm_suppcm_indcsys");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id]['cm_suppcm_indcod'], $data[$record][$this_event_id]['cm_suppcm_indcsys'], 'cm_suppcm_indcsys', $debug, $recode_soc);
				if ($debug) {
					error_log("DEBUG: Coded INDCSYS: subject=$record, event=$this_event_id for INDC {$data[$record][$this_event_id]['cm_suppcm_indcod']}");
				}
				/**
				 * CM_SUPPCM_ATCNAME
				 * CM_SUPPCM_ATC2NAME
				 */
				$fields = array("cm_cmdecod", "cm_suppcm_atcname", "cm_suppcm_atc2name");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_atc_soc($project_id, $record, $this_event_id, $data[$record][$this_event_id]['cm_cmdecod'], $data[$record][$this_event_id]['cm_suppcm_atcname'], $data[$record][$this_event_id]['cm_suppcm_atc2name'], $debug, $recode_atc);
				if ($debug) {
					error_log("DEBUG: Coded ATCs: subject=$record, event=$this_event_id for CONMED {$data[$record][$this_event_id]['cm_cmdecod']}");
				}
				/**
				 * XFSN_CMDECOD
				 */
				$fields = array("xfsn_cmtrt", "xfsn_cmdecod", "xfsn_cmindc", "xfsn_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				foreach ($data AS $subject_id => $subject) {
					foreach ($subject AS $event_id => $event) {
						if (isset($event['xfsn_cmtrt']) && $event['xfsn_cmtrt'] != '') {
							$med = array();
							$med_result = db_query("SELECT DISTINCT drug_coded FROM _target_xfsn_coding WHERE drug_name = '" . prep($event['xfsn_cmtrt']) . "'");
							if ($med_result) {
								$med = db_fetch_assoc($med_result);
								if (isset($med['drug_coded']) && $med['drug_coded'] != '') {
									update_field_compare($subject_id, $project_id, $event_id, $med['drug_coded'], $event['xfsn_cmdecod'], 'xfsn_cmdecod', $debug);
								}
							}
							if ($debug) {
								error_log("DEBUG: Coded Transfusion: subject=$subject_id, event=$event_id for CMTRT {$event['xfsn_cmtrt']}");
							}
						} else {
							update_field_compare($subject_id, $project_id, $event_id, '', $event['xfsn_cmdecod'], 'xfsn_cmdecod', $debug);
						}
					}
				}
				/**
				 * XFSN_SUPPCM_INDCOD
				 */
				$fields = array("xfsn_cmindc", "xfsn_suppcm_indcod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_llt($project_id, $record, $this_event_id, fix_case($data[$record][$this_event_id]['xfsn_cmindc']), fix_case($data[$record][$this_event_id]['xfsn_oth_cmindc']), $data[$record][$this_event_id]['xfsn_suppcm_indcod'], 'xfsn_suppcm_indcod', $debug, $recode_llt);
				if ($debug) {
					error_log("DEBUG: Coded XFSN INDC: subject=$record, event=$this_event_id for CONMED {$data[$record][$this_event_id]['xfsn_cmdecod']}");
				}
				/**
				 * XFSN_SUPPCM_INDCSYS
				 */
				$fields = array("xfsn_suppcm_indcod", "xfsn_suppcm_indcsys");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id]['xfsn_suppcm_indcod'], $data[$record][$this_event_id]['xfsn_suppcm_indcsys'], 'xfsn_suppcm_indcsys', $debug, $recode_soc);
				if ($debug) {
					error_log("DEBUG: Coded XFSN INDCSYS: subject=$record, event=$this_event_id for INDC {$data[$record][$this_event_id]['xfsn_suppcm_indcod']}");
				}
				/**
				 * XFSN_SUPPCM_ATCNAME
				 * XFSN_SUPPCM_ATC2NAME
				 */
				$fields = array("xfsn_cmdecod", "xfsn_suppcm_atcname", "xfsn_suppcm_atc2name");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_atc_xfsn($project_id, $record, $this_event_id, $data[$record][$this_event_id]['xfsn_cmdecod'], $data[$record][$this_event_id]['xfsn_suppcm_atcname'], $data[$record][$this_event_id]['xfsn_suppcm_atc2name'], $debug, $recode_atc);
				if ($debug) {
					error_log("DEBUG: Coded XFSN ATCs: subject=$record, event=$this_event_id for CONMED {$data[$record][$this_event_id]['xfsn_cmdecod']}");
				}
				unset($data);
				break;

			case 'ex_coding':
				$recode_pt = true;
				$recode_soc = true;
				$prefix = 'eot';
				/**
				 * PREFIX_AEDECOD
				 * uses $tx_prefixes preset array
				 */
				$fields = array($prefix . "_aemodify", $prefix . "_aedecod");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_pt($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_aemodify"], $data[$record][$this_event_id][$prefix . "_aedecod"], $prefix . "_aedecod", $debug, $recode_pt);
				if ($debug) {
					error_log("DEBUG: Coded " . strtoupper($prefix) . "_AEDECOD {$data[$record][$this_event_id][$prefix . '_aedecod']}: subject=$record, event=$this_event_id for AEMODIFY {$data[$record][$this_event_id][$prefix . '_aemodify']}");
				}
				/**
				 * PREFIX_AEBODSYS
				 * uses $tx_prefixes preset array
				 */
				$fields = array($prefix . "_aedecod", $prefix . "_aebodsys");
				$data = REDCap::getData('array', $record, $fields, $this_event_id);
				code_bodsys($project_id, $record, $this_event_id, $data[$record][$this_event_id][$prefix . "_aedecod"], $data[$record][$this_event_id][$prefix . "_aebodsys"], $prefix . "_aebodsys", $debug, $recode_soc);
				if ($debug) {
					error_log("DEBUG: Coded SOC: subject=$record, event=$this_event_id for AE {$data[$record][$this_event_id][$prefix . "_aedecod"]}");
				}
				unset($data);
				break;
			default:
				break;
		}
	}
 * plugin
 */
echo "<h3>$plugin_title</h3>";
/**
 * MAIN
 */
if ($debug) {
	$timer['main_start'] = microtime(true);
}
$fields = array();
foreach ($mh_prefixes AS $prefix) {
	$fields[] = $prefix . "_oth_mhterm";
	$fields[] = $prefix . "_mhmodify";
	$fields[] = $prefix . "_mhdecod";
	$fields[] = $prefix . "_mhbodsys";
}
$data = REDCap::getData('array', $subjects, $fields);
foreach ($data AS $subject_id => $subject) {
    foreach ($subject AS $event_id => $event) {
	    foreach ($mh_prefixes AS $prefix) {
		    code_llt($project_id, $subject_id, $event_id, fix_case($event[$prefix . '_oth_mhterm']), '', $event[$prefix . '_mhmodify'], $prefix . '_mhmodify', $debug, $recode_llt);
		    code_pt($project_id, $subject_id, $event_id, get_single_field($subject_id, $project_id, $event_id, $prefix . "_mhmodify", ''), $event[$prefix . "_mhdecod"], $prefix . "_mhdecod", $debug, $recode_pt);
		    code_bodsys($project_id, $subject_id, $event_id, get_single_field($subject_id, $project_id, $event_id, $prefix . "_mhdecod", ''), $event[$prefix . "_mhbodsys"], $prefix . "_mhbodsys", $debug, $recode_soc);
	    }
    }
}
if ($debug) {
	$timer['main_end'] = microtime(true);
	$init_time = benchmark_timing($timer);
	echo $init_time;
}
$fields = array("dm_subjid", "ae_aeterm", "ae_oth_aeterm", "ae_aemodify", "ae_aedecod", "ae_aebodsys");
$data = REDCap::getData('array', $subjects, $fields);
foreach ($data AS $subject_id => $subject) {
	foreach ($subject AS $event_id => $event) {
		/**
		 * AE_AEDECOD
		 */
		code_llt($project_id, $subject_id, $event_id, fix_case($event['ae_aeterm']), fix_case($event['ae_oth_aeterm']), $event['ae_aemodify'], 'ae_aemodify', $debug, $recode_llt, $recode_msg);
		if ($debug) {
			error_log("DEBUG: Coded AE_AEMODIFY {$event['ae_aemodify']}: subject=$subject_id, event=$event_id for AE {$event['ae_aeterm']} - {$event['ae_oth_aeterm']}");
		}
		/**
		 * AE_AEDECOD
		 */
		$aemodify = get_single_field($subject_id, $project_id, $event_id, 'ae_aemodify', '');
		code_pt($project_id, $subject_id, $event_id, fix_case($aemodify), $event['ae_aedecod'], 'ae_aedecod', $debug, $recode_pt, $recode_msg);
		if ($debug) {
			error_log("DEBUG: Coded AE_AEDECOD {$event['ae_aedecod']}: subject=$subject_id, event=$event_id for AE {$aemodify}");
		}
		/**
		 * AE_AEBODSYS
		 */
		$aedecod = get_single_field($subject_id, $project_id, $event_id, 'ae_aedecod', '');
		code_bodsys($project_id, $subject_id, $event_id, $aedecod, $event['ae_aebodsys'], 'ae_aebodsys', $debug, $recode_soc, $recode_msg);
		if ($debug) {
			error_log("DEBUG: Coded SOC: subject=$subject_id, event=$event_id for AE {$event['ae_aedecod']}");
		}
		$timer_stop = microtime(true);
		$timer_time = number_format(($timer_stop - $timer_start), 2);
		if ($debug) {
			error_log("DEBUG: This DET action (Code AE) took $timer_time seconds");
Exemple #6
0
		/**
		 * DCV_AEDECOD
		 */
		code_pt($project_id, $subject_id, $event_id, fix_case($event['dcv_aemodify']), $event['dcv_aedecod'], 'dcv_aedecod', $debug, $recode_pt);
		/**
		 * HVN_AEDECOD
		 */
		code_pt($project_id, $subject_id, $event_id, fix_case($event['hvn_aemodify']), $event['hvn_aedecod'], 'hvn_aedecod', $debug, $recode_pt);
		/**
		 * VPK_AEDECOD
		 */
		code_pt($project_id, $subject_id, $event_id, fix_case($event['vpk_aemodify']), $event['vpk_aedecod'], 'vpk_aedecod', $debug, $recode_pt);
		/**
		 * DBV_AEDECOD
		 */
		code_pt($project_id, $subject_id, $event_id, fix_case($event['dbv_aemodify']), $event['dbv_aedecod'], 'dbv_aedecod', $debug, $recode_pt);
		/**
		 * AE_AEBODSYS
		 */
		code_bodsys($project_id, $subject_id, $event_id, $event['ae_aedecod'], $event['ae_aebodsys'], 'ae_aebodsys', $debug, $recode_soc);
		/**
		 * MH_MHBODSYS
		 */
		code_bodsys($project_id, $subject_id, $event_id, $event['othca_mhdecod'], $event['othca_mhbodsys'], 'othca_mhbodsys', $debug, $recode_soc);
		/**
		 * MH_MHBODSYS
		 */
		code_bodsys($project_id, $subject_id, $event_id, $event['othpsy_mhdecod'], $event['othpsy_mhbodsys'], 'othpsy_mhbodsys', $debug, $recode_soc);
		/**
		 * IFN_AEBODSYS
		 */
					} else {
						error_log(db_error() . ': ' . $flush_sql);
						echo db_error() . "<br />" . $flush_sql;
					}
				} else {
					show_var($flush_sql);
					error_log("DEBUG: " . $flush_sql);
				}

			}
		}
 	}
	foreach ($data[$subject_id] AS $subject) {
		foreach ($subject AS $event_id => $event) {
			if ($event['cm_cmoccur'] == 'Y') {
				code_pt($project_id, $subject_id, $event_id, $event['cm_suppcm_indcmodf'], $event['cm_suppcm_indcod'], 'cm_suppcm_indcod', $debug, $recode_pt, $message);
				if ($debug) {
					error_log("DEBUG: Coded INDC PT: subject=$subject_id, event=$event_id for INDICATION {$event['cm_suppcm_indcmodf']}");
				}
				code_bodsys($project_id, $subject_id, $event_id, $event['cm_suppcm_indcod'], $event['cm_suppcm_indcsys'], 'cm_suppcm_indcsys', $debug, $recode_soc, $message);
				if ($debug) {
					error_log("DEBUG: Coded INDCSYS: subject=$subject_id, event=$event_id for INDC {$event['cm_suppcm_indcod']}");
				}
				code_atc_soc($project_id, $subject_id, $event_id, $event['cm_cmdecod'], $event['cm_suppcm_atcname'], $event['cm_suppcm_atc2name'], $debug, $recode_atc, $update_message);
				if ($debug) {
					error_log("DEBUG: Coded ATCs: subject=$subject_id, event=$event_id for CONMED {$event['cm_cmdecod']}");
				}
			}
		}
	}
}
 $recode_llt = false;
 $recode_pt = true;
 $recode_soc = true;
 $recode_atc = false;
 $recode_cm = true;
 $tx_prefixes[] = 'eot';
 /**
  * PREFIX_AEDECOD
  * uses $tx_prefixes preset array
  */
 foreach ($tx_prefixes as $prefix) {
     $fields = array($prefix . "_aemodify", $prefix . "_aedecod");
     $data = REDCap::getData('array', $record, $fields, $redcap_event_name);
     foreach ($data as $subject_id => $subject) {
         foreach ($subject as $event_id => $event) {
             code_pt($project_id, $subject_id, $event_id, $event[$prefix . "_aemodify"], $event[$prefix . "_aedecod"], $prefix . "_aedecod", $debug, $recode_pt);
             if ($debug) {
                 error_log("DEBUG: Coded " . strtoupper($prefix) . "_AEDECOD {$event[$prefix . '_aedecod']}: subject={$subject_id}, event={$event_id} for AEMODIFY {$event[$prefix . '_aemodify']}");
             }
         }
     }
 }
 /**
  * PREFIX_AEBODSYS
  * uses $tx_prefixes preset array
  */
 foreach ($tx_prefixes as $prefix) {
     $fields = array($prefix . "_aedecod", $prefix . "_aebodsys");
     $data = REDCap::getData('array', $record, $fields, $redcap_event_name);
     foreach ($data as $subject_id => $subject) {
         foreach ($subject as $event_id => $event) {