-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExternalModule.php
More file actions
421 lines (357 loc) · 13.8 KB
/
ExternalModule.php
File metadata and controls
421 lines (357 loc) · 13.8 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* @file
* Provides ExternalModule class for Project Ownership module.
*/
namespace ProjectOwnership\ExternalModule;
use Exception;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use Project;
use RCView;
use REDCapEntity\EntityDB;
use REDCapEntity\EntityFactory;
use Records;
/**
* ExternalModule class for Project Ownership module.
*/
class ExternalModule extends AbstractExternalModule {
/**
* @inheritdoc
*/
function redcap_every_page_before_render($project_id = null) {
if (!defined('REDCAP_ENTITY_PREFIX')) {
$this->delayModuleExecution();
// Exits gracefully when REDCap Entity is not available.
return;
}
if (strpos(PAGE, substr(APP_PATH_WEBROOT_PARENT, 1) . 'index.php') === 0 && !empty($_GET['action']) && $_GET['action'] == 'project_ownership') {
$this->redirect($this->getUrl('plugins/ownership_list.php'));
return;
}
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
return;
}
if (PAGE == 'ProjectGeneral/create_project.php' || (PAGE == 'ProjectGeneral/edit_project_settings.php' && empty($_GET['action']))) {
// Saving onwership when project create or edit form is submitted.
$this->saveProjectOwnership($project_id);
}
}
function redcap_entity_types() {
$types = [];
$types['project_ownership'] = [
'label' => 'Project Ownership',
'label_plural' => 'Project Owners',
'icon' => 'key',
'class' => [
'name' => 'ProjectOwnership\Entity\ProjectOwnership',
'path' => 'classes/entity/ProjectOwnership.php',
],
'properties' => [
'pid' => [
'name' => 'Project',
'type' => 'project',
'required' => true,
],
'username' => [
'name' => 'Owner user account',
'type' => 'user',
],
'email' => [
'name' => 'Owner email',
'type' => 'email',
],
'firstname' => [
'name' => 'Owner first name',
'type' => 'text',
],
'lastname' => [
'name' => 'Owner last name',
'type' => 'text',
],
]
];
if ($this->getSystemSetting('enable_uf_features')) {
$types['project_ownership']['properties']['billable'] = [
'name' => 'Billable',
'type' => 'boolean'
];
$types['project_ownership']['properties']['sequestered'] = [
'name' => 'Sequestered',
'type' => 'boolean',
];
}
return $types;
}
/**
* @inheritdoc
*/
function redcap_every_page_top($project_id) {
if (!defined('REDCAP_ENTITY_PREFIX')) {
$this->delayModuleExecution();
// Exits gracefully when REDCap Entity is not enabled.
return;
}
if (strpos(PAGE, 'ExternalModules/manager/control_center.php') !== false) {
$this->includeJs('js/config.js');
$this->setJsSettings(['modulePrefix' => $this->PREFIX]);
return;
}
if (PAGE == 'ProjectSetup/index.php') {
// Edit project form.
$context = 'edit';
}
elseif (PAGE == 'ProjectGeneral/copy_project_form.php') {
// Copy project form.
$context = 'copy';
}
elseif (strpos(PAGE, substr(APP_PATH_WEBROOT_PARENT, 1) . 'index.php') === 0 && !empty($_GET['action'])) {
if ($_GET['action'] == 'create') {
// Create project form.
$context = 'create';
}
elseif ($_GET['action'] == 'myprojects') {
$helper = RCView::a(['href' => APP_PATH_WEBROOT_PARENT . 'index.php?action=project_ownership'], 'Project Ownership List');
$helper = 'To review and edit ownership of the projects you have access to, visit the ' . $helper . '.';
$this->setJsSettings(['ownershipListHelper' => RCView::div(['class' => 'ownership-list-helper col-sm-12'], $helper)]);
$this->includeJs('js/my-projects.js');
$this->includeCss('css/my-projects.css');
}
}
if (!empty($context)) {
$this->buildOwnershipFieldset($context, $project_id);
}
}
/**
* @inheritdoc
*/
function redcap_module_system_enable($version) {
$this->checkModuleDependencies();
// Making sure the module is enabled on all projects.
$this->setSystemSetting(ExternalModules::KEY_ENABLED, true);
// Building project ownership entity.
EntityDB::buildSchema($this->PREFIX);
}
/**
* @inheritdoc
*/
function redcap_module_system_change_version($version, $old_version) {
$this->checkModuleDependencies();
// Making sure we are upgrading from v1.x to 2.x or greater.
if (strpos($old_version, 'v1') !== 0 || !is_numeric($version[1]) || $version[1] < 2) {
return;
}
// Making sure project ownership entity has not been initialized yet.
if (db_query('SELECT 1 FROM redcap_entity_project_ownership LIMIT 1')) {
return;
}
// Creating project ownership table.
EntityDB::buildSchema($this->PREFIX);
// Getting legacy ownership entries.
if (!$q = $this->framework->query('SELECT * FROM redcap_project_ownership', [])) {
return;
}
if (!db_num_rows($q)) {
return;
}
// Migrating projects ownership.
$factory = new EntityFactory();
while ($result = db_fetch_assoc($q)) {
$factory->create('project_ownership', $result);
}
}
/**
* Checks for module dependencies.
*
* @throws Exception
* Throws an error if dependencies do not meet. Do nothing otherwise.
*/
function checkModuleDependencies() {
// Making sure REDCap Entity is enabled.
if (!defined('REDCAP_ENTITY_PREFIX')) {
throw new Exception('Project Ownership requires REDCap Entity to work.');
}
}
/**
* Builds ownership fieldset.
*/
protected function buildOwnershipFieldset($context, $project_id = null) {
if (!defined('USERID')) { return; } // prevent error email from unauthorized page load attempts
// Setting up default values.
$po_data = [
'username' => '',
'firstname' => '',
'lastname' => '',
'email' => '',
];
// Loading stored values.
if ($project_id && ($entity = $this->getProjectOwnership($project_id))) {
$po_data = $entity->getData();
}
// Required field marker.
$req_ast = RCView::span(['class' => 'required-ast'], '*') . ' ';
// Username field.
$output = RCView::span([], 'REDCap username (if applicable)') . RCView::br();
$output .= RCView::text([
'id' => 'project_ownership_username',
'name' => 'project_ownership_username',
'class' => 'x-form-text x-form-field po-row',
'placeholder' => 'Search',
'value' => $po_data['username'],
]);
// Adding search icon to username field.
$output .= RCView::img(['class' => 'search-icon', 'src' => APP_PATH_IMAGES . 'magnifier.png']);
// Adding ownership auto assign link.
$output .= RCView::a(['href' => '#', 'class' => 'po-auto-assign'], '(I am the owner)');
// Adding helper text to username field.
$output .= RCView::div(['class' => 'newdbsub po-row'], 'If the project owner does not have a REDCap account, leave this field blank and fill the information manually below.');
// Building first and last name fields.
$name_fields = '';
foreach (['firstname' => 'First name', 'lastname' => 'Last name'] as $suffix => $label) {
$field_name = 'project_ownership_' . $suffix;
$name_fields .= RCView::div([], $req_ast . RCView::span(['class' => 'po-info-label'], $label) . RCView::br() . RCView::text([
'id' => $field_name,
'name' => $field_name,
'class' => 'x-form-text x-form-field po-required-info',
'value' => $po_data[$suffix],
]));
}
// Wrapping first and last name fields into a single row.
$output .= RCView::div(['class' => 'po-name-wrapper clearfix'], $name_fields);
// Email field.
$output .= RCView::div([], $req_ast . RCView::span(['class' => 'po-info-label'], 'Email') . RCView::br() . RCView::text([
'id' => 'project_ownership_email',
'name' => 'project_ownership_email',
'class' => 'x-form-text x-form-field po-required-info po-row',
'value' => $po_data['email'],
]));
// Configurable text field.
if ($this->getSystemSetting("additional_text_toggle")) {
$output .= RCView::div(
['class' => 'po-row'],
$this->getSystemSetting("additional_text")
);
}
// Fieldset title.
$output = RCView::td(['class' => 'po-label po-col'], 'Project Ownership') . RCView::td(['class' => 'po-col'], $output);
// Passing fieldset content to JS.
$settings = [
'context' => $context,
'userId' => USERID,
'userInfoAjaxPath' => $this->getUrl('plugins/user_info_ajax.php'),
'fieldsetContents' => RCView::tr(['id' => 'po-tr', 'valign' => 'top'], $output),
];
if ($context == 'copy') {
global $lang;
$settings['copyTitleErrorMsg'] = $lang['copy_project_11'];
}
elseif ($context == 'edit') {
global $user_rights;
$settings['openProjectEditPopup'] = $user_rights['design'] && !empty($_GET['open_project_edit_popup']);
}
$this->setJsSettings($settings);
// Including JS and CSS files.
$this->includeJs('js/owner-fieldset.js');
$this->includeCss('css/owner-fieldset.css');
}
/**
* Gets project ownership.
*/
protected function getProjectOwnership($project_id) {
$factory = new EntityFactory();
if (!$results = $factory->query('project_ownership')->condition('pid', $project_id)->execute()) {
return false;
}
return reset($results);
}
/**
* Saves project ownership.
*/
protected function saveProjectOwnership($project_id) {
if (!$project_id) {
$project_id = 1;
// If we are at project creation page, we need to calculate the ID
// of the project being created.
//
// TODO: improve this, since concurrent requests can lead to
// inconsistencies.
$q = $this->framework->query('SELECT project_id FROM redcap_projects ORDER BY project_id DESC LIMIT 1', []);
if (db_num_rows($q)) {
$row = db_fetch_assoc($q);
$project_id += $row['project_id'];
}
}
// Specifying required fields for each case.
$suffixes = ['username', 'firstname', 'lastname', 'email'];
$required = empty($_POST['project_ownership_username']) ? ['firstname', 'lastname', 'email'] : ['username'];
$values = ['pid' => $project_id];
foreach ($suffixes as $suffix) {
$field_name = 'project_ownership_' . $suffix;
if (!in_array($suffix, $required)) {
$value = null;
}
elseif (!empty($_POST[$field_name])) {
$value = $_POST[$field_name];
}
else {
echo 'Missing ' . $suffix . ' field.';
exit;
}
$values[$suffix] = $value;
unset($_POST[$field_name]);
}
if (!$entity = $this->getProjectOwnership($project_id)) {
$factory = new EntityFactory();
$entity = $factory->getInstance('project_ownership');
}
if ($entity->setData($values)) {
echo 'An error has occurred. Please contact administration or try again later.';
}
$entity->save();
}
/**
* Includes a local CSS file.
*
* @param string $path
* The relative path to the css file.
*/
function includeCss($path) {
echo '<link rel="stylesheet" href="' . $this->getUrl($path) . '">';
}
/**
* Includes a local JS file.
*
* @param string $path
* The relative path to the js file.
*/
function includeJs($path) {
echo '<script src="' . $this->getUrl($path) . '"></script>';
}
/**
* 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();
}
/**
* Sets JS settings.
*
* @param mixed $settings
* The setting settings.
*/
protected function setJsSettings($settings) {
echo '<script>projectOwnership = ' . json_encode($settings) . ';</script>';
}
}