-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataResolutionReminder.php
More file actions
326 lines (288 loc) · 12.2 KB
/
DataResolutionReminder.php
File metadata and controls
326 lines (288 loc) · 12.2 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
namespace UWMadison\DataResolutionReminder;
use ExternalModules\AbstractExternalModule;
use REDCap;
class DataResolutionReminder extends AbstractExternalModule
{
/*
*Redcap hook to load trivial css on project's config page
*/
public function redcap_every_page_top($project_id)
{
if ($this->isPage("ExternalModules/manager/project.php") && $project_id != NULL) {
?>
<style>
.external-modules-input-td label {
display: inline;
}
.sub_start[field$=-em-drr] td {
background-color: #e6e6e6;
}
</style>
<?php
}
}
/*
*Redcap cron
*/
public function cron($cronInfo)
{
// Stash original PID, probably not needed, but docs recommend
$originalPid = $_GET['pid'];
// Get server info (has protocol + domain)
global $redcap_base_url;
// Loop over every pid using this EM
foreach ($this->getProjectsWithModuleEnabled() as $pid) {
// Act like we are in that project, make a link, call core function
$_GET['pid'] = $pid;
$link = "{$redcap_base_url}redcap_v" . REDCAP_VERSION . "/DataQuality/resolve.php?pid=$pid&status_type=OPEN";
$this->checkForSelfReminders($pid, $link);
$this->checkForGroupReminders($pid, $link);
}
// Put the pid back the way it was before this cron job
// likely doesn't matter, but is good housekeeping practice
$_GET['pid'] = $originalPid;
return "The \"{$cronInfo['cron_description']}\" cron job completed successfully.";
}
/*
*Core functionality, send users reminders if they own an open data query
*/
private function checkForSelfReminders($project_id, $project_link)
{
$enable = $this->getProjectSetting("self_send", $project_id);
if (!$enable) {
return;
}
$projectName = $this->getTitle();
$sent = $this->getProjectSetting("self_sent", $project_id);
$frequency = $this->getProjectSetting("self_frequency", $project_id)[0] ?? 7; // Default to 7 days if not set
$hour = intval($this->getProjectSetting("self_hour", $project_id)[0] ?? "9"); // Default to 9 AM if not set
$hour = max(0, min(23, $hour)); // Ensure hour is between 0 and 23
$hour = str_pad($hour, 2, '0', STR_PAD_LEFT); // Pad hour to two digits
$now = date("Y-m-d") . " $hour:00";
$target = date('Y-m-d H:00', strtotime("$sent + $frequency days"));
if (!empty($sent) && $target > $now) {
return; // Not enough time has passed to send the next reminder
}
$statusIDs = $this->getProjectResolutions($project_id, true);
// If we have no status IDs then bail
if (empty($statusIDs)) {
return;
}
// Regroup by user, include only open statuses
$users = $this->getProjectUsers();
$map_id_name = array_combine(array_column($users, 'id'), array_keys($users));
$statusIDs = array_filter($statusIDs, function ($status) {
return $status['open'];
});
$userStatusIDs = [];
foreach ($statusIDs as $id => $status) {
$user_id = $status['user'];
$userStatusIDs[$map_id_name[$user_id]][$id] = $status;
}
// Loop over each user with a status ID
foreach ($userStatusIDs as $user => $statuses) {
$link = "<a href=\"$project_link\">$projectName</a>";
$msg = "There are open data queries in the REDCap project \"$link\" that need to be addressed.";
$this->sendEmail($users[$user]['email'], $msg);
}
// Update the project setting to reflect that we sent a reminder
$this->setProjectSetting("self_sent", date("Y-m-d H:00"), $project_id);
}
/*
*Util: Get all project data quality resolutions
*/
private function getProjectResolutions($project_id, $requireUser = false)
{
// Gather all valid Status IDs for project ID
$sql = 'SELECT status_id, field_name, event_id, instance, record, assigned_user_id, query_status
FROM redcap_data_quality_status
WHERE project_id = ?';
if ($requireUser) {
$sql .= ' AND assigned_user_id IS NOT NULL';
}
$result = $this->query($sql, [$project_id]);
$statusIDs = [];
while ($row = $result->fetch_assoc()) {
$statusIDs[$row['status_id']] = [
'field' => $row['field_name'],
'event' => $row['event_id'],
'instance' => $row['instance'],
'record' => $row['record'],
'user' => $row['assigned_user_id'],
'open' => $row['query_status'] === 'OPEN'
];
}
return $statusIDs;
}
/*
*Util: Get all users in the project, reformat to username => [id, email]
*/
private function getProjectUsers()
{
// Gather User IDs and reformat
$users = array_map(function ($obj) {
return $obj->getUsername();
}, $this->getUsers());
$query = $this->createQuery();
$query->add('
SELECT ui_id, username, user_email
FROM redcap_user_information
WHERE');
$query->addInClause('username', $users);
$result = $query->execute();
$projectUsers = [];
while ($row = $result->fetch_assoc()) {
$projectUsers[$row['username']] = [
'id' => $row['ui_id'],
'email' => $row['user_email']
];
}
return $projectUsers;
}
/*
*Util: Basic wrapper for sending emails
*/
private function sendEmail($to, $msg)
{
global $project_contact_email;
$from = $project_contact_email;
$subject = "[REDCap] Data query reminder";
REDCap::email($to, $from, $subject, $msg);
}
/*
*Core functionality, check a pid for DQ reminders and send emails
*/
private function checkForGroupReminders($project_id, $project_link)
{
// Gather project settings
$settings = $this->getProjectSettings($project_id);
$sentSetting = $settings['sent'];
$updateProjectSetting = false;
$now = date("Y-m-d H:i");
$projectName = $this->getTitle();
$projectUsers = $this->getProjectUsers();
$statusIDs = $this->getProjectResolutions($project_id);
// If we have no status IDs then bail
if (empty($statusIDs)) {
return;
}
// Loop over all setting groups (using freq for no real reason)
foreach ($settings['frequency'] as $index => $freq) {
$condition = $settings['condition'][$index];
$days = intval($settings['days'][$index]);
$userList = $settings['user'][$index];
$sent = $settings['sent'][$index];
$dagList = array_filter($settings['dag'][$index]);
$sendDetails = $settings['details'][$index];
if (empty($condition) || empty($freq) || (empty($userList) && empty($dagList))) {
continue; // We need every setting, except days which we parse to int (i.e. ""->0)
}
if ($sent != "" && $now < date('Y-m-d H:i', strtotime("$sent + $freq days"))) {
continue; // Not enough time has passed to send the next reminder
}
// Expand userList to include those in DAGs
if (!empty($dagList)) {
$query = $this->createQuery();
$query->add('
SELECT username
FROM redcap_data_access_groups_users
WHERE');
$query->addInClause('group_id', $dagList);
$result = $query->execute();
while ($row = $result->fetch_assoc()) {
$userList[] = $row['username'];
}
}
// Remove any duplicates from the user list and check it
$userList = array_filter(array_unique($userList));
if (empty($userList)) {
continue;
}
// Prep for our query to find open DQs
// Here we opt to avoid using addInClause due to the complexity of the sql
// We find open DQs for all users of the project, we check user below
$query = $this->createQuery();
$statusString = implode(',', array_fill(0, count($statusIDs), '?'));
$query->add('
SELECT A.res_id, A.status_id, B.ts, B.user_id, B.comment FROM
(SELECT MAX(res_id) as res_id, status_id FROM redcap_data_quality_resolutions GROUP BY status_id) AS A
JOIN
(SELECT res_id, status_id, ts, user_id, comment FROM redcap_data_quality_resolutions WHERE current_query_status = "OPEN" AND response_requested = "1" AND status_id IN (' . $statusString . ') ) AS B
ON A.res_id=B.res_id', array_keys($statusIDs));
$result = $query->execute();
$userIds = array_combine(array_keys($projectUsers), array_column($projectUsers, 'id'));
// If user in the list has open data query
if ($condition == 1) {
$userIds = array_intersect_key($userIds, array_flip($userList));
}
// If any user on the project has an open data query
if ($condition == 2) {
// Nothing to do here, users id list is correct already
}
$userIds = array_values($userIds);
// Check if enough time has passed sense the DQ was opened
$sendEmail = false;
$comments = [];
while ($row = $result->fetch_assoc()) {
$ts = date("Y-m-d H:i", strtotime($row['ts'] . " + $days days"));
if (in_array($row['user_id'], $userIds) && $now > $ts) {
$sendEmail = true;
if (!$sendDetails) {
break;
}
$comments[$row['status_id']] = $row['comment'];
}
}
// Skip to next if nothing to send
if (!$sendEmail) {
continue;
}
// Build out the email
global $project_contact_email;
$from = $project_contact_email;
$subject = "[REDCap] Data query reminder";
$link = "<a href=\"$project_link\">$projectName</a>";
$msg = "There are open data queries in the REDCap project \"$link\" that need to be addressed.";
if (!empty($comments)) {
$msg = "$msg<br><br><style>th, td { padding-left: 1%; padding-right: 1% }</style>
<table style='border:none'>
<thead>
<tr>
<th>Record</th>
<th>Field</th>
<th>Event</th>
<th>Instance</th>
<th>Recent Comment</th>
</tr>
</thead>
<tbody>";
foreach ($comments as $id => $comment) {
$msg = "$msg
<tr>
<td>{$statusIDs[$id]['record']}</td>
<td>{$statusIDs[$id]['field']}</td>
<td>{$statusIDs[$id]['event']}</td>
<td>{$statusIDs[$id]['instance']}</td>
<td>{$comment}</td>
</tr>";
}
$msg = "$msg</tbody></table>";
}
// Send the email and set flag to save
foreach ($userList as $user) {
$to = $projectUsers[$user]['email'];
if (!empty($to)) {
$sentSetting[$index] = $now;
$updateProjectSetting = true;
REDCap::email($to, $from, $subject, $msg);
}
}
}
// We've flipped through all of the userLits in the project
// Update the project setting if anything was sent
if ($updateProjectSetting) {
$this->setProjectSetting("sent", $sentSetting, $project_id);
}
}
}