-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUniqueActionTag.php
319 lines (263 loc) · 9.85 KB
/
UniqueActionTag.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
<?php namespace STPH\UniqueActionTag;
use Exception;
if (file_exists("vendor/autoload.php")) {
require 'vendor/autoload.php';
}
class UniqueActionTag extends \ExternalModules\AbstractExternalModule {
private Array $data;
private Array $errors;
private Array $params;
private Array $DTO;
private Object $request;
/**
* ActionTag Definitions
*
*/
private $actionTagUnique = [
"tag" => "@UNIQUE",
"params" => [
[
"name" => "with_all_records",
"type" => "boolean",
"default" => false,
"required" => false
],
[
"name" => "with_all_instances",
"type" => "boolean",
"default" => false,
"required" => false
],
[
"name" => "with_all_events",
"type" => "boolean",
"default" => false,
"required" => false
],
[
"name" => "targets",
"type" => "array",
"required" => false
],
[
"name" => "title",
"type" => "number",
"required" => false
],
[
"name" => "message",
"type" => "string",
"required" => false
]
],
"allowed_field_types" => [
"text"
],
// allow to be used without parameters, default=true
"allowFlat" => true,
// allow to be used with multiple instances of same action tag on the same field
"allowMultiple" => false,
// allow to be used with other action tags, default=true
//"allowStacking" => []
];
private $actionTagTest = [
"tag" => "@TEST",
"allowed_field_types" => [],
"allowFlat" => true,
"allowMultiple" => true,
"allowStacking" => ["@UNIQUE"]
];
/**
* REDCap Hook - AJAX
*/
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){
// Set query parameters per ajax request
$this->request = (object)[
"project_id" => $project_id,
"record" => $record,
"instrument" => $instrument,
"event_id" => $event_id,
"repeat_instance" => $repeat_instance,
"survey_hash" => $survey_hash
];
// Switch action
switch ($action) {
case 'check-unique':
$response = $this->ajax_check_unique($payload);
break;
default:
break;
}
return $response;
}
/**
* REDCap Hook - Data Entry Form
*/
public function redcap_data_entry_form ( int $project_id, string $record = NULL, string $instrument, int $event_id, int $group_id = NULL, int $repeat_instance = 1 ): void
{
$this->getModuleParams();
$this->getModuleData($project_id, $instrument, $record, $event_id, $repeat_instance, NULL);
$this->getDataTransferObject();
$this->renderStyles();
$this->renderStaticHTML();
$this->initializeJavascriptModuleObject();
$this->renderJavascript($record);
}
private function getModuleParams() {
// Gather strict-unique exceptions
$exceptions = array();
$exceptions_str = $this->getProjectSetting("exceptions");
if(!empty($exceptions_str)) {
$exceptions = array_map('trim', explode(',', $exceptions_str ));
}
$this->params = [
"exceptions" => (Array) $exceptions,
"show_debug" => (bool) $this->getProjectSetting("javascript-debug") === true,
"show_errors" => (bool) $this->getProjectSetting("show-errors") === true,
"show_labels" => (bool) $this->getProjectSetting("show-labels") === true,
"enable_hard_check" => (bool) $this->getProjectSetting("enable-hard-check") === true,
"disable_summary" => (bool) $this->getProjectSetting("disable-summary") === true
];
}
private function getModuleData($project_id, $instrument, $record, $event_id, $instance, $survey_hash = null)
{
if (!class_exists("ActionTagHelper")) include_once("classes/ActionTagHelper.php");
$actionTagHelper = new ActionTagHelper();
$actionTagHelper->define($this->actionTagUnique);
//$actionTagHelper->define($this->actionTagTest);
list($this->data, $this->errors) = $actionTagHelper->getData(null, [$instrument]);
}
private function getDataTransferObject() {
$queue = [];
$data = [];
foreach ($this->data as $field => $tags) {
foreach ($tags as $tag => $detail) {
if(!(array)$detail["errors"]) {
$queue[] = array(
"field"=> $field,
"tag" => $tag,
"checked" => false
);
}
$data[] = $detail;
}
}
$this->DTO = array(
"data" => $data,
"errors" => $this->errors,
"params" => $this->params ,
"summary" => array(
"queue" => $queue,
"duplicates" => []
)
);
}
private function renderStaticHTML() {
$path = $this->getUrl('src/unique_modal.html');
$html = file_get_contents($path);
echo $html;
}
private function renderStyles() {
?>
<link rel="stylesheet" href="<?= $this->getUrl('css/style.css')?>">
<?php
}
private function renderJavascript($record){
?>
<script>
/**
* Passthrough:
* JavascriptModuleObject JS(M)O
* Data Transfer Object DTO
*
*/
const JSO_STPH_UAT = <?=$this->getJavascriptModuleObjectName()?>;
const DTO_STPH_UAT = <?= json_encode($this->DTO) ?>;
</script>
<script
type="module"
src="<?php print $this->getUrl('dist/unique.js'); ?>">
</script>
<?php
}
private function ajax_check_unique($payload) {
if(empty($payload)) {
throw new Exception("Payload must be not empty.");
}
list($data, $value) = $payload;
return $this->query_unique($data, $value);
}
/**
* Query method to check for uniqueness with different parameters
* Returns array with duplicate entries:
* $duplicate = array('event_id' => , 'record' => , 'field_name' => , 'instance' => )
*
*/
private function query_unique($data, $value) {
$field = $data["field"];
$params = (object) $data["params"];
$tag = $data["tag"];
$duplicates = [];
# Request parameters from ajax hook
$request = $this->request;
# Support multiple redcap_data tables
$data_table = method_exists('\REDCap', 'getDataTable') ? \REDCap::getDataTable($request->project_id) : "redcap_data";
# Prepare query
$query = $this->createQuery();
# base query
# ignores empty records
$sql = "SELECT * FROM ".$data_table." WHERE project_id = ? AND value = ? AND record != ''";
$prepared = [$request->project_id, $value];
# defaults to check all records but current
$sql .= " AND record != ?";
$prepared[] = $request->record;
# defaults to check only current event
if ($params->with_all_events !== true) {
$sql .= " AND event_id = ?";
$prepared[] = $request->event_id;
}
# defaults to check only current instance
if ($params->with_all_instances !== true) {
$sql .= " AND IFNULL(INSTANCE,1)= ?";
$prepared[] = $request->repeat_instance;
} else {
$sql .= " AND IFNULL(instance, 1) != ?";
$prepared[] = $request->repeat_instance;
}
$query->add($sql, $prepared);
# Specify all fields to be checked including current field and targets
if(isset($params->targets) && is_array($params->targets) && count($params->targets) > 0) {
$fields = array_merge( [$field], $params->targets);
} else {
$fields = [$field];
}
$query->add("AND")->addInClause('field_name', $fields);
$result = $query->execute();
while($row = $result->fetch_assoc()) {
$row["trigger"] = array(
"field" => $field,
"tag" => $tag
);
$duplicates[]= $row;
}
// Additional query within record if we have all records and params set
if($params->with_all_records == true && isset($params->targets) && is_array($params->targets) && count($params->targets) > 0) {
$duplicates_war= [];
$sql_war = "SELECT * FROM ".$data_table." WHERE project_id = ? AND value = ? AND record = ?";
$prepared_war = [$request->project_id, $value, $request->record];
$query_war = $this->createQuery();
$query_war->add($sql_war, $prepared_war);
$query_war->add("AND")->addInClause("field_name", $params->targets);
$result_war = $query_war->execute();
while($row = $result_war->fetch_assoc()) {
$row["trigger"] = array(
"field" => $field,
"tag" => $tag
);
$duplicates_war[]= $row;
}
$duplicates = array_merge($duplicates, $duplicates_war);
}
return $this->escape($duplicates);
}
}