-
Notifications
You must be signed in to change notification settings - Fork 4
/
import.php
311 lines (258 loc) · 9.79 KB
/
import.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
<?php
namespace Vanderbilt\DataQualityExternalModule;
$modulePid = $project_id;
global $format, $returnFormat, $post;
$module->checkApiToken();
## Trying to run module using wrong pid, could allow access of module on non-enabled projects
if($post["projectid"] != $modulePid || $post["projectid"] == "") {
die();
}
// Get user's user rights
$user_rights = UserRights::getPrivileges(PROJECT_ID, USERID);
$user_rights = $user_rights[PROJECT_ID][strtolower(USERID)];
$ur = new UserRights();
$ur->setFormLevelPrivileges();
$Proj = new Project(PROJECT_ID);
// Prevent data imports for projects in inactive or archived status
if ($Proj->project['status'] > 1) {
if ($Proj->project['status'] == '2') {
$statusLabel = "Inactive";
} elseif ($Proj->project['status'] == '3') {
$statusLabel = "Archived";
} else {
$statusLabel = "[unknown]";
}
die(RestUtility::sendResponse(403, "Data may not be imported because the project is in $statusLabel status."));
}
$insertValues = [];
$resolutionInsertIds = [];
$userIdConversion = array(NULL => NULL);
$projectSettings = $Proj->project;
$dataQualityProcess = false;
$dataCommentProcess = false;
$editComments = false;
## Check if project has data resolution workflow turned on and ability to delete data resolution comments
if($projectSettings['data_resolution_enabled'] == 2) {
$dataQualityProcess = true;
}
else if($projectSettings['data_resolution_enabled'] == 1) {
$dataCommentProcess = true;
}
else {
die(RestUtility::sendResponse(403, "Data may not be imported because the project does not have data quality enabled."));
}
if($projectSettings['field_comment_edit_delete'] == 1) {
$editComments = true;
}
$single_event = false;
if(sizeof($Proj->events)==1 && $projectSettings['repeatforms']==0){
$single_event = true;
}
## TODO Convert flat/csv data into nested array format
$importData = json_decode($post['data'],true);
foreach($importData as $dataRow) {
$statusId = $dataRow['status_id'];
$record = $dataRow['record'];
$projectId = $dataRow['project_id'];
$eventId = $dataRow['event_id'];
$fieldName = $dataRow['field_name'];
$instance = $dataRow['instance'];
$repeatInstrument = $dataRow['repeat_instrument'];
$assignedUsername = $dataRow['assigned_username'];
$newStatusId = false;
## Verify field name and event ID exist on this project. Skip this row if it doesn't
if(!array_key_exists($fieldName,$Proj->metadata)) {
continue;
}
$eventFound = false;
foreach($Proj->events as $armDetails) {
foreach($armDetails["events"] as $tempEventId => $eventDetails) {
// check for non-longitudinal project
if (count($armDetails['events']) == 1 && $single_event){
$eventId = $tempEventId;
$eventFound = true;
break 2;
}else if($eventId == $tempEventId) {
$eventFound = true;
break 2;
}
}
}
if(!$eventFound) {
continue;
}
## Skip if record/project/event/field is blank
if($record == "" || $projectId == "" || $eventId == "" || $fieldName == "") continue;
## Skip data where the projectId doesn't match the token's projectId
if($projectId != PROJECT_ID) continue;
## Confirm if this status is already in the database
$sql = "SELECT s.status_id, s.instance, s.repeat_instrument, s.record, s.project_id, s.event_id, s.field_name
FROM redcap_data_quality_status s
WHERE ".(($statusId == "" || !is_numeric($statusId)) ? "" : "s.status_id = '".db_escape($statusId)."'")."
OR (s.record = '".db_escape($record)."'
AND s.project_id = '".db_escape($projectId)."'
AND s.event_id = '".db_escape($eventId)."'
AND s.field_name = '".db_escape($fieldName)."'".
($repeatInstrument != NULL ? " AND s.repeat_instrument = '".db_escape($repeatInstrument)."'" :
($instance == NULL ? " AND s.instance is NULL" : " AND s.instance = '".db_escape($instance)."'")).")";
$q = db_query($sql);
## Cache User ID to username conversion to reduce DB calls
if(!array_key_exists($assignedUsername,$userIdConversion)) {
$userIdConversion[$assignedUsername] = User::getUIIDByUsername($assignedUsername);
}
$existingStatus = "";
while($row = db_fetch_assoc($q)) {
## Found a matching record/project/event/field/instance with a different status
if($row['status_id'] != $statusId) {
$existingStatus = $row['status_id'];
$newStatusId = true;
}
## Found a row for that statusId, verify if record/project/event/field/instance matches
else if($row['record'] == $record && $row['project_id'] == $projectId
&& $row['event_id'] == $eventId && $row['field_name'] == $fieldName){
if($instance == "" || $row['instance'] == $instance) {
$existingStatus = $row['status_id'];
$newStatusId = false;
}
}
}
## Add new status row
## Need to verify project ID and event ID are integers
if($existingStatus == ""
&& (is_int($projectId) || ctype_digit($projectId))
&& (is_int($eventId) || ctype_digit($eventId))) {
$sql = "INSERT INTO redcap_data_quality_status
(non_rule,project_id,record,event_id,field_name,instance,assigned_user_id)
VALUES (1,".db_escape($projectId).",'".db_escape($record)."',".db_escape($eventId).",'".
db_escape($fieldName)."',".checkNull($instance).",".checkNull($userIdConversion[$assignedUsername]).")";
db_query($sql);
$existingStatus = db_insert_id();
$newStatusId = true;
}
## Do date conversion on resolutions so they can be quickly sorted
foreach($dataRow['resolutions'] as $resKey => $resolutionRow) {
$dataRow['resolutions'][$resKey]['tsInSeconds'] = strtotime($resolutionRow['ts']);
}
## Sort resolutions by timestamp and get last timestamp in DB
usort($dataRow['resolutions'],function($a,$b) {
if($a['tsInSeconds'] > $b['tsInSeconds']) {
return 1;
}
else if($a['tsInSeconds'] == $b['tsInSeconds']) {
return 0;
}
return -1;
});
$sql = "SELECT r.ts,r.user_id
FROM redcap_data_quality_resolutions r
WHERE r.status_id = '".$existingStatus."'
ORDER BY r.ts DESC";
$q = db_query($sql);
$existingResolutions = array();
while($row = db_fetch_assoc($q)) {
$existingResolutions[$row['ts'].$row['user_id']] = 1;
}
$dbTs = db_result($q,0,"ts");
$lastTs = "";
foreach($dataRow['resolutions'] as $resolutionRow) {
if($editComments) {
# TODO Allow update of data resolution status/comments
//DataQuality::editFieldComment();
}
// Determine the status to set
if (in_array($resolutionRow['current_query_status'], array('OPEN','CLOSED','VERIFIED','DEVERIFIED'))) {
$resStatus = $resolutionRow['current_query_status'];
} elseif ((isset($resolutionRow['response_requested']) && $resolutionRow['response_requested'])
|| ($resolutionRow['response'] && $resolutionRow['response'])) {
$resStatus = 'OPEN';
} else {
$resStatus = '';
}
// Make sure response is in enum list
if(in_array($resolutionRow['response'],array('DATA_MISSING','TYPOGRAPHICAL_ERROR','CONFIRMED_CORRECT','WRONG_SOURCE','OTHER'))) {
$response = $resolutionRow['response'];
}
else {
$response = '';
}
$resolutionId = $resolutionRow['res_id'];
$resStatusId = $resolutionRow['status_id'];
$username = $resolutionRow['username'];
## Skip resolution rows that don't have matching status IDs unless this is a new status
if(!$newStatusId && $resStatusId != "" && $resStatusId != $existingStatus) continue;
## Cache User ID to username conversion to reduce DB calls
if(!array_key_exists($username,$userIdConversion)) {
$userIdConversion[$username] = User::getUIIDByUsername($username);
}
## Skip resolution rows that already have a resolution from the same user for the same timestamp
## This is to prevent duplicate response rows
if(array_key_exists($resolutionRow['ts'].$userIdConversion[$username],$existingResolutions)) {
continue;
}
$lastTs = $resolutionRow['ts'];
$insertValues[] = "(".checkNull($existingStatus).",".checkNull($resolutionRow['ts']).",".
checkNull($userIdConversion[$username]).",".(in_array($resolutionRow['response_requested'],[0,1]) ? $resolutionRow['response_requested'] : 0).",".
checkNull($response).",".checkNull($resolutionRow['comment']).",".
checkNull($resStatus).")";
$existingResolutions[$resolutionRow['ts'].$userIdConversion[$username]] = 1;
## Do inserts in groups of 10 to reduce DB calls
if(count($insertValues) >= 10) {
$sql = "INSERT INTO redcap_data_quality_resolutions
(status_id,ts,user_id,response_requested,response,comment,current_query_status)
VALUES ".implode(",",$insertValues);
db_query($sql);
if($e = db_error()) {
error_log("Data Quality Import: ".$e);
}
else {
$nextId = db_insert_id();
for($i = 0; $i < 10; $i++) {
$resolutionInsertIds[] = $nextId + $i;
}
}
$insertValues = [];
}
}
## If last timestamp is after last timestamp in DB, then update main query_status
if($existingStatus && $lastTs != "" && strtotime($lastTs) > strtotime($dbTs)) {
$sql = "UPDATE redcap_data_quality_status
SET query_status = ".checkNull($resStatus)."
WHERE status_id = ".$existingStatus;
db_query($sql);
}
}
if(count($insertValues) > 0) {
## Do last inserts pending in $insertValues
$sql = "INSERT INTO redcap_data_quality_resolutions
(status_id,ts,user_id,response_requested,response,comment,current_query_status)
VALUES ".implode(",",$insertValues);
db_query($sql);
if($e = db_error()) {
error_log("Data Quality Import: ".$e);
}
else {
$nextId = db_insert_id();
for($i = 0; $i < count($insertValues); $i++) {
$resolutionInsertIds[] = $nextId + $i;
}
}
$insertValues = [];
}
## TODO Send other types of responses based on request
$content = json_encode($resolutionInsertIds);
# Send the response to the requestor
RestUtility::sendResponse(200, $content, $format);
function csv($data,$headers) {
foreach($data as $dataRow) {
foreach($headers as $column => $label) {
if($label == "record") {
## If record is blank, this must be a data resolution row
if($dataRow[$column] == "") {
}
## Else, this must be a status row
else {
}
}
}
}
}