-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
1463 lines (1299 loc) · 61.5 KB
/
index.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
/**
This is an attempt to re-write the dashboard as an ajax version that allows for creating customized dashboards almost like reports with a few new features
**/
error_reporting(E_ALL);
//dirname(dirname(__FILE__)) . '/Config/init_project.php';
//require_once dirname(dirname(__FILE__)) . '/Config/init_project.php';
require_once '../../redcap_connect.php';
require_once APP_PATH_DOCROOT . "ProjectGeneral/form_renderer_functions.php";
$debug = array();
// The $config array contains key-value attributes that control the rendering of the page:
// filter = logic to be applied to filter records
// arm = active arm number being displayed
// num_per_page = number of records per page
// pagenum = current page number
// group_by = form or event for headers in longitudinal table
// excluded_forms = csv list of forms to exclude from grid
// ext_id = The bookmark ID if saved to the database
// vertical_header = 1/0 to twist header
// record_label = like a custom record label to add a text column to the dashboard..
// Set the default config values
$config = array(
'title' => 'Default Custom Dashboard',
'description' => 'A custom dashboard allows you to build separate record status views to support various workflows in your project. You can exclude certain forms and events to make it easier to read and faster to render - either by using the config panel or by clicking on the column headers. You can also add record-level filters to omit those records you are not concerned with. Lastly, there are a number of visualization enhancements such as row grouping and vertical alignment that make the table easier to read. Users with project-design rights can save custom dashboards as bookmarks to be used by other users in the proejct.',
'record_label'=>'',
'filter' => '',
'arm' => 1,
'num_per_page' => 25,
'pagenum' => 1,
'group_by' => REDCap::isLongitudinal() ? 'event' : 'form', //form or event
'vertical_header' => 1, // default to horizontal
'excluded_forms' => '',
'excluded_events' => '',
'order_by' => ''
);
// Load the query string and script URI
parse_str($_SERVER['QUERY_STRING'], $qs_params);
$scriptUri = $_SERVER['SCRIPT_URI'];
$parseUrl = parse_url($_SERVER['REQUEST_URI']);
$relativePath = $parseUrl['path'];
$debug['qs_params'] = $qs_params;
$debug['scriptUri'] = $scriptUri;
$debug['relativePath'] = $relativePath;
// Saved bookmarks use the 'settings' attribute in the query string. If set, apply these values over the defaults
$settings = isset($qs_params['settings']) ? json_decode(urldecode($qs_params['settings']),true) : NULL;
$debug['settings'] = $settings;
if (!empty($settings)) $config = array_merge($config,$settings);
$debug['config_after_settings'] = $config;
// Get User Rights
global $user_rights;
$user_rights = REDCap::getUserRights(USERID);
$user_rights = $user_rights[USERID];
// Determien whether or not the current user can 'edit' the custom dashboard (requires reports rights)
$config['can_edit'] = (SUPER_USER || $user_rights['reports']);
// This is the initial load (via GET) - so lets render the page
if (empty($_POST)) {
// RENDER THE PAGE
include APP_PATH_DOCROOT . 'ProjectGeneral/header.php';
include 'custom_dashboard.js';
renderPageTitle("<img src='".APP_PATH_IMAGES."application_view_icons.png' class='imgfix2'> Custom {$lang['global_91']}");
// Determien whether or not to display the 'config' bar:
$display_config = $config['can_edit'] ? 'block' : 'none';
if ($config['can_edit']) {
// Make the Config Button
$html = RCView::button(array('id'=>'editConfig', 'class'=>'jqbutton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only', 'onclick'=>"$('#configbox').slideToggle()"),
RCView::span(array('style'=>'font-size:10px; font-weight:normal; cursor:pointer;'),
RCView::img(array('src'=>'table__pencil.png', 'class'=>'imgfix')) . " Customize Dashboard"
)
);
print $html;
}
// print RCView::div(array('id'=>'cd_container'), customDashboard::getFullContainer($config));
print customDashboard::getConfigBox($config);
print RCView::div(
array('id'=>'cd_container'),
customDashboard::getDashboard($config)
);
print RCView::div(
array('id'=>'overlay'),
RCView::div(array('style'=>'font-size:16px; font-weight:bold; color:#333;padding:25px;'),"Updating Table...")
);
//print "Proj->events: <pre>" . print_r($Proj->events,true) . "</pre>";
// Page footer
include APP_PATH_DOCROOT . 'ProjectGeneral/footer.php';
}
// AJAX POST from AJAX to tweak settings
else
{
$debug['post'] = $_POST;
// Merge the POSTED settings into the existing array (this will override query string parameters)
if (!empty($_POST['settings'])) $config = array_merge($config,$_POST['settings']);
$debug['config_after_post'] = $config;
// DETERMINE WHAT ACTION WE ARE DOING
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
// ACTION: TESTFILTER - Called to test the validity of modified filter logic
if ($action == 'testFilter') {
$result = customDashboard::testFilter($config['filter']);
print json_encode($result);
//print RCView::div(array('id'=>'debug1','style'=>'margin-top:20px;color:#999;'),
// "DEBUG: <pre>".print_r($debug,true)."</pre>"
//);
exit();
}
// ACTION: TESTRECORDLABEL - Called to test the validity of modified record label
if ($action == 'testRecordLabel') {
$result = customDashboard::testTextLogic($config['record_label'], $config['arm']);
print json_encode($result);
//print RCView::div(array('id'=>'debug1','style'=>'margin-top:20px;color:#999;'),
// "DEBUG: <pre>".print_r($debug,true)."</pre>"
//);
exit();
}
// ACTION: SAVE - saves the current settings to a bookmark
if ($action == 'saveDashboard') {
$errors = array();
$actionLog = array();
// Determine the ext_id to use to save the bookmark
$ext_id = isset($config['ext_id']) && $config['ext_id'] > 0 ? $config['ext_id'] : NULL;
// This is a NEW resource - we have to get a new extID first
if (!$ext_id) {
// Get the next order number
$sql = "select max(link_order) from redcap_external_links where project_id = $project_id";
$q = db_query($sql);
$max_link_order = db_result($q, 0);
$next_link_order = (is_numeric($max_link_order) ? $max_link_order+1 : 1);
// Insert into table
$sql = "insert into redcap_external_links (project_id, link_order, append_record_info, append_pid) values
($project_id, $next_link_order, 0, 1)";
$actionLog['sql'] = $sql;
$q = db_query($sql);
if (!$q) {
$errors[] = "ERROR: Unable to create a new bookmark";
} else {
$new_ext_id = $ext_id = db_insert_id();
$actionLog['new_ext_id'] = $new_ext_id;
$config['ext_id'] = $new_ext_id;
}
}
// Build the url from the settings
$settings = urlencode(json_encode($config));
//$url = 'https://redcap.stanford.edu/plugins/custom_dashboard/index.php?settings=' . $settings;
// Cinly caught this omission...
$url = $relativePath . "?settings=" . $settings;
// Name the bookmark
$link_label = 'Custom Dashboard: ' . $config['title'];
$actionLog['link_label'] = $link_label;
// Update the Resource
$sql = "UPDATE redcap_external_links SET
link_label = '". prep($link_label) . "',
link_url = " . checkNull($url) . "
WHERE
ext_id = $ext_id AND
project_id = $project_id
LIMIT 1";
$actionLog['sql2'] = $sql;
$q = db_query($sql);
if (!$q) $errors[] = "ERROR: Unable to update external link $ext_id";
// Return results
if (!empty($errors)) {
$actionLog['errors'] = implode("\n",$errors);
}
print json_encode($actionLog);
}
// ACTION: DELETE - deletes the current
if ($action == 'deleteDashboard') {
$result = array();
$ext_id = isset($config['ext_id']) && $config['ext_id'] > 0 ? $config['ext_id'] : NULL;
if ($ext_id) {
$where = "ext_id = " . prep($ext_id) . " AND project_id = " . prep($project_id);
// Check existance
$sql = "SELECT link_label FROM redcap_external_links WHERE $where";
$q = db_query($sql);
$result['sql1'] = $sql;
$result['db_num_rows'] = db_num_rows($q);
if ($q && db_num_rows($q) == 1) {
// Get link label
$result['link_label'] = db_result($q, 0);
// Delete link
$sql = "DELETE FROM redcap_external_links WHERE $where";
//$result['sql'] = $sql;
$q = db_query($sql);
if (!$q) {
$result['errors'] = "ERROR: Unable to delete external link $ext_id in project $project_id";
}
} else {
$result['errors'] = "ERROR: The specified bookmark to delete ($ext_id) does not appear to be valid.";
}
} else {
$result['errors'] = "The following dashboard does not appear to be associated with an existing bookmark.";
}
print json_encode($result);
}
// ACTION: RENDER FULL DASHBOARD CONTAINER - returns the full cd_container
if ($action == 'getDashboard') {
print customDashboard::getDashboard($config);
}
// ACTION: RENDER TABLE ONLY - returns only the table / pagination
if ($action == 'getTableContainer') {
print customDashboard::getTable($config);
print dumpDebugToConsole($debug);
}
}
exit();
//// END OF SCRIPT
class customDashboard {
// Generate the code to edit the filter
public static function getConfigBox($config) {
global $Proj, $debug, $user_rights, $record_label;
$filter_logic = $config['filter'];
// Arms Options
$arms_select = false;
if ($Proj->multiple_arms) {
$arms_records = Records::getRecordListPerArm();
$arms_options = array();
foreach ($Proj->events as $arm_num => $arm_detail) {
$arm_label = $arm_detail['name'] . (isset($arms_records[$arm_num]) ? " - " . count($arms_records[$arm_num]) . " records" : " - 0 records");
$arms_options[$arm_num] = $arm_label;
}
$arms_select = RCView::select(array(
'id'=>'arm_num','class'=>'x-form-text x-form-field',
'onchange'=>'refreshDashboard();'),
$arms_options, $config['arm']);
}
// Make the Config box (initially not displayed)
$html = RCView::div(array('id'=>'configbox', 'class'=>'chklist trigger', 'style'=>"max-width:775px;display:none;"),
RCView::div(array('class'=>'chklisthdr', 'style'=>'font-size:13px;color:#393733;margin-bottom:5px;'), RCView::img(array('src'=>'gear.png', 'class'=>'imgfix'))." Configuration Options:"
).
RCView::p(array(),"Configuration options are only available to users with design rights. Permissions to each saved dashboard can be edited under the 'Add Edit Bookmarks' section.").
RCView::table(array('cellspacing'=>'5', 'class'=>'tbi', 'style'=>'width:100%'),
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "<label>Dashboard Title:</label>").
RCView::td(array('class'=>'td2'),
RCView::input(array(
'id'=>'dashboard_title',
'class'=>'x-form-text x-form-field',
'style'=>'font-size:14px;font-weight:bold;width:608px;',
'name'=>'dashboard_title',
'value'=>htmlentities($config['title'],ENT_QUOTES))
).
RCView::input(array(
'id'=>'ext_id',
'type'=>'hidden',
'name'=>'ext_id',
'value'=>$config['ext_id'])
)
)
).
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "<label>Description / Instructions:</label>").
RCView::td(array('class'=>'td2'),
RCView::textarea(array(
'class'=>'x-form-text x-form-field',
'style'=>'width:608px;height:30px;',
'id'=>'dashboard_description'),
htmlentities($config['description'],ENT_QUOTES))
)
).
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "<label>Custom Record Label:</label>").
RCView::td(array('class'=>'td2'),
RCView::textarea(array(
'class'=>'x-form-text x-form-field code',
'style'=>'width:608px;',
'id'=>'record_label',
'onchange'=>"javascript:testRecordLabel()"), $record_label)
)
).
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "<label>Filter Logic:</label>").
RCView::td(array('class'=>'td2'),
RCView::textarea(array(
'class'=>'x-form-text x-form-field code',
'style'=>'width:608px;',
'id'=>'filter_logic',
'onchange'=>"javascript:testFilter()"), $filter_logic)
)
).
($arms_select ? RCView::tr(array(),
RCView::td(array('class'=>'td1'), "<label>Arm:</label>").
RCView::td(array('class'=>'td2'),
$arms_select
)
) : '').
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "Excluded Forms:").
RCView::td(array('class'=>'td2'),
RCView::div(array('class'=>'x-form-text x-form-field','style'=>'font-weight:normal','onclick'=>'toggleExcludeForms()'),
RCView::img(array('src'=>'pencil_small2.png')).
RCView::input(array('id'=>'excluded_forms',
'style'=>'border:none; background-color: transparent; width: 580px;',
'value'=> $config['excluded_forms'],'disabled'=>'disabled')
)
).
self::renderExcludeForms($config)
)
).
( REDCap::isLongitudinal() ?
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "Excluded Events:").
RCView::td(array('class'=>'td2'),
RCView::div(array('class'=>'x-form-text x-form-field','style'=>'font-weight:normal;','onclick'=>'toggleExcludeEvents()'),
RCView::img(array('src'=>'pencil_small2.png')).
RCView::input(array('id'=>'excluded_events',
'style'=>'border:none; background-color: transparent; width: 580px;',
'value'=> $config['excluded_events'],'disabled'=>'disabled')
)
).
self::renderExcludeEvents($config)
)
) : ''
).
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "Header Orientation").
RCView::td(array('class'=>'td2'),
RCView::select(array(
'id'=>'vertical_header','class'=>'x-form-text x-form-field',
'onchange'=>"refreshDashboard();"),
array('0'=>'Horizontal (default)','1'=>'Vertical'), $config['vertical_header']
)
)
).
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "Order By (in development)").
RCView::td(array('class'=>'td2'),
RCView::select(array(
'id'=>'order_by','class'=>'x-form-text x-form-field',
'onchange'=>"refreshDashboard();"),
array('0'=>'Record ID Asc (default)','1'=>'Record ID Desc'), $config['order_by']
)
)
).
RCView::tr(array(),
RCView::td(array('class'=>'td1'), "").
RCView::td(array('class'=>'td2'),
RCView::button(array('id'=>'btn_refresh', 'class'=>'jqbuttonmed ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only','onclick'=>'refreshDashboard()', 'style'=>'margin-top: 5px;'), 'Refresh Dashboard').
RCView::button(array('id'=>'btn_save', 'class'=>'jqbuttonmed ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only','onclick'=>'saveDashboard()', 'style'=>'margin-top: 5px;' . (empty($config['ext_id']) ? 'display:none;' : '')), 'Save Dashboard').
RCView::button(array('id'=>'btn_save_new', 'class'=>'jqbuttonmed ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only','onclick'=>'saveNewDashboard()', 'style'=>'margin-top: 5px;'), 'Save New Dashboard').
RCView::button(array('id'=>'btn_delete', 'class'=>'jqbuttonmed ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only','onclick'=>'deleteDashboard()', 'style'=>'margin-top: 5px;' . (empty($config['ext_id']) ? 'display:none;' : '')), 'Delete Dashboard')
)
)
)
);
$html .= "<script>autosize( $('#configbox textarea') );</script>";
return $html;
}
// Generate the entire dashboard from head to toe
public static function getDashboard($config) {
global $user_rights, $Proj, $debug;
// Build table
$table_html = RCView::div(array('id'=>'table_container'), self::getTable($config));
//$arm_tabs_html = self::getArmSelector($config['arm']);
//$filter_html = '';//self::getConfigBox($config);
$instructions_html = self::getInstructions($config);
// Build debug (must be last)
$debug_html = RCView::div(array('id'=>'debug_container','style'=>'cursor:pointer;','onclick'=>'$("#debug").slideToggle();'),
RCView::img(array('src'=>'updown.gif', 'class'=>'imgfix')) . "SHOW DEBUG" .
RCView::div(array('id'=>'debug','style'=>'display:none;font-size:10px;margin-top:20px;color:#999;'),
"<pre>".print_r($debug,true)."</pre>"
)
);
// Show/hide filter box
return (USERID == 'andy123' ? $debug_html : '' ) .
$instructions_html .
$table_html;
}
// Returns an array the events where this field is present
public static function getValidEventsForField($field, $arm = null) {
global $Proj, $debug;
$form_name = $Proj->metadata[$field]['form_name'];
// Get all events where this form is defined
$events = array();
foreach ($Proj->eventsForms as $event_id => $forms) {
if (in_array($form_name,$forms)) {
$events[$event_id] = REDCap::getEventNames(true,false,$event_id);
}
}
// If multi-arm we might need to filter out events not in the current arm
if ($Proj->multiple_arms && isset($Proj->events[$arm])) {
// Arm is valid - get defined events in the arm
$valid_arm_events = $Proj->events[$arm]['events'];
$events = array_intersect_key($events, $valid_arm_events);
}
return $events;
}
// Test custom record label logic
public static function testTextLogic($logic, $arm = null) {
global $debug;
$new_logic = $logic;
$debug['testingTextLogic'] = $logic;
$result = array('valid'=>true);
// Apply Record Filter
if (!empty($logic)) {
// Test filter for explicit event definitions (not intuitive for users)
if (REDCap::IsLongitudinal()) {
// Get all parsed fields as event.field
$event_fields = array_keys(getBracketedFields($logic, true, true, false));
if (!empty($event_fields)) {
$errors = array();
$debug['Event Fields'] = $event_fields;
$errors[] = "Event Fields: " . json_encode($event_fields);
foreach ($event_fields as $field) {
// If missing event
if (strpos($field, '.') === false) {
// Get all valid events for this field
$field_events = self::getValidEventsForField($field, $arm);
if (count($field_events) == 0) {
$errors[] = "$field is not present in the current arm ($arm)";
} else {
$errors[] = "<b>$field</b>: <b>[" . implode("]</b>, or <b>[", $field_events) . "]</b>";
// Substitute in first valid event as guess for each missing field
$first_event = array_shift($field_events);
$pattern = '/(?<!\])\['.$field.'\]/';
$new_logic = preg_replace($pattern,'['.$first_event.']['.$field.']',$new_logic);
}
}
}
}
}
}
$result = array();
if (!empty($errors)) {
$result['valid'] = false;
$result['msg'] = "In a longitudinal project each variable must be prefixed with an explicit unique event name. The following were missing events:<div style='padding:10px;'>" . implode("<br>",$errors) . "</div>The proposed modified label assumes you want to use the first available events for each of the above fields:<div style=\"font-size:11px;font-face:Monaco, monospace;background-color:#fafafa;border:1px solid #ddd;color:#393733;padding:5px;\">" . $new_logic . "</div>Press cancel to manually modify the expression.";
$result['new_logic'] = $new_logic;
} else {
$result['valid'] = true;
}
return $result;
}
// Test the filter logic - returns an array with keys valid, msg, filter_explicit
public static function testFilter($filter_logic) {
global $debug;
$debug['testingFilter'] = $filter_logic;
$result = array('valid'=>true);
// Apply Record Filter
if (!empty($filter_logic)) {
// Test filter for explicit event definitions (not intuitive for users)
if (REDCap::IsLongitudinal()) {
// Get name of first event
$events = REDCap::getEventNames(true);
$first_event = array_shift($events);
// Verify that the filter is explicit
$filter_explicit = trim(LogicTester::logicPrependEventName($filter_logic, $first_event));
if ($filter_explicit !== $filter_logic) {
$msg = "In a longitudinal project each filter variable must be prefixed with an explicit unique event name. You can cancel to modify the filter or accept the proposed modification which assumes all fields belong to the first event/arm in the project ($first_event)<div style=\"font-size:11px;font-face:Monaco, monospace;background-color:#fafafa;border:1px solid #ddd;color:#393733;padding:5px;\">$filter_explicit</div>";
$result = array(
'valid'=>'false',
'msg'=>$msg,
'filter_explicit'=>$filter_explicit
);
}
} else {
// TBD - test a non-longitudinal filter
}
} else {
// filter is empty
}
return $result;
}
// Takes the object's recordNames array and filters it down based based on the logic supplied
public static function filterRecords($records, $filter_logic) {
global $debug;
$debug['filter_start'] = "Starting with " . count($records) . " records and $filter_logic";
// Apply Filter
if (!empty($filter_logic)) {
$start_time = microtime(true);
$start_count = count($records);
// Return an array with values of 0 or 1 for each record to determine if they pass the filter
$logicResults = self::evaluateLogicMultipleRecords($filter_logic, $records);
// Filter the recordNames array to only retain records that evaluated to true with the logic
$filteredRecords = array_filter(
$records,
function($record_id) use ($logicResults) {
return isset($logicResults[$record_id]) && $logicResults[$record_id] == 1;
}
);
// Rekey the filtered array
$records = array_values($filteredRecords);
// Prepare debug summary
$end_count = count($filteredRecords);
$filter_time = round(microtime(true) - $start_time,2);
$debug['summary'][] = "Filtered $start_count records down to $end_count in $filter_time seconds.";
}
return $records;
}
// Generates the pagenumber dropdown and slices the records to the current page.
// Returns an array of $pageNumDropdown and $recordsThisPage
private static function getPageNumAndSlice($records, $config) {
global $lang, $debug;
$num_records = count($records);
$num_per_page = $config['num_per_page'] == 'ALL' ? $num_records : $config['num_per_page'];
$num_pages = ceil($num_records/$num_per_page);
$pagenum = min($config['pagenum'],$num_pages); // Cannot specify a pagenum over the max number
$limit_begin = ($pagenum - 1) * $num_per_page;
$debug['getPageNumAndSlice'] = "NumRecords: $num_records";
// if ($num_records == 0) return array(RCView::div(array(),"No records"), array(''));
// Build drop-down list of page numbers
$pageNumDropdownOptions = array();
for ($i = 1; $i <= $num_pages; $i++) {
$end_num = $i * $num_per_page;
$begin_num = $end_num - $num_per_page + 1;
$value_num = $end_num - $num_per_page;
if ($end_num > $num_records) $end_num = $num_records;
$pageNumDropdownOptions[$i] = "Page $i of $num_pages: \"".removeDDEending($records[$begin_num-1]).
"\" {$lang['data_entry_216']} \"".removeDDEending($records[$end_num-1])."\"";
}
// Build drop-down list of records per page options, including any legacy values
$recordsPerPageOptions = array('ALL' => $lang['docs_44'] . " ($num_records)");
$defaultRecordsPerPage = array(10,25,50,100,250,500,1000);
if (is_numeric($config['num_per_page']) && !in_array($config['num_per_page'])) {
array_push($defaultRecordsPerPage,$config['num_per_page']);
sort($defaultRecordsPerPage);
}
foreach ($defaultRecordsPerPage as $opt) {
$recordsPerPageOptions[$opt] = $opt;
}
// Build the group-by selector (longitudinal only)
$groupBySelector = RCView::div(array('style'=>'display:inline-block;'),
"Group by" .
RCView::select(
array('id'=>'group_by',
'class'=>'x-form-text x-form-field',
'style'=>'margin-left:8px;margin-right:4px;padding-right:0;height:22px;',
'onchange'=>"refreshTable();"),
array('form'=>'Form','event'=>'Event'), $config['group_by'])
);
// Record Selection Block
$pageNumDropdown = RCView::div(array('style'=>'display:inline-block;'),
"Displaying".
RCView::select(array('id'=>'pagenum','class'=>'x-form-text x-form-field','style'=>'margin-left:8px;margin-right:4px;padding-right:0;height:22px;',
'onchange'=>"refreshTable();"),
$pageNumDropdownOptions, $pagenum, 1) .
"with" .
RCView::select(
array('id'=>'num_per_page',
'class'=>'x-form-text x-form-field',
'style'=>'margin-left:8px;margin-right:4px;padding-right:0;height:22px;',
'onchange'=>"refreshTable();"
), $recordsPerPageOptions, $num_per_page
) . " records per page"
);
// SLICE RESULTS
if ($num_records > $num_per_page) {
$debug[] = "Slicing: $num_records from $limit_begin for $num_per_page";
$recordNamesThisPage = array_slice($records, $limit_begin, $num_per_page, true);
} else {
$debug[] = "Not Slicing: $num_records";
$recordNamesThisPage = $records;
}
if (count($recordNamesThisPage) == 0) $recordNamesThisPage = array('');
// BUILD RESULTS
$recordToolbar =
RCView::div(array('style'=>'font-weight:bold;margin:0 4px;font-size:13px;'),
"Showing " . count($recordNamesThisPage) . " of " . $num_records . " records" .
($num_pages > 1 ? " (page $pagenum of $num_pages pages)" : "")
).
RCView::div(array('class'=>'chklist','style'=>'padding:8px 15px 7px;margin:5px 0 20px;max-width:770px;'),
$pageNumDropdown .
RCView::div(array('style'=>'display:inline-block;float:right;'),
//$recordsPerPageSelector .
(REDCap::isLongitudinal() ? $groupBySelector : '')
)
);
return array($recordToolbar, $recordNamesThisPage);
}
// Returns an array of formEvents to be displayed
private static function buildFormsEvents($group_by = 'form', $excluded_forms = array(), $excluded_events = array(), $arm = NULL) {
global $Proj, $debug;
// Get User Rights
$user_rights = REDCap::getUserRights(USERID);
$user_rights = $user_rights[USERID];
// Convert $excluded_events into $excluded_event_ids
$events = REDCap::getEventNames(true);
$excluded_event_ids = array_keys(array_intersect($events,$excluded_events));
// Build a form-events array that contains necessary information to create the colspan headers
$formsEvents = array();
if ($group_by == 'form') {
foreach ($Proj->events as $this_arm=>$arm_attr) {
// If multi-arm, filter events to current arm
//$debug['arm:'.$this_arm] = $group_by . " : " . json_encode($arm_attr);
if ($arm && $arm != $this_arm) continue;
// Loop through each instrument
foreach ($Proj->forms as $form_name=>$form_attr) {
// If user does not have form-level access to this form, then do not display it
if (isset($user_rights['forms'][$form_name]) && $user_rights['forms'][$form_name] === "0") continue;
// Check for excluded forms
if (in_array($form_name, $excluded_forms)) continue;
// Loop through each event and output each where this form is designated
foreach ($Proj->eventsForms as $this_event_id=>$these_forms) {
// If event does not belong to the current arm OR the form has not been designated for this event, then go to next loop
if (!($arm_attr['events'][$this_event_id] && in_array($form_name, $these_forms))) continue;
// Check for excluded events
if (in_array($this_event_id, $excluded_event_ids)) continue;
// Add to array
$formsEvents[] = array('form_name'=>$form_name, 'event_id'=>$this_event_id, 'form_label'=>$form_attr['menu']);
}
}
}
} else {
// Loop through each event and output each where this form is designated
// If an arm is specified, get all events defined for that arm
if ($arm) $arm_events = array_keys($Proj->events[$arm]['events']);
foreach ($Proj->eventsForms as $this_event_id=>$these_forms) {
// If multi-arm, lets skip events not in the current arm
if ($arm && !in_array($this_event_id, $arm_events)) continue;
// Check for excluded events
if (in_array($this_event_id, $excluded_event_ids)) continue;
// Loop through forms
foreach ($these_forms as $form_name) {
// If user does not have form-level access to this form, then do not display it
if (isset($user_rights['forms'][$form_name]) && $user_rights['forms'][$form_name] === "0") continue;
// Check for excluded forms
if (in_array($form_name, $excluded_forms)) continue;
// Add to array
$formsEvents[] = array('form_name'=>$form_name, 'event_id'=>$this_event_id, 'form_label'=>$Proj->forms[$form_name]['menu']);
}
}
}
// Add the colspan attributes to the array for display purposes
//$debug['formsEvents1'] = $formsEvents;
$formsEvents = self::addColSpan($formsEvents, $group_by == 'form' ? 'form_name' : 'event_id');
//$debug['formsEvents2'] = $formsEvents;
return $formsEvents;
}
// Returns a properly grouped header row of events/forms
private static function buildHeaderRows($formsEvents, $config) {
global $Proj, $showRTWS, $DDP, $debug;
$longitudinal = REDCap::isLongitudinal();
$group_by = $longitudinal ? $config['group_by'] : 'form';
// HEADERS: Add all row HTML into $rows. Add header to table first.
$hdrs = RCView::th(array('class'=>'header', 'style'=>'text-align:center;color:#800000;padding:3px;vertical-align:bottom;', 'rowspan'=>($longitudinal ? '2' : '1')),
$config['vertical_header'] ? self::wrapVerticalSpan($Proj->table_pk_label) : $Proj->table_pk_label);
// Add column for custom record label
if (!empty($config['record_label'])) {
$record_label_title = $config['record_label']; //"Custom Label";
$hdrs .= RCView::th(array('class'=>'header', 'style'=>'text-align:center;color:#800000;padding:3px;vertical-align:bottom;', 'rowspan'=>($longitudinal ? '2' : '1')),
$config['vertical_header'] ? self::wrapVerticalSpan($record_label_title) : $record_label_title);
}
// If RTWS is enabled, then display column for it
// THIS HAS NOT BEEN TESTED!!!!
if ($showRTWS) {
$hdrs .= RCView::th(array('id'=>'rtws_rsd_hdr', 'class'=>'wrap darkgreen', 'rowspan'=>($longitudinal ? '2' : '1'), 'style'=>'line-height:10px;width:100px;font-size:11px;text-align:center;padding:5px;white-space:normal;vertical-align:bottom;'),
RCView::div(array('style'=>'font-weight:bold;font-size:12px;margin-bottom:7px;'),
RCView::img(array('src'=>'databases_arrow.png', 'class'=>'imgfix')) .
$lang['ws_30']
) .
$lang['ws_06'] . RCView::SP . $DDP->getSourceSystemName()
);
}
$longHdrs = ''; //Longitudinal Header if needed
$colCount = 1; //Start with one column for the record_id
foreach ($formsEvents as $attr) {
// Add columns to header trs
$form_label = $attr['form_label'];
$event_label = $Proj->eventInfo[$attr['event_id']]['name_ext'];
// Strip arm from event labels in multi-arm projects
if ($Proj->multiple_arms) $event_label = preg_replace('/\(.*\)/','',$event_label);
$group_detail = array(
'form' => array(
'label' => $attr['form_label'],
'id' => $attr['form_name']
),
'event' => array(
'label' => $event_label,
'id' => $attr['event_id']
)
);
//print "ATTR: <pre>" . print_r($attr,true) . "</pre>";
//print "GROUP TYPES: <pre>" . print_r($group_types,true) . "</pre>";
// Add pop-up info
//$event_label = RCView::a(array('href'=>'javascript:;','onclick'=>'showEventDetail('.$attr['event_id'].');'), $event_label);
if(isset($attr['colspan'])) {
$colCount = $colCount + $attr['colspan'];
$group = $group_detail[$group_by];
// Set label based on vertical or horizontal
$label = $group['label'];
$vertical = (!$longitudinal && $config['vertical_header']);
if ($vertical) {
$label = self::wrapVerticalSpan($label, array(
'style'=>'font-size:11px;white-space:nowrap;padding:0 1px;'));
} else {
$label = RCView::span(array(
'style'=>'font-size:11px;text-align:center;white-space:normal;'),
$label);
}
// Make it excludable and add the group/id attribute for javascript routine
$hdrs .= RCView::th(array(
'class'=>'header excludable',
'style'=>'padding:2px;white-space:normal;vertical-align:bottom;' . ($vertical ? '': 'text-align:center;'),
'colspan'=>$attr['colspan'],
'e_type'=>$group_by,
'e_value'=>$group['id'],
'onClick'=>"excludeHeader('$group_by','{$group['id']}')"),
$label
);
}
// Add second header column for longitudinal projects
if ($longitudinal) {
$vertical = $config['vertical_header'];
// Take opposite of group_by from previous row
$group_by_other = ($group_by == 'form' ? 'event' : 'form');
$group = $group_detail[$group_by_other];
$label = $group['label'];
if ($vertical) {
$label = self::wrapVerticalSpan($label,array('style'=>'padding:0 1px;font-size:11px;font-weight:normal;color:#800000;'));
} else {
$label = RCView::span(array('style'=>'font-size:11px;font-weight:normal;color:#800000;'), $label);
}
$longHdrs .= RCView::th(array(
'class'=>'header excludable',
'style'=>'text-align:center;padding:0px;white-space:normal;vertical-align:bottom;',
'e_type'=>$group_by_other,
'e_value'=>$group['id'],
'onClick'=>"excludeHeader('$group_by_other','{$group['id']}')"),
$label
);
}
}
// Add a header row if a specific arm is being displayed:
$arm_row = $Proj->multiple_arms ?
RCView::tr('', RCView::th(array(
'class'=>'x-panel-header',
'colspan'=>$colCount,
'style'=>'text-align:center;'),
$Proj->events[$config['arm']]['name'])) :
'';
$rows = RCView::thead('',
$arm_row .
RCView::tr('', $hdrs).
(REDCap::isLongitudinal() ? RCView::tr('', $longHdrs) : '')
);
return $rows;
}
// Takes the content and wraps it in two spans for vertical display - adding $attributes to inner span for css
public static function wrapVerticalSpan($content, $attributes = array()) {
return RCView::span(array('class'=>'vertical-text'),
RCView::span(array_merge($attributes, array('class'=>'vertical-text-inner')), $content)
);
}
// New method for making the actual table
public static function getTable($config) {
global $Proj, $user_rights, $DDP, $project_id, $table_pk_label, $lang, $surveys_enabled, $debug, $realtime_webservice_offset_days, $realtime_webservice_offset_plusminus;
$last_lap_ts = microtime(true);
// Get all records (filtering for arm if set)
$recordNames = self::getRecordList(PROJECT_ID, $user_rights['group_id'], false, $Proj->multiple_arms ? $config['arm'] : NULL);
$debug['recordNames'] = count($recordNames) . " Records";
// Apply Record Filter
if (!empty($config['filter'])) {
// TBD? Test Filter
$recordNames = self::filterRecords($recordNames, $config['filter']);
}
// Sort Records
//if (USERID == 'andy123') print "<pre>".print_r($config,true)."</pre>";
if ($config['order_by'] == 1) {
arsort($recordNames,SORT_NUMERIC);
$recordNames = array_values($recordNames);
//if (USERID == 'andy123') print "<pre>".print_r($recordNames,true)."</pre>";
}
// Get the page dropdown header and sliced records
list($pageNumDropdown, $recordNamesThisPage) = self::getPageNumAndSlice($recordNames, $config);
// Get form status of just this page's records
$formStatusValues = Records::getFormStatus(PROJECT_ID, $recordNamesThisPage);
$numRecordsThisPage = count($formStatusValues);
// If a Record Label is defined, get it!
if (!empty($config['record_label'])) {
// Step 1: get the data for all records/field/events
//$label_fields = array_keys(getBracketedFields($config['record_label'], true, true, false));
// We should get all data first but for testing now I'm going to get it one record at a time... Shame on me.
//$recordLabelValues = Piping::replaceVariablesInLabel($config['record_label'],null,null,)
}
$lap_duration = round(microtime(true) - $last_lap_ts,2);
$last_lap_ts = microtime(true);
$debug['getFormStatus'] = "getFormStatus for $numRecordsThisPage records took in $lap_duration seconds.";
//$debug['formStatusValues'] = $formStatusValues;
//$debug['numRecordsThisPage'] = $numRecordsThisPage;
// Get custom record labels
$extra_record_labels = Records::getCustomRecordLabelsSecondaryFieldAllRecords($recordNamesThisPage);
// Determine if records also exist as a survey response for some instruments
$surveyResponses = $surveys_enabled ? Survey::getResponseStatus(PROJECT_ID, array_keys($formStatusValues)) : array();
// Determine if Real-Time Web Service is enabled, mapping is set up, and that this user has rights to adjudicate
$showRTWS = ($DDP->isEnabledInSystem() && $DDP->isEnabledInProject() && $DDP->userHasAdjudicationRights());
// If RTWS is enabled, obtain the cached item counts for the records being displayed on the page
if ($showRTWS)
{
// Collect records with cached data into array with record as key and last fetch timestamp as value
$records_with_cached_data = array();
$sql = "select r.record, r.item_count from redcap_ddp_records r
where r.project_id = $project_id and r.record in (" . prep_implode(array_keys($formStatusValues)) . ")";
$q = db_query($sql);
while ($row = db_fetch_assoc($q)) {
if ($row['item_count'] === null) $row['item_count'] = ''; // Avoid null values because isset() won't work with it as an array value
$records_with_cached_data[$row['record']] = $row['item_count'];
}
}
$lap_duration = round(microtime(true) - $last_lap_ts,2);
$last_lap_ts = microtime(true);
$debug['step2'] = "getting labels and survey responses took in $lap_duration seconds.";
$group_by = $config['group_by'];
$excluded_forms = empty($config['excluded_forms']) ? null : array_map('trim',explode(",",$config['excluded_forms']));
$excluded_events = empty($config['excluded_events']) ? null : array_map('trim',explode(",",$config['excluded_events']));
// Make summary of formEvent filter
$formEventsSummary = array();
if (count($excluded_forms)) $formEventsSummary[] = RCView::span(array('title'=>$config['excluded_forms']), count($excluded_forms) . " form" . (count($excluded_forms) > 1 ? 's' : ''));
if (count($excluded_events)) $formEventsSummary[] = RCView::span(array('title'=>$config['excluded_events']), count($excluded_events) . " event" . (count($excluded_events) > 1 ? 's' : ''));
if (count($formEventsSummary)) $debug['summary'][] = "Removing " . implode(' and ', $formEventsSummary);
$formsEvents = self::buildFormsEvents($group_by,$excluded_forms,$excluded_events,$config['arm']);
//$debug['formEvents'] = $formsEvents;
$lap_duration = round(microtime(true) - $last_lap_ts,2);
$last_lap_ts = microtime(true);
$debug['filter_formsEvents'] = "Calculating formsEvents took in $lap_duration seconds.";
$recordLockSigStatus = self::getLockAndEsignedStatus($project_id, $formStatusValues);
$debug['formStatusValues'] = print_r($formStatusValues,true);
$debug['locksigstatus'] = print_r($recordLockSigStatus,true);
// Look to see if there are some locked and/or esigned records
$results = self::getLockAndEsignedStatus($project_id, $formStatusValues);
$displayLocking = $results[0];
$locked_records = $results[1];
$displayEsignature = $results[2];
$esigned_records = $results[3];
$lockimg_display = count($locked_records) == 0 ? false : true;
$esigimg_display = count($esigned_records) == 0 ? false : true;
// Start building the table with the Header rows
$rows = self::buildHeaderRows($formsEvents, $config);
// IF NO RECORDS EXIST, then display a single row noting that
if (empty($formStatusValues))
{
$rows .= RCView::tr('',
RCView::td(array('class'=>'data','colspan'=>count($formsEvents)+($showRTWS ? 1 : 0)+1,'style'=>'font-size:12px;padding:10px;color:#555;'),
$lang['data_entry_179']
)
);
}
// ADD ROWS: Get form status values for all records/events/forms and loop through them
foreach ($formStatusValues as $this_record=>$rec_attr)
{
// For each record (i.e. row), loop through all forms/events
$this_row = RCView::td(array('class'=>'data','style'=>'font-size:12px;padding:0 10px;'),
// For longitudinal, create record name as link to event grid page
(REDCap::isLongitudinal()
? RCView::a(array('href'=>APP_PATH_WEBROOT . "DataEntry/record_home.php?pid=$project_id&arm=".$config['arm']."&id=".removeDDEending($this_record), 'style'=>'text-decoration:underline;'), removeDDEending($this_record))
: removeDDEending($this_record)
) .
// Display custom record label or secondary unique field (if applicable)
(isset($extra_record_labels[$this_record]) ? ' ' . $extra_record_labels[$this_record] : '')
);
if (!empty($config['record_label'])) {
$this_row .= RCView::td(array(
'class'=>'data',
'style'=>'font-size:12px;padding:0 5px;'
), RCView::div(array('style'=>'white-space: nowrap;'),
// We should get all data for the piping and pass it in as the 4th argument, but for now I'm going to do it one record at a time... Shame on me.
Piping::replaceVariablesInLabel($config['record_label'], $this_record)
)
);
}
// If RTWS is enabled, then display column for it
if ($showRTWS) {
// If record already has cached data, then obtain count of unadjudicated items for this record
if (isset($records_with_cached_data[$this_record])) {
// Get number of items to adjudicate and the html to display inside the dialog
if ($records_with_cached_data[$this_record] != "") {
$itemsToAdjudicate = $records_with_cached_data[$this_record];
} else {
list ($itemsToAdjudicate, $newItemsTableHtml)
= $DDP->fetchAndOutputData($this_record, null, array(), $realtime_webservice_offset_days, $realtime_webservice_offset_plusminus,
false, true, false, false);
}
} else {
// No cached data for this record
$itemsToAdjudicate = 0;
}
// Set display values