-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExternalModule.php
More file actions
360 lines (304 loc) · 13 KB
/
Copy pathExternalModule.php
File metadata and controls
360 lines (304 loc) · 13 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* @file
* Provides ExternalModule class for Linear Data Entry Workflow.
*/
namespace LinearDataEntryWorkflow\ExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use Records;
use REDCap;
/**
* ExternalModule class for Linear Data Entry Workflow.
*/
class ExternalModule extends AbstractExternalModule {
/**
* @inheritdoc
*/
function redcap_every_page_top($project_id) {
if (!$project_id) {
return;
}
// Initializing settings JS variable.
echo '<script>var linearDataEntryWorkflow = {};</script>';
$record = null;
switch (PAGE) {
case 'DataEntry/record_home.php':
if (empty($_GET['id'])) {
break;
}
$record = $this->framework->escape($_GET['id']);
case 'DataEntry/record_status_dashboard.php':
$location = str_replace('.php', '', str_replace('DataEntry/', '', PAGE));
$arm = empty($_GET['arm']) ? 1 : $this->framework->escape($_GET['arm']);
$this->loadRFIO($location, $arm, $record);
break;
}
}
/**
* @inheritdoc
*/
function redcap_data_entry_form($project_id, $record = null, $instrument, $event_id, $group_id = null) {
global $Proj;
if (!$record) {
$record = $this->framework->escape($_GET['id']);
}
if ($this->loadRFIO('data_entry_form', $Proj->eventInfo[$event_id]['arm_num'], $record, $event_id, $instrument)) {
$this->loadFDEC($instrument);
$this->loadAutoLock($instrument);
}
}
/**
* Loads RFIO (review fields in order) feature.
*
* @param string $location
* The location to apply RFIO. Can be:
* - data_entry_form
* - record_home
* - record_status_dashboard
* @param string $arm
* The arm name.
* @param int $record
* The data entry record ID.
* @param int $event_id
* The event ID. Only required when $location = "data_entry_form".
* @param string $instrument
* The form/instrument name.
*
* @return bool
* TRUE if the current user has access to the current form;
* FALSE if the user is going to be redirected out the page.
*/
protected function loadRFIO($location, $arm, $record = null, $event_id = null, $instrument = null) {
// Proj is a REDCap var used to pass information about the current project.
global $Proj;
$records = $record ? array($record) : Records::getRecordList($Proj->project_id);
if ( !$forms_status = Records::getFormStatus($Proj->project_id, $records, $arm) ) {
// Synthesize $forms_status for first record in a DAG
$id = $records[0];
$forms_status = [$id => $Proj->eventsForms];
foreach($forms_status[$id] as $event => $forms) {
$forms_status[$id][$event] = array_fill_keys($forms, []);
}
}
if ($independent_events_allowed = $this->getProjectSetting('allow-independent-events')) {
foreach ($forms_status as $id => $data) {
foreach (array_keys($data) as $event) {
// Appending fake "completed" forms to the beggining of list
// to make sure at least the first form will be displayed.
$forms_status[$id][$event] = array('___ldew_aux_form' => array(2)) + $forms_status[$id][$event];
}
}
}
else {
reset($Proj->eventsForms);
$first_event = key($Proj->eventsForms);
foreach (array_keys($forms_status) as $id) {
// Appending fake "completed" forms to the beggining of list
// to make sure at least the first form will be displayed.
$forms_status[$id][$first_event] = array('___ldew_aux_form' => array(2)) + $forms_status[$id][$first_event];
}
}
if (!$exceptions = $this->getProjectSetting('forms-exceptions', $Proj->project_id)) {
$exceptions = array();
}
// Handling possible conflicts with CTSIT's Form Render Skip Logic.
$frsl = array();
if (defined('FORM_RENDER_SKIP_LOGIC_PREFIX')) {
$frsl = ExternalModules::getModuleInstance(FORM_RENDER_SKIP_LOGIC_PREFIX)->getFormsAccessMatrix($event_id, $record);
}
// Getting denied forms.
$denied_forms = array();
foreach ($forms_status as $id => $data) {
$denied_forms[$id] = array();
$prev_event = '';
$prev_form = '';
foreach (array_reverse($data, true) as $event => $event_forms) {
$denied_forms[$id][$event] = array();
foreach (array_reverse($event_forms, true) as $form => $form_status) {
if (in_array($form, $exceptions)) {
// Skip exception forms.
continue;
}
if (isset($frsl[$id][$event][$form]) && !$frsl[$id][$event][$form]) {
// Skip FRSL hidden forms.
continue;
}
if (!empty($form_status)) {
$complete = true;
foreach ($form_status as $instance_status) {
if ($instance_status != 2) {
$complete = false;
break;
}
}
if ($complete) {
// Since this form is complete, let's rollback the
// access block to the next form.
unset($denied_forms[$id][$prev_event][$prev_form]);
}
break;
}
$denied_forms[$id][$event][$form] = $form;
$prev_event = $event;
$prev_form = $form;
}
if ($independent_events_allowed) {
$prev_event = '';
$prev_form = '';
}
elseif ( !isset($denied_forms[$id][$prev_event][$prev_form]) && $prev_event !== '' ) {
break;
}
}
}
if ($record && $event_id && isset($denied_forms[$record][$event_id][$instrument])) {
// Access denied to the current page.
$this->redirect(APP_PATH_WEBROOT . 'DataEntry/record_home.php?pid=' . $Proj->project_id . '&id=' . $record . '&arm=' . $arm);
return false;
}
$settings = array(
'deniedForms' => $denied_forms,
'location' => $location,
'instrument' => $instrument,
'isException' => in_array($instrument, $exceptions),
'forceButtonsDisplay' => $Proj->lastFormName == $instrument ? 'show' : false,
'hideNextRecordButton' => $this->getProjectSetting('hide-next-record-button', $Proj->project_id),
);
if (!$settings['forceButtonsDisplay'] && !is_null($event_id)) {
$i = array_search($instrument, $Proj->eventsForms[$event_id]);
$next_form = $Proj->eventsForms[$event_id][$i + 1];
if (in_array($next_form, $exceptions)) {
// Handling the case where the next form is an exception,
// so we need to show the buttons no matter the form status.
$settings['forceButtonsDisplay'] = 'show';
}
elseif ($settings['isException']) {
// Handling 2 cases for exception forms:
// - Case A: the next form is not accessible, so we need to keep
// the buttons hidden, no matter if form gets shifted to
// Complete status.
// - Case B: the next form is accessible, so we need to keep the
// buttons visible, no matter if form gets shifted to a non
// Completed status.
$settings['forceButtonsDisplay'] = $forms_access[$record][$event_id][$next_form] ? 'show' : 'hide';
}
}
$this->setJsSetting('rfio', $settings);
$this->includeJs('js/rfio.js');
return true;
}
/**
* Loads FDEC (force data entry constraints) feature.
*
* @param string $instrument
* The instrument/form ID.
* (optional) @param array $statuses_bypass
* An array of form statuses to bypass FDEC. Possible statuses:
* - 0 (Incomplete)
* - 1 (Unverified)
* - 2 (Completed)
* - "" (Empty status)
*/
protected function loadFDEC($instrument, $statuses_bypass = array('', 0, 1)) {
$exceptions = $this->getProjectSetting('forms-exceptions', $project_id);
if ($exceptions && in_array($instrument, $exceptions)) {
return;
}
global $Proj;
// Markup of required fields bullets list.
$bullets = '';
// Selectors to search for empty required fields.
$req_fields_selectors = array();
// Getting required fields from form config.
foreach (array_keys($Proj->forms[$instrument]['fields']) as $field_name) {
$field_info = $Proj->metadata[$field_name];
if (!$field_info['field_req']) {
continue;
}
// The bullets are hidden for default, since we do not know which ones will be empty.
$field_label = filter_tags(label_decode($field_info['element_label']));
$bullets .= '<div class="req-bullet req-bullet--' . $field_name . '" style="margin-left: 1.5em; text-indent: -1em; display: none;"> • ' . $field_label . '</div>';
$selector = '#questiontable ';
if ($field_info['element_type'] == 'file') {
$selector .= "tr:visible > td > input[name='{$field_name}']:hidden";
} else {
$selector .= ($field_info['element_type'] == 'select' ? 'select' : 'input') . "[name='{$field_name}']:visible";
}
$req_fields_selectors[] = $selector;
}
// Printing required fields popup (hidden yet).
echo '
<div id="preemptiveReqPopup" title="Some fields are required!" style="display:none;text-align:left;">
<p>You did not provide a value for some fields that require a value. Please enter a value for the fields on this page that are listed below.</p>
<div style="font-size:11px; font-family: tahoma, arial; font-weight: bold; padding: 3px 0;">' . $bullets . '</div>
</div>';
$settings = array(
'statusesBypass' => array_map(function($value) { return (string) $value; }, $statuses_bypass),
'requiredFieldsSelector' => implode(',', $req_fields_selectors),
'instrument' => $instrument,
);
$this->setJsSetting('fdec', $settings);
$this->includeJs('js/fdec.js');
}
/**
* Loads auto-lock feature.
*/
protected function loadAutoLock($instrument) {
global $user_rights;
global $Proj;
//get list of exceptions
if (!$exceptions = $this->getProjectSetting('forms-exceptions', $Proj->project_id)) {
$exceptions = array();
}
//if current form is in the exception list then disable auto-locking
if (in_array($instrument, $exceptions)) {
return;
}
//get list of roles to enforce auto-locking on
$roles_to_lock = $this->getProjectSetting("auto-locked-roles", $Proj->project_id);
if (!isset($roles_to_lock[0])) return; // do not autolock if no autolock roles are set
//load auto-lock script if user is in an auto-locked role
if (in_array($user_rights["role_id"], $roles_to_lock)) {
$this->includeJs("js/auto-lock.js");
}
}
/**
* Redirects user to the given URL.
*
* This function basically replicates redirect() function, but since EM
* throws an error when an exit() is called, we need to adapt it to the
* EM way of exiting.
*/
protected function redirect($url) {
if (headers_sent()) {
// If contents already output, use javascript to redirect instead.
echo '<script>window.location.href="' . $url . '";</script>';
}
else {
// Redirect using PHP.
header('Location: ' . $url);
}
$this->exitAfterHook();
}
/**
* Includes a local JS file.
*
* @param string $path
* The relative path to the js file.
*/
protected function includeJs($path) {
echo '<script src="' . $this->getUrl($path) . '"></script>';
}
/**
* Sets a JS setting.
*
* @param string $key
* The setting key to be appended to the module settings object.
* @param mixed $value
* The setting value.
*/
protected function setJsSetting($key, $value) {
echo '<script>linearDataEntryWorkflow.' . $key . ' = ' . json_encode($value) . ';</script>';
}
}