-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocompleteExternalModule.php
More file actions
312 lines (238 loc) · 10.4 KB
/
Copy pathAutocompleteExternalModule.php
File metadata and controls
312 lines (238 loc) · 10.4 KB
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
<?php
/// A sample piece of code.
/**
* AutocompleteExternalModule
* - CLASS for some features.
*
*
* - MGB - Mass General Brigham DRO.
* @author David L. Heskett
* @version1.0.0
* @date20250710
* @copyright © 2025 Mass General Brigham, DRA, Digital Research Applications <a href="https://rc.partners.org//">MGB DRA\</a> <a href="https://redcap.partners.org/redcap/">redcap.partners.org</a>
*/
namespace MGB\AutocompleteExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use \HtmlPage;
use \Logging;
use \REDCap;
class AutocompleteExternalModule extends AbstractExternalModule
{
public $debug_mode_log_system;
public $debug_mode_log_project;
private $projectId;
const NAME_IDENTIFIER = 'Autocomplete';
const TAG_NAME_IDENTIFIER = '@AUTOCOMPLETE';
// **********************************************************************
// **********************************************************************
// **********************************************************************
/**
* loadSystemConfig - System configuration settings here.
*/
public function loadSystemConfig()
{
$this->debug_mode_log_system = $this->getSystemSetting('debug_mode_log_system');
// put some of your other config settings here
}
/**
* loadProjectConfig - Project configuration settings here.
*/
public function loadProjectConfig($projectId = 0)
{
if ($projectId == 0) {
global $Proj;
$projectId = $Proj->project_id;
}
if ($projectId > 0) {
$this->debug_mode_log_project = $this->getProjectSetting('debug_mode_log_project');
// put some of your other config settings here
}
}
/**
* loadProjectConfigDefaults - set up our defaults.
*/
public function loadProjectConfigDefaults()
{
$this->debug_mode_log_system = false;
$this->debug_mode_log_project = false;
}
// **********************************************************************
// **********************************************************************
// **********************************************************************
/**
* redcap_survey_page - redcap survey page will show our autocomplete data in a field.
*/
public function redcap_survey_page($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance)
{
echo $this->buildJs($project_id, $instrument);
}
/**
* redcap_data_entry_form - redcap data entry form will show our autocomplete data in a field.
*/
public function redcap_data_entry_form($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance)
{
echo $this->buildJs($project_id, $instrument);
}
// getProjectTagFields() returns an array of all project instrument fields in which some tag appears.
// Each array contains the variable Name of the field,
// and an array of elements_index and params
//
// getProjectTagFields() replaces hooks init_tags()
//
// **OLD**
// init_tags() returns an array of all project instrument fields in which some tag
// appears. Each array contains the variable Name of the field, and an array of
// elements_index and params
//
// $tag_functions = init_tags(__FILE__);
//
// see hooks_common.php and some Luke Stevens variant
// see: hooks/redcap_hooks/framework/hooks_common.php
/**
* getProjectActionTagFields - find targetTag in the project metadata, we are hunting down the action tags here.
*/
public function getProjectActionTagFields($targetTag = '')
{
global $Proj;
// Get project metadata
$metadata = $Proj->metadata;
// Initialize array to store matching fields
$matchingFields = [];
foreach ($metadata as $field_name => $field_info) {
if(stripos($field_info['misc'], $targetTag) !== false){
$matchingFields[$field_name] = $field_name;
}
}
return $matchingFields;
}
/**
* buildJs - gets fields with specified Action Tag, build the JS code using data from the fields list, returns the JS as a string.
*/
public function buildJs($project_id = '', $instrument = '')
{
$this->debug_mode_log_project = $this->getProjectSetting('debug_mode_log_project');
$tag = AutocompleteExternalModule::TAG_NAME_IDENTIFIER;
$msg = 'Running Autocomplete - ' . $tag . ' for PID: ' . $project_id . ', INSTR: [' . $instrument . ']';
$this->emlogging($msg);
$tag_functions = $this->getProjectActionTagFields($tag);
if (empty($tag_functions)) {
return;
}
// If this particular tag is not used on this survey/form, no need to load
if (!isset($tag_functions)) {
return;
}
// Ok, tag is used in this project - process it
$msg = 'Running ' . $tag . ' for PID: ' . $project_id . ', INSTR: [' . $instrument . ']';
$this->emlogging($msg);
// Step 1 - Create array of fields containing the tag, by variable name
$startup_vars = $tag_functions;//[$tag];
$msg = 'vars = ' . var_export($startup_vars,true);
$this->emlogging($msg);
// Step 2 - include the file that actually puts the JS in there
$jsString = $this->makeJs($startup_vars);
return $jsString;
}
/**
* makeJS - using a list of fields given, do a SQL lookup to get data from previous records to build a dropdown list to select from in the field for a record.
*/
public function makeJS($startup_vars = '')
{
global $Proj;
$nl = "\n";
$html = '';
// find the correct data table for the project
$dataTable = method_exists('\REDCap', 'getDataTable') ? REDCap::getDataTable($Proj->project_id) : 'redcap_data';
$html .= "<script type='text/javascript'>";
foreach($startup_vars as $field => $details) {
$field_tags = '';
$field_type = $Proj->metadata[$field]['element_type'];
if (strtolower(trim($field_type)) == 'text') {
$sqlParams[] = db_escape($Proj->project_id); // PROJECT ID
$sqlParams[] = strip_tags($field);
// select value as value from redcap_data2 where project_id=? and field_name='?' group by value order by value;
//
$recordSql = 'SELECT value AS value FROM ' . $dataTable . ' WHERE project_id = ? AND field_name = ? GROUP BY VALUE ORDER BY VALUE;';
$queryResult = $this->sqlQueryAndExecute($recordSql, $sqlParams);
while ($row = $queryResult->fetch_assoc()) {
if (strlen(trim(strip_tags($row['value']))) > 0) {
$field_tags .= ',"' . $row['value'] . '"';
}
}
unset($sqlParams); // clear the params
}
if (strlen($field_tags) > 0) {
$field_tags = substr($field_tags, 1);
// Only do this if there is some data in the field
$html .= "$(document).ready(function() {
var tags$field = [$field_tags];
$( $(\"[name = '$field']\") ).autocomplete({
autoFocus: true,
source: tags$field,
});
// Handle TAB key to select the first item
$( $(\"[name = '$field']\") ).keydown( function(e) {
if( e.keyCode != $.ui.keyCode.TAB) return;
e.keyCode = $.ui.keyCode.DOWN;
$(this).trigger(e);
e.keyCode = $.ui.keyCode.ENTER;
$(this).trigger(e);
$(this).siblings(\"input\").select();
});
});";
}
} // end foreach startup_vars
$html .= '</script>';
$html .= $nl;
$html .= '';
return $html;
}
/**
* emlogging - logging.
*/
public function emlogging($msg)
{
if ($this->debug_mode_log_project) {
if ($msg) {
$this->log($msg);
}
}
}
/**
* sqlQueryAndExecute - encapsulate some of the repeative details. pass one param or many params.
*/
private function sqlQueryAndExecute($sql, $params = null)
{
$queryResult = null;
try {
$query = ExternalModules::createQuery();
$this->queryHandle = $query;
if ($params == null) {
$this->emlogging('ERROR: NO Params: ' . $sql);
return null;
}
$query->add($sql, $params);
$queryResult = $query->execute();
}
catch (Throwable $e) {
$this->emlogging('ERROR: ' . $sql . ' err:' . $e->__toString());
}
return $queryResult;
}
/**
* testMethod - USE ONLY IN DEVELOPMENT and retained for any future DEV effort. A simple test method to show the JS code produced.
*/
public function testMethod($pid, $inst)
{
$this->debug_mode_log_project = $this->getProjectSetting('debug_mode_log_project');
$msg = 'TEST: Pid = [' . $pid . ']' . ' Form = [' . $inst . ']';
$this->emlogging($msg);
$js = $this->buildJs($pid, $inst);
echo $js;
}
// **********************************************************************
// **********************************************************************
// **********************************************************************
} // *** end class
?>