-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntakeDashboard.php
874 lines (739 loc) · 33.4 KB
/
IntakeDashboard.php
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
<?php
namespace Stanford\IntakeDashboard;
require_once "emLoggerTrait.php";
require_once "Utilities/RepeatingForms.php";
require_once "Utilities/Sanitizer.php";
//require_once "classes/ModuleCore/ModuleCore.php";
require_once("classes/Child.php");
use ExternalModules;
use Exception;
use REDCap;
use Project;
use Survey;
class IntakeDashboard extends \ExternalModules\AbstractExternalModule
{
use emLoggerTrait;
const BUILD_FILE_DIR = 'dashboard-ui/dist/assets';
// private $moduleCore;
public function __construct()
{
parent::__construct();
}
// public function setModuleCore(){
// $moduleCore = new ModuleCore($this);
// }
//
// public function getModuleCore(){
// if(!$this->moduleCore)
// $this->setModuleCore(new ModuleCore($this));
// return $this->moduleCore;
// }
/**
* Helper method for inserting the JSMO JS into a page along with any preload data
* @param $data
* @param $init_method
* @return void
*/
public function injectJSMO($data = null, $init_method = null): void
{
echo $this->initializeJavascriptModuleObject();
// Output the script tag for loading the JSMO JavaScript file
echo sprintf('<script src="%s"></script>', $this->getUrl("jsmo/jsmo.js", true));
}
/**
* @return array
*/
public function generateAssetFiles(): array
{
$cwd = $this->getModulePath();
$assets = [];
$full_path = $cwd . self::BUILD_FILE_DIR . '/';
$dir_files = scandir($full_path);
// Check if scandir failed
if ($dir_files === false) {
$this->emError("Failed to open directory: $full_path");
return $assets; // Return an empty array or handle the error as needed
}
$dir_files = array_diff($dir_files, array('..', '.'));
foreach ($dir_files as $file) {
$url = $this->getUrl(self::BUILD_FILE_DIR . '/' . $file);
$html = '';
if (str_contains($file, '.js')) {
$html = "<script type='module' crossorigin src='{$url}'></script>";
} elseif (str_contains($file, '.css')) {
$html = "<link rel='stylesheet' href='{$url}'>";
}
if ($html !== '') {
$assets[] = $html;
}
}
return $assets;
}
/**
* This is the primary ajax handler for JSMO calls
* @param $action
* @param $payload
* @param $project_id
* @param $record
* @param $instrument
* @param $event_id
* @param $repeat_instance
* @param $survey_hash
* @param $response_id
* @param $survey_queue_hash
* @param $page
* @param $page_full
* @param $user_id
* @param $group_id
* @return array|array[]|bool
* @throws Exception
*/
public function redcap_module_ajax($action, $payload, $project_id, $record, $instrument, $event_id, $repeat_instance,
$survey_hash, $response_id, $survey_queue_hash, $page, $page_full, $user_id, $group_id)
{
try{
$sanitized = $this->sanitizeInput($payload);
return match ($action) {
'fetchIntakeParticipation' => $this->fetchIntakeParticipation(),
'getUserDetail' => $this->getUserDetail($payload),
'fetchRequiredSurveys' => $this->fetchRequiredSurveys($payload),
'toggleProjectActivation' => $this->toggleProjectActivation($payload),
'newChildRequest' => $this->newChildRequest($payload),
'getChildSubmissions' => $this->getChildSubmissions($payload),
default => throw new Exception ("Action $action is not defined"),
};
} catch (\Exception $e ) {
$this->handleGlobalError($e);
}
}
/**
* @param $project_id
* @param $record
* @param $instrument
* @param $event_id
* @param $group_id
* @param $survey_hash
* @param $response_id
* @param $repeat_instance
* @return void
* @throws Exception
* Function that runs on each survey completion
*/
public function redcap_survey_complete(
$project_id,
$record = null,
$instrument,
$event_id,
$group_id = null,
$survey_hash,
$response_id = null,
$repeat_instance = 1
)
{
try {
$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);
$detailsParams = [
"return_format" => "json",
"project_id" => $project_id,
"records" => $record
];
$completedIntake = json_decode(REDCap::getData($detailsParams), true);
$completedIntake = reset($completedIntake);
if(!empty($completedIntake)){
// Child survey has been saved, we have to copy data from the parent project
if($parent_id !== $project_id){
$child = new Child($this, $project_id, $parent_id, $pSettings);
$child->saveParentData($completedIntake['universal_id'], $record);
// $child->saveParentData($record);
} else {
// Universal immutable survey form has been saved for the first time (new intake)
if($instrument === $pSettings['universal-survey-form-immutable']) {
// Determine username
$requesterUsername = $this->determineREDCapUsername($completedIntake['requester_sunet_sid'], $completedIntake['requester_email']);
if (!empty($requesterUsername)) {
$this->saveUser($requesterUsername, $completedIntake['record_id']);
} else {
$submitted_sunet_sid = $completedIntake['requester_sunet_sid'];
$submitted_email = $completedIntake['requester_email'];
$this->emError("Username cannot be determined for Universal Survey: record $record project $parent_id");
REDCap::logEvent(
"Username cannot be determined on Intake submission",
"Username : $submitted_sunet_sid \n Email: $submitted_email \n Dashboard access has not been granted, likely due to an incorrect email \n access will require manual entry",
"",
"$record"
);
}
} else if($instrument === $pSettings['universal-survey-form-mutable']) { // Editable survey has been altered
$proj = new Project($parent_id);
$event_name = $this->generateREDCapEventName($proj, $pSettings['user-info-event']);
// Function will add new users / delete old users
$this->validateUserPermissions($project_id, $record, $event_name);
foreach($pSettings['project-id'] as $childProjectId) {
$child = new Child($this, $childProjectId, $parent_id, $pSettings);
$child->updateParentData($record);
}
//TODO Iterate through all linked children and overwrite with new parent data
}
}
}
} catch (\Exception $e ) {
$this->handleGlobalError($e);
}
}
/**
* @param $payload
* @return void
*/
public function toggleProjectActivation($payload): string
{
try {
if (empty($payload['uid']))
throw new \Exception("UID is empty");
$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);
$completedIntake = $this->fetchParentRecordData($parent_id, $payload['uid'], $pSettings['universal-survey-event']);
$completedIntake = reset($completedIntake);
if($completedIntake['intake_active'] === "0")
$completedIntake['intake_active'] = "1";
else //if active is null or any other value besides 0, set explicitly as inactive.
$completedIntake['intake_active'] = "0";
//Set activation date change
$completedIntake['active_change_date'] = date('Y-m-d');
$completedIntake['deactivation_reason'] = $payload['reason'] ?? null;
$completedIntake['deactivation_user'] = $_SESSION['username'] ?? null;
$saveData = [
$completedIntake
];
// Save data using REDCap's saveData function
$response = REDCap::saveData($parent_id, 'json', json_encode($saveData), 'overwrite');
if(!empty($response['errors'])){
throw new \Exception("Error on reactivation/deactivation save" . implode(', ', $response['errors']));
}
return json_encode(["data" => $completedIntake, "success" => true]);
} catch (\Exception $e) {
$this->handleGlobalError($e);
}
}
/**
* @return
*
*/
public function validateUserPermissions($projectId, $recordId, $childEventName) {
$parentParams = [
"return_format" => "json",
"project_id" => $projectId,
"records" => $recordId,
];
// This parent data has already been saved, let it be source of truth
$parentData = json_decode(REDCap::getData($parentParams), true);
$parentData = reset($parentData);
$savedUsers = array_filter([
$this->determineREDCapUsername($parentData['requester_sunet_sid'], $parentData['requester_email']) ?? null => 'requester_sunet_sid',
$this->determineREDCapUsername($parentData['sunet_sid'], $parentData['email']) ?? null => 'sunet_sid',
$this->determineREDCapUsername($parentData['pi_sunet_sid'], $parentData['pi_email']) ?? null => 'pi_sunet_sid',
$this->determineREDCapUsername($parentData["op_sunet_sid_1"], $parentData["op_email_1"]) ?? null => 'op_sunet_sid_1',
$this->determineREDCapUsername($parentData["op_sunet_sid_2"], $parentData["op_email_2"]) ?? null => 'op_sunet_sid_2',
$this->determineREDCapUsername($parentData["op_sunet_sid_3"], $parentData["op_email_3"]) ?? null => 'op_sunet_sid_3',
], function ($key) {
return !empty($key);
}, ARRAY_FILTER_USE_KEY);
$userParams = [
"return_format" => "json",
"project_id" => $projectId,
"events" => $childEventName
];
//Get all users that exist within the permission schema for the dashboard
$fullUserData = json_decode(REDCap::getData($userParams), true);
$deletions = [];
$instance_count = [];
// Goal : check if user already has access, if removed, remove permissions, if not add permissions.
// Iterate through each instance of user_info
foreach($fullUserData as $entry) {
if(array_key_exists($entry['record_id'],$instance_count))
$instance_count[$entry['record_id']] += 1;
else
$instance_count[$entry['record_id']] = 1;
if($entry['intake_id'] === $recordId) { // We have encountered a user that has access to this intake on dashboard
$username = $entry['record_id'];
if(!array_key_exists($username, $savedUsers)) { // saved users are only usernames that should have access.
//username does not exist as an entry in fullUserData (source of truth), remove
$deletions[] = $entry;
} else { // User exists in fullUserData & saved users
//remove username from the list of users we have to check for later removal
unset($savedUsers[$username]);
}
}
}
//Delete users, this will allow users to delete themselves
foreach($deletions as $userEntry) {
//If users only have one repeating instance, delete the entire record
if(array_key_exists($userEntry['record_id'], $instance_count) && $instance_count[$userEntry['record_id']] === 1) {
$res = REDCap::deleteRecord($projectId, $userEntry['record_id'], null, null, null, null);
} else { // Otherwise delete the specific instance
$res = REDCap::deleteRecord($projectId, $userEntry['record_id'], null, $childEventName, $userEntry['redcap_repeat_instrument'], $userEntry['redcap_repeat_instance']);
}
}
// SavedUsers now only has individuals who should be added
foreach($savedUsers as $username => $role) {
$res = $this->saveUser($username, $recordId);
}
}
public function determineREDCapUsername($su_sid_field, $email_field) {
if (!isset($email_field)) {
$this->emError("Email field has not been submitted for $su_sid_field, no dashboard access will be given");
return null; // Return null if email field is not set
}
$domains = [
'stanford.edu' => $su_sid_field,
'stanfordchildrens.org' => $email_field,
'stanfordhealthcare.org' => $su_sid_field . '@stanfordhealthcare.org',
];
foreach ($domains as $domain => $returnValue) {
if (strpos($email_field, $domain) !== false) {
return $returnValue;
}
}
return null; // Return null if no domain matches
}
/**
* @param $username
* @return mixed
* @throws Exception
* Saves a user in the hash table with reference to a universal intake submission
*/
public function saveUser($username, $universalId)
{
$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);
$proj = new Project($parent_id);
// Find the event ID for the "User" event
$event_id = $pSettings['user-info-event'];
// Ensure the event ID and form name are found
if ($event_id === null || !isset($proj->eventsForms[$event_id])) {
throw new Exception("User event or form not found.");
}
$form_name = reset($proj->eventsForms[$event_id]);
$event_name = $this->generateREDCapEventName($proj, $event_id);
// Create a RepeatingForms instance and get the next instance ID
$rForm = new RepeatingForms('user_info', $event_id, $parent_id);
$next_instance_id = $rForm->getNextInstanceId($username);
// No need to check if the linkage already exists in user table, intake ID will always be unique
// Prepare data for saving
$saveData = [
[
"record_id" => $username,
"type" => "Secondary",
"intake_id" => $universalId,
"redcap_event_name" => $event_name,
"redcap_repeat_instrument" => $form_name,
"redcap_repeat_instance" => $next_instance_id,
"{$form_name}_complete" => 2
]
];
// Save data using REDCap's saveData function
return REDCap::saveData($parent_id, 'json', json_encode($saveData), 'overwrite');
}
/**
* Sanitizes user input in the action queue nested array
* @param $payload
* @return array|null
*/
public function sanitizeInput($payload): array|string
{
$sanitizer = new Sanitizer();
return $sanitizer->sanitize($payload);
}
/**
* @param $e
* @return false|string
*/
public function handleGlobalError($e): false|string
{
$msg = $e->getMessage();
$this->emError("Error: $msg");
return json_encode([
"error" => $msg,
"success" => false
]);
}
/**
* @param $proj
* @param $event_id
* @return string
* @throws Exception
*/
public function generateREDCapEventName($proj, $event_id): string
{
if(empty($event_id))
throw new \Exception("Event ID not passed, check config.json");
$event_name = $proj->eventInfo[$event_id]['name'];
$arm_num = $proj->eventInfo[$event_id]['arm_num'];
$convertedName = strtolower(str_replace(' ', '_', $event_name));
return $convertedName . "_arm_" . $arm_num;
}
/**
* Fetches intake participation data for the current user.
* Rendered on the intake dashboard view
*
* @return string|false JSON-encoded string of intake participation data, or false on error.
*/
public function fetchIntakeParticipation(): string|false
{
try {
$username = $_SESSION['username'] ?? null;
if (is_null($username)) {
throw new \Exception('No username for current session found, please refresh');
}
$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);
$proj = new Project($parent_id);
$full_user_event_name = $this->generateREDCapEventName($proj, $pSettings['user-info-event']);
$full_intake_event_name = $this->generateREDCapEventName($proj, $pSettings['universal-survey-event']);
// Fetch all intake IDs for the given user
$initialParams = [
"return_format" => "json",
"project_id" => $parent_id,
"redcap_event_name" => $full_user_event_name,
"fields" => ["type", "intake_id", "record_id"],
"records" => $username
];
$userIntakes = json_decode(REDCap::getData($initialParams), true);
if (empty($userIntakes)) {
return json_encode(["data" => [], "success" => true]);
}
// Extract intake IDs from the response
$intakeIds = array_column($userIntakes, 'intake_id');
// Fetch additional details for each intake ID
$detailsParams = [
"return_format" => "json",
"project_id" => $parent_id,
"redcap_event_name" => $full_intake_event_name,
"records" => $intakeIds
];
$intakeDetails = json_decode(REDCap::getData($detailsParams), true);
// Merge additional information into the user's intake data
foreach ($userIntakes as &$intake) {
foreach ($intakeDetails as $detail) {
if ($detail['record_id'] === $intake['intake_id']) {
$survey_id = $proj->forms[$pSettings['universal-survey-form-immutable']]['survey_id'];
$timestamp = Survey::isResponseCompleted($survey_id, $detail['record_id'], $pSettings['universal-survey-event'], 1, true);
$intake['completion_timestamp'] = date('Y-m-d', strtotime($timestamp));
$intake['intake_complete'] = $detail['intake_complete'] ?? null;
$intake['pi_name'] = trim($detail['pi_f_name'] . " " . $detail['pi_l_name']);;
$intake['research_title'] = $detail['research_title'] ?? null;
$intake['intake_active'] = $detail['intake_active'] ?? null;
$intake['active_change_date'] = $detail['active_change_date'] ?? null;
break;
}
}
}
// Generate survey link for main page
$link = $this->getPublicSurveyUrl($parent_id);
// $reserved = REDCap::reserveNewRecordId($parent_id);
// $link = REDCap::getSurveyLink($reserved, $pSettings['universal-survey-form-immutable'], $pSettings['universal-survey-event'], 1, $parent_id);
return json_encode([
"data" => $userIntakes,
"success" => true,
"link" => $link
]);
} catch (\Exception $e) {
return $this->handleGlobalError($e);
}
}
public function checkIntakeActivity($parentId, $uid, $surveyEvent){
$completedIntake = $this->fetchParentRecordData($parentId, $uid, $surveyEvent);
$completedIntake = reset($completedIntake);
if($completedIntake['intake_active'] === "0") //If explicitly set to zero return false, otherwise default to true
return false;
return true;
}
/**
* @param $payload
* @return string
*/
public function getUserDetail($payload): string
{
try {
if (empty($payload['username']) || empty($payload['uid'])) {
throw new \Exception("Either username or UID is empty");
}
$username = $payload['username'];
$uid = $payload['uid'];
$parentId = $this->getSystemSetting('parent-project');
$projectSettings = $this->getProjectSettings($parentId);
$project = new Project($parentId);
//Prevent detail page rendering (navigating) for inactive intake projects
// if(!$this->checkIntakeActivity($parentId, $uid, $projectSettings['universal-survey-event']))
// throw new \Exception("Intake is inactive, no detail access can be granted");
$userEventName = $this->generateREDCapEventName($project, $projectSettings['user-info-event']);
// Fetch all intake IDs for the given user
$params = [
"return_format" => "json",
"project_id" => $parentId,
"redcap_event_name" => $userEventName,
"fields" => ["type", "intake_id", "record_id"],
"records" => $username,
];
$userIntakes = json_decode(REDCap::getData($params), true);
// Iterate over to determine if current user has linked access to detail form
foreach ($userIntakes as $submission) {
if ($submission['intake_id'] === $uid) {
return $this->fetchRequiredSurveys($payload);
}
}
// $this->emLog()
return json_encode([
"success" => false
]);
} catch (\Exception $e) {
return $this->handleGlobalError($e);
}
}
public function checkChildDataExists($universalId, $pid){
$params = [
"return_format" => "json",
"project_id" => $pid,
"filterLogic" => "[universal_id] = $universalId"
];
$response = json_decode(REDCap::getData($params), true);
if(count($response))
return reset($response);
return [];
}
/**
* @param $project
* @param $fields
* @return array
* Removes hidden fields before sending the contents of getData to client
*/
public function filterHiddenFields($project, &$fields) {
$new = [];
$excluded = ["requester_lookup", "pi_lookup", "one_lookup"];
foreach($fields as $k => $v) {
if($project->metadata[$k] && (str_contains($project->metadata[$k]['misc'],'HIDDEN') || in_array($project->metadata[$k], $excluded))) { // Hidden fields should not be shown to client
unset($fields[$k]);
} else {
$label = trim($project->metadata[$k]['element_label']);
if(!empty($label)){
$new[$label] = $v;
}
}
}
return $new;
}
/**
* @param $universalId
* @return false|string
*/
public function fetchRequiredSurveys($payload)
{
try {
if (empty($payload['uid']))
throw new \Exception("No Universal ID passed to fetchRequiredSurveys");
$parentId = $this->getSystemSetting('parent-project');
$project = new \Project($parentId);
$projectSettings = $this->getProjectSettings($parentId);
$completedIntake = $this->fetchParentRecordData($parentId, $payload['uid'], $projectSettings['universal-survey-event'], $projectSettings['universal-survey-form-immutable']);
$mutableIntake = $this->fetchParentRecordData($parentId, $payload['uid'], $projectSettings['universal-survey-event'], $projectSettings['universal-survey-form-mutable']);
$requiredChildPIDs = $this->getRequiredChildPIDs($completedIntake, $projectSettings);
$pretty = $completedIntake[0];
$pretty = $this->filterHiddenFields($project, $pretty);
$childSurveys = $project->surveys;
$mutableUrl = [];
foreach($childSurveys as $id => $survey) {
$childEventId = $this->getChildEventId($project, $survey['form_name']);
if($survey['form_name'] === $projectSettings['universal-survey-form-mutable'])
$mutableUrl = REDCap::getSurveyLink(reset($completedIntake)['record_id'], $survey['form_name'], $childEventId, 1, $parentId);
}
//Grab survey completion timestamp
$survey_id = $project->forms[$projectSettings['universal-survey-form-immutable']]['survey_id'];
// Reduce array
$completedIntake = reset($completedIntake);
$mutableIntake = reset($mutableIntake);
//Manually add timestamp if completed
$completedIntake['completion_ts'] = Survey::isResponseCompleted($survey_id, $payload['uid'], $projectSettings['universal-survey-event'], 1, true);
//Manually add agnostic completed variable if form changes for frontend logic
$completedIntake['complete'] = $completedIntake[$projectSettings['universal-survey-form-immutable'] . '_complete'];
unset($completedIntake[$projectSettings['universal-survey-form-immutable'] . '_completed']);
$mutableIntake['complete'] = $mutableIntake[$projectSettings['universal-survey-form-mutable'] . '_complete'];
unset($mutableIntake[$projectSettings['universal-survey-form-mutable'] . '_completed']);
return json_encode([
"surveys" => $this->generateSurveyTitles($payload['uid'], $requiredChildPIDs),
"completed_form_immutable" => $completedIntake,
"completed_form_mutable" => $mutableIntake,
"completed_form_pretty" => $pretty,
"mutable_url" => $mutableUrl,
"success" => true
]);
} catch (\Exception $e) {
return $this->handleGlobalError($e);
}
}
/**
* @param $parentId
* @param $universalId
* @return mixed
*/
private function fetchParentRecordData($parentId, $universalId, $event, $form = 'intake')
{
$formFields = json_decode(REDCap::getDataDictionary($parentId, 'json', true, null, $form), true);
$fields = [];
foreach($formFields as $field)
$fields[] = $field['field_name'];
$fields[] = $form . '_complete';
$detailsParams = [
"return_format" => "json",
"project_id" => $parentId,
"records" => $universalId,
"fields" => $fields,
"events" => $event
];
return json_decode(REDCap::getData($detailsParams), true);
}
/**
* @param $completedIntake
* @param $projectSettings
* @return array
*/
private function getRequiredChildPIDs($completedIntake, $projectSettings)
{
$requiredChildPIDs = [];
$firstRecord = reset($completedIntake);
foreach($projectSettings['project-id'] as $pid)
$requiredChildPIDs[] = $pid;
// foreach ($firstRecord as $key => $value) {
// if ($this->isValidServiceKey($key, $value)) {
// $mappingKey = array_search($key, $projectSettings['mapping-field']);
// if ($mappingKey !== false) {
// $requiredChildPIDs[] = $projectSettings['project-id'][$mappingKey];
// }
// }
// }
return $requiredChildPIDs;
}
/**
* @param $key
* @param $value
* @return bool
*/
private function isValidServiceKey($key, $value)
{
return preg_match('/^serv_map_\d+$/', $key) && $value === "1";
}
//Creates the project-specific survey titles for a given array of Child PIDs
private function generateSurveyTitles($universalId, $requiredChildPIDs): array
{
$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);
$titles = [];
foreach($requiredChildPIDs as $childProjectId) {
$child = new Child($this, $childProjectId, $parent_id, $pSettings);
$titles = array_merge($titles, $child->getSurveyTitle());
}
return $titles;
}
private function newChildRequest($payload){
try {
if(empty($payload['child_id']) || empty($payload['universal_id']))
throw new \Exception("Missing child ID or Universal ID");
$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);
$child = new Child($this, $payload['child_id'], $parent_id, $pSettings);
$url = $child->getNewSurveyUrl($payload['universal_id']);
return json_encode(["url" => $url, "success" => true]);
} catch (\Exception $e) {
$this->handleGlobalError($e);
}
}
private function getChildSubmissions($payload){
try {
if(empty($payload['child_id']) || empty($payload['universal_id']))
throw new \Exception("Missing child ID or Universal ID");
$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);
$child = new Child($this, $payload['child_id'], $parent_id, $pSettings);
$submissions = $child->childRecordExists($payload['universal_id']);
// Grab all field names for first two required surveys
$mainSurvey = $child->getMainSurveyFormName();
//Iterate over all submissions to determine if we need to render a return link
foreach($submissions as $in => $submission){
$completionVariable = $mainSurvey . "_complete";
if(isset($submission[$completionVariable]) && $submission[$completionVariable] !== "2"){
$submissions[$in]['survey_url'] = $child->getSurveyLink($submission['record_id']);
}
// Grab timestamp for dashboard display
$submissions[$in]['survey_completion_ts'] = $child->getSurveyTimestamp($submission['record_id']);
// Set default field to make it easier to render completion status on dashboard (form name agnostic)
$submissions[$in]['child_survey_complete'] = $submission[$completionVariable];
}
return json_encode(["data" => $submissions, "success" => true]);
} catch (\Exception $e) {
$this->handleGlobalError($e);
}
}
/**
* @param $universalId
* @param $requiredChildPIDs
* @return array
* @throws Exception
*/
private function generateSurveyLinks($universalId, $requiredChildPIDs)
{
$surveyLinks = [];
foreach ($requiredChildPIDs as $childProjectId) {
$item = [];
$project = new \Project($childProjectId);
$childInstrument = $this->getChildInstrument($project);
$childEventId = $this->getChildEventId($project, $childInstrument);
$check = $this->checkChildDataExists($universalId, $childProjectId);
// if (empty($check)) {
$recordId = $this->preCreateChildRecord($childProjectId, $universalId);
$item['url'] = REDCap::getSurveyLink($recordId, $childInstrument, $childEventId, 1, $childProjectId);
// } else {
// $item['url'] = REDCap::getSurveyLink($check['record_id'], $childInstrument, $childEventId, 1, $childProjectId);
// }
// $item['complete'] = $check[$childInstrument . '_complete'];
// $item['child_pid'] = $childProjectId;
$item['form_name'] = reset($project->surveys)['form_name'];
$item['title'] = reset($project->surveys)['title'];
$surveyLinks[] = $item;
}
return $surveyLinks;
}
/**
* @param $project
* @return mixed|string
*/
private function getChildInstrument($project)
{
$surveyInfo = $project->surveys;
return reset($surveyInfo)['form_name']; // Assumes a single survey per child project
}
/**
* @param $project
* @param $childInstrument
* @return int|string|null
*/
private function getChildEventId($project, $childInstrument)
{
foreach ($project->eventsForms as $eventId => $forms) {
if (in_array($childInstrument, $forms, true)) {
return $eventId;
}
}
return null;
}
private function preCreateChildRecord($childProjectId, $universalId)
{
$saveData = [
["record_id" => $universalId],
];
$response = REDCap::saveData($childProjectId, 'json', json_encode($saveData), 'overwrite');
if (!empty($response['errors'])) {
$errorDetails = json_encode($response['errors']);
throw new \Exception("Error in pre-creation save data call: $errorDetails");
}
return array_key_first($response['ids']);
}
}