generated from susom/redcap-em-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOnCoreIntegration.php
1905 lines (1681 loc) · 79.7 KB
/
OnCoreIntegration.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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Stanford\OnCoreIntegration;
require_once "emLoggerTrait.php";
require_once 'classes/Users.php';
require_once 'classes/Entities.php';
require_once 'classes/Protocols.php';
require_once 'classes/Subjects.php';
require_once 'classes/Mapping.php';
/**
* Class OnCoreIntegration
* @package Stanford\OnCoreIntegration
* @property \Stanford\OnCoreIntegration\Users $users;
* @property \Stanford\OnCoreIntegration\Protocols $protocols;
*/
class OnCoreIntegration extends \ExternalModules\AbstractExternalModule
{
use emLoggerTrait;
const EXCLUDE_SUBJECTS = 'excluded_subjects';
const FIELD_MAPPINGS = 'oncore_field_mappings';
const ONCORE_PROTOCOLS = 'oncore_protocols';
const REDCAP_ENTITY_ONCORE_PROTOCOLS = 'redcap_entity_oncore_protocols';
const ONCORE_REDCAP_API_ACTIONS_LOG = 'oncore_redcap_api_actions_log';
const REDCAP_ENTITY_ONCORE_REDCAP_API_ACTIONS_LOG = 'redcap_entity_oncore_redcap_api_actions_log';
const ONCORE_ADMINS = 'oncore_admins';
const REDCAP_ENTITY_ONCORE_ADMINS = 'redcap_entity_oncore_admins';
const ONCORE_SUBJECTS = 'oncore_subjects';
const REDCAP_ENTITY_ONCORE_SUBJECTS = 'redcap_entity_oncore_subjects';
const ONCORE_REDCAP_RECORD_LINKAGE = 'oncore_redcap_records_linkage';
const REDCAP_ENTITY_ONCORE_REDCAP_RECORD_LINKAGE = 'redcap_entity_oncore_redcap_records_linkage';
const REDCAP_ONLY = 0;
const ONCORE_ONLY = 1;
const FULL_MATCH = 2;
const PARTIAL_MATCH = 3;
const REDCAP_ONCORE_FIELDS_MAPPING_NAME = 'redcap-oncore-fields-mapping';
const REDCAP_ONCORE_PROJECT_SITE_STUDIES = 'redcap-oncore-project-site-studies';
const REDCAP_ONCORE_PROJECT_ONCORE_SUBSET = 'redcap-oncore-project-oncore-subset';
const REDCAP_ONCORE_PROJECT_PUSHPULL_PREF = 'redcap-oncore-project-pushpull-pref';
const ONCORE_PROTOCOL_STATUS_NO = 0;
const ONCORE_PROTOCOL_STATUS_PENDING = 1;
const ONCORE_PROTOCOL_STATUS_YES = 2;
const ONCORE_CONSENT_FILTER_LOGIC = 'redcap-oncore-consent-filter-logic';
const ONCORE_BIRTHDATE_FIELD = 'birthDate';
const ONCORE_BIRTHDATE_NOT_REQUIRED_FIELD = 'birthDateNotAvailable';
const ONCORE_STUDY_SITE = 'studySites';
const YES = 1;
const NO = 0;
const ONCORE_SUBJECT_ON_STUDY = 1;
const ONCORE_SUBJECT_OFF_STUDY = 0;
const ONCORE_SUBJECT_SOURCE_TYPE_ONCORE = 'OnCore';
const ONCORE_SUBJECT_SOURCE_TYPE_ONSTAGE = 'Onstage';
const SUBJECTS_MYSQL_LOCK = 'SUBJECTS_LOCK';
public static $ONCORE_DEMOGRAPHICS_FIELDS = array(
"subjectDemographicsId",
"mrn",
"gender",
"ethnicity",
"race",
"birthDate",
"lastName",
"firstName",
"middleName",
"suffix",
"approximateBirthDate",
"birthDateNotAvailable",
"expiredDate",
"approximateExpiredDate",
"lastDateKnownAlive",
"ssn",
"subjectComments",
"additionalSubjectIds",
"streetAddress",
"addressLine2",
"city",
"state",
"zip",
"county",
"country",
"phoneNo",
"alternatePhoneNo",
"email");
public static $ONCORE_DEMOGRAPHICS_REQUIRED_FIELDS = array(
"mrn",
"gender",
"ethnicity",
"race",
"birthDate",
"lastName",
"firstName",
);
/**
* @var Users
*/
private $users;
/**
* @var Protocols
*/
private $protocols;
/**
* @var Mapping
*/
private $mapping;
private $oncore_protocols;
private $oncore_integrations;
private $has_oncore_integrations;
public function redcap_save_record($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance)
{
try {
$protocol = Protocols::getOnCoreProtocolEntityRecord($project_id);
if (!empty($protocol)) {
if ($protocol['status'] == OnCoreIntegration::ONCORE_PROTOCOL_STATUS_YES) {
$this->initiateProtocol();
$this->getProtocols()->syncIndividualRecord($record);
}
}
} catch (\Exception $e) {
Entities::createException($e->getMessage());
// $this->emError($e->getMessage());
}
}
public function redcap_module_project_enable($version, $project_id)
{
// disabled because user has to pick protocol from project setup page.
// global $Proj;
// if ($Proj->project['project_irb_number']) {
// $this->getProtocols()->processCron($this->getProjectId(), $Proj->project['project_irb_number'], $this->getDefinedLibraries());
// }
}
public function redcap_module_system_enable($version)
{
$enabled = $this->isModuleEnabled('redcap_entity');
if (!$enabled) {
// TODO: what to do when redcap_entity is not enabled in the system
Entities::createException("Cannot use this module OncoreIntegration because it is dependent on the REDCap Entities EM");
// $this->emError("Cannot use this module OncoreIntegration because it is dependent on the REDCap Entities EM");
} else {
\REDCapEntity\EntityDB::buildSchema($this->PREFIX);
Entities::createLog("Created all OnCore Entity tables");
// $this->emDebug("Created all OnCore Entity tables");
}
}
public function initiateProtocol()
{
try {
if (!$this->users) {
$this->setUsers(new Users($this->getProjectId(), $this->PREFIX, $this->framework->getUser() ?: null, $this->getCSRFToken()));
}
} catch (\LengthException $e) {
\REDCap::logEvent('Empty Response', 'OnCore is on maintenance. ' . $e->getMessage());
} catch (\Exception $e) {
// this is a special case for cron no redcap user.
$this->setUsers(new Users($this->getProjectId(), $this->PREFIX, null, $this->getCSRFToken()));
}
// When OnCore in maintenance mode Users object will be empty.
if ($this->users && !$this->protocols) {
$this->setProtocols(new Protocols($this->getUsers(), $this->getMapping(), $this->getProjectId()));
global $Proj;
// only load Protocol library and subject only if IRB is available.
$irb = $Proj->project['project_irb_number'];
if ($irb) {
// after protocol is init find its OnCore library and load it.
$this->setProtocolLibrary();
// update Subject can push flag.
if ($this->getProtocols()->getEntityRecord()) {
$this->getProtocols()->getSubjects()->setCanPush($this->getProtocols()->canPushToProtocol());
}
}
}
}
public function redcap_every_page_top($project_id)
{
try {
// in case we are loading record homepage load its the record children if existed
preg_match('/redcap_v[\d\.].*\/index\.php/m', $_SERVER['SCRIPT_NAME'], $matches, PREG_OFFSET_CAPTURE);
if (strpos($_SERVER['SCRIPT_NAME'], 'ProjectSetup') !== false || !empty($matches)) {
//TODO MAY NEED TO MOVE PROTOCOL INITIATION TO __construct
$this->initiateProtocol();
//TODO ANY MORE PERFORMANT WAY TO DO THIS THAN HITTING IT EVERY ProjectSetup page?
$sql = sprintf("SELECT project_irb_number from redcap_projects WHERE project_id = %s ", db_escape($project_id));
$record = db_query($sql);
if ($record->num_rows) {
$r = db_fetch_assoc($record);
$project_irb = $r["project_irb_number"];
$this->injectIntegrationUI($project_irb);
}
}
} catch (\Exception $e) {
$this->emError('Exception initiating OnCore Protocol.', $e->getMessage());
}
}
public static function nestedLowercase($array)
{
$result = array();
foreach ($array as $key => $item) {
if (is_array($item)) {
return self::nestedLowercase($item);
} else {
$result[$key] = strtolower($item);
}
}
return $result;
}
public function redcap_entity_types()
{
$types = [];
$types[self::ONCORE_PROTOCOLS] = [
'label' => 'OnCore protocols',
'label_plural' => 'OnCore Protocols',
'icon' => 'home_pencil',
'properties' => [
'redcap_project_id' => [
'name' => 'REDCap Project',
'type' => 'project',
'required' => true,
],
'irb_number' => [
'name' => 'IRB Number',
'type' => 'text',
'required' => false,
],
'oncore_protocol_id' => [
'name' => 'OnCore Protocol ID',
'type' => 'integer',
'required' => true,
],
'last_date_scanned' => [
'name' => 'Timestamp of last scan',
'type' => 'date',
'required' => true,
],
'redcap_event_id' => [
'name' => 'REDCap Event Id',
'type' => 'integer',
'required' => true,
],
'oncore_library' => [
'name' => 'OnCore Protocol Library',
'type' => 'integer',
'default' => 0,
'required' => true,
],
'status' => [
'name' => 'Linkage Status',
'type' => 'integer',
'required' => true,
'default' => 0,
'choices' => [
self::ONCORE_PROTOCOL_STATUS_NO => 'No',
self::ONCORE_PROTOCOL_STATUS_PENDING => 'Pending',
self::ONCORE_PROTOCOL_STATUS_YES => 'Yes',
],
],
],
'special_keys' => [
'label' => 'redcap_project_id', // "name" represents the entity label.
],
];
$types[self::ONCORE_REDCAP_API_ACTIONS_LOG] = [
'label' => 'OnCore protocols',
'label_plural' => 'OnCore Protocols',
'icon' => 'home_pencil',
'properties' => [
'message' => [
'name' => 'Action Body',
'type' => 'text',
'required' => false,
],
'redcap_project_id' => [
'name' => 'Project',
'type' => 'project',
'required' => false,
],
'url' => [
'name' => 'Called URL',
'type' => 'text',
'required' => false,
],
'response' => [
'name' => 'OnCore API Response',
'type' => 'text',
'required' => false,
],
'type' => [
'name' => 'Action Type',
'type' => 'integer',
'required' => true,
'default' => 0,
],
],
'special_keys' => [
'label' => 'message', // "name" represents the entity label.
],
];;
$types[self::ONCORE_ADMINS] = [
'label' => 'OnCore Admin',
'label_plural' => 'OnCore Admins',
'icon' => 'home_pencil',
'properties' => [
'oncore_contact_id' => [
'name' => 'OnCore Contact Id',
'type' => 'text',
'required' => true,
],
'oncore_additional_identifier' => [
'name' => 'OnCore Contact Staff ID(REDCap username)',
'type' => 'text',
'required' => true,
],
'oncore_email' => [
'name' => 'OnCore Contact Staff Email',
'type' => 'text',
'required' => true,
],
'oncore_role' => [
'name' => 'OnCore Role',
'type' => 'text',
'required' => true,
],
'oncore_stop_date' => [
'name' => 'OnCore Contact Stop Date',
'type' => 'text',
'required' => false,
],
],
'special_keys' => [
'label' => 'redcap_username', // "name" represents the entity label.
],
];
$types[self::ONCORE_REDCAP_RECORD_LINKAGE] = [
'label' => 'OnCore REDCap records Linkage',
'label_plural' => 'OnCore REDCap records Linkage',
'icon' => 'home_pencil',
'properties' => [
'redcap_project_id' => [
'name' => 'REDCap project Id',
'type' => 'text',
'required' => false,
],
'oncore_protocol_id' => [
'name' => 'OnCore Protocol Id',
'type' => 'text',
'required' => false,
],
'redcap_record_id' => [
'name' => 'REDCap record Id',
'type' => 'text',
'required' => false,
],
'oncore_protocol_subject_id' => [
'name' => 'OnCore Protocol Subject Id(NOT Demographics)',
'type' => 'integer',
'required' => false,
],
'oncore_protocol_subject_status' => [
'name' => 'OnCore Protocol Subject Status (On/Off Study)',
'type' => 'integer',
'required' => false,
'default' => self::ONCORE_SUBJECT_ON_STUDY,
'choices' => [
self::ONCORE_SUBJECT_ON_STUDY => 'ON STUDY',
self::ONCORE_SUBJECT_OFF_STUDY => 'OFF STUDY'
],
],
'status' => [
'name' => 'Linkage Status',
'type' => 'integer',
'required' => true,
'default' => self::REDCAP_ONLY,
'choices' => [
self::REDCAP_ONLY => 'REDCAP_ONLY',
self::ONCORE_ONLY => 'ONCORE_ONLY',
self::FULL_MATCH => 'FULL_MATCH',
self::PARTIAL_MATCH => 'PARTIAL_MATCH',
],
],
'excluded' => [
'name' => 'Is Record Excluded?',
'type' => 'integer',
'required' => true,
'default' => self::NO,
'choices' => [
self::NO => 'No',
self::YES => 'Yes',
],
],
],
'special_keys' => [
'label' => 'redcap_project_id', // "name" represents the entity label.
],
];;
$types[self::ONCORE_SUBJECTS] = [
'label' => 'OnCore Subject',
'label_plural' => 'OnCore Subjects',
'icon' => 'home_pencil',
'properties' => [
'subjectDemographicsId' => [
'name' => 'OnCore Subject Demographics Id',
'type' => 'integer',
'required' => false,
],
'subjectSource' => [
'name' => 'Subject Source',
'type' => 'text',
'required' => false,
],
'mrn' => [
'name' => 'OnCore MRN',
'type' => 'text',
'required' => false,
],
'lastName' => [
'name' => 'OnCore Lastname',
'type' => 'text',
'required' => false,
],
'firstName' => [
'name' => 'OnCore Firstname',
'type' => 'text',
'required' => false,
],
'middleName' => [
'name' => 'OnCore middle Name',
'type' => 'text',
'required' => false,
],
'suffix' => [
'name' => 'OnCore suffix',
'type' => 'text',
'required' => false,
],
'birthDate' => [
'name' => 'OnCore Date of Birth',
'type' => 'text',
'required' => false,
],
'approximateBirthDate' => [
'name' => 'OnCore Approximate Date of Birth',
'type' => 'boolean',
'required' => false,
],
'birthDateNotAvailable' => [
'name' => 'OnCore Date of Birth not available',
'type' => 'boolean',
'required' => false,
],
'expiredDate' => [
'name' => 'OnCore Expired Date',
'type' => 'text',
'required' => false,
],
'approximateExpiredDate' => [
'name' => 'OnCore Approximate Expired Date',
'type' => 'boolean',
'required' => false,
],
'lastDateKnownAlive' => [
'name' => 'OnCore Last date known alive',
'type' => 'text',
'required' => false,
],
'ssn' => [
'name' => 'OnCore SSN',
'type' => 'text',
'required' => false,
],
'gender' => [
'name' => 'OnCore Gender',
'type' => 'text',
'required' => false,
],
'ethnicity' => [
'name' => 'OnCore Ethnicity',
'type' => 'text',
'required' => false,
],
'race' => [
'name' => 'OnCore Race',
'type' => 'json',
'required' => false,
],
'subjectComments' => [
'name' => 'OnCore SubjectComments',
'type' => 'text',
'required' => false,
],
'additionalSubjectIds' => [
'name' => 'OnCore Additional Subject Ids',
'type' => 'json',
'required' => false,
],
'streetAddress' => [
'name' => 'OnCore street Address',
'type' => 'text',
'required' => false,
],
'addressLine2' => [
'name' => 'OnCore address Line2',
'type' => 'text',
'required' => false,
],
'city' => [
'name' => 'OnCore City',
'type' => 'text',
'required' => false,
],
'state' => [
'name' => 'OnCore State',
'type' => 'text',
'required' => false,
],
'zip' => [
'name' => 'OnCore Zip Code',
'type' => 'text',
'required' => false,
],
'county' => [
'name' => 'OnCore County',
'type' => 'text',
'required' => false,
],
'country' => [
'name' => 'OnCore Country',
'type' => 'text',
'required' => false,
],
'phoneNo' => [
'name' => 'OnCore Phone No',
'type' => 'text',
'required' => false,
],
'alternatePhoneNo' => [
'name' => 'OnCore Alternate Phone No',
'type' => 'text',
'required' => false,
],
'email' => [
'name' => 'OnCore Email',
'type' => 'text',
'required' => false,
]
],
'special_keys' => [
'label' => 'subject_demographics_id', // "name" represents the entity label.
],
];;
// TODO redcap entity for redcap records not in OnCore
// TODO redcap entity for OnCore records not in REDCap
// TODO redcap entity to save the linkage between redcap and OnCore records
return $types;
}
//ONCORE INTEGRATION/STATUS METHODS
public function injectIntegrationUI($project_irb)
{
$field_map_url = $this->getUrl("pages/field_map.php");
$integration_jsmo = $this->getUrl("assets/scripts/integration_jsmo.js");
$notif_js = $this->getUrl("assets/scripts/notif_modal.js");
$oncore_js = $this->getUrl("assets/scripts/oncore.js");
$notif_css = $this->getUrl("assets/styles/notif_modal.css");
$oncore_css = $this->getUrl("assets/styles/oncore.css");
$protocols = $this->getProtocols()->getOnCoreProtocolsViaIRB($project_irb);
$displaySummary = true;
// edge case if redcap project irb number is changed. we need to check entity record to confirm.
if (empty($protocols)) {
$entity = $this->getProtocols()->getEntityRecord();
// entity number has changed.
if (!empty($entity) && $entity['irb_number'] != $project_irb) {
// delete all linkage and protocol records
$this->deleteLinkageRecords($this->getProtocols()->getEntityRecord()['redcap_project_id'], $this->getProtocols()->getEntityRecord()['oncore_protocol_id']);
$this->deleteProtocolRecord($this->getProtocols()->getEntityRecord()['redcap_project_id'], $this->getProtocols()->getEntityRecord()['oncore_protocol_id']);
// prevent summary from being displayed
$displaySummary = false;
}
}
if ($displaySummary) {
$integrations = $this->getOnCoreIntegrations();
$last_adjudication = $this->getSyncDiffSummary();
//if no integrations, and only one protocol, pre-pull the protocol into the entity table
$matching_library = false;
if (is_array($protocols) && count($protocols) == 1 && empty($integrations)) {
$protocol = current($protocols);
$protocolId = $protocol["protocolId"];
$library = $protocol["protocol"]["library"];
$lib_names = array();
foreach ($this->getDefinedLibraries() as $lib) {
array_push($lib_names, $lib["library-name"]);
}
// $this->emDebug($protocol);
if (in_array($library, $lib_names)) {
$matching_library = true;
$new_entity_record = $this->getProtocols()->processCron($this->getProjectId(), $project_irb, $protocolId, $this->getDefinedLibraries());
$integrations[$protocolId] = $new_entity_record;
}
}
//DATA TO INIT JSMO module
$notifs_config = array(
"field_map_url" => $field_map_url,
"oncore_protocols" => $protocols,
"oncore_integrations" => $integrations,
"has_oncore_integration" => $this->hasOnCoreIntegration(),
"has_field_mappings" => !empty($this->getMapping()->getProjectFieldMappings()['pull']) || !empty($this->getMapping()->getProjectFieldMappings()['push']) ? true : false,
"last_adjudication" => $last_adjudication,
"matching_library" => $matching_library,
"alert_notifications" => $this->getSystemSetting('display-alert-notification') ? htmlspecialchars($this->getSystemSetting('alert-notification')) : "",
"disable_functionality" => $this->getSystemSetting('disable-functionality')
);
//Initialize JSMO
$this->initializeJavascriptModuleObject();
?>
<script src="<?= $oncore_js ?>" type="text/javascript"></script>
<script src="<?= $notif_js ?>" type="text/javascript"></script>
<script src="<?= $integration_jsmo ?>" type="text/javascript"></script>
<script>
$(function () {
const module = <?=$this->getJavascriptModuleObjectName()?>;
module.config = decode_object("<?=$this->escape(json_encode($notifs_config))?>");
console.log(module.config)
module.afterRender(<?=$this->getJavascriptModuleObjectName()?>.InitFunction);
})
</script>
<link rel="stylesheet" href="<?= $oncore_css ?>">
<link rel="stylesheet" href="<?= $notif_css ?>">
<?php
}
}
/**
* @return null
*/
public function integrateOnCoreProject($entityId, $integrate = false)
{
$this->initiateProtocol();
$setStatus = $integrate ? self::ONCORE_PROTOCOL_STATUS_YES : self::ONCORE_PROTOCOL_STATUS_NO;
$this->getProtocols()->updateProtocolEntityRecordStatus($entityId, $setStatus);
// if user is unlinking protocol delete all linkage records.
if ($setStatus == self::ONCORE_PROTOCOL_STATUS_NO) {
$this->deleteLinkageRecords($this->getProtocols()->getEntityRecord()['redcap_project_id'], $this->getProtocols()->getEntityRecord()['oncore_protocol_id']);
}
return $integrate;
}
/**
* @return array
*
*/
public function getOnCoreProtocols(): array
{
if (!$this->oncore_protocols) {
$this->initiateProtocol();
$available_oncore_protocols = $this->getProtocols()->getOnCoreProtocol();
$this->oncore_protocols = array();
if (!empty($available_oncore_protocols)) {
if ($this->isAssoc($available_oncore_protocols)) {
$available_oncore_protocols = array($available_oncore_protocols);
}
}
foreach ($available_oncore_protocols as $oncore_project) {
$this->oncore_protocols[$oncore_project["protocolId"]] = $oncore_project;
}
}
return $this->oncore_protocols;
}
/**
* @return array
*/
public function getOnCoreIntegrations()
{
if (!$this->oncore_integrations) {
$this->initiateProtocol();
$entity_records = $this->getProtocols()->getEntityRecord();
$this->oncore_integrations = array();
if (!empty($entity_records)) {
if ($this->isAssoc($entity_records)) {
$entity_records = array($entity_records);
}
}
foreach ($entity_records as $oncore_integration) {
$this->oncore_integrations[$oncore_integration["oncore_protocol_id"]] = $oncore_integration;
}
//Instantiate Mapping Class
$this->setMapping(new Mapping($this));
}
return $this->oncore_integrations;
}
/**
* @return bool
*/
public function hasOnCoreIntegration()
{
if (!$this->has_oncore_integrations) {
$this->has_oncore_integrations = false;
$current_oncore_integrations = $this->getOnCoreIntegrations();
foreach ($current_oncore_integrations as $protocol_id => $oncore_integration) {
if ($oncore_integration["status"] == 2) {
$this->has_oncore_integrations = true;
break;
}
}
}
return $this->has_oncore_integrations;
}
/**
* @return protocol array
*/
public function getIntegratedProtocol()
{
$protocol = null;
if ($this->hasOnCoreIntegration()) {
$integrations = $this->getOnCoreIntegrations();
foreach ($integrations as $protocol_id => $integration) {
if ($integration["status"] == 2) {
$project_irb = $integration["irb_number"];
$protocol_id = $integration["oncore_protocol_id"];
$protocols = $this->getProtocols()->getOnCoreProtocolsViaIRB($project_irb);
foreach ($protocols as $p) {
if ($p["protocolId"] == $protocol_id) {
$protocol = $p;
break;
}
}
break;
}
}
}
return $protocol;
}
/**
* @return date time Y-m-d H:i
*/
public function formatTS($ts)
{
return date("Y-m-d H:i", $ts);
}
//DATA SYNC METHODS
/**
* @return fields_event array of redcap project fields and thier respective event id
*/
public function redcapFieldEventIDMap()
{
$fields_event = array();
if (\REDCap::isLongitudinal()) {
$events = \REDCap::getEventNames(true);
} else {
$events = array($this->getFirstEventId() => $this->getFirstEventId());
}
if (!empty($events)) {
foreach ($events as $event_id => $event) {
$temp = \REDCap::getValidFieldsByEvents(PROJECT_ID, array($event));
foreach ($temp as $field_name) {
$fields_event[$field_name] = $event_id;
}
}
}
return $fields_event;
}
/**
* @return sync_diff
*/
public function getSyncDiff($use_filter = null)
{
$this->initiateProtocol();
//$this->getProtocols()->getSubjects()->setSyncedRecords($this->getProtocols()->getEntityRecord()['redcap_project_id'], $this->getProtocols()->getEntityRecord()['oncore_protocol_id']);
//THIS MA
$fields_event = $this->redcapFieldEventIDMap();
$records = $this->getProtocols()->getSyncedRecords($use_filter);
$mapped_fields = $this->getMapping()->getProjectFieldMappings();
$sync_diff = array();
$bin_match = array("excluded" => array(), "included" => array());
$bin_partial = array("excluded" => array(), "included" => array());
$bin_oncore = array("excluded" => array(), "included" => array());
$bin_redcap = array("excluded" => array(), "included" => array());
$bin_array = array("bin_redcap", "bin_oncore", "bin_match", "bin_partial");
$exclude = array("mrn");
foreach ($records as $record) {
$link_status = $record["status"];
$entity_id = $record["entity_id"];
$excluded = $record["excluded"] ?? 0;
$oncore = null;
$redcap = null;
$last_scan = null;
$full = false;
$oc_id = null;
$oc_pr_id = null;
$rc_id = null;
$oc_data = null;
$rc_data = null;
$rc_field = null;
$rc_event = null;
$oc_status = null;
switch ($link_status) {
case OnCoreIntegration::FULL_MATCH:
//full
$full = true;
case OnCoreIntegration::PARTIAL_MATCH:
case OnCoreIntegration::ONCORE_ONLY:
//oncore only
$oncore = $record["oncore"];
$oc_id = $oncore["protocolId"];
$oc_pr_id = $oncore["protocolSubjectId"];
$oc_status = $oncore['status'];
$mrn = $oncore["demographics"]["mrn"];
$last_scan = date("Y-m-d H:i", $oncore["demographics"]["updated"]);
case OnCoreIntegration::REDCAP_ONLY:
//redcap only
if (array_key_exists("redcap", $record)) {
// set the keys for redcap array
$arr = $record["redcap"];
$mrn_event_id = $fields_event[$this->getMapping()->getProjectFieldMappings()['push']['mrn']['redcap_field']];
$mrn = $arr[$mrn_event_id][$this->getMapping()->getProjectFieldMappings()['push']['mrn']['redcap_field']];
$primary_field_event_id = $fields_event[\REDCap::getRecordIdField()];
$rc_id = $arr[$primary_field_event_id][\REDCap::getRecordIdField()];
// edge case if redcap record is missing try pull it from main array.
if (!$rc_id or $rc_id == '') {
$rc_id = $record[\REDCap::getRecordIdField()];
}
// foreach ($record['redcap'] as $key => $value) {
// Entities::createLog('---');
// Entities::createLog('Key: ' . $key);
// if (is_array($value)) {
// Entities::createLog(implode(',', $value));
// } else {
// Entities::createLog($value);
// }
//
// Entities::createLog('---');
// }
// we are using pull fields to map redcap data
$temp = $this->getProtocols()->getSubjects()->prepareREDCapRecordForSync($rc_id, $this->getMapping()->getProjectFieldMappings()['push'] ?: $this->getMapping()->getProjectFieldMappings()['pull'], $this->getMapping()->getOnCoreFieldDefinitions());
// handle data scattered over multiple events
$redcap = [];
foreach ($temp as $onCoreField => $value) {
// Use redcap fields name instead of oncore to work with Irvin UI.
$redcapField = $this->getMapping()->getMappedRedcapField($onCoreField, true);
if ($redcapField) {
$redcap[$redcapField] = $value;
}
}
}
default:
//partial
$mrn = $mrn . '_' . $entity_id ?: rand(1000, 9999);
$bin_var = $bin_array[$link_status];
$bin = $excluded ? $$bin_var["excluded"] : $$bin_var["included"];
if (!array_key_exists($mrn, is_array($bin) ? $bin : [])) {
if ($excluded) {
$$bin_var["excluded"][$mrn] = array();
} else {
$$bin_var["included"][$mrn] = array();
}
}
$fields = $link_status == OnCoreIntegration::REDCAP_ONLY ? $mapped_fields["push"] : $mapped_fields["pull"];
foreach ($fields as $oncore_field => $redcap_details) {
$rc_field = $redcap_details["redcap_field"];
$rc_event = $redcap_details["event"];
$rc_data = $redcap && isset($redcap[$redcap_details["redcap_field"]]) ? $redcap[$redcap_details["redcap_field"]] : null;
$oc_data = $oncore && isset($oncore["demographics"][$oncore_field]) ? $oncore["demographics"][$oncore_field] : (isset($oncore[$oncore_field]) ? $oncore[$oncore_field] : null);
if (empty($rc_field) && !empty($redcap_details["default_value"])) {
$rc_data = $redcap_details["default_value"];
}
$temp = array(
"entity_id" => $entity_id
, "ts_last_scan" => $last_scan
, "oc_id" => $oc_id
, "oc_status" => $oc_status
, "oc_pr_id" => $oc_pr_id
, "rc_id" => $rc_id
, "oc_data" => $oc_data
, "rc_data" => $rc_data
, "oc_field" => $oncore_field
, "rc_field" => $rc_field
, "rc_event" => $rc_event
, "full" => $full
);
if ($excluded) {
array_push($$bin_var["excluded"][$mrn], $temp);
} else {
array_push($$bin_var["included"][$mrn], $temp);
}
}
}
if (!empty($bin_match) || !empty($bin_oncore) || !empty($bin_redcap) || !empty($bin_partial)) {
$sync_diff = array(
"match" => $bin_match,
"oncore" => $bin_oncore,
"redcap" => $bin_redcap,
"partial" => $bin_partial
);
}
}
// $this->emDebug("sync diff redcap", $bin_redcap);
return $sync_diff;
}
/**
* @return array
*/
public function getSyncDiffSummary()
{
$last_adjudication = $this->getProtocols()->getSyncedRecordsSummaries();
return $last_adjudication;
}
/**