-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReportTweaks.php
341 lines (306 loc) · 12 KB
/
ReportTweaks.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
namespace UWMadison\ReportTweaks;
use ExternalModules\AbstractExternalModule;
use REDCap;
use DataExport;
class ReportTweaks extends AbstractExternalModule
{
private $jsGlobal = "";
private $defaultSettings = ['includeEvent' => true];
private $maxJsonLen = (2 << 15) - (2 << 10); // 99% of 64kb
/*
Primary Redcap Hook, loads config and Report pages
*/
public function redcap_every_page_top($project_id)
{
// Bail if user isn't logged in
if (!defined("USERID")) {
return;
}
$report_id = $_GET['report_id'];
// Custom Config page
if ($this->isPage('ExternalModules/manager/project.php') && $project_id) {
$this->loadSettings();
$this->includeJs('config.js');
}
// Reports Page (Edit or View Report, Not the all-reports page or stats/charts)
elseif ($this->isPage('DataExport/index.php') && $project_id && ($report_id || $_GET['create']) && !$_GET['stats_charts']) {
$this->loadSettings($report_id);
$this->includeCSS();
include('templates.php');
if ($_GET['addedit']) {
$this->includeDateFields();
$this->includeJs('editTweaks.js');
} else {
$this->loadReportDetails($report_id);
$this->loadReportHeaders($report_id);
$this->includeJs('viewTweaks.js');
}
}
}
/*
Save all report config to a single json setting for the EM.
Invoked via router/ajax
*/
public function saveReportConfig()
{
$json = $this->getProjectSetting('json');
$json = empty($json) ? array() : json_decode($json, true);
// Escape 3 feilds that are html enabled
$new = json_decode($_POST['settings'], true);
if (!empty($new['_wb'])) {
foreach ($new['_wb'] as $index => $data) {
foreach (['footer', 'modalBtn', 'modalText'] as $html) {
$new['_wb'][$index][$html] = REDCap::escapeHtml($data[$html]);
}
}
}
$json[$_POST['report']] = $new;
$save = json_encode($json);
$save = strlen($save) > $this->maxJsonLen ? gzcompress($save) : $save;
$this->setProjectSetting('json', $save);
}
/*
Perform a write back from a report. Write some value to a series of
records with event/instrument/instance info. Invoked via router/ajax
*/
public function reportWrite()
{
// Gather info
$writeBackData = (array) json_decode($_POST['writeArray'], true);
$pid = $_GET['pid'];
$saveType = 'array';
$record_id_name = REDCap::getRecordIdField();
$overwrite = json_decode($_POST['overwrite']);
$instrumentMap = array_flip(REDCap::getInstrumentNames());
$writeArray = [];
// Data is broken into groups based on what fields we are writing to
foreach ($writeBackData as $fieldName => $dataList) {
// Get User rights to check if we can write to the field
$field = $fieldName;
$user = $this->getUser()->getUsername();
$rights = REDCap::getUserRights($user)[$user]['forms'];
$form = REDCap::getDataDictionary($pid, 'array')[$field]['form_name'];
if ($rights[$form] != "1") { // 1 is View&Edit, 0 is Hidden, 2 is View Only
echo json_encode([
"form" => $form,
"warnings" => [$this->tt('warning_1')],
"errors" => []
]);
return;
}
// Loop over every line of the report we got back
foreach ($dataList as $data) {
// If we were sent a display name, swap it to an id (or internal instrument name)
$event = $data['event'];
$instrument = $instrumentMap[$data['instrument']] ?? "";
$record = $data['record'];
$instance = $data['instance'] ?? "";
// Make sure field exists on event, shouldn't be an issue
if (REDCap::isLongitudinal() && (empty($event) || !in_array($field, REDCap::getValidFieldsByEvents($pid, $event)))) {
continue;
}
// If no overwritting then make sure we don't blow away data
if (!$overwrite) {
$existingData = REDCap::getData($pid, 'array', $record, $field, $event)[$record];
if (!empty($instrument)) {
if (!empty($existingData["repeat_instances"][$event][$instrument][$instance][$field]))
continue; // Don't do write
} elseif (!empty($existingData[$event][$field])) {
continue; // Don't do write
}
}
// Set value on repeat or single instrument
if (!empty($instrument)) {
// Note: Field might not be on instrument if malicious, saveData will catch this though
$writeArray[$record]["repeat_instances"][$event][$instrument][$instance][$field] = $data['val'];
} elseif (empty($event)) {
// Single event project
$saveType = 'json-array';
$writeArray[] = [
$record_id_name => $record,
$field => $data['val']
];
} else {
$writeArray[$record][$event][$field] = $data['val'];
}
}
}
// Save and return or pass error
if (!empty($writeArray)) {
$out = REDCap::saveData($pid, $saveType, $writeArray, 'overwrite'); #Note: overwrite here is just for saving blank values
} else {
$out = [
"warnings" => [$this->tt('warning_2')],
"errors" => []
];
}
echo json_encode($out);
}
/*
Inits the ReportTweaks global and loads the settings for
a report ID. Also packs the Redcap JS object
*/
private function loadSettings($report = Null)
{
// Setup Redcap JS object
$this->initializeJavascriptModuleObject();
$this->tt_transferToJavascriptModuleObject();
$this->jsGlobal = $this->getJavascriptModuleObjectName();
$data = ["prefix" => $this->getPrefix()];
if (!empty($report)) {
// Get the EM's settings, decompress, and decode
$json = $this->getProjectSetting('json');
if (empty($json)) {
$json = $this->defaultSettings;
} else {
$json = gzuncompress($json) ?: $json;
$json = ((array)json_decode($json))[$report];
}
// Organize the strucutre
$data = array_merge($data, [
"isLong" => REDCap::isLongitudinal(),
"csrf" => $this->getCSRFToken(),
"router" => $this->getUrl('router.php'),
"record_id" => REDCap::getRecordIdField(),
"settings" => $json,
"username" => ($this->getUser())->getUsername(),
"eventMap" => $this->makeEventMap()
]);
}
// Pass down to JS
$data = json_encode($data);
echo "<script>Object.assign({$this->jsGlobal}, {$data});</script>";
}
/*
Pass down sorting info and filter logic for the report.
The datatalbes API doesn't store inital sorting order.
*/
private function loadReportDetails($report)
{
$details = DataExport::getReports($report);
$logic = json_encode($details["advanced_logic"] ?? $details["limiter_logic"]);
$sort = [];
foreach (range(1, 3) as $i) {
$sort[] = [
'field' => htmlentities($details["orderby_field$i"], ENT_QUOTES),
'sort' => htmlentities($details["orderby_sort$i"], ENT_QUOTES)
];
}
$sort = json_encode($sort);
echo "<script>{$this->jsGlobal}.logic = {$logic};</script>";
echo "<script>{$this->jsGlobal}.sort = {$sort};</script>";
}
/*
Pass down a mapping of report headers. Only way to do this in DataExport
is by getting a full copy of the report
*/
private function loadReportHeaders($report)
{
// Init some global values and constants
global $Proj;
$proj = (array)$Proj;
$isLongitudinal = REDCap::isLongitudinal();
$record_id = REDCap::getRecordIdField();
// Check if the report has repeat fields shown
$sql = '
SELECT report_display_include_repeating_fields AS re
FROM redcap_reports
WHERE report_id = ?;
';
$result = $this->query($sql, [$report]);
$hasRepeatingFormsOrEvents = $result->fetch_assoc()["re"] == "1";
// Grab the fields on the report
$sql = '
SELECT field_name FROM redcap_reports_fields
WHERE report_id = ?
AND isNull(limiter_group_operator)
ORDER BY field_order';
$result = $this->query($sql, [$report]);
// Flip through and build out our headers object
$headers = [];
$idx = 0;
while ($row = $result->fetch_assoc()) {
$name = $row["field_name"];
$headers[$name] = [
"index" => $idx++,
"validation" => $proj["metadata"][$name]["element_validation_type"]
];
if ($name != $record_id) continue;
if ($isLongitudinal) {
$headers["redcap_event_name"] = [
"index" => $idx++,
"validation" => ""
];
}
if ($hasRepeatingFormsOrEvents) {
$headers["redcap_repeat_instrument"] = [
"index" => $idx++,
"validation" => ""
];
$headers["redcap_repeat_instance"] = [
"index" => $idx++,
"validation" => ""
];
}
}
// Prep for export to JS
$headers = array_merge(["record_id" => $headers[$record_id]], $headers);
$formated = json_encode([
"all" => $headers,
"core" => [
"record_id" => $headers[$record_id]["index"],
"redcap_repeat_instrument" => $headers["redcap_repeat_instrument"]["index"],
"redcap_repeat_instance" => $headers["redcap_repeat_instance"]["index"],
"redcap_event_name" => $headers["redcap_event_name"]["index"]
]
]);
echo "<script>{$this->jsGlobal}.headers = {$formated};</script>";
}
/*
Pass down a list of all date(time) fields
*/
private function includeDateFields()
{
global $Proj;
$load = [];
foreach ($Proj->metadata as $field => $data) {
if (!empty($data["element_validation_type"]) && (strpos($data["element_validation_type"], "date") !== false)) {
$load[] = $field;
}
}
$load = json_encode($load);
echo "<script>{$this->jsGlobal}.fields = {$load};</script>";
}
/*
Util functions used by writeback. Creates a map of event display
names to event ids.
*/
private function makeEventMap()
{
$map = [];
if (REDCap::isLongitudinal()) {
$map = array_flip(REDCap::getEventNames(false));
$map = $map + REDCap::getEventNames(true);
}
if (empty($map)) {
$data = REDCap::getData('array', null, REDCap::getRecordIdField());
$map[""] = !empty($data) ? reset(array_keys(reset($data))) : "";
}
return $map;
}
/*
HTML to include some local JS file
*/
private function includeJs($path)
{
echo "<script src={$this->getUrl('js/' .$path)}></script>";
}
/*
HTML to include the local css file
*/
private function includeCSS()
{
echo "<link rel='stylesheet' href={$this->getURL('style.css')}>";
}
}