-
Notifications
You must be signed in to change notification settings - Fork 7
/
CrossprojectpipingExternalModule.php
executable file
·1294 lines (1133 loc) · 46.8 KB
/
CrossprojectpipingExternalModule.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 Vanderbilt\CrossprojectpipingExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
require_once dirname(__FILE__) . '/hooks_common.php';
class CrossprojectpipingExternalModule extends AbstractExternalModule
{
public $pipingMode;
public $pipeOnStatus;
public $modSettings;
public $hideButton = false;
function redcap_every_page_before_render($project_id) {
$user_is_at_record_status_dashboard = $_SERVER['SCRIPT_NAME'] == APP_PATH_WEBROOT . "DataEntry/record_status_dashboard.php";
$pipe_all_records_button_configured = $this->getProjectSetting('piping-all-records-button');
if ($user_is_at_record_status_dashboard && $pipe_all_records_button_configured) {
// add 'Pipe All Records' button to record status dashboard screen (should appear next to the '+Add New Record' button
$pipe_all_records_ajax_url = $this->getUrl('php/pipe_all_data_ajax.php');
$css_url = $this->getUrl('css/pipe_all_data_ajax.css');
$javascript_file_contents = file_get_contents($this->getModulePath() . 'js/record_status_dashboard.js');
$javascript_file_contents = str_replace("AJAX_ENDPOINT", $pipe_all_records_ajax_url, $javascript_file_contents);
echo "<script type='text/javascript'>$javascript_file_contents</script>";
echo "<link rel='stylesheet' href='$css_url'>";
}
}
function redcap_data_entry_form_top($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance) {
$this->processRecord($project_id, $record, $instrument, $event_id, $repeat_instance);
}
function redcap_survey_page_top($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance) {
/**
* Piping on surveys is intentionally disabled.
* It used to work on surveys only for users also logged into REDCap,
* but this created confusion as to why it didn't work for anyone on those surveys.
* We considered enabling piping for NOAUTH users, but decided against it for security reasons.
* This hook could have been removed, but was left in place in hopes that any
* developer considering adding support for surveys would quickly see this comment,
* and be made aware of concerns & past discussion.
* For details, see https://redcap.vanderbilt.edu/community/post.php?id=99013
*/
}
function redcap_module_save_configuration($project_id) {
## This function was started to prevent unauthorized users from pulling data from other projects
## Currently, this is unneeded as only superusers can configure the module
## At some point, it would be nice to allow non-superusers to configure
## But further testing and development is needed
return;
## Don't allow users to specify project/field combinations that they don't have read access to
## Unless that project/field combination has already been accepted by someone with read access
$pipedProjects = $this->getProjectSetting('project-id',$project_id);
$destinationFields = $this->getProjectSetting('data-destination-field',$project_id);
$sourceFields = $this->getProjectSetting('data-source-field',$project_id);
error_log("Cross Project Piping: Piped<br /><br />\n\n".var_export($pipedProjects,true));
error_log("Cross Project Piping: Destination<br /><br />\n\n".var_export($destinationFields,true));
error_log("Cross Project Piping: Source<br /><br />\n\n".var_export($sourceFields,true));
foreach($pipedProjects as $projectKey => $thisProject) {
## List of fields for this project that are already confirmed
$confirmedFields = $this->getProjectSetting('confirmed-fields-'.$thisProject,$project_id);
foreach($destinationFields[$projectKey] as $fieldKey => $fieldName) {
if($sourceFields[$projectKey][$fieldKey] != "") {
$fieldName = $sourceFields[$projectKey][$fieldKey];
}
}
$userRights = \UserRights::getPrivileges($thisProject,USERID);
$isSuperUser = \User::isSuperUser(USERID);
$userRights = $userRights[$thisProject][USERID];
error_log("Cross Project Piping: User Rights : $isSuperUser<br /><br />\n\n".var_export($userRights,true));
if($isSuperUser) {
}
}
}
function redcap_module_system_change_version($version, $old_version) {
## This function was being tested to set up a test project on module version enabling
## It's not been tested thoroughly enough to leave in
return;
$this->setupTestProjects();
}
function redcap_module_system_enable($version) {
## This function was being tested to set up a test project on module version enabling
## It's not been tested thoroughly enough to leave in
return;
error_log("Enabling Cross Project Piping");
$this->setupTestProjects();
}
private function setupTestProjects() {
$parentProject = $this->createOrWipeTestProject("test_project_1","Cross Project Piping Test - Parent Project");
$pipingProject = $this->createOrWipeTestProject('test_project_2',"Cross Project Piping Test - Receiving Project");
$url = $this->getUrl("updateTestMetadata.php",true);
if($parentProject) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'projectType' => "parent"
));
$output = curl_exec($ch);
curl_close($ch);
if($output != "success") {
error_log("Cross Project Piping: Error Enabling Parent:<br />\n".$output);
}
}
if($parentProject && $pipingProject) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'projectType' => "output"
));
$output = curl_exec($ch);
curl_close($ch);
if($output != "success") {
error_log("Cross Project Piping: Error Enabling Output:<br />\n".$output);
}
}
}
function createOrWipeTestProject($settingName,$projectName) {
$projectId = $this->getSystemSetting($settingName);
if($projectId == "") {
## Create testing project using API
$apiToken = \Project::apiCreate(USERID,[
"project_title" => $projectName,
"record_autonumbering_enabled" => 1,
"purpose" => 1,
"purpose_other" => "Module Testing"
]);
if(!$apiToken) {
error_log("Cross Project Piping: Error Generating Parent Project Token");
return "";
}
$sql = "SELECT u.project_id
FROM redcap_user_rights u
WHERE u.api_token = '".db_escape($apiToken)."'";
$q = $this->query($sql);
$projectId = db_result($q,0,'project_id');
if($e = db_error()) {
error_log("Cross Project Piping: Error Enabling: $sql <br /><br />\n\n".var_export($e,true));
}
## Error occurred if this is empty
if(empty($projectId)) {
return "";
}
$this->setSystemSetting($settingName,$projectId);
}
else {
## Wipe data so it can be re-created from scratch
$table = $this->getDataTable($projectId);
$sql = "DELETE FROM $table
WHERE project_id = '".db_escape($projectId)."'";
$q = $this->query($sql);
if($e = db_error()) {
error_log("Cross Project Piping: Error Enabling: $sql <br /><br />\n\n".var_export($e,true));
return "";
}
\REDCap::logEvent("Data deleted from test project when enabling new version","Data Deleted",$sql,null,null,$projectId);
}
return $projectId;
}
function getDataTable($project_id){
return method_exists('\REDCap', 'getDataTable') ? \REDCap::getDataTable($project_id) : "redcap_data";
}
function setTestMetadataAndData($projectId,$metadata,$data) {
if($projectId != "" && !defined("PROJECT_ID")) {
global $Proj,$AllProjObjects;
define("PROJECT_ID",$projectId);
$this->query("BEGIN");
$Proj = new \Project($projectId,false);
## Update Data Dictionary
$metaDataResults = \MetaData::saveMetadataFlat($metadata);
## If errors on setting metadata
if(count($metaDataResults[1]) > 0) {
error_log("Cross Project Piping: Error Setting Metadata ".var_export($metaDataResults[1],true));
$this->query("ROLLBACK");
return;
}
## Unset from AllProjObjects to force lookup of newly set metadata
## Must be done before saving data or saveData will generate errors
unset($AllProjObjects[$projectId]);
## Save records for testing
$eventId = $this->getFirstEventId($projectId);
foreach($data as $recordData) {
$result = $this->saveData($projectId,"1",$eventId,$recordData);
## If save data successful
if(count($result['errors']) > 0) {
$this->query("ROLLBACK");
error_log("Cross Project Piping: Error Enabling: ".var_export($result['errors'],true));
return;
}
}
$this->query("COMMIT");
}
}
/**
* Generates nested array of settings keys. Used for multi-level sub_settings.
*/
function getKeysFromConfig($config) {
foreach($config['sub_settings'] as $subSetting){
if(!empty($subSetting['sub_settings'])) {
$keys[] = array('key' => $subSetting['key'], 'sub_settings' => $this->getKeysFromConfig($subSetting));
} else {
$keys[] = array('key' => $subSetting['key'], 'sub_settings' => array());
}
}
return $keys;
}
/**
* Used for processing nested sub_settings while generating settings data array.
*/
function processSubSettings($rawSettings, $key, $inc, $depth = 0) {
$returnArr = array();
$eachData = $rawSettings[$key['key']]['value'];
foreach($inc AS $i) {
$eachData = $eachData[$i];
}
foreach($eachData AS $k => $v) {
foreach($key['sub_settings'] AS $skey => $sval) {
if(!empty($sval['sub_settings'])) {
$sinc = $inc;
$sinc[] = $k;
$depth++;
$returnArr[$k][$sval['key']] = $this->processSubSettings($rawSettings, $sval, $sinc, $depth);
$depth--;
} else {
$retData = $rawSettings[$sval['key']]['value'];
foreach($inc AS $i) {
$retData = $retData[$i];
}
$returnArr[$k][$sval['key']] = $retData[$k];
}
}
}
return $returnArr;
}
/**
* Get full nested settings/sub_settings data.
*/
function getPipingSettings($project_id) {
$keys = [];
$config = $this->getSettingConfig('pipe-projects');
$keys = $this->getKeysFromConfig($config);
$subSettings = [];
$rawSettings = ExternalModules::getProjectSettingsAsArray([$this->PREFIX], $project_id);
# Quick-Fix for PHP8 Support
$subSettingCount = count((array)$rawSettings[$keys[0]['key']]['value']);
$this->pipingMode = (isset($rawSettings['piping-mode']['value'])) ? $rawSettings['piping-mode']['value'] : 0 ;
$this->pipeOnStatus = (isset($rawSettings['pipe-on-status']['value'])) ? $rawSettings['pipe-on-status']['value'] : 0 ;
for($i=0; $i<$subSettingCount; $i++){
$subSetting = [];
foreach($keys as $key){
if(!empty($key['sub_settings'])) {
$subSetting[$key['key']] = $this->processSubSettings($rawSettings, $key, array($i));
} else {
$subSetting[$key['key']] = $rawSettings[$key['key']]['value'][$i];
}
}
$subSettings[] = $subSetting;
}
return $subSettings;
}
function processRecord($project_id, $record, $instrument, $event_id, $repeat_instance) {
if(defined("PROJECT_ID")) {
$this->modSettings = $this->getPipingSettings(PROJECT_ID);
}
// Do not run on new records with no record ID
if(empty($record)) {
return;
}
if(!is_int($repeat_instance)) {
$repeat_instance = 1;
}
// If there are specific forms specified in the config settings then check to make sure we are currently on one of those forms. If not stop piping.
$rawSettings = ExternalModules::getProjectSettingsAsArray([$this->PREFIX], $project_id);
if (!empty($rawSettings['active-forms']['value']) && !in_array($instrument, $rawSettings['active-forms']['value'])) {
if(count($rawSettings['active-forms']['value']) > 1 || !empty($rawSettings['active-forms']['value'][0])) {
return;
}
}
// If this record is locked let's just stop here, no point in piping on a locked record.
$escInst = db_real_escape_string($instrument);
$sql = "SELECT * FROM redcap_locking_data WHERE project_id = {$project_id} AND record = '{$record}' AND event_id = {$event_id} AND form_name = '{$escInst}' AND instance = {$repeat_instance}";
$results = $this->query($sql);
$lockData = db_fetch_assoc($results);
if(!empty($lockData)) {
return;
}
// If this instrument's status is currently HIGHER than 'pipe-on-status' config value then DO NOT pipe data
$fieldName = db_real_escape_string($instrument).'_complete';
$table = $this->getDataTable($project_id);
$sql = "SELECT * FROM $table WHERE project_id = {$project_id} AND record = '{$record}' AND event_id = {$event_id} AND field_name = '{$fieldName}'";
if($repeat_instance >= 2) {
$sql .= " AND instance = ".$repeat_instance;
} else {
$sql .= " AND instance IS NULL";
}
$compResults = $this->query($sql);
$compData = db_fetch_assoc($compResults);
if(!empty($compData) && $compData['value'] > $this->pipeOnStatus) {
return;
}
// Looks like we're good to start piping
$term = '@PROJECTPIPING';
$matchTerm = '@FIELDMATCH';
$matchSourceTerm = '@FIELDMATCHSOURCE';
// If we have module settings lets overwrite $hook_functions with our module settings data
$useConfigSettings = false;
if(!empty($this->modSettings) && !empty($this->modSettings[0]['project-id'])) {
$useConfigSettings = true;
$hook_functions = array();
foreach($this->modSettings AS $mdk => $mdv) {
$projId = $mdv['project-id'];
$fieldMatch = $mdv['field-match'];
$fieldMatchSource = $mdv['field-match-source'];
foreach($mdv['project-fields'] AS $pfk => $pfv) {
$hook_functions[$term][$pfv['data-destination-field']] = array('params' => '['.$projId.']['.(empty($pfv['data-source-field']) ? $pfv['data-destination-field'] : $pfv['data-source-field']).']');
$hook_functions[$matchTerm][$pfv['data-destination-field']] = array('params' => $fieldMatch);
if(!empty($fieldMatchSource)) {
$hook_functions[$matchSourceTerm][$pfv['data-destination-field']] = array('params' => $fieldMatchSource);
}
}
}
}
// See if the term defined in this hook is used on this page
if (!isset($hook_functions[$term])) {
hook_log ("Skipping $term on $instrument of $project_id - not used.", "DEBUG");
return;
}
$startup_vars = $hook_functions[$term];
$match = $hook_functions[$matchTerm];
$matchSource = $hook_functions[$matchSourceTerm];
$choicesForFields = array();
$cachedDataDictionaries = array();
foreach ($startup_vars as $field => $params) {
$nodes = preg_split("/\]\[/", $params['params']);
for ($i=0; $i < count($nodes); $i++) {
$nodes[$i] = preg_replace("/^\[/", "", $nodes[$i]);
$nodes[$i] = preg_replace("/\]$/", "", $nodes[$i]);
}
$otherpid = $nodes[0];
## Only lookup the DD once per project
if(!array_key_exists($otherpid,$cachedDataDictionaries)) {
$cachedDataDictionaries[$otherpid] = \REDCap::getDataDictionary($otherpid, 'array');
}
$metadata = $cachedDataDictionaries[$otherpid];
if (count($nodes) == 2) {
$fieldName = $nodes[1];
} else {
$fieldName = $nodes[2];
}
if (preg_match("/\*/", $fieldName)) {
$fieldRegEx = "/^".preg_replace("/\*/", ".*", $fieldName)."$/";
$fieldNames = array();
foreach ($metadata as $field => $values) {
if (preg_match($fieldRegEx, $field)) {
$fieldNames[] = $field;
}
}
} else {
$fieldNames = array($fieldName);
}
foreach ($fieldNames as $fieldName) {
if (is_array($metadata) && is_array($metadata[$fieldName]) && $metadata[$fieldName]["select_choices_or_calculations"]) {
$choices = preg_split("/\|\s*/", $metadata[$fieldName]["select_choices_or_calculations"]);
$newChoices = array();
for ($i=0; $i < count($choices); $i++) {
$choices[$i] = preg_split("/,\s*/", $choices[$i]);
if (count($choices[$i]) > 2) {
$j = 0;
$newChoice = array();
$newValue = array();
foreach ($choices as $choice) {
if ($j === 0) {
$newChoice[] = $choice;
} else {
$newValue[] = $choice;
}
}
$newChoice[] = implode(",", $newValue);
$choice[$i] = $newChoice;
}
$choices[$i][0] = trim($choices[$i][0]);
$choices[$i][1] = trim($choices[$i][1]);
}
$choicesForFields[$fieldName] = array();
foreach ($choices as $choice) {
$choicesForFields[$fieldName][$choice[0]] = $choice[1];
}
}
}
}
list($prefix, $version) = ExternalModules::getParseModuleDirectoryPrefixAndVersion($this->getModuleDirectoryName());
$url = ExternalModules::getUrl($prefix, "getValue.php");
$ajaxLoaderGif = APP_PATH_WEBROOT.'../modules/'.$this->getModuleDirectoryName()."/ajax-loader.gif";
?>
<img style="display: none;" src="<?php echo $ajaxLoaderGif; ?>">
<script type='text/javascript'>
<?php if($this->pipingMode != 1): ?>
initiateLoadingOverlay();
<?php endif; ?>
$(document).ready(function() {
<?php if($this->pipingMode == 1 && !$this->hideButton): ?>
$('#form table#questiontable>tbody').prepend('<tr style="border-top: 1px solid #DDDDDD;"><td style="text-align: center; padding: 6px;" colspan="2"><button id="ccpPipeData">Initiate Data Piping</button></td></tr>');
$('#ccpPipeData').click(function(evt){
evt.preventDefault();
runCrossProjectPiping();
});
<?php else: ?>
runCrossProjectPiping();
<?php endif; ?>
});
function initiateLoadingOverlay() {
// Create a loading overlay to indicate piping in process
var jsCppAjaxLoader = document.createElement('div');
jsCppAjaxLoader.setAttribute("id", "cppAjaxLoader");
jsCppAjaxLoader.setAttribute("style", "overflow: hidden; text-align: center; position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(255, 255, 255, 0.7); z-index: 9999;");
jsCppAjaxLoader.innerHTML = '<!-- x -->';
var centerEl = document.getElementById('center');
// On REDCap v8.4.2, centerEl was not being set on surveys
// This fixes that and prevents piping errors on surveys
if(typeof centerEl == "undefined" || centerEl === null) {
centerEl = document.getElementById('container');
}
centerEl.insertBefore(jsCppAjaxLoader, centerEl.firstChild);
// END loading overlay
}
var branchingFields = {};
function addBranchingField(field, element) {
branchingFields[field] = {element: element};
}
function branchingPipingFix(fields) {
$.each(fields, function(field,params) {
if(field in branchingFields) {
branchingFields[field].element.change();
var dblVal = doBranching(field);
}
});
}
function runCrossProjectPiping() {
<?php if($this->pipingMode == 1): ?>
initiateLoadingOverlay();
<?php endif; ?>
var fields = <?php print json_encode($startup_vars) ?>;
var choices = <?= json_encode($choicesForFields) ?>;
$('#form').addClass('piping-loading');
$('#center').css('position', 'realative');
<?php if($useConfigSettings): ?>
// Lets add a little more context to the loading overlay (like text and a loading gif)
$('#cppAjaxLoader').prepend('<div id="cppAjaxLoaderInner" style="left: 0; right: 0; text-align: center; display: inline-block; position: absolute; top: 200px;"><div style="display: inline-block; padding: 40px; background-color: #fff; border-radius: 3px; font-size: 16px; font-weight: bold; color: #424242; border: 1px solid #757575;">PIPING DATA<br><img src="<?php echo $ajaxLoaderGif; ?>"></div></div>');
// A little quick math for a nice position
var cppAjaxLoaderInnerOffset = Math.ceil($(window).scrollTop() + (($(window).height()-$('#cppAjaxLoaderInner').outerHeight()) * 0.5));
$('#cppAjaxLoaderInner').css('top', cppAjaxLoaderInnerOffset+'px');
<?php endif; ?>
const formatDate = (data, dateFormatStr, ) => {
var dateFormatParams = dateFormatStr.split('_');
var dFFormatParams = dateFormatParams.slice(-1)[0].split('');
var dFFormatArr = [];
for (var i = 0, len = dFFormatParams.length; i < len; i++) {
dFFormatArr.push(dFFormatParams[i]+dFFormatParams[i]);
}
dFFormat = dFFormatArr.join('-');
const dataParams = data.split(' ')
const datepickerDate = new Date(dataParams[0])
datepickerDate.setTime(datepickerDate.getTime()+datepickerDate.getTimezoneOffset()*60000) // Fix any timezone vs. UTC related date shifting
var newDate = $.datepicker.formatDate(dFFormat, datepickerDate);
if(!newDate.includes('NaN') && data.length >= 1) {
var dateTimeStr = '';
if(dateFormatParams[0] == 'datetime') {
let dateTimeData
if(dataParams.length === 1){
dateTimeData = []
}
else{
dateTimeData = dataParams[1].split(':')
}
while(dateTimeData.length < 3){
dateTimeData.push('00')
}
var dateTimeVal = dateTimeData[0]+':'+dateTimeData[1];
if(dateFormatParams.length > 2) {
var dateTimeVal = dateTimeVal+':'+dateTimeData[2];
}
if(dateTimeVal.length) {
dateTimeStr = ' '+dateTimeVal;
}
}
return newDate + dateTimeStr
}
return false
}
// Only run unit tests when on a dev/test server
if(<?=json_encode(($GLOBALS['is_development_server']) === '1')?>){
const tests = [
{
input: '2001-02-03',
outputs: {
date: {
dmy: '03-02-2001',
mdy: '02-03-2001',
ymd: '2001-02-03'
},
time: '00:00',
seconds: '00'
}
},
{
input: '2011-02-03 04:05',
outputs: {
date: {
dmy: '03-02-2011',
mdy: '02-03-2011',
ymd: '2011-02-03'
},
time: '04:05',
seconds: '00'
}
},
{
input: '2021-02-03 06:07:08',
outputs: {
date: {
dmy: '03-02-2021',
mdy: '02-03-2021',
ymd: '2021-02-03'
},
time: '06:07',
seconds: '08'
}
},
{
/**
* Make sure timezone adjustment does not shift times later in the day
* over into the next day
*/
input: '2022-11-13 19:34',
outputs: {
date: {
dmy: '13-11-2022',
mdy: '11-13-2022',
ymd: '2022-11-13'
},
time: '19:34',
seconds: '00'
}
}
]
let testOutput = ''
tests.forEach(test => {
[
'date_dmy',
'date_mdy',
'date_ymd',
'datetime_dmy',
'datetime_mdy',
'datetime_ymd',
'datetime_seconds_dmy',
'datetime_seconds_mdy',
'datetime_seconds_ymd'
].forEach(format => {
const actualOutput = formatDate(test.input, format)
const parts = format.split('_')
const dateFormat = parts.pop()
let expectedOutput = test.outputs.date[dateFormat]
if(parts[0] === 'datetime'){
expectedOutput += ' ' + test.outputs.time
}
if(parts.length === 2){
expectedOutput += ':' + test.outputs.seconds
}
if(actualOutput !== expectedOutput){
testOutput += 'The formateDate() function returned "' + actualOutput + '" instead of "' + expectedOutput + '" for input "' + test.input + '" and format "' + format + '"!<br>'
}
})
})
if(testOutput.length > 0){
simpleDialog(testOutput, 'Cross Project Piping Module - Test Failures', null, 1100)
}
}
var cppAjaxConnections = 0;
var cppFoundField = false;
var cppProcessing = true;
//console.log(fields);
$.each(fields, function(field,params) {
var value = params.params;
var nodes = value.split(/\]\[/);
for (var i=0; i < nodes.length; i++) {
nodes[i] = nodes[i].replace(/^\[/, "");
nodes[i] = nodes[i].replace(/\]$/, "");
}
if ((nodes[0].match(/^\d+$/)) && (nodes.length >= 2)) {
var remaining;
if (nodes.length == 2) {
remaining = "[" + nodes[1] + "]";
} else {
remaining = "[" + nodes[1] + "][" + nodes[2] + "]";
}
var match = <?= json_encode($match) ?>;
var matchSource = <?= json_encode($matchSource) ?>;
var matchSourceParam = null
if(matchSource && matchSource[field]){
matchSourceParam = matchSource[field]['params'];
}
var url = "<?= $url ?>"+"&pid="+<?= intval($_GET['pid']) ?>;
var getLabel = 0;
if (($('[name="'+field+'"]').attr("type") == "text") || ($('[name="'+field+'"]').attr("type") == "notes")) {
getLabel = 1;
}
if(field.length && $('tr[sq_id='+field+']').length) {
cppFoundField = true;
cppAjaxConnections++;
var ajaxCountLimit = 0;
// console.log('++cppAjaxConnections = '+cppAjaxConnections);
//console.log(url);
$.post(url, { thisrecord: '<?= htmlspecialchars($_GET['id'], ENT_QUOTES) ?>', thispid: <?= intval($_GET['pid']) ?>, thisinstance: <?= intval($repeat_instance) ?>, thismatch: match[field]['params'], matchsource: matchSourceParam, getlabel: getLabel, otherpid: nodes[0], otherlogic: remaining, choices: JSON.stringify(choices) }, function(data) {
//console.log(data);
if(data.length && typeof(data) == 'string' && data.indexOf(<?=json_encode(\RCView::tt("dataqueries_352"))?>) >= 0) {
if(ajaxCountLimit >= 1000) {
return;
}
// console.log('Trying '+field+' again.');
ajaxCountLimit++;
$.ajax(this);
return;
}
cppAjaxConnections--;
var lastNode = nodes[1];
if (nodes.length > 2) {
lastNode = nodes[2];
}
var tr = $('tr[sq_id='+field+']');
var id = lastNode.match(/\([^\s]+\)/);
//console.log("Setting "+field+" to "+data);
if (typeof(data) == 'object') { // checkbox
$.each(data, function( index, value ) {
var input = $('input:checkbox[code="'+index+'"]', tr);
if ($(input).length > 0) {
if (value == 1) {
$(input).prop('checked', true);
$('input:hidden[name="__chk__'+field+'_RC_'+index+'"]').val(index);
} else {
$(input).prop('checked', false);
$('input:hidden[name="__chk__'+field+'_RC_'+index+'"]').val('');
}
addBranchingField(field, input);
// $(input).change();
}
});
} else if (id) { // checkbox
id = id[0].replace(/^\(/, "");
id = id.replace(/\)$/, "");
var input = $('input:checkbox[code="'+id+'"]', tr);
if ($(input).length > 0) {
if (data == 1) {
// console.log("A Setting "+field+" to "+data);
$(input).prop('checked', true);
} else { // data == 0
// console.log("B Setting "+field+" to "+data);
$(input).prop('checked', false);
}
addBranchingField(field, input);
// $(input).change();
} else {
// console.log("C Setting "+field+" to "+data);
$('[name="'+field+'"]').val(data);
addBranchingField(field, $('[name="'+field+'"]'));
// $('[name="'+field+'"]').change();
}
} else {
// Is this a date field? If so we need to format this date correctly.
if($('[name="'+field+'"]').hasClass('hasDatepicker') || (typeof $('[name="'+field+'"]').attr('fv') !== 'undefined' && $('[name="'+field+'"]').attr('fv').includes('date_'))) {
var dateFormatStr = $('[name="'+field+'"]').attr('fv');
const newDateString = formatDate(data, dateFormatStr)
if(newDateString){
$('[name="'+field+'"]').val(newDateString);
addBranchingField(field, $('[name="'+field+'"]'));
// $('[name="'+field+'"]').change();
}
} else {
const unescapedData = data
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, "\"")
.replace(/'/g, "'")
$('[name="'+field+'"]').val(unescapedData);
addBranchingField(field, $('[name="'+field+'"]'));
// $('[name="'+field+'"]').change();
}
//console.log("D Setting "+field+" to "+$('[name="'+field+'"]').val());
if ($('[name="'+field+'___radio"][value="'+data+'"]').length > 0) {
$('[name="'+field+'___radio"][value="'+data+'"]').prop('checked', true);
addBranchingField(field, $('[name="'+field+'___radio"][value="'+data+'"]'));
// $('[name="'+field+'___radio"][value="'+data+'"]').change();
}
}
if(cppAjaxConnections == 0) {
// Looks like all the ajax requests might be finished. Run a couple checks and then remove loading overlay.
if(cppProcessing) {
cppProcessing = false;
setTimeout(function(){
if(cppAjaxConnections == 0 && cppProcessing == false) {
$('#form').removeClass('piping-loading');
$('#form').addClass('piping-complete');
$('#cppAjaxLoader').remove();
branchingPipingFix(fields);
} else {
cppProcessing == true;
}
}, 50);
}
} else if(cppProcessing == false) {
cppProcessing = true;
}
});
}
}
});
if(cppFoundField == false) {
// Looks like we never found a field. Remove loading overlay.
cppProcessing = false;
$('#form').removeClass('piping-loading');
$('#form').addClass('piping-complete');
$('#cppAjaxLoader').remove();
branchingPipingFix(fields);
}
}
</script>
<?php
}
function validateSettings($settings){
error_reporting(E_ALL);
if(!SUPER_USER){
if(defined('USERID')) {
$userID = USERID;
} else {
return "No User ID Defined!";
}
$projectIds = $settings['project-id'];
foreach($projectIds AS $proj_id) {
if(!empty($proj_id) && $proj_id != 'null') {
$rights = \UserRights::getPrivileges($proj_id, $userID);
if(empty($rights) || $rights[$proj_id][$userID]['design'] != 1){
return "You must have design rights for every source project in order to save this module's settings.";
}
}
}
}
return parent::validateSettings($settings);
}
// the functions below are used by the Pipe All Records button (only)
function getProjects() {
// prepare array that will be returned
$projects = [
'destination' => [],
'source' => [],
];
global $Proj;
$projects['destination']['project_id'] = $this->getProjectId();
$project_ids = $this->getProjectSetting('project-id');
$dest_match_fields = $this->getProjectSetting('field-match');
$source_match_fields = $this->getProjectSetting('field-match-source');
$dest_fields = $this->getProjectSetting('data-destination-field');
$source_fields = $this->getProjectSetting('data-source-field');
// fill $projects['source'] array with source project info arrays
foreach ($project_ids as $project_index => $pid) {
$source_project = [
'project_id' => $pid,
'source_match_field' => $source_match_fields[$project_index],
'dest_match_field' => $dest_match_fields[$project_index],
'dest_fields' => $dest_fields[$project_index],
'source_fields' => $source_fields[$project_index],
'dest_forms_by_field_name' => []
];
// where source data/match fields are empty, use destination match/data field names
if (empty($source_project['source_match_field'])) {
$source_project['source_match_field'] = $source_project['dest_match_field'];
}
foreach ($source_project['source_fields'] as $list_index => $field_name) {
// set to destination name if no alternate name used for source project
$matching_destination_field_name = $source_project['dest_fields'][$list_index];
if (empty($field_name)) {
$source_project['source_fields'][$list_index] = $matching_destination_field_name;
}
// add an entry to dest_forms_by_field_name for this source field
$actual_field_name = $source_project['dest_fields'][$list_index];
$source_project['dest_forms_by_field_name'][$actual_field_name] = $Proj->metadata[$matching_destination_field_name]['form_name'];
}
// add event id/name pairs
$source_project['events'] = [];
$project_obj = new \Project($pid);
foreach ($project_obj->events[1]['events'] as $event_id => $event_array) {
$source_project['events'][$event_id] = $event_array['descrip'];
}
// add 'valid_match_events' array to this source project -- this will contain the event_id values associated with each form that contains the destinatiion match field
$valid_match_event_ids = [];
$dest_match_field_form = $Proj->metadata[$source_project['dest_match_field']]['form_name'] ?? null;
foreach ($Proj->eventsForms as $eid => $formlist) {
if (in_array($dest_match_field_form, $formlist) !== false) {
$dst_event_name = $Proj->eventInfo[$eid]['name_ext'];
if (!empty($dst_event_name)) {
foreach ($project_obj->eventInfo as $eid2 => $info) {
if ($info['name_ext'] === $dst_event_name)
$src_eid = $eid2;
}
if (!empty($src_eid)) {
$valid_match_event_ids[] = $src_eid;
}
}
}
}
$source_project['valid_match_event_ids'] = $valid_match_event_ids;
// store project info array in projects['source'] array
$projects['source'][] = $source_project;
unset($project_obj);
}
// for destination project, prepare list of forms to limit piping to
// and remember which form statuses are ok to pipe on (incomplete, complete, etc)
$active_forms = $this->getProjectSetting('active-forms');
if (!empty($active_forms)) {
$projects['destination']['active_forms'] = $active_forms;
}
$projects['destination']['pipe_on_status'] = $this->getProjectSetting('pipe-on-status');
// add event id/names to destination project from global Project instance ($Proj is the destination/host project)
foreach($Proj->events as $arm_number => $arm_details) {
foreach ($arm_details['events'] as $event_id => $event_array) {
$projects['destination']['events'][$event_id] = $event_array['descrip'];
$projects['destination']['event_details'][$event_id] = [
"arm"=>$arm_number,
"name"=>$event_array['descrip'],
"unique_name"=>strtolower(str_replace(" ", "_", $event_array['descrip']) . "_arm_$arm_number")
];
}
}
return $projects;
}
function getFormStatusAllRecords($active_forms) {
/* for the forms given in the array above, return an array structured like
$form_status_all_records = [
// record id
[1] => [
// event id
[393] => [
"record_id" => 3,
"my_form_1" => 0 // designates incomplete (raw value from [my_form_1_complete] of destination project)
"my_form_2" => 2 // raw value 'Complete'
...
],
...
],
...
]
*/
if (empty($active_forms)) {
global $Proj;
$active_forms = array_keys($Proj->forms);
}
$fields = [];
foreach($active_forms as $form_name) {
$fields[] = $form_name . "_complete";
}
$data = \REDCap::getData('array', null, $fields);
return $data;
}
function getSourceProjectsData() {
if (gettype($this->projects['source']) == 'Array') {
throw new \Exception("The Cross Project Piping module expected \$module->projects['source'] to be an array before calling pipeToRecord()");
}
// fetch pipe and match data for all records in each source project
foreach ($this->projects['source'] as $project_index => $project) {
$project_id = $project['project_id'];
$match_field = $project['source_match_field'];
$fields = $project['source_fields'];
if (!in_array($match_field, $fields)) {
$fields[] = $match_field;
}
$params = [