-
Notifications
You must be signed in to change notification settings - Fork 0
/
datalookup.php
199 lines (184 loc) · 5.45 KB
/
datalookup.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
<?php
if ( ! $doDataLookup && ! isset( $_SERVER['HTTP_X_RC_ECF_REQ'] ) )
{
exit;
}
if ( ! $doDataLookup ) // AJAX request
{
header( 'Content-Type: application/json' );
$name = $_POST['name'];
$args = json_decode( $_POST['args'], true );
}
$result = '';
// Only perform a lookup if custom data lookup is enabled.
if ( $module->getProjectSetting( 'custom-data-lookup-enable' ) )
{
// Loop through the custom data lookup settings to find the lookup name.
$lookupIndex = -1;
$listLookupNames = $module->getProjectSetting( 'custom-data-lookup-name' );
for ( $i = 0; $i < count( $listLookupNames ); $i++ )
{
if ( $listLookupNames[$i] == $name )
{
$lookupIndex = $i;
break;
}
}
// If the specified lookup name was found in the settings:
if ( $lookupIndex >= 0 )
{
// Get the corresponding settings.
$lookupProject = $module->getProjectSetting( 'custom-data-lookup-project' )[ $lookupIndex ];
$lookupFilter = $module->getProjectSetting( 'custom-data-lookup-filter' )[ $lookupIndex ];
$lookupField = $module->getProjectSetting( 'custom-data-lookup-field' )[ $lookupIndex ];
$lookupUseLabel = $module->getProjectSetting( 'custom-data-lookup-use-label' )[ $lookupIndex ];
$lookupType = $module->getProjectSetting( 'custom-data-lookup-type' )[ $lookupIndex ];
$lookupListSep = $module->getProjectSetting( 'custom-data-lookup-list-sep' )[ $lookupIndex ];
$lookupCheckboxSplit =
( $module->getProjectSetting( 'custom-data-lookup-split-checkbox' )[ $lookupIndex ] === true
&& REDCap::getFieldType( $lookupField ) === 'checkbox' );
// Allow a newline to be specified as the list separator using '\n'.
if ( $lookupListSep == "\\n" )
{
$lookupListSep = "\n";
}
// If a project is not specified, use the current project.
if ( $lookupProject == '' )
{
$lookupProject = $module->getProjectId();
}
// Replace '?' placeholders with the supplied arguments.
foreach ( $args as $arg )
{
$pos = strpos( $lookupFilter, '?' );
if ( $pos === false )
{
break;
}
$lookupFilter = substr_replace( $lookupFilter, "'" . addslashes( $arg ) . "'", $pos, 1 );
}
$lookupFilter = str_replace( '?', "''", $lookupFilter );
// Attempt to perform the lookup.
try
{
$lookupResult =
json_decode( REDCap::getData( [ 'project_id' => $lookupProject,
'return_format' => 'json',
'filterLogic' => $lookupFilter,
'exportDataAccessGroups' => true,
'exportSurveyFields' => true,
'combine_checkbox_values' => true, // excl unchkd items
'exportAsLabels' => $lookupUseLabel ] ),
true );
// Remove any returned records where the lookup field is empty.
if ( count( $lookupResult ) > 0 )
{
foreach ( $lookupResult as $lookupResultIndex => $lookupResultItem )
{
if ( $lookupResultItem[$lookupField] == '' )
{
unset( $lookupResult[$lookupResultIndex] );
}
}
$lookupResult = array_values( $lookupResult );
}
// Determine the total non-blank results and return a value if the total is at least 1.
if ( ( $lookupCount = count( $lookupResult ) ) > 0 )
{
if ( $lookupType == 'list' )
{
// List each item, separated by the defined separator.
foreach ( $lookupResult as $lookupResultItem )
{
if( $lookupCheckboxSplit )
{
$cbResult = explode(',', $lookupResultItem[$lookupField]);
foreach($cbResult as $cbItem)
{
if ( $result != '' )
{
$result .= $lookupListSep;
}
$result .= $cbItem;
}
}
else
{
if ( $result != '' )
{
$result .= $lookupListSep;
}
$result .= $lookupResultItem[$lookupField];
}
}
}
elseif ( $lookupType == 'plus' )
{
// Return the first item, with an indication of how many more items there are
// (if the total is greater than 1).
$cbCount = 0;
if( $lookupCheckboxSplit )
{
foreach ( $lookupResult as $lookupResultItem )
{
$cbResult = explode(',', $lookupResultItem[$lookupField]);
if($cbCount == 0)
{
$result = $cbResult[0];
}
$cbCount += count($cbResult);
}
$lookupCount = $cbCount;
}
else
{
$result = $lookupResult[0][$lookupField];
}
if ( $lookupCount > 1 )
{
$result .= ' (+' . ( $lookupCount - 1 ) . ')';
}
}
elseif ( $lookupType == 'count' )
{
// Return only the total.
$cbCount = 0;
if( $lookupCheckboxSplit )
{
foreach ( $lookupResult as $lookupResultItem )
{
$cbResult = explode(',', $lookupResultItem[$lookupField]);
$cbCount += count($cbResult);
}
$lookupCount = $cbCount;
}
$result = $lookupCount;
}
else
{
// Return only the first item.
if( $lookupCheckboxSplit )
{
$cbResult = explode(',', $lookupResult[0][$lookupField]);
$result = $cbResult[0];
}
else
{
$result = $lookupResult[0][$lookupField];
}
}
}
}
// Ignore any exceptions, as any error in finding the project, applying filter logic, etc.
// means we return the empty string, which the $result variable has already been set to.
catch ( Exception $e ) {}
}
}
if ( $doDataLookup )
{
return $result;
}
else
{
$module->echoText( json_encode( $result ) );
}