forked from uwctri/ReportTweaks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReportTweaks.php
323 lines (285 loc) · 11.8 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
<?php
namespace UWMadison\ReportTweaks;
use ExternalModules\AbstractExternalModule;
use REDCap;
class ReportTweaks extends AbstractExternalModule {
private $module_global = 'ReportTweaks';
private $defaultSettings = ['includeEvent'=>true];
/*
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->includePrefix();
$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->includeCookies();
$this->loadReportSorting($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( ['footer','modalBtn','modalText'] as $html ) {
$new['_wb'][$html] = REDCap::escapeHtml($new['_wb'][$html]);
}
}
$json[$_POST['report']] = $new;
$this->setProjectSetting('json', json_encode($json));
}
/*
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'];
$field = $_POST['field'];
$overwrite = json_decode($_POST['overwrite']);
$eventMap = $this->makeEventMap($pid);
$instrumentMap = $this->makeInstrumentMap();
$writeArray = [];
// Get User rights to check if we can write to the field
$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 ( $writeBackData as $data ) {
// If we were sent a display name, swap it to an id (or internal instrument name)
$event = is_numeric($data['event']) ? $data['event'] : $eventMap[$data['event']];
$instrument = $instrumentMap[$data['instrument']] ?? "";
$record = $data['record'];
$instance = $data['instance'] ?? "";
// Make sure field exists on event, shouldn't be an issue
if (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'];
} else {
$writeArray[$record][$event][$field] = $data['val'];
}
}
// Save and return or pass error
if ( !empty($writeArray) ) {
$out = REDCap::saveData($pid, 'array', $writeArray);
} 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 ) {
// Setup Redcap JS object
$this->initializeJavascriptModuleObject();
$this->tt_transferToJavascriptModuleObject();
// Get the EM's settings
$json = ((array)json_decode( $this->getProjectSetting('json') ))[$report];
$json = empty($json) ? $this->defaultSettings : $json;
// Organize the strucutre
$data = json_encode([
"isLong" => REDCap::isLongitudinal(),
"csrf" => $this->getCSRFToken(),
"router" => $this->getUrl('router.php'),
"record_id" => REDCap::getRecordIdField(),
"settings" => $json
]);
// Pass down to JS
echo "<script>var {$this->module_global} = {$data};</script>";
echo "<script> {$this->module_global}.em = {$this->getJavascriptModuleObjectName()}</script>";
}
/*
Pass down sorting info for the report. The datatalbes
API doesn't store inital sorting order.
*/
private function loadReportSorting( $report ) {
$sql = '
SELECT orderby_field1, orderby_field2, orderby_field3,
orderby_sort1, orderby_sort2, orderby_sort3
FROM redcap_reports
WHERE report_id = ?';
$result = $this->query($sql, [$report]);
$row = $result->fetch_assoc();
foreach( range(1,3) as $i ) {
$orders[] = ['field'=>htmlentities($row["orderby_field$i"], ENT_QUOTES),
'sort' =>htmlentities($row["orderby_sort$i"], ENT_QUOTES)];
}
$orders = json_encode($orders);
echo "<script>{$this->module_global}.sort = {$orders};</script>";
}
/*
Pass down a mapping of key headers on the report.
*/
private function loadReportHeaders( $report ) {
// Global with metadata
global $Proj;
$proj = (array)$Proj;
$record_id = REDCap::getRecordIdField();
$headers = [];
$redcap_fields = ["redcap_repeat_instrument","redcap_repeat_instance","redcap_event_name"];
// Some users won't be able to get fields via getReport below, this is our fallback
$idx = 0;
$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]);
while ( $row = $result->fetch_assoc() ){
$name = $row["field_name"];
$headers[$name] = [
"index" => $idx,
"validation" => $proj["metadata"][$name]["element_validation_type"]
];
$idx += 1;
}
// Grab all data via the api as a csv, strip it down to just headers and package it
$csv = explode(',',preg_split("@[\s+ ]@u",REDCap::getReport($report,'csv'))[0]);
$csv = array_combine($csv, range(0, count($csv)-1));
$maxRedcap = -1;
$minRedcap = 999;
$rowCount = 0;
foreach ( $csv as $name => $index ) {
$headers[$name] = [
"index" => $index,
"validation" => $proj["metadata"][$name]["element_validation_type"]
];
if ( in_array($name, $redcap_fields) ) {
if ($index > $maxRedcap) $maxRedcap = $index;
if ($index < $minRedcap) $minRedcap = $index;
} else {
$rowCount += 1;
}
}
// If the user didn't have full export rights then they only get the 3 redcap_
// vars, we can adjust values we got from the SQL query to yeild correct indexes
if ( ($maxRedcap >= 0) && ($maxRedcap != $minRedcap) && ($idx != $rowCount) ) {
foreach ( $headers as $name => $data ) {
if ( !in_array($name, $redcap_fields) && $data["index"] >= $minRedcap ) {
$headers[$name]["index"] = $data["index"] + $maxRedcap;
}
}
}
// 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->module_global}.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->module_global}.fields = {$load};</script>";
}
/*
Util functions used by writeback. Creates a map of event display
names to event ids.
*/
private function makeEventMap($project_id) {
$map = array_flip(REDCap::getEventNames(false));
if ( empty($map) ) {
$map[""] = reset(array_keys(reset(REDCap::getData($project_id,'array', null, REDCap::getRecordIdField()))));
}
return $map;
}
/*
Util functions used by writeback. Creates a map of instrument
display names to internal names (i.e. Hello world -> hello_world)
*/
private function makeInstrumentMap() {
return array_flip(REDCap::getInstrumentNames());
}
/*
HTML to pass down module prefix for the config page.
*/
private function includePrefix() {
echo "<script>var {$this->module_global} = {'modulePrefix': '{$this->getPrefix()}'};</script>";
}
/*
HTML to include the cookie.js library
*/
private function includeCookies() {
echo "<script type='text/javascript' src={$this->getURL('js/cookie.min.js')}></script>";
}
/*
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')}>";
}
}