-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathInstanceTable.php
1001 lines (898 loc) · 55.8 KB
/
InstanceTable.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
/**
* Instance Table External Module
* @author Luke Stevens, Murdoch Children's Research Institute
*/
namespace MCRI\InstanceTable;
use ExternalModules\AbstractExternalModule;
use DateTimeRC;
use Files;
use Form;
use RCView;
use REDCap;
class InstanceTable extends AbstractExternalModule
{
protected $isSurvey=false;
protected $taggedFields=array();
// global vars dependencies
protected $Proj;
protected $lang;
protected $user_rights;
protected $event_id;
protected $record;
protected $instrument;
protected $instance;
protected $group_id;
protected $repeat_instance;
protected $defaultValueForNewPopup;
const ACTION_TAG = '@INSTANCETABLE';
const ACTION_TAG_HIDE_FIELD = '@INSTANCETABLE_HIDE';
const ACTION_TAG_LABEL = '@INSTANCETABLE_LABEL';
const ACTION_TAG_SCROLLX = '@INSTANCETABLE_SCROLLX';
const ACTION_TAG_HIDEADDBTN = '@INSTANCETABLE_HIDEADD'; // i.e. hide "Add" button even if user has edit access to form
const ACTION_TAG_HIDEINSTANCECOL = '@INSTANCETABLE_HIDEINSTANCECOL'; // i.e. hide the "#" column containing instance numbers
const ACTION_TAG_VARLIST = '@INSTANCETABLE_VARLIST'; // provide a comma-separated list of variables to include (not including any tagged HIDE)
const ACTION_TAG_PAGESIZE = '@INSTANCETABLE_PAGESIZE'; // Override default choices for page sizing: specify integer default page size, use -1 for All
const ACTION_TAG_REF = '@INSTANCETABLE_REF';
const ACTION_TAG_SRC = '@INSTANCETABLE_SRC'; // deprecated
const ACTION_TAG_DST = '@INSTANCETABLE_DST'; // deprecated
const ACTION_TAG_FILTER = '@INSTANCETABLE_FILTER';
const ACTION_TAG_ADDBTNLABEL = '@INSTANCETABLE_ADDBTNLABEL';
const ACTION_TAG_HIDECHOICEVALUES = '@INSTANCETABLE_HIDECHOICEVALUES';
const ACTION_TAG_HIDEFORMSTATUS = '@INSTANCETABLE_HIDEFORMSTATUS';
const ADD_NEW_BTN_YSHIFT = '0px';
const MODULE_VARNAME = 'MCRI_InstanceTable';
const ERROR_NOT_REPEATING_CLASSIC = '<div class="red">ERROR: "%s" is not a repeating form. Contact the project designer.';
const ERROR_NOT_REPEATING_LONG = '<div class="red">ERROR: "%s" is not a repeating form for event "%s". Contact the project designer.';
const ERROR_NO_VIEW_ACCESS = '<div class="yellow">You do not have permission to view this form\'s data.';
const REPLQUOTE_SINGLE = 'REPLQUOTE_SINGLE';
const REPLQUOTE_DOUBLE = 'REPLQUOTE_DOUBLE';
/**
* redcap_save_record
* When saving an instance in the popup, get &extmod_instance_table=1 into the redirect link e.g. when missing required fields found
*/
public function redcap_save_record($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance) {
if (isset($_GET['extmod_instance_table']) && $_GET['extmod_instance_table']=='1') {
$_GET['pid'] .= '&extmod_instance_table=1'; // $_GET['instance'] .= '&extmod_instance_table=1';
}
}
/**
* redcap_every_page_before_render
* When doing Save & Exit or Delete Instance from popup then persist a flag on the session.
* If Record Home page is loading when this flag is present then window.close()
* @param type $project_id
*/
public function redcap_every_page_top($project_id) {
if (PAGE==='DataEntry/record_home.php' && isset($_GET['id']) && isset($_SESSION['extmod_closerec_home']) && $_GET['id']==$_SESSION['extmod_closerec_home']) {
?>
<script type="text/javascript">/* EM Instance Table */ window.close();</script>
<?php
unset($_SESSION['extmod_closerec_home']);
}
}
public function redcap_data_entry_form_top($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance) {
$this->initHook($record, $instrument, $event_id, false, $group_id, $repeat_instance);
$this->pageTop();
if (isset($_GET['extmod_instance_table']) && $_GET['extmod_instance_table']=='1') {
// this is in the popup
$this->popupViewTweaks();
}
}
public function redcap_survey_page_top($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance) {
$this->initHook($record, $instrument, $event_id, true, $group_id, $repeat_instance);
$this->pageTop();
}
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) {
$this->initHook($record, $instrument, $event_id, false, $group_id, $repeat_instance);
if ($action == "get-data") {
$event = intval($payload["event_id"]);
$form = $this->framework->escape($payload["form_name"]);
$fields = $this->framework->escape($payload["fields"]);
$filter = $payload["filter"];
$hideChoiceValues = (bool)$payload["hide_vals"];
$hideFormStatus = (bool)$payload["hide_form_status"];
$data = $this->getInstanceData($record, $event, $form, $fields, $filter, !$hideFormStatus, $hideChoiceValues);
return $data;
}
}
protected function initHook($record, $instrument, $event_id, $isSurvey, $group_id, $repeat_instance) {
global $Proj, $lang, $user_rights;
$this->Proj = $Proj;
$this->lang = &$lang;
$this->user_rights = &$user_rights;
$this->isSurvey = (PAGE==='surveys/index.php');
$this->record = $record;
$this->instrument = $instrument;
$this->event_id = $event_id;
$this->isSurvey = $isSurvey;
$this->group_id = $group_id;
$this->repeat_instance = $repeat_instance;
}
protected function pageTop() {
// find any descriptive text fields tagged with @FORMINSTANCETABLE=form_name
$this->setTaggedFields();
if (count($this->taggedFields)>0) {
// check each specified form is a repeating form or a form in a repeating event
$this->checkIsRepeating();
// check user has read access to each specified form
$this->checkUserPermissions();
// set the markup for the DataTable (or error/no permission message)
$this->setMarkup();
// write the JavaScript to the page
$this->insertJS();
}
}
protected function setTaggedFields() {
$this->taggedFields = array();
$instrumentFields = REDCap::getDataDictionary('array', false, true, $this->instrument);
foreach ($instrumentFields as $fieldName => $fieldDetails) {
$matches = array();
// /@INSTANCETABLE='?((\w+_arm_\d+[a-z]?:)?\w+)'?\s?/
// asdf@INSTANCETABLE='eee_arm_1:fff_fff' asdf
// Full match 4-39 @INSTANCETABLE='eee_arm_1:fff_fff'
// Group 1. 20-37 eee_arm_1:fff_fff
// Group 2. 20-30 eee_arm_1:
if ($fieldDetails['field_type']==='descriptive' &&
preg_match("/".self::ACTION_TAG."\s*=\s*'?((\w+_arm_\d+[a-z]?:)?\w+)'?\s?/", $fieldDetails['field_annotation'], $matches)) {
if (REDCap::isLongitudinal() && strpos(trim($matches[1]), ':')>0) {
$eventform = explode(':', trim($matches[1]), 2);
$eventId = REDCap::getEventIdFromUniqueEvent($eventform[0]);
$eventName = $eventform[0];
$formName = $eventform[1];
} else {
$eventId = $this->event_id;
$eventName = '';
$formName = $matches[1];
}
$repeatingFormDetails = array();
$repeatingFormDetails['field_name'] = $fieldName;
$repeatingFormDetails['event_id'] = $eventId;
$repeatingFormDetails['event_name'] = $eventName;
$repeatingFormDetails['form_name'] = $formName;
$repeatingFormDetails['permission_level'] = 0;
$repeatingFormDetails['form_fields'] = array();
$repeatingFormDetails['html_table_id'] = self::MODULE_VARNAME.'_'.$fieldName.'_tbl_'.$eventName.'_'.$formName;
$repeatingFormDetails['html_table_class'] = self::MODULE_VARNAME.'_'.$eventName.'_'.$formName; // used to find tables to refresh after add/edit
// filter records on an optional supplemental join key
// useful in cases where repeating data entry forms are being used to represent relational data
// e.g. when multiple medications are recorded on each visit (of which there can also be multiple),
// the medications shown in the instance table on a visit instance should be filtered by their relationship with that visit instance
$filter = '';
$matches = array();
$this->defaultValueForNewPopup = '';
if (preg_match("/".self::ACTION_TAG_REF."\s*=\s*'?((\w+_arm_\d+[a-z]?:)?\w+)'?\s?/", $fieldDetails['field_annotation'], $matches)) {
$join_val = ($this->repeat_instance == null || empty($this->repeat_instance))
? 1 : $this->repeat_instance;
$linkField = trim($matches[1]);
$filter = "[" . $linkField ."]='" .$join_val."'";
$repeatingFormDetails['link_field'] = $linkField;
$repeatingFormDetails['link_instance'] = $join_val;
}
// keep legacy support for _SRC and _DST tags but any in use should be converted to _REF tags
$matches = array();
if (preg_match("/".self::ACTION_TAG_SRC."='?((\w+_arm_\d+[a-z]?:)?\w+)'?\s?/", $fieldDetails['field_annotation'], $matches)) {
$recordData = REDCap::getData('array', $this->record, $matches[1], $this->event_id, null, false, false, false, null, false); // export raw
$join_val = $recordData[1]['repeat_instances'][$this->event_id][$this->instrument][$this->repeat_instance][$matches[1]];
if (preg_match("/".self::ACTION_TAG_DST."='?((\w+_arm_\d+[a-z]?:)?\w+)'?\s?/", $fieldDetails['field_annotation'], $matches)) {
$filter = $this->escape("[" . $matches[1] ."]='" .$join_val."'");
}
}
// pick up option for hiding instance col
$matches = array();
if (preg_match("/".self::ACTION_TAG_HIDEINSTANCECOL."\s?/", $fieldDetails['field_annotation'], $matches)) {
$repeatingFormDetails['show_instance_col'] = false;
} else {
$repeatingFormDetails['show_instance_col'] = true;
}
// pick up option for page size override
$matches = array();
if (preg_match("/".self::ACTION_TAG_PAGESIZE."\s*=\s*'?(-?\d+)'?\s?/", $fieldDetails['field_annotation'], $matches)) {
$pageSize = intval($matches[1]);
if ($pageSize < -1) {
$pageSize = -1;
}
$repeatingFormDetails['page_size'] = $pageSize;
} else {
$repeatingFormDetails['page_size'] = 0;
}
// pick up option for additional filter expression
$matches = array();
$outerQuote = array('"',"'"); // work for both @INSTANCETABLE_FILTER='[v]="1"' and @INSTANCETABLE_FILTER="[v]='1'" quote patterns
foreach ($outerQuote as $quoteChar) {
if (preg_match("/".self::ACTION_TAG_FILTER."\s*=\s*$quoteChar(.+)$quoteChar/", $fieldDetails['field_annotation'], $matches)) {
$addnlFilter = trim($matches[1]);
$filter = (empty(trim($filter)))
? $addnlFilter
: "($filter) and ($addnlFilter)";
}
}
$filter = str_replace('<>','!=',$filter); // '<>' gets removed by REDCap::filterHtml()
$repeatingFormDetails['filter']=REDCap::filterHtml($filter);
// make column list for table: all form vars or supplied list, remove any with @INSTANCETABLE_HIDE
$repeatingFormFields = REDCap::getDataDictionary('array', false, null, $formName);
$includeVars = $requestedVars = $matches = array();
if (preg_match("/".self::ACTION_TAG_VARLIST."\s*=\s*'?([a-z][a-z0-9_]*(,[a-z][a-z0-9_]*)*)'?\s?/", $fieldDetails['field_annotation'], $matches)) {
$requestedVars = explode(',',trim($matches[1]));
$includeVars = array_intersect($requestedVars, array_keys($repeatingFormFields));
} else {
$requestedVars = array();
$includeVars = array_keys($repeatingFormFields);
}
foreach ($includeVars as $idx => $fieldName) {
// remove descriptive text fields
if ($repeatingFormFields[$fieldName]['field_type']==='descriptive') {
unset($includeVars[$idx]);
} else if (!in_array($fieldName, $requestedVars)){
// ignore fields tagged @FORMINSTANCETABLE_HIDE - unless requested explicitly in varlist
$matches = array();
if (preg_match("/".self::ACTION_TAG_HIDE_FIELD."/", $repeatingFormFields[$fieldName]['field_annotation'])) {
unset($includeVars[$idx]);
}
}
}
reset($includeVars);
$repeatingFormDetails['var_list'] = $includeVars;
if (preg_match("/".self::ACTION_TAG_SCROLLX."/", $fieldDetails['field_annotation'])) {
$repeatingFormDetails['scroll_x'] = true;
} else {
$repeatingFormDetails['scroll_x'] = false;
}
if (preg_match("/".self::ACTION_TAG_HIDEADDBTN."/", $fieldDetails['field_annotation'])) {
$repeatingFormDetails['hide_add_btn'] = true;
} else {
$repeatingFormDetails['hide_add_btn'] = false;
}
$matches = array();
if (preg_match("/".self::ACTION_TAG_ADDBTNLABEL."\s*=\'([^\']+)\'/", $fieldDetails['field_annotation'], $matches)) {
$repeatingFormDetails['button_label'] = REDCap::filterHtml($matches[1]);
} else {
$repeatingFormDetails['button_label'] = '';
}
if (preg_match("/".self::ACTION_TAG_HIDECHOICEVALUES."/", $fieldDetails['field_annotation'])) {
$hideVals = $repeatingFormDetails['hide_choice_values'] = true;
} else {
$hideVals = $repeatingFormDetails['hide_choice_values'] = false;
}
if ($this->isSurvey || preg_match("/".self::ACTION_TAG_HIDEFORMSTATUS."/", $fieldDetails['field_annotation'])) {
$hideStatus = $repeatingFormDetails['hide_form_status'] = true;
} else {
$hideStatus = $repeatingFormDetails['hide_form_status'] = false;
}
$repeatingFormDetails["ajax"] = [
"event_id" => $eventId,
"form_name" => $formName,
"filter" => $filter,
"fields" => $includeVars,
'hide_vals' => $hideVals,
'hide_form_status' => $hideStatus
];
$repeatingFormDetails['markup'] = '';
$this->taggedFields[] = $repeatingFormDetails;
}
}
}
protected function checkIsRepeating() {
foreach ($this->taggedFields as $key => $repeatingFormDetails) {
if (!$this->Proj->isRepeatingFormOrEvent($repeatingFormDetails['event_id'], $repeatingFormDetails['form_name'])) {
$repeatingFormDetails['permission_level'] = -1; // error
}
$this->taggedFields[$key] = $repeatingFormDetails;
}
}
protected function checkUserPermissions() {
foreach ($this->taggedFields as $key => $repeatingFormDetails) {
if ($this->isSurvey) {
$repeatingFormDetails['permission_level'] = 2; // always read only in survey view
} else if ($repeatingFormDetails['hide_add_btn']) {
$repeatingFormDetails['permission_level'] = 2; // Hide "Add" button = "read only" (effectively!)
} else if ($repeatingFormDetails['permission_level'] > -1) {
switch ($this->user_rights['forms'][$repeatingFormDetails['form_name']]) {
case '1': $repeatingFormDetails['permission_level'] = 1; break; // view/edit
case '2': $repeatingFormDetails['permission_level'] = 2; break; // read only
case '3': $repeatingFormDetails['permission_level'] = 1; break; // view/edit + edit survey responses
case '0': $repeatingFormDetails['permission_level'] = 0; break; // no access
default : $repeatingFormDetails['permission_level'] = -1; break;
}
}
$this->taggedFields[$key] = $repeatingFormDetails;
}
}
protected function setMarkup() {
foreach ($this->taggedFields as $key => $repeatingFormDetails) {
switch ($repeatingFormDetails['permission_level']) {
case 1: // view & edit
$repeatingFormDetails['markup'] = $this->makeHtmlTable($repeatingFormDetails, true);
break;
case '2': // read only
$repeatingFormDetails['markup'] = $this->makeHtmlTable($repeatingFormDetails, false);
break;
case '0': // no access
$repeatingFormDetails['markup'] = self::ERROR_NO_VIEW_ACCESS;
break;
default: // -1 error
if (REDCap::isLongitudinal()) {
$eventName = $this->Proj->eventInfo[$repeatingFormDetails['event_id']]['name'];
$repeatingFormDetails['markup'] = sprintf(self::ERROR_NOT_REPEATING_LONG, $repeatingFormDetails['form_name'], $eventName);
} else {
$repeatingFormDetails['markup'] = sprintf(self::ERROR_NOT_REPEATING_CLASSIC, $repeatingFormDetails['form_name']);
}
break;
}
$this->taggedFields[$key] = $repeatingFormDetails;
}
}
protected function makeHtmlTable($repeatingFormDetails, $canEdit) {
$tableElementId = $repeatingFormDetails['html_table_id'];
$tableFormClass = $repeatingFormDetails['html_table_class'];
$eventId = $repeatingFormDetails['event_id'];
$formName = $repeatingFormDetails['form_name'];
$scrollX = $repeatingFormDetails['scroll_x'];
$linkField = $repeatingFormDetails['link_field'];
$linkValue = $repeatingFormDetails['link_instance'];
$filter = $repeatingFormDetails['filter']; // The filter actually contains linkfield=linkvalue
$varList = $repeatingFormDetails['var_list'];
$btnLabel = ($repeatingFormDetails['button_label']=='') ? $this->lang['data_entry_247'] : $repeatingFormDetails['button_label'];
$hideChoiceValues = $repeatingFormDetails['hide_choice_values'];
$hideFormStatus = $repeatingFormDetails['hide_form_status'];
$scrollStyle = ($scrollX) ? "display:block; max-width:790px;" : "";
$nColumns = 1; // start at 1 for # (Instance) column
$html = '<div class="" style="margin-top:10px; margin-bottom:'.self::ADD_NEW_BTN_YSHIFT.';">';
$html .= '<table id="'.$tableElementId.'" class="table table-striped table-bordered table-condensed table-responsive '.self::MODULE_VARNAME.' '.$tableFormClass.'" width="100%" cellspacing="0" style="'.$scrollStyle.'">';
$html .= '<thead><tr><th>#</th>'; // .$this->lang['data_entry_246'].'</th>'; // Instance
$repeatingFormFields = REDCap::getDataDictionary('array', false, null, $formName);
foreach ($varList as $var) {
$relabel = preg_match("/".self::ACTION_TAG_LABEL."='(.+)'/", $repeatingFormFields[$var]['field_annotation'], $matches);
$colHeader = ($relabel) ? $matches[1] : $repeatingFormFields[$var]['field_label'];
$html .= "<th>$colHeader</th>";
$nColumns++;
}
if (!$hideFormStatus) {
$html.='<th>Form Status</th>'; // "Form Status" wording is hardcoded in MetaData::save_metadata()
$nColumns++;
}
$html.='</tr></thead>';
// if survey form get data on page load (as no add/edit and have no auth for an ajax call)
if ($this->isSurvey) {
$instanceData = $this->getInstanceData($this->record, $eventId, $formName, $varList, $filter, false, $hideChoiceValues);
if (count($instanceData) > 0) {
$html.='<tbody>';
foreach ($instanceData as $rowValues) {
$html.='<tr>';
foreach ($rowValues as $value) {
$value = $this->escape($value);
if (is_array($value)) {
$sort = (array_key_exists('sort',$value)) ? " data-sort='{$value['sort']}" : '';
$filter = (array_key_exists('filter',$value)) ? " data-filter='{$value['filter']}" : '';
if (array_key_exists('display',$value)) {
$display = $value['display'];
} else if (array_key_exists('_',$value)) {
$display = $value['_'];
} else {
$display = $value($value[array_key_first($value)]);
}
$html.="<td $sort $filter>$display</td>";
} else {
$value = str_replace('removed="window.open(','onclick="window.open(',REDCap::filterHtml($value)); // <button removed="window.open( ... >Download</button> file downloads
$html.="<td>$value</td>";
}
}
$html.='</tr>';
}
$html.='</tbody>';
} else {
// $html.='<tr><td colspan="'.$nColumns.'">No data available in table</td></tr>';
// unnecessary and DT does not support colspan in body tr // https://datatables.net/forums/discussion/32575/uncaught-typeerror-cannot-set-property-dt-cellindex-of-undefined
}
}
$html.='</table>';
if ($canEdit) {
$html.='<div style="position:relative;top:'.self::ADD_NEW_BTN_YSHIFT.';margin-bottom:5px;"><button type="button" class="btn btn-sm btn-success " onclick="'.self::MODULE_VARNAME.'.addNewInstance(\''.$this->record.'\','.$eventId.',\''.$formName.'\',\''.$linkField.'\',\''.$linkValue.'\');"><span class="fas fa-plus-circle mr-1" aria-hidden="true"></span>'.$btnLabel.'</button></div>'; // Add new
}
return $html;
}
public function getInstanceData($record, $event, $form, $fields, $filter, $includeFormStatus=true, $hideChoiceValues=false) {
global $Proj, $lang, $user_rights;
$this->Proj = $Proj;
$this->lang = &$lang;
$this->user_rights = &$user_rights;
$this->isSurvey = (PAGE==='surveys/index.php');
$instanceData = array();
// find any descriptive text fields tagged with @FORMINSTANCETABLE=form_name
if (!$this->isSurvey) {
$this->setTaggedFields();
}
$this->checkUserPermissions();
$hasPermissions = false;
foreach($this->taggedFields as $fieldDetails) {
if($fieldDetails["form_name"] == $form && $fieldDetails["permission_level"] > 0) {
$hasPermissions = true;
}
}
$recordDag = $this->getDAG($record);
if(!empty($this->user_rights["group_id"]) && $this->user_rights["group_id"] != $recordDag) {
$hasPermissions = false;
}
if(!$hasPermissions) {
return array();
}
$repeatingFormFields = REDCap::getDataDictionary('array', false, null, $form);
// ignore any supplied fields not on the repeating form
$fields = array_intersect($fields, array_keys($repeatingFormFields));
if ($includeFormStatus) { $fields[] = $form.'_complete'; }
$recordData = REDCap::getData('array', $record, $fields, $event, null, false, false, false, $filter, true); // export labels not raw
$formKey = ($this->Proj->isRepeatingEvent($event))
? '' // repeating event - empty string key
: $form; // repeating form - form name key
if (!empty($recordData[$record]['repeat_instances'][$event][$formKey])) {
foreach ($recordData[$record]['repeat_instances'][$event][$formKey] as $instance => $instanceFieldData) {
$instance = \intval($instance);
$thisInstanceValues = array();
$thisInstanceValues[] = $this->makeInstanceNumDisplay($instance, $record, $event, $form, $instance);
// foreach ($instanceFieldData as $fieldName => $value) {
foreach ($fields as $fieldName) { // loop through $fields not data array so cols can be in order specified in varlist
$value = $instanceFieldData[$fieldName];
if (is_string($value) && trim($value)==='') { // PHP 8 doesn't like trimming checkbox array!
$thisInstanceValues[] = '';
continue;
}
$fieldType = $repeatingFormFields[$fieldName]['field_type'];
if ($fieldName===$form.'_complete') {
if ($this->isSurvey) { continue; }
$outValue = $this->makeFormStatusDisplay($value, $record, $event, $form, $instance);
} else if (in_array($fieldType, array("advcheckbox", "radio", "select", "checkbox", "dropdown", "sql", "yesno", "truefalse"))) {
$outValue = $this->makeChoiceDisplay($value, $repeatingFormFields, $fieldName, $hideChoiceValues);
} else if ($fieldType==='text') {
$ontologyOption = $this->Proj->metadata[$fieldName]['element_enum'];
if ($ontologyOption!=='' && preg_match('/^\w+:\w+$/', $ontologyOption)) {
// ontology fields are text fields with an element enum like "BIOPORTAL:ICD10"
list($ontologyService, $ontologyCategory) = explode(':',$ontologyOption,2);
$outValue = $this->makeOntologyDisplay($value, $ontologyService, $ontologyCategory);
} else {
// regular text fields have null element_enum
$outValue = $this->makeTextDisplay($value, $repeatingFormFields, $fieldName);
}
} else if ($fieldType==='notes') {
$outValue = $this->makeTextDisplay($value, $repeatingFormFields, $fieldName);
} else if ($fieldType==='file') {
$outValue = $this->makeFileDisplay($value, $record, $event, $instance, $fieldName);
} else {
$outValue = htmlentities($value, ENT_QUOTES);
}
$thisInstanceValues[] = $outValue;
}
$instanceData[] = $thisInstanceValues;
}
}
return $instanceData;
}
protected function makeOpenPopupAnchor($val, $record, $event, $form, $instance, $outline=false) {
if ($this->isSurvey) {
return $val;
}
$class = ($outline) ? 'class="btn btn-xs btn-outline-secondary"' : '';
return '<a '.$class.' title="Open instance" href="javascript:;" onclick="'.self::MODULE_VARNAME.'.editInstance(\''.$record.'\','.$event.',\''.$form.'\','.$instance.');">'.$val.'</a>';
}
protected function makeInstanceNumDisplay($val, $record, $event, $form, $instance) {
return $this->makeOpenPopupAnchor($val, $record, $event, $form, $instance, true);
}
protected function makeFormStatusDisplay($val, $record, $event, $form, $instance) {
switch ($val) {
case '2':
$circle = '<img src="'.APP_PATH_IMAGES.'circle_green.png" style="height:16px;width:16px;">';
break;
case '1':
$circle = '<img src="'.APP_PATH_IMAGES.'circle_yellow.png" style="height:16px;width:16px;">';
break;
default:
$circle = '<img src="'.APP_PATH_IMAGES.'circle_red.png" style="height:16px;width:16px;">';
break;
}
return $this->makeOpenPopupAnchor($circle, $record, $event, $form, $instance);
}
protected function makeChoiceDisplay($val, $repeatingFormFields, $fieldName, $hideChoiceValues=false) {
if ($this->Proj->metadata[$fieldName]['element_type']==='sql') {
$choices = parseEnum(getSqlFieldEnum($this->Proj->metadata[$fieldName]['element_enum']));
} else {
$choices = parseEnum($this->Proj->metadata[$fieldName]['element_enum']);
}
if (is_array($val)) {
foreach ($val as $valkey => $cbval) {
if ($cbval==='1') {
$val[$valkey] = $this->makeChoiceDisplayHtml($valkey, $choices, $hideChoiceValues);
} else {
unset($val[$valkey]);
}
}
$outValue = implode('<br>', $val); // multiple checkbox selections one per line
} else {
$outValue = $this->makeChoiceDisplayHtml($val, $choices, $hideChoiceValues);
}
return REDCap::filterHtml($outValue);
}
protected function makeChoiceDisplayHtml($val, $choices, $hideChoiceValues=false) {
if (array_key_exists($val, $choices)) {
$valDisplay = ($hideChoiceValues) ? '' : ' <span class="text-muted">('.$val.')</span>';
return $choices[$val].$valDisplay;
}
return $val;
}
protected function makeTextDisplay($val, $repeatingFormFields, $fieldName) {
if (trim($val)=='') { return ''; }
$val = REDCap::filterHtml($val);
$valType = $repeatingFormFields[$fieldName]['text_validation_type_or_show_slider_number'];
switch ($valType) {
case 'date_mdy':
case 'date_dmy':
case 'datetime_mdy':
case 'datetime_dmy':
case 'datetime_seconds_mdy':
case 'datetime_seconds_dmy':
$displayVal = DateTimeRC::datetimeConvert($val, 'ymd', substr($valType, -3)); // reformat raw ymd date/datetime value to mdy or dmy, if appropriate
$outVal = array('_'=>$val, 'sort'=>$val, 'filter'=>$displayVal, 'display'=>$displayVal);
break;
case 'email':
$outVal = "<a href='mailto:$val'>$val</a>";
break;
default:
$outVal = $val;
break;
}
return $outVal;
}
protected function makeFileDisplay($val, $record, $event_id, $instance, $fieldName) {
if ($this->isSurvey) {
$surveyHash = REDCap::filterHtml($_GET['s']);
$downloadDocUrl = APP_PATH_SURVEY . "index.php?pid=".PROJECT_ID."&__passthru=".urlencode("DataEntry/file_download.php")."&doc_id_hash=".Files::docIdHash($val)."&id=$val&s=$surveyHash&record=$record&event_id=$event_id&field_name=$fieldName&instance=$instance";
} else {
$downloadDocUrl = APP_PATH_WEBROOT.'DataEntry/file_download.php?pid='.PROJECT_ID."&s=&record=$record&event_id=$event_id&instance=$instance&field_name=$fieldName&id=$val&doc_id_hash=".Files::docIdHash($val);
}
$fileDlBtn = "<button class='btn btn-defaultrc btn-xs' style='font-size:8pt;' onclick=\"window.open('$downloadDocUrl','_blank');return false;\">{$this->lang['design_121']}</button>";
return str_replace('removed=','onclick=',REDCap::filterHtml($fileDlBtn));
}
protected function makeOntologyDisplay($val, $service, $category) {
$sql = "select label from redcap_web_service_cache where project_id=? and service=? and category=? and `value`=?";
$q = $this->query($sql, [PROJECT_ID, $service, $category, $val]);
$r = db_fetch_assoc($q);
$cachedLabel = $r["label"];
$ontDisplay = (is_null($cachedLabel) || $cachedLabel==='')
? $val
: $cachedLabel.' <span class="text-muted">('.$val.')</span>';
return REDCap::filterHtml($ontDisplay);
}
protected function insertJS() {
global $lang;
$this->framework->initializeJavascriptModuleObject();
$jsmo_name = $this->framework->getJavascriptModuleObjectName();
?>
<style type="text/css">
.<?php echo self::MODULE_VARNAME;?> tbody tr { font-weight:normal; }
/*.greenhighlight {background-color: inherit !important; }*/
/*.greenhighlight table td {background-color: inherit !important; }*/
</style>
<script type="text/javascript">
'use strict';
var <?php echo self::MODULE_VARNAME;?> = (function(window, document, $, app_path_webroot, pid, simpleDialog, undefined) { // var MCRI_InstanceTable ...
var isSurvey = <?php echo ($this->isSurvey)?'true':'false';?>;
var tableClass = '<?php echo self::MODULE_VARNAME;?>';
var langYes = '<?php echo js_escape($this->lang['design_100']);?>';
var langNo = '<?php echo js_escape($this->lang['design_99']);?>';
var config = <?php echo json_encode($this->taggedFields, JSON_PRETTY_PRINT);?>;
var taggedFieldNames = [];
var lengthVal;
var lengthLbl;
var lengthChange;
var JSMO = <?=$jsmo_name?>;
function init() {
// console.log('Instance Table', config);
config.forEach(function(taggedField) {
taggedFieldNames.push(taggedField.field_name);
$('#'+taggedField.field_name+'-tr td:last')
.append(taggedField.markup);
switch(taggedField.page_size) {
case 0:
lengthVal = [10, 25, 50, 100, -1];
lengthLbl = [10, 25, 50, 100, "<?=$lang['docs_44']?>"]; // "ALL"
lengthChange = true;
break;
case -1:
lengthVal = [-1];
lengthLbl = ["<?=$lang['docs_44']?>"]; // "ALL"
lengthChange = false;
break;
default:
lengthVal = lengthLbl = [taggedField.page_size];
lengthChange = false;
}
var thisTbl;
if (isSurvey) {
thisTbl = $('#'+taggedField.html_table_id)
.DataTable( {
"stateSave": true,
"stateDuration": 0,
"lengthMenu": [lengthVal, lengthLbl],
"lengthChange": lengthChange,
"columnDefs": [{
"render": function (data, type, row) {
let val = data;
if ($.isPlainObject(data)) {
if (data.hasOwnProperty(type)) { // e.g. sort, filter for dates
val = data[type];
}
}
return val;
},
"targets": "_all"
}]
} );
if (!taggedField.show_instance_col) {
thisTbl.column( 0 ).visible( false );
}
}
else {
JSMO.ajax('get-data', taggedField.ajax).then(function(data) {
thisTbl = $('#'+taggedField.html_table_id)
.DataTable( {
"stateSave": true,
"stateDuration": 0,
"lengthMenu": [lengthVal, lengthLbl],
"lengthChange": lengthChange,
"columnDefs": [{
"render": function (data, type, row) {
let val = data;
if ($.isPlainObject(data)) {
if (data.hasOwnProperty(type)) { // e.g. sort, filter for dates
val = data[type];
}
}
return val;
},
"targets": "_all"
}],
"data": data
} );
if (!taggedField.show_instance_col) {
thisTbl.column( 0 ).visible( false );
}
});
}
});
// override global function doGreenHighlight() so we can skip the descriptive text fields with tables
var globalDoGreenHighlight = doGreenHighlight;
doGreenHighlight = function(rowob) {
if ( $.inArray(rowob.attr('sq_id'), taggedFieldNames) === -1) {
globalDoGreenHighlight(rowob);
}
};
}
$(document).ready(function() {
init();
});
function instancePopup(title, record, event, form, instance) {
var url = app_path_webroot+'DataEntry/index.php?pid='+pid+'&id='+record+'&event_id='+event+'&page='+form+'&instance='+instance+'&extmod_instance_table=1';
popupWindow(url,title,window,700,950);
//refreshTableDialog(event, form);
return false;
}
function popupWindow(url, title, win, w, h) {
var y = win.top.outerHeight / 2 + win.top.screenY - (h / 2);
var x = win.top.outerWidth / 2 + win.top.screenX - (w / 2);
return win.open(url, title, 'status,scrollbars,resizable,width='+w+',height='+h+',top='+y+',left='+x);
}
function refreshTableDialog() {
simpleDialog('Refresh the table contents and display any changes (instances added, updated or deleted).'
,'Refresh Table?',null,500
,null,langNo
,function() {
// refresh all instance tables (e.g. to pick up changes to multiple forms across repeating event
$('.'+tableClass).each(function() {
performTableRefresh(this);
});
},langYes
);
}
return {
addNewInstance: function(record, event, form, linkFld, linkIns) {
var ref = (linkFld=='')?'':'&link_field='+linkFld+'&link_instance='+linkIns;
instancePopup('Add instance', record, event, form, '1&extmod_instance_table_add_new=1'+ref);
return false;
},
editInstance: function(record, event, form, instance) {
instancePopup('View instance', record, event, form, instance);
return false;
},
getConfig: function() {
return config;
},
getJSMO: function() {
return JSMO;
}
}
})(window, document, jQuery, app_path_webroot, pid, simpleDialog);
function refreshTables() {
// refresh immediately , just in case the server has already processed the save/delete call
actuallyRefreshTables();
// now start a timer and try it again after a second, to give the server ample time to persist the data
window.setTimeout( actuallyRefreshTables(), 1500);
}
function actuallyRefreshTables() {
var tableClass = '<?php echo self::MODULE_VARNAME;?>';
$('.'+tableClass).each(function() {
performTableRefresh(this);
});
}
function performTableRefresh(tbl) {
var id = tbl.id;
var config = <?=self::MODULE_VARNAME?>.getConfig();
var jsmo = <?=self::MODULE_VARNAME?>.getJSMO();
var it = config.filter(function(x) { return x.html_table_id == id; })[0];
jsmo.ajax('get-data', it.ajax).then(function(data) {
var dt = $(tbl).DataTable();
dt.clear();
dt.rows.add(data);
dt.draw();
});
}
</script>
<?php
}
/**
* popupViewTweaks
* JS to hide unwanted elements in instance data entry popup window
* Hide:
* - left-hand menu column
* - Save & Exit Record button
* - Save & Go To Next Record button
*/
protected function popupViewTweaks() {
global $lang, $longitudinal, $Proj, $user_rights;
$record = \htmlspecialchars($_GET['id'], ENT_QUOTES);
$delFormAlertMsg = ($longitudinal) ? RCView::tt("data_entry_243") : RCView::tt("data_entry_239");
if (isset($Proj->forms[$_GET['page']]['survey_id']) && $user_rights['forms'][$_GET['page']] == '3' && isset($_GET['editresp'])) {
$delFormAlertMsg .= RCView::div(array('style'=>'margin-top:15px;color:#C00000;'), RCView::tt("data_entry_241"));
}
$delFormAlertMsg .= RCView::div(array('style'=>'margin-top:15px;color:#C00000;'), RCView::tt_i("data_entry_559", array($_GET['instance'])));
$delFormAlertMsg .= RCView::div(array('style'=>'margin-top:15px;color:#C00000;font-weight:bold;'), RCView::tt("data_entry_190"));
$delFormAlertMsg = js_escape($delFormAlertMsg, true);
?>
<style type="text/css">
.navbar-toggler, #west, #formSaveTip, #dataEntryTopOptionsButtons, #formtop-div { display: none !important; }
/*div[aria-describedby="reqPopup"] > .ui-dialog-buttonpane > button { color:red !important; visibility: hidden; }*/
#submit-btn-savenextform, #submit-btn-saveexitrecord, #submit-btn-savenextrecord { display: none; }
</style>
<script type="text/javascript">
/* EM Instance Table JS */
$.urlParamReplace = function (url, name, value) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(url);
if (results !== null) {
return url.replace(name + '=' + results[1], name + '=' + value);
} else {
return url;
}
}
$.urlGetParam = function (param_name) {
var results = new RegExp('[\?&]' + param_name + '=([^&#]*)').exec(window.location.href);
if (results !== null) {
return results[1];
} else {
return null;
}
}
$.redirectUrl = function(url) {
window.location.href = url;
return;
}
$(document).ready(function() {
// add this window.unload for added reliability in invoking refreshTables on close of popup
window.onunload = function(){window.opener.refreshTables();};
$('#form').attr('action',$('#form').attr('action')+'&extmod_instance_table=1');
// set default value of ref field, if supplied
var linkField = $.urlGetParam('link_field');
if (linkField!==null) {
var linkInput = $('[name='+linkField+']');
if (linkInput.length) {
$(linkInput).val($.urlGetParam('link_instance'));
}
}
// changes to save/cancel/delete buttons in popup window
$('button[name=submit-btn-saverecord]')// Save & Exit Form (here means save and close the popup)
.removeAttr('onclick')
.click(function(event) {
$('#form').append('<input type="hidden" name="extmod_closerec_home" value="<?=$record?>">');
event.preventDefault();
window.opener.refreshTables();
dataEntrySubmit(this);
});
$('#submit-btn-savecontinue') // Save & Stay - preserve &extmod_instance_table=1 in url when reload
.attr('name', 'submit-btn-savecontinue')
.removeAttr('onclick')
.click(function(event) {
var redirectUrl = window.location.href.replace('&extmod_instance_table_add_new=1','');
event.preventDefault();
window.opener.refreshTables();
dataEntrySubmit(this);
window.setTimeout($.redirectUrl, 500, redirectUrl);
});
$('#submit-btn-savenextinstance')// Save & Next Instance (not necessarily a new instance)
.attr('name', 'submit-btn-savenextinstance')
.removeAttr('onclick')
.click(function(event) {
var currentUrl = window.location.href;
var redirectUrl = $.urlParamReplace(currentUrl, "instance", 1+(1*$.urlGetParam("instance")));
event.preventDefault();
window.opener.refreshTables();
dataEntrySubmit(this);
window.setTimeout($.redirectUrl, 500, redirectUrl);
});
$('button[name=submit-btn-cancel]')
.removeAttr('onclick')
.click(function() {
window.opener.refreshTables();
window.close();
});
<?php
if ( isset($_GET['extmod_instance_table_add_new'])) {
?>
$('button[name=submit-btn-deleteform]').css("display", "none");
<?php
} else {
?>
$('button[name=submit-btn-deleteform]')
.removeAttr('onclick')
.click(function(event) {
$('#form').append('<input type="hidden" name="extmod_closerec_home" value="<?=$record?>">');
simpleDialog(
'<div style="margin:10px 0;font-size:13px;"><?=$delFormAlertMsg?></div>',
'<?=$lang['data_entry_237']?> \"<?=$record?>\"<?=$lang['questionmark']?>'
,null,600,null,lang.global_53,
function(){
event.preventDefault();
window.opener.refreshTables();
dataEntrySubmit( 'submit-btn-deleteform' );
},
'<?=$lang['data_entry_234']?>' //'Delete data for THIS FORM only'
);
return false;
});
<?php
}
if (isset($_GET['__reqmsg'])) {
?>
setTimeout(function() {
$('div[aria-describedby="reqPopup"]').find('div.ui-dialog-buttonpane').find('button').not(':last').hide(); // .css('visibility', 'visible'); // required fields message show only "OK" button, not ignore & leave
}, 100);
<?php
}
?>
});
</script>
<?php
}
/**
* redcap_every_page_before_render
* - When doing Save & Exit or Delete Instance from popup then persist a flag on the session so can close popup on record home page.
* - If adding a new instance, read the current max instance and redirect to a form with instance value current + 1
* @param type $project_id
*/
public function redcap_every_page_before_render($project_id) {
if (isset($_POST['extmod_closerec_home'])) {
$_SESSION['extmod_closerec_home'] = $_POST['extmod_closerec_home'];
} else if (PAGE==='DataEntry/index.php' && isset($_GET['extmod_instance_table']) && isset($_GET['extmod_instance_table_add_new']) && !is_null($project_id) && isset(($_GET['event_id']))) {
global $Proj;
$this->Proj = $Proj;
$this->isSurvey = false;
// adding new instance - read current max and redirect to + 1
$formKey = ($this->Proj->isRepeatingEvent($_GET['event_id']))
? '' // repeating event - empty string key
: $_GET['page']; // repeating form - form name key
$recordData = REDCap::getData('array',$_GET['id'],$_GET['page'].'_complete',$_GET['event_id']);
if (array_key_exists($_GET['id'],$recordData) &&
array_key_exists('repeat_instances',$recordData[$_GET['id']]) &&
array_key_exists($_GET['event_id'], $recordData[$_GET['id']]['repeat_instances'])) {
$currentInstances = array_keys($recordData[$_GET['id']]['repeat_instances'][$_GET['event_id']][$formKey]);
$_GET['instance'] = (is_null($currentInstances)) ? 1 : 1 + end($currentInstances);
} else {
$_GET['instance'] = 1;
}
}
}