Skip to content

Commit f522f3d

Browse files
authored
[redcap] Improve REDCap module 2 (#9904)
Contains a bunch of small improvements to the redcap module. It is a preparatory PR to the import records script (#9905) but the changes do make sense by themselves. - Simplify `RedcapHttpCLient` URL parameter. - Include record ID and unique event name in REDCap API records. - Decouple REDCap record import from REDCap notification handling using a new `RedcapRecordImporter` class.
1 parent 11097fe commit f522f3d

File tree

7 files changed

+531
-377
lines changed

7 files changed

+531
-377
lines changed

modules/redcap/php/client/models/records/redcaprecord.class.inc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ use LORIS\redcap\client\RedcapProps;
2626
class RedcapRecord
2727
{
2828
/**
29-
* The date at which the record was completed if it was completed using a
30-
* survey.
29+
* The REDCap record ID.
3130
*/
32-
public readonly ?\DateTimeImmutable $datetime;
31+
public readonly string $record_id;
32+
33+
/**
34+
* The REDCap unique event name.
35+
*/
36+
public readonly string $unique_event_name;
3337

3438
/**
3539
* The record completion status.
@@ -39,6 +43,12 @@ class RedcapRecord
3943
*/
4044
public readonly int $complete;
4145

46+
/**
47+
* The date and time at which the record was completed. This is an undocumented
48+
* field in the REDCap API.
49+
*/
50+
public readonly ?\DateTimeImmutable $datetime;
51+
4252
private string $_form_name;
4353

4454
private array $_props;
@@ -67,8 +77,10 @@ class RedcapRecord
6777
$datetime = null;
6878
}
6979

70-
$this->datetime = $datetime;
71-
$this->complete = $props->getInt("{$form_name}_complete");
80+
$this->record_id = $props->getString('record_id');
81+
$this->unique_event_name = $props->getString('redcap_event_name');
82+
$this->datetime = $datetime;
83+
$this->complete = $props->getInt("{$form_name}_complete");
7284
}
7385

7486
/**

modules/redcap/php/client/redcaphttpclient.class.inc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class RedcapHttpClient
4646
private bool $_verbose;
4747

4848
/**
49-
* REDCap URL.
49+
* REDCap API URL.
5050
*/
5151
private string $_url;
5252

@@ -70,18 +70,20 @@ class RedcapHttpClient
7070
/**
7171
* Create a new REDCap Client for a specific REDCap instance and project.
7272
*
73-
* @param string $instance_api_url A REDCap instance API URL.
73+
* @param string $instance_url A REDCap instance URL.
7474
* @param string $project_api_token A REDCap project API token.
7575
* @param bool $verbose Verbose mode.
7676
*/
7777
public function __construct(
78-
string $instance_api_url,
78+
string $instance_url,
7979
string $project_api_token,
8080
bool $verbose = false
8181
) {
82-
$this->_url = $instance_api_url;
82+
$trimmed_url = rtrim($instance_url, '/');
83+
$api_url = "{$trimmed_url}/api/";
84+
$this->_url = $api_url;
8385
$this->_token = $project_api_token;
84-
$this->_client = new Client($instance_api_url);
86+
$this->_client = new Client($api_url);
8587
$this->_verbose = $verbose;
8688
}
8789

@@ -784,7 +786,9 @@ class RedcapHttpClient
784786
'type' => 'flat',
785787
'csvDelimiter' => '',
786788
'forms' => $instrument_names,
787-
'fields' => [],
789+
// The 'record_id' parameter adds both the 'record_id' and
790+
// 'redcap_event_name' fields to the REDCap records.
791+
'fields' => ['record_id'],
788792
'events' => $unique_event_names,
789793
'records' => $record_ids,
790794
'rawOrLabel' => 'raw',

modules/redcap/php/endpoints/notifications.class.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Notifications extends Endpoint
162162
// get a new redcap client based on the notification info
163163
try {
164164
$redcap_client = new RedcapHttpClient(
165-
"{$config->redcap_instance_url}/api/",
165+
$config->redcap_instance_url,
166166
$config->redcap_api_token,
167167
);
168168

modules/redcap/php/redcapmapper.class.inc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,22 @@ class RedcapMapper
8989
* session creation is enabled for that visit in the REDCap module
9090
* configuration, or throws an error if it is not.
9191
*
92-
* @param RedcapRecord $redcap_record The REDCap record.
93-
* @param string $unique_event_name The unique event name associated with
94-
* that REDCap record.
95-
* @param \Candidate $candidate The LORIS candidate associated with
96-
* that REDCap record.
92+
* @param RedcapRecord $redcap_record The REDCap record.
93+
* @param \Candidate $candidate The LORIS candidate associated with
94+
* that REDCap record.
9795
*
9896
* @return string The visit label of the relevant session.
9997
*/
10098
public function getVisitLabel(
10199
RedcapRecord $redcap_record,
102-
string $unique_event_name,
103100
\Candidate $candidate,
104101
): string {
105-
$visit_config = $this->getVisitConfig($unique_event_name);
102+
$visit_config = $this->getVisitConfig($redcap_record->unique_event_name);
106103

107104
// If no visit mappings are defined in the configuration, use the REDCap
108105
// unique event name as the visit label directly.
109106
if ($visit_config === null) {
110-
return $unique_event_name;
107+
return $redcap_record->unique_event_name;
111108
}
112109

113110
$session = $this->checkOrCreateSession($candidate, $visit_config);
@@ -140,7 +137,7 @@ class RedcapMapper
140137
// Find the REDCap event that matches the REDCap notification event.
141138
$redcap_event = array_find(
142139
$redcap_events,
143-
fn($redcap_event) => $unique_event_name === $redcap_event->unique_name,
140+
fn($redcap_event) => $redcap_event->unique_name === $unique_event_name,
144141
);
145142

146143
// There should always be a REDCap event that matches the REDCap

0 commit comments

Comments
 (0)