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 5 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
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
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