diff --git a/README.md b/README.md index 71364b2..6ac10f9 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,11 @@ example if Project A records are subject_id but Project B records are record_id. ![This picture shows the Alternate Source Match Field setting](/docs/readme_img_4.png) +**5. Use Latest Record ID:** +If your source project has multiple records with the same Record ID or Source Match Field, this module will not pipe the data because multiple source records exist. In this scenario, you may use this option to import from the latest record (by record ID). When checked, and multiple source project's exist, the last source project created will be used. + +![This picture shows the Latest Record ID Setting](/docs/readme_img_10.png) + Note all configurations can repeat, in the instance you need to pipe values from multiple projects into one. Simply select the + icon in the gray space at the top. Setting up your piped field. diff --git a/config.json b/config.json index 64ed478..fd6e8c2 100755 --- a/config.json +++ b/config.json @@ -66,6 +66,13 @@ "type": "text", "repeatable": false }, + { + "key": "use-latest-record-id", + "name": "If multiple Records for Unique Match Field, use the latest Record Id?", + "required": false, + "type": "checkbox", + "repeatable": false + }, { "key": "project-fields", "name": "Pipe Field", diff --git a/docs/readme_img_10.png b/docs/readme_img_10.png new file mode 100644 index 0000000..16c9dff Binary files /dev/null and b/docs/readme_img_10.png differ diff --git a/getValue.php b/getValue.php index b240018..b8d1daf 100755 --- a/getValue.php +++ b/getValue.php @@ -3,8 +3,9 @@ use ExternalModules\AbstractExternalModule; use ExternalModules\ExternalModules; +use Records; - require_once APP_PATH_DOCROOT.'Classes/LogicTester.php'; +require_once APP_PATH_DOCROOT.'Classes/LogicTester.php'; $thismatch = trim($_POST['thismatch']); $thismatch = preg_replace("/^[\'\"]/", "", $thismatch); @@ -50,16 +51,28 @@ } else { $matchSource = (empty($matchSource)) ? $thismatch : $matchSource ; $filterData = \REDCap::getData($_POST['otherpid'], 'array', null, null, null, null, false, false, false, "([$matchSource] = '$matchRecord')"); - if(count($filterData) != 1){ - // Either there were no matches or multiple matches. Either way, we want to return without echo-ing any values. - return; - } - reset($filterData); - $recordId = key($filterData); + if (count($filterData) != 1) { + $useLatestRecordIdSetting = $module->getProjectSetting('use-latest-record-id'); + if (!is_null($useLatestRecordIdSetting) && isset($useLatestRecordIdSetting[0]) && $useLatestRecordIdSetting[0] === true) { + $recordKeys = array_keys($filterData); + $records = Records::getRecordList($_POST['otherpid'], [], false, false, null, null, 0, $recordKeys); + if ($records) { + $recordId = end($records); + } else { + $recordId = key(array_slice($filterData, -1, 1, true)); + } + } else { + // Either there were no matches or multiple matches. Either way, we want to return without echo-ing any values. + return; + } + } else { + reset($filterData); + $recordId = key($filterData); + } } - $data = \Records::getData($_POST['otherpid'], 'array', array($recordId)); + $data = Records::getData($_POST['otherpid'], 'array', array($recordId)); if(empty($data)) { return; }