-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAutoRecordGenerationExternalModule.php
More file actions
526 lines (453 loc) · 22.6 KB
/
AutoRecordGenerationExternalModule.php
File metadata and controls
526 lines (453 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
/**
* Created by PhpStorm.
* User: moorejr5
* Date: 5/31/2018
* Time: 3:28 PM
*/
namespace Vanderbilt\AutoRecordGenerationExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use mysql_xdevapi\Exception;
use REDCap;
use function PHPUnit\Framework\isNull;
class AutoRecordGenerationExternalModule extends AbstractExternalModule
{
const RECORD_CREATED_BY_MODULE = "auto_record_module_saved";
function redcap_data_entry_form($project_id, $record, $instrument, $event_id, $group_id = NULL, $repeat_instance = 1) {
}
function redcap_save_record($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance = 1) {
## Prevent hook from being called multiple times on each project/record pair
if(defined(self::RECORD_CREATED_BY_MODULE.$project_id."~".$record) &&
constant(self::RECORD_CREATED_BY_MODULE.$project_id."~".$record) == 1) {
return;
}
define(self::RECORD_CREATED_BY_MODULE.$project_id."~".$record,1);
## Make REDCap think we're importing from ODM so it allows certain "errors"
if(!defined('CREATE_PROJECT_ODM')) {
define("CREATE_PROJECT_ODM",1);
}
## In case this gets triggered by a cron, set PID
$_GET['pid'] = $project_id;
$this->copyValuesToDestinationProjects($record, $event_id, $repeat_instance);
}
function getNewRecordName(\Project $project, $recordData,$destIndex,$recordSetting,$srcProjectID,$event_id,$repeat_instance = 1) {
$newRecordID = "";
if(!is_array($recordData) || empty($recordData)) {
// return default in case of missing data
return $newRecordID;
}
$srcRecordID = array_keys($recordData)[0];
if ($recordSetting == "") {
$destinationRecordData = [];
$queryLogResults = $this->queryLogs("SELECT message, record, destination_record_id WHERE message='Auto record for $srcRecordID'",[]);
$recordMapSetting = $this->getProjectSetting("destination_record_id_".$srcRecordID."_".$project->project_id,$srcProjectID);
if ($recordMapSetting != "") {
$destinationRecordData = json_decode($recordMapSetting,true);
}
else {
while ($row = db_fetch_assoc($queryLogResults)) {
if ($row['destination_record_id'] != "") {
$destinationRecordData[$destIndex] = $row['destination_record_id'];
$this->removeLogs("message ='Auto record for $srcRecordID'", []);
} elseif ($row['record'] != "") {
$destinationRecordData[$destIndex] = $row['record'];
}
}
}
$newRecordID = ($destinationRecordData[$destIndex] ?? \DataEntry::getAutoId($project->project_id));
}
else {
$newRecordID = \Piping::replaceVariablesInLabel($recordSetting,$srcRecordID,$event_id,$repeat_instance,$recordData,true,$srcProjectID,false);
}
return $newRecordID;
}
function copyValuesToDestinationProjects($record, $event_id, $repeat_instance = 1) {
$destinationProjects = $this->framework->getSubSettings('destination_projects');
$project_id = $this->getProjectId();
$currentProject = new \Project($project_id);
$eventName = $currentProject->uniqueEventNames[$event_id];
$logProjectRecords = [];
foreach ($destinationProjects as $destIndex => $destinationProject) {
$flagFieldName = $destinationProject['field_flag'];
$results = json_decode(REDCap::getData($project_id, 'json', $record, $flagFieldName, $event_id),true);
## Need to set default value as $flagFieldName may not exist
$triggerFieldValue = "";
foreach ($results as $indexData) {
if ((!isset($indexData['redcap_event_name']) || $indexData['redcap_event_name'] == $eventName) && $indexData[$flagFieldName] != "") {
$triggerFieldValue = $indexData[$flagFieldName];
}
}
$triggerFieldType = $this->getFieldType($flagFieldName);
if(in_array($triggerFieldType, ['yesno', 'truefalse'])){
$triggerFieldSet = $triggerFieldValue === "1";
}
else{
$triggerFieldSet = $triggerFieldValue != "";
}
//echo "Trigger field set: ".($triggerFieldSet ? "True" : "False")."<br/>";
if ($triggerFieldSet) {
$handleResult = $this->handleDestinationProject($record, $event_id, (string)$destIndex, $destinationProject, $repeat_instance);
if (!empty($handleResult)) {
$logProjectRecords = $logProjectRecords + $handleResult;
}
}
}
if (!empty($logProjectRecords)) {
foreach ($logProjectRecords as $logProjectID => $logProjectRecord) {
$this->setProjectSetting("destination_record_id_" . $record . "_" . $logProjectID, json_encode($logProjectRecord), $project_id);
}
}
//$this->exitAfterHook();
}
private function handleDestinationProject($record, $event_id, $destIndex, $destinationProject, $repeat_instance = 1) {
$project_id = $this->getProjectId();
$targetProjectID = $destinationProject['destination_project'];
$overwrite = ($destinationProject['overwrite-record'] == "overwrite" ? $destinationProject['overwrite-record'] : "normal");
$targetProject = new \Project($targetProjectID);
$sourceProject = new \Project($project_id);
$debug = $destinationProject['enable_debug_logging'];
$destProjectRecord = [];
$recordData = \Records::getData($project_id,'array',$record);
$uniqueEventName = $sourceProject->getUniqueEventNames()[$event_id];
$destRecordExists = false;
$recordToCheck = $this->getNewRecordName($targetProject,$recordData,$destIndex,$destinationProject["new_record"],$project_id,$event_id,$repeat_instance);
if ($recordToCheck != "") {
$table = $this->getDataTable($targetProjectID);
$targetRecordSql = "SELECT record FROM $table WHERE project_id=? && record=? LIMIT 1";
$result = db_query($targetRecordSql,[$targetProjectID,$recordToCheck]);
while ($row = db_fetch_assoc($result)) {
if ($row['record'] == $recordToCheck) {
$destRecordExists = true;
}
}
}
if($debug == "1"){
$this->log("Checking values for pid $targetProjectID", [
'targetProjectID' => $targetProjectID,
'destinationRecordID' => $recordToCheck,
'overwrite' => $overwrite,
'destRecordExists' => $destRecordExists
]);
}
if ($targetProjectID != "" && is_numeric($targetProjectID) && ((!$destRecordExists && $overwrite == "normal") || $overwrite == "overwrite")) {
$sourceFields = $this->getSourceFields($project_id,$destinationProject['pipe_fields']);
//$recordData = \Records::getData($project_id,'array',array($record),$targetFields);
$dataToPipe = array();
$dataToPipe = $this->translateRecordData($recordData,$sourceProject,$targetProject,$sourceFields,$recordToCheck,$event_id,$repeat_instance);
if ($recordToCheck != "") {
$results = \Records::saveData($targetProjectID, 'array', $dataToPipe,$overwrite);
$errors = $results['errors'];
/*echo "Result:<br/>";
echo "<pre>";
print_r($results);
echo "</pre>";*/
if(!empty($errors)){
$errorString = stripslashes(json_encode($errors, JSON_PRETTY_PRINT));
$errorString = str_replace('""', '"', $errorString);
$message = "The " . $this->getModuleName() . " module could not copy values for record " . $recordToCheck . " from project $project_id to project $targetProjectID because of the following error(s):\n\n$errorString";
error_log($message);
$errorEmail = $this->getProjectSetting('error_email');
//if ($errorEmail == "") $errorEmail = "james.r.moore@vumc.org";
if(!empty($errorEmail)){
## Add check for universal from email address
global $from_email;
if($from_email != '') {
$headers = "From: ".$from_email."\r\n";
}
else {
$headers = null;
}
mail($errorEmail, $this->getModuleName() . " Module Error", $message, $headers);
}
}
else {
if ($destinationProject["new_record"] == "") {
$newRecord = true;
$recordMapSetting = $this->getProjectSetting("destination_record_id_".$record."_".$targetProjectID,$project_id);
if (is_array($recordMapSetting) && isset($recordMapSetting[$destIndex])) {
$newRecord = false;
}
if ($newRecord) {
$destProjectRecord[$targetProjectID][$destIndex] = (string)$recordToCheck;
//echo "Log ID: $logID for " . $recordToCheck . "<br/>";
}
}
$target_project_index = array_search($targetProjectID, $this->getProjectSetting("destination_project"));
if ($this->getProjectSetting("trigger_save_hook_flag")[$target_project_index] === true) {
global $Proj;
## Call the save record hook on the new record
# Cache get params to reset later
$oldId = $_GET['id'];
$oldPid = $_GET['pid'];
$oldProj = $Proj;
## Set the $_GET parameter to avoid errors / source project being affected
$_GET['pid'] = $targetProjectID;
$_GET['id'] = array_keys($dataToPipe)[0];
$Proj = $targetProject;
## Prevent module errors from crashing the whole import process
## NOTE: this does NOT catch errors thrown while the target module's redcap_save_record hook is running;
## errors from the target module will be handled as if the target module itself were running
try {
$redcap_save_record_args = [
/* $project_id = */ $_GET['pid'],
/* $record = */ $_GET['id'],
/* $instrument = */ NULL,
/* $event_id = */ $targetProject->firstEventId,
/* $group_id = */ NULL,
/* $survey_hash = */ NULL,
/* $response_id = */ NULL,
/* $repeat_instance = */ $repeat_instance
];
ExternalModules::callHook("redcap_save_record", $redcap_save_record_args);
}
catch(\Exception $e) {
error_log("External Module Error - Project: ".$_GET['pid']." - Record: ".$_GET['id'].": ".$e->getMessage());
}
$_GET['id'] = $oldId;
$_GET['pid'] = $oldPid;
$Proj = $oldProj;
}
}
}
}
//$this->exitAfterHook();
return $destProjectRecord;
}
private function getFieldType($fieldName) {
if(empty($fieldName)){
return null;
}
$fieldName = db_real_escape_string($fieldName);
$sql = "select element_type
from redcap_metadata
where project_id = ?
and field_name = ?";
$result = $this->query($sql,[$this->getProjectId(),$fieldName]);
$row = $result->fetch_assoc();
return $row['element_type'];
}
function getSourceFields($project_id,$pipeSettings) {
$project = new \Project($project_id);
$allFields = array_keys($project->metadata);
$returnFields = array();
if (is_array($pipeSettings) && !empty($pipeSettings) && $pipeSettings[0] != "") {
$returnFields = array_intersect($allFields, $pipeSettings);
}
else {
$returnFields = $allFields;
}
return $returnFields;
}
function processFieldEnum($enum) {
$enumArray = array();
$splitEnum = explode("\\n",$enum);
foreach ($splitEnum as $valuePair) {
$splitPair = explode(",",$valuePair);
$enumArray[trim($splitPair[0])] = trim($splitPair[1]);
}
return $enumArray;
}
function redcap_module_system_enable($version) {
// A version of this module with the older settings format could have previously been enabled.
// Make sure any old settings are updated.
self::ensureProperSubSettingsFormat();
}
function redcap_module_system_change_version($version, $old_version) {
// This could be a transition from a version of this module with the older settings format.
// Make sure any old settings are updated.
self::ensureProperSubSettingsFormat();
}
// This function is required to update existing settings after 'pipe_fields' was wrapped in the 'destination_projects' sub settings group.
// This should have no effect on subsequent runs and should be safe and efficient to repeatedly run indefinitely on future updates.
private function ensureProperSubSettingsFormat() {
$query = function($beginning, $setClause, $fieldName, $leadingBracketsRequired){
$prefix = '';
while($leadingBracketsRequired > 0){
$prefix .= '[';
$leadingBracketsRequired--;
}
$sql = "
$beginning
redcap_external_module_settings s
join redcap_external_modules m
on m.external_module_id = s.external_module_id
$setClause
where
m.directory_prefix = '" . $this->PREFIX . "'
and s.`key` = ?
and
(
type <> 'json-array'
or
s.value not like '$prefix%'
)";
return $this->query($sql,[$fieldName]);
};
$handleField = function($fieldName, $leadingBracketsRequired) use ($query){
$result = $query('select project_id, value from', '', $fieldName, $leadingBracketsRequired);
while($row = $result->fetch_assoc()){
$this->log("Logging old '$fieldName' value before wrapping in extra array", $row);
$projectId = $row['project_id'];
$value = $this->getProjectSetting($fieldName, $projectId);
$this->setProjectSetting($fieldName, [$value], $projectId);
}
};
$handleField('destination_project', 1);
$handleField('field_flag', 1);
$handleField('new_record', 1);
$handleField('overwrite-record', 1);
$handleField('pipe_fields', 2);
}
function redcap_module_import_page_top() {
require_once __DIR__ . '/import-page-top.php';
}
function getEventNames(){
global $longitudinal;
$originalValue = $longitudinal;
// Override the longitudinal value so that event details are returned even if the project is not longitudinal
$longitudinal = true;
$result = REDCap::getEventNames(true);
$longitudinal = $originalValue;
return $result;
}
function validateSettings($settings){
$fieldFlags = $settings['field_flag'];
foreach($fieldFlags as $fieldName){
$type = $this->getFieldType($fieldName);
if($type === 'sav'){
// Checkboxes would be difficult to support since there could be multiple values and it's unclear whether any/all/certain values should be considered the trigger.
return "Checkbox fields are not currently supported as trigger fields. Please select a different field, or change the type of the current trigger field.";
}
}
}
function translateRecordData($sourceData, \Project $sourceProject, \Project $destProject, $fieldsToUse, $recordToUse, $eventToUse = "", $instanceToUse = "") {
$eventMapping = array();
$sourceEvents = $sourceProject->eventInfo;
$destEvents = $destProject->eventInfo;
$destEventIDLeft = $destEvents;
$eventOffset = 0;
$sourceMeta = $sourceProject->metadata;
$destMeta = $destProject->metadata;
$destRecordField = $destProject->table_pk;
$destFields = array_keys($destMeta);
$destData = array();
foreach ($sourceEvents as $eventID => $eventInfo) {
if (count($destEvents) > 1) {
foreach ($destEvents as $destID => $destEventInfo) {
if ($eventInfo['name'] == $destEventInfo['name']) {
$eventMapping[$eventID] = $destID;
unset($destEventIDLeft[$destID]);
}
}
}
elseif (($eventToUse != "" && $eventID == $eventToUse) || $eventToUse == "") {
$destEventID = array_keys($destEvents)[0];
if ($destEventID != "") {
$eventMapping[$eventID] = $destEventID;
unset($destEventIDLeft[$destEventID]);
break;
}
}
$eventOffset++;
}
$eventOffset = 0;
foreach ($sourceEvents as $eventID => $eventInfo) {
if (!isset($eventMapping[$eventID]) && count($destEventIDLeft) > 0) {
$eventMapping[$eventID] = array_keys(array_slice($destEventIDLeft,$eventOffset,1,true))[0];
$eventOffset++;
}
}
if (!empty($sourceData)) {
foreach ($sourceData as $recordID => $recordData) {
foreach ($recordData as $eventID => $eventData) {
if ($eventID == "repeat_instances") {
foreach ($eventData as $subEventID => $subEventData) {
if (isset($eventMapping[$subEventID])) {
$destEventID = $eventMapping[$subEventID];
foreach ($subEventData as $instrument => $instrumentData) {
foreach ($instrumentData as $instance => $instanceData) {
if (($instanceToUse != "" && $instance == $instanceToUse) || $instanceToUse == "") {
foreach ($instanceData as $fieldName => $fieldValue) {
if ($fieldValue == "") continue;
if ((in_array($fieldName,$fieldsToUse) || empty($fieldsToUse)) && in_array($fieldName,$destFields)) {
if ($fieldName == $destRecordField && $fieldValue != "") $fieldValue = $recordToUse;
$fieldInstrument = $sourceMeta[$fieldName]['form_name'];
$instrumentRepeats = $sourceProject->isRepeatingForm($subEventID, $fieldInstrument);
if (($instrument == $fieldInstrument && !$instrumentRepeats) || ($instrument != "" && $instrument != $fieldInstrument)) continue;
$this->setDestinationData($destData, $sourceProject, $destProject, $fieldName, $fieldValue, $recordToUse, $destEventID, $instance);
}
}
}
}
}
}
}
}
elseif (isset($eventMapping[$eventID])) {
//TODO Need to check if a field is on a repeating/non-repeating basis when looking here for a valid field value, it will be empty ALWAYS otherwise
$destEventID = $eventMapping[$eventID];
foreach ($eventData as $fieldName => $fieldValue) {
if ((in_array($fieldName,$fieldsToUse) || empty($fieldsToUse)) && in_array($fieldName,$destFields)) {
if ($fieldValue == "") continue;
if ($fieldName == $destRecordField && $fieldValue != "") $fieldValue = $recordToUse;
$fieldInstrument = $sourceMeta[$fieldName]['form_name'];
$instrumentRepeats = $sourceProject->isRepeatingForm($eventID, $fieldInstrument);
if ($instrumentRepeats) continue;
$this->setDestinationData($destData, $sourceProject, $destProject, $fieldName, $fieldValue, $recordToUse, $destEventID);
}
}
}
}
}
}
return $destData;
}
function setDestinationData(&$destData, \Project $sourceProject, \Project $destProject, $srcFieldName, $srcFieldValue, $destRecord, $destEvent,$destRepeat = 1)
{
$destMeta = $destProject->metadata;
$destEventForms = $destProject->eventsForms[$destEvent];
$destInstrument = $destMeta[$srcFieldName]['form_name'];
$destRecordField = $destProject->table_pk;
$srcMeta = $sourceProject->metadata;
$destInstrumentRepeats = $destProject->isRepeatingForm($destEvent, $destInstrument);
$destEventRepeats = $destProject->isRepeatingEvent($destEvent);
if (in_array($destInstrument,$destEventForms) && $srcMeta[$srcFieldName]['element_type'] == $destMeta[$srcFieldName]['element_type'] && $this->matchEnum($srcMeta[$srcFieldName]['element_enum'],$destMeta[$srcFieldName]['element_enum'])) {
if ($destInstrumentRepeats) {
$destData[$destRecord][$destEvent][$destRecordField] = $destRecord;
//$destData[$destRecord][$destEvent]['redcap_repeat_instrument'] = "";
//$destData[$destRecord][$destEvent]['redcap_repeat_instance'] = $destRepeat;
$destData[$destRecord]['repeat_instances'][$destEvent][$destInstrument][$destRepeat][$srcFieldName] = $srcFieldValue;
} elseif ($destEventRepeats) {
$destData[$destRecord][$destEvent][$destRecordField] = $destRecord;
//$destData[$destRecord][$destEvent]['redcap_repeat_instrument'] = "";
//$destData[$destRecord][$destEvent]['redcap_repeat_instance'] = $destRepeat;
$destData[$destRecord]['repeat_instances'][$destEvent][''][$destRepeat][$srcFieldName] = $srcFieldValue;
} else {
$destData[$destRecord][$destEvent][$srcFieldName] = $srcFieldValue;
}
}
}
function matchEnum($srcEnum, $destEnum) {
$destEnum = str_replace(' \n','\n',$destEnum);
$destEnum = str_replace('\n ','\n',$destEnum);
$destEnum = str_replace(' ,',',',$destEnum);
$destEnum = str_replace(', ',',',$destEnum);
$destEnum = rtrim($destEnum);
$srcEnum = str_replace(' \n','\n',$srcEnum);
$srcEnum = str_replace('\n ','\n',$srcEnum);
$srcEnum = str_replace(' ,',',',$srcEnum);
$srcEnum = str_replace(', ',',',$srcEnum);
$srcEnum = rtrim($srcEnum);
if ($srcEnum == $destEnum) {
return true;
}
return false;
}
function getDataTable($project_id){
return method_exists('\REDCap', 'getDataTable') ? \REDCap::getDataTable($project_id) : "redcap_data";
}
function escape($arg){
return $this->framework->escape($arg);
}
}