Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config and code to get latest record ID if multiple #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<span style='color: #ff0000;'>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.</span>

<span style='font-weight: 600; text-decoration: underline;'>Setting up your piped field.<span>
Expand Down
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file added docs/readme_img_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 21 additions & 8 deletions getValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down