-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathExternalModule.php
812 lines (656 loc) · 30.6 KB
/
ExternalModule.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<?php
/**
* @file
* Provides ExternalModule class for Linear Data Entry Workflow.
*/
namespace SendRx\ExternalModule;
require_once 'includes/RxSender.php';
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use Records;
use RCView;
use RedCapDB;
use REDCap;
use SendRx\RxSender;
use UserProfile\UserProfile;
/**
* ExternalModule class for Linear Data Entry Workflow.
*/
class ExternalModule extends AbstractExternalModule {
/**
* @inheritdoc.
*/
function hook_data_entry_form_top($project_id, $record = null, $instrument, $event_id, $group_id = null, $repeat_instance = 1) {
// New PDF is generated based on pdf_is_updated flag.
// Reset flag once PDF is generated to avoid duplicate generation of the same PDF.
global $Proj;
if ($config = send_rx_get_project_config($project_id, 'site')) {
if ($Proj->metadata['send_rx_dag_id']['form_name'] == $instrument) {
if (!SUPER_USER && !ACCOUNT_MANAGER) {
// Hiding DAG ID field.
$Proj->metadata['send_rx_dag_id']['misc'] .= ' @HIDDEN';
}
}
elseif ($Proj->metadata['send_rx_user_id']['form_name'] == $instrument) {
global $lang;
$this->includeJs('js/create-staff.js');
$this->setJsSetting('usernameValidateRegexElement', RCView::div(array(
'id' => 'valregex-username',
'datatype' => 'text',
'label' => $lang['control_center_45'],
), '/^([a-zA-Z0-9_\.\-\@])+$/'));
$this->setJsSetting('getUserProfileInfoUrl', $this->getUrl('ajax/get_user_profile_info.php'));
// Creating artificial fields to make user able to create
// to account + profile directly from this form.
$base_field = $Proj->metadata['send_rx_user_id'];
$select_field_name = 'send_rx_new_user_opt';
$form_name = $base_field['form_name'];
$Proj->forms[$form_name]['fields'][$select_field_name] = $label;
$options = array(
'existing' => 'Select an existing user',
'new' => 'Create a new user account from scratch',
);
$enum = array();
foreach ($options as $key => $label) {
$enum[] = $key . ', ' . $label;
}
// Adding select list field that asks user whether to select an
// existing user or create a new account.
$Proj->metadata[$select_field_name] = array(
'field_name' => $select_field_name,
'element_label' => '',
'element_type' => 'radio',
'element_enum' => implode(' \n', $enum),
'misc' => '@DEFAULT="existing"',
) + $base_field;
$base_field['misc'] = '';
$base_field['element_type'] = 'text';
$base_field['element_enum'] = '';
$base_field['element_preceding_header'] = '';
$base_field['field_order']++;
$fields = array(
'id' => 'Username',
'first_name' => 'First name',
'last_name' => 'Last name',
'email' => 'Email',
);
// Shifting position of existing fields to make room for the
// fake fields.
$count = count($fields) + 1;
foreach (array_keys($Proj->forms[$form_name]['fields']) as $field_name) {
if ($Proj->metadata[$field_name]['field_order'] > $Proj->metadata[$select_field_name]['field_order']) {
$Proj->metadata[$field_name]['field_order'] += $count;
}
}
$Proj->metadata['send_rx_user_id']['field_order']++;
$Proj->metadata['send_rx_user_id']['element_preceding_header'] = '';
// Updating fields count.
$Proj->numFields += $count;
// Adding username, first name, last name and email fake fields.
foreach ($fields as $suffix => $label) {
$field_name = 'send_rx_new_user_' . $suffix;
$label = 'New user -- ' . $label;
$base_field['field_name'] = $field_name;
$base_field['element_label'] = $label;
$base_field['field_order']++;
$Proj->metadata[$field_name] = $base_field;
$Proj->forms[$form_name]['fields'][$field_name] = $label;
}
// Adding email validation.
$Proj->metadata[$field_name]['element_validation_type'] = 'email';
// Re-sorting form fields.
uksort($Proj->forms[$form_name]['fields'], array($this, '_formFieldCmp'));
}
return;
}
// Checking if PDF file exists.
if (!isset($Proj->metadata['send_rx_pdf'])) {
return;
}
// Checking if we are on PDF form step.
if ($Proj->metadata['send_rx_pdf']['form_name'] != $instrument) {
return;
}
// Getting Rx sender to make sure we are in a patient project.
if (!$sender = RxSender::getSender($project_id, $event_id, $record)) {
return;
}
$table = '<div class="info">This prescription has not been sent yet.</div>';
if ($logs = $sender->getLogs()) {
// Message types.
$types = array(
'email' => 'Email',
'hl7' => 'HL7',
);
// Rows header.
$header = array('Type', 'Success', 'Time', 'Recipients', 'User ID', 'Subject', 'Body');
// Creating logs table.
$table = '<div class="table-responsive"><table class="table table-condensed"><thead><tr>';
foreach (range(0, 2) as $i) {
$table .= '<th>' . $header[$i] . '</th>';
}
$table .= '<th></th></thead></tr><tbody>';
// Modals container for the logs details.
$modals = '<div id="send-rx-logs-details">';
// Populating tables and creating one modal for each entry.
$rx_sent = false;
foreach (array_reverse($logs) as $key => $row) {
$table .= '<tr>';
$row[0] = $types[$row[0]];
$row[2] = date('m/d/y - h:i a', $row[2]);
$row[3] = str_replace(',', '<br>', $row[3]);
if ($row[1]) {
$rx_sent = true;
$row[1] = 'Yes';
}
else {
$row[1] = 'No';
}
foreach (range(0, 2) as $i) {
$table .= '<td>' . $row[$i] . '</td>';
}
// Setting up the button that triggers the details modal.
$table .= '<td><button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#send-rx-logs-details-' . $key . '">See details</button></td>';
$table .= '</tr>';
$modals .= '
<div class="modal fade" id="send-rx-logs-details-' . $key . '" role="dialog">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4>Message Details</h4>
</div>
<div class="modal-body" style="overflow-wrap:break-word;word-wrap:break-word;"><form>';
foreach (range(3, 6) as $i) {
$modals .= '
<div class="form-group row">
<label class="col-sm-2 col-form-label">' . $header[$i] . '</label>
<div class="col-sm-10">' . $row[$i] . '</div>
</div>';
}
$modals .= '</form></div></div></div></div>';
}
$table .= '</tbody></table></div>';
$modals .= '</div>';
// Placing modals.
echo $modals;
}
// Checking for PDF "is updated" flag.
// We are not using REDCap::getData() because this field is not registered
// at any instrument.
$sql = '
SELECT value FROM redcap_data
WHERE
field_name = "send_rx_pdf_is_updated" AND
project_id = ' . db_escape($project_id) . ' AND
event_id = ' . db_escape($event_id) . ' AND
record = "' . db_escape($record) . '"
LIMIT 1';
$q = db_query($sql);
$pdf_is_updated = false;
if (db_num_rows($q)) {
$result = db_fetch_assoc($q);
$pdf_is_updated = $result['value'];
}
// Generate PDF if needed.
if (!$pdf_is_updated) {
if ($error = !$sender->getPrescriberData()) {
REDCap::logEvent('Rx file generation failed', 'Prescriber information is not available.', '', $record, $event_id, $project_id);
}
else {
$error = !$sender->generatePDFFile();
}
// log the appropriate message
$msg = $error ? 'There was an error while creating the PDF prescription file. Check logs for further details.' : 'A new prescription PDF preview has been created.';
$this->setJsSetting('statusMessage', send_rx_build_status_message($msg, $error));
}
$prescriber_field = 'send_rx_prescriber_id';
$pdf_field = 'send_rx_pdf';
$data = REDCap::getData($project_id, 'array', $record, array($prescriber_field, $pdf_field), $event_id);
$settings = array(
'currentUserIsPrescriber' => strtolower($data[$record][$event_id][$prescriber_field]) == strtolower(USERID),
'instrument' => $instrument,
'table' => $table,
'pdfIsSet' => !empty($data[$record][$event_id][$pdf_field]),
'completeReplacement' => RCView::hidden(array('value' => $rx_sent ? 2 : 0, 'name' => $instrument . '_complete')),
'sendBtn' => RCView::button(array('name' => 'send-rx-btn', 'class' => 'btn btn-primary btn-success send-rx-btn'), 'Send Rx'),
);
$this->setJsSetting('sendForm', $settings);
$this->includeJs('js/send-form.js');
$this->includeCss('css/send-form.css');
if ($settings['currentUserIsPrescriber']) {
$msg = $rx_sent ? 'This prescription ' . RCView::b('has already been sent') . '. Are you sure you want to re-send it?' : 'Are you sure you want to send this prescription?';
$msg = RCView::img(array('src' => APP_PATH_IMAGES . 'warning.png')) . ' ' . RCView::span(array(), $msg);
echo RCView::div(array('id' => 'send-rx-confirmation-modal', 'title' => 'Send Rx?'), RCView::p(array(), $msg));
}
}
/**
* @inheritdoc.
*/
function hook_every_page_before_render($project_id = null) {
if (empty($project_id)) {
return;
}
if (PAGE == 'ProjectSetup/export_project_odm.php' || PAGE == 'DataExport/data_export_ajax.php') {
// Avoiding any interferences in exports.
return;
}
if ($config = send_rx_get_project_config($project_id, 'site')) {
global $Proj;
$options = array();
foreach (UserProfile::getProfiles() as $username => $user_profile) {
$data = $user_profile->getProfileData();
$options[] = $username . ',' . $data['send_rx_user_first_name'] . ' ' . $data['send_rx_user_last_name'];
}
// Setting dropdown options dynamically.
$Proj->metadata['send_rx_user_id']['element_enum'] = implode('\\n', $options);
return;
}
// Checking if we are on data entry form.
if (PAGE != 'DataEntry/index.php') {
return;
}
global $Proj;
// Getting record ID.
if (!empty($_GET['id'])) {
$record = $_GET['id'];
}
elseif (!empty($_POST[$Proj->table_pk])) {
$record = $_POST[$Proj->table_pk];
}
else {
return;
}
require_once 'includes/send_rx_functions.php';
// Checking if this project has a Send Rx config.
if (!$config = send_rx_get_project_config($project_id, 'patient')) {
return;
}
// Checking if we are at the prescriber field's step.
if (!isset($Proj->metadata['send_rx_prescriber_id']) || $_GET['page'] != $Proj->metadata['send_rx_prescriber_id']['form_name']) {
return;
}
// Getting record group ID.
if (!$group_id = Records::getRecordGroupId($project_id, $record)) {
$parts = explode('-', $record);
if (count($parts) != 2) {
return;
}
$group_id = $parts[0];
}
// Getting list of prescribers.
if (!$prescribers = send_rx_get_group_members($project_id, $group_id, 'prescriber')) {
return;
}
// Creating prescribers list to be used on dropdown.
$options = array();
foreach ($prescribers as $username => $prescriber) {
$options[$username] = $username . ',' . $prescriber['user_firstname'] . ' ' . $prescriber['user_lastname'];
}
// Checking if current user is a prescriber and non-admin.
if (isset($prescribers[USERID]) && !SUPER_USER && !ACCOUNT_MANAGER) {
// If prescriber, we need to turn prescriber into readonly.
$username = USERID;
$data = send_rx_get_record_data($project_id, $record, $_GET['event_id']);
if (!empty($data) && !empty($data['send_rx_prescriber_id']) && $data['send_rx_prescriber_id'] != USERID && isset($options[$data['send_rx_prescriber_id']])) {
$username = $data['send_rx_prescriber_id'];
}
$options = array($options[$username]);
$parts = explode(',', reset($options));
// Losing the key part.
array_shift($parts);
$settings = array(
'username' => $username,
'fullname' => implode(',', $parts),
);
$this->includeJs('js/init.js');
$this->setJsSetting('prescriberField', $settings);
$this->includeJs('js/prescriber-field.js');
}
// Adding prescriber options.
$Proj->metadata['send_rx_prescriber_id']['element_enum'] = implode('\\n', $options);
}
/**
* @inheritdoc.
*/
function hook_every_page_top($project_id) {
if ($project_id) {
$this->includeJs('js/init.js');
if (isset($_SESSION['send_rx_status_message'])) {
$this->setJsSetting('statusMessage', $_SESSION['send_rx_status_message']);
unset($_SESSION['send_rx_status_message']);
}
}
if (strpos(PAGE, 'ExternalModules/manager/project.php') !== false) {
$this->includeCss('css/config.css');
$this->includeJs('js/config.js');
$this->setJsSetting('modulePrefix', $this->PREFIX);
return;
}
// Checking if we are on record status dashboard page.
if (PAGE != 'DataEntry/record_status_dashboard.php' || !$project_id) {
return;
}
global $Proj;
if (count($Proj->eventInfo) != 1) {
// Multiple events are not supported.
return;
}
require_once 'includes/send_rx_functions.php';
if (!$config = send_rx_get_project_config($project_id, 'site')) {
return;
}
if (!$valid_role_names = array_keys(parseEnum($Proj->metadata['send_rx_user_role']['element_enum']))) {
return;
}
$count = count($valid_role_names);
if (!$valid_role_names = send_rx_get_user_role_ids($config['target_project_id'], $valid_role_names)) {
// There are no remote roles.
return;
}
if (count($valid_role_names) != $count) {
// Thre are remote roles missing.
return;
}
$valid_role_ids = array_flip($valid_role_names);
$user_role_ids = send_rx_get_user_roles($config['target_project_id'], $valid_role_names);
reset($Proj->eventInfo);
$event_id = key($Proj->eventInfo);
$bypass = array_combine($Proj->eventsForms[$event_id], $Proj->eventsForms[$event_id]);
// Detecting mandatory instruments to check completeness.
foreach ($Proj->metadata as $field_name => $field_info) {
if (strpos($field_name, 'send_rx_') === 0) {
unset($bypass[$field_info['form_name']]);
}
}
$all_dags = array();
foreach (REDCap::getData($project_id) as $id => $data) {
$data = $data[$event_id];
if (!isset($data['send_rx_dag_id']) || empty($data['send_rx_site_name'])) {
return;
}
if (!$dag = $data['send_rx_dag_id']) {
if (!$dag = send_rx_add_dag($config['target_project_id'], $data['send_rx_site_name'])) {
return;
}
send_rx_save_record_field($project_id, $event_id, $record, 'send_rx_dag_id', $dag);
}
if (!send_rx_event_is_complete($project_id, $id, $event_id, $bypass)) {
return;
}
$all_dags[$id] = REDCap::escapeHtml($dag);
}
if (empty($all_dags)) {
return;
}
$msg = '';
$user_dags = send_rx_get_user_dags($config['target_project_id']);
if ($_SERVER['REQUEST_METHOD'] === 'POST' && in_array($_POST['operation'], array('rebuild', 'revoke'))) {
$msg = 'The permissions have been revoked.';
// Revoking dags.
foreach (array_unique(array_merge(array_keys($_POST['user_dags']), array_keys($user_dags))) as $user_id) {
if (isset($user_dags[$user_id])) {
$user_dags[$user_id] = array_diff($user_dags[$user_id], $all_dags);
}
}
if ($_POST['operation'] == 'rebuild') {
$msg = 'The permissions have been rebuilt.';
// Granting dags.
foreach ($_POST['user_dags'] as $user_id => $dags) {
// In order to be assigned to a DAG, the user needs to have
// some role.
if (!isset($user_role_ids[$user_id])) {
continue;
}
$user_dags[$user_id] = isset($user_dags[$user_id]) ? array_merge($user_dags[$user_id], $dags) : $dags;
sort($user_dags[$user_id], SORT_NUMERIC);
}
}
REDCap::logEvent('DAG Switcher', json_encode($user_dags), '', null, null, $config['target_project_id']);
// Showing success message.
$msg = send_rx_build_status_message($msg);
if (!empty($_POST['error_count'])) {
// Showing error message.
$msg .= send_rx_build_status_message($_POST['error_count'] . ' role assignments failed. See logs for further details.', true);
}
}
$this->setJsSetting('statusMessage', $msg);
$form = RCView::hidden(array('name' => 'operation')) . RCView::hidden(array('name' => 'error_count'));
$input_dags = array();
$input_roles = array();
$db = new RedCapDB();
$rebuild_btn_enabled = false;
$revoke_btn_enabled = !empty($user_role_ids);
foreach ($all_dags as $id => $dag) {
if (!$staff = send_rx_get_site_users($project_id, $id)) {
continue;
}
foreach ($staff as $member) {
$user_id = $member['send_rx_user_id'];
if (!$db->usernameExists($user_id)) {
continue;
}
$role_id = $member['send_rx_user_role'];
if (!$rebuild_btn_enabled && (!isset($user_dags[$user_id]) || array_search($dag, $user_dags[$user_id]) === false)) {
$rebuild_btn_enabled = true;
}
// If a user member of multiple sites, it's assumed that
// its role is the same for all of them.
$input_roles[$user_id] = $member['send_rx_user_role'];
if (!isset($input_dags[$user_id])) {
$input_dags[$user_id] = array();
}
$input_dags[$user_id][] = $dag;
$form .= RCView::hidden(array('name' => 'user_dags[' . REDCap::escapeHtml($user_id) . '][]', 'value' => $dag));
}
}
// Calculating the roles that need to be granted/revoked.
$rebuild_roles = array();
foreach ($input_roles as $user_id => $role_id) {
$role_id = $valid_role_ids[$role_id];
if (!isset($user_role_ids[$user_id]) || $user_role_ids[$user_id] != $role_id) {
$rebuild_roles[$user_id] = $role_id;
}
}
foreach (array_keys($user_role_ids) as $user_id) {
if (!isset($input_roles[$user_id])) {
$rebuild_roles[$user_id] = 0;
}
}
// Defining whether Rebuild and Revoke buttons should be enabled.
if (!$rebuild_btn_enabled && !empty($rebuild_roles)) {
$rebuild_btn_enabled = true;
}
if (!$rebuild_btn_enabled || !$revoke_btn_enabled) {
foreach ($user_dags as $user_id => $dags) {
if (!$dags = array_intersect($dags, $all_dags)) {
continue;
}
$revoke_btn_enabled = true;
if ($rebuild_btn_enabled) {
break;
}
if (!isset($input_dags[$user_id]) || array_diff($dags, $input_dags[$user_id])) {
$rebuild_btn_enabled = true;
break;
}
}
}
$attrs = array(
'type' => 'button',
'style' => 'margin-right: 5px;',
'class' => 'rebuild-perms-btn btn btn-success',
);
if (!$rebuild_btn_enabled) {
$attrs['disabled'] = true;
}
// Adding Rebuild permissions button.
$form .= RCView::button($attrs, 'Rebuild staff permissions');
$attrs = array(
'type' => 'button',
'class' => 'revoke-perms-btn btn btn-danger',
);
if (!$revoke_btn_enabled) {
$attrs['disabled'] = true;
}
// Adding Revoke permissions button.
$form .= RCView::button($attrs, 'Revoke staff permissions');
$settings = array(
'url' => APP_PATH_WEBROOT . 'UserRights/assign_user.php?pid=' . REDCap::escapeHtml($config['target_project_id']),
'errorLogEndpointUrl' => $this->getUrl('ajax/assign_role_error_log.php'),
'form' => RCView::form(array('method' => 'post', 'id' => 'send_rx_perms', 'style' => 'margin-top: 20px;'), $form),
'rebuildRoles' => $rebuild_roles,
'revokeRoles' => array_combine(array_keys($user_role_ids), array_fill(0, count($user_role_ids), 0)),
);
$this->setJsSetting('permsRebuild', $settings);
$this->includeJs('js/perm-buttons.js');
}
/**
* @inheritdoc.
*/
function hook_save_record($project_id, $record = null, $instrument, $event_id, $group_id = null, $survey_hash = null, $response_id = null, $repeat_instance = 1) {
global $Proj;
// Auto create a DAG in patients project when a new site is created.
// Link this DAG Id to the site created.
if ($config = send_rx_get_project_config($project_id, 'site')) {
if ($Proj->metadata['send_rx_dag_id']['form_name'] == $instrument) {
$data = send_rx_get_record_data($project_id, $record, $event_id);
$error = false;
if (!$data['send_rx_dag_id']) {
// Creating new DAG.
$group_id = send_rx_add_dag($config['target_project_id'], $data['send_rx_site_name']);
send_rx_save_record_field($project_id, $event_id, $record, 'send_rx_dag_id', $group_id);
$msg = 'DAG "' . REDCap::escapeHtml($data['send_rx_site_name']) . '" was created on the patient project.';
}
elseif (!$old_name = send_rx_get_dag_name($config['target_project_id'], $data['send_rx_dag_id'])) {
// DAG does not exist.
$msg = 'The selected DAG does not exist.';
$error = true;
}
elseif ($old_name != $data['send_rx_site_name']) {
// TODO: check if the DAG is already taken by other site.
// Renaming DAG.
send_rx_rename_dag($config['target_project_id'], $data['send_rx_site_name'], $data['send_rx_dag_id']);
$msg = 'DAG was renamed from "' . REDCap::escapeHtml($old_name) . '" to "' . REDCap::escapeHtml($data['send_rx_site_name']) . '" on the patient project.';
}
else {
// The DAG does not need to be renamed. Do nothing.
return;
}
$_SESSION['send_rx_status_message'] = send_rx_build_status_message($msg, $error);
}
elseif ($Proj->metadata['send_rx_user_id']['form_name'] == $instrument) {
if (empty($_POST['send_rx_new_user_opt']) || $_POST['send_rx_new_user_opt'] != 'new') {
return;
}
$values = array();
foreach (array('id', 'first_name', 'last_name', 'email') as $suffix) {
if (empty($_POST['send_rx_new_user_' . $suffix])) {
return;
}
$values['send_rx_user_' . $suffix] = trim(strip_tags(label_decode($_POST['send_rx_new_user_' . $suffix])));
}
$username = preg_replace('/[^a-z A-Z0-9_\.\-\@]/', '', $values['send_rx_user_id']);
if ($username != $values['send_rx_user_id']) {
// Invalid username.
$_SESSION['send_rx_status_message'] = send_rx_build_status_message('Invalid username.', true);
return;
}
if (!UserProfile::createProfile($values) || !send_rx_create_user($username, $values['send_rx_user_first_name'], $values['send_rx_user_last_name'], $values['send_rx_user_email'])) {
// Setting up error message.
$_SESSION['send_rx_status_message'] = send_rx_build_status_message('There was an error while creating the user. Check logs for further details.', true);
}
send_rx_save_record_field($project_id, $event_id, $record, 'send_rx_user_id', $username, $repeat_instance);
}
return;
}
// Checking if we are at the prescriber field's step.
$id_field_name = 'send_rx_prescriber_id';
$target_field_name = 'send_rx_prescriber_email';
if (isset($Proj->metadata[$id_field_name]) && $instrument == $Proj->metadata[$id_field_name]['form_name'] && isset($Proj->metadata[$target_field_name]) && !empty($_POST[$id_field_name])) {
$user_profile = new UserProfile($_POST[$id_field_name]);
$data = $user_profile->getProfileData();
$source_field_name = 'send_rx_user_email';
if (!empty($data[$source_field_name])) {
send_rx_save_record_field($project_id, $event_id, $record, $target_field_name, $data[$source_field_name]);
}
}
// Checking if PDF file exists.
if (!isset($Proj->metadata['send_rx_pdf'])) {
return;
}
// Getting Rx sender to make sure we are in a patient project.
if (!$sender = RxSender::getSender($project_id, $event_id, $record)) {
return;
}
// Checking if we are on PDF form step.
if ($Proj->metadata['send_rx_pdf']['form_name'] == $instrument) {
$patient_data = $sender->getPatientData();
if (empty($patient_data[$event_id]['send_rx_pdf'])) {
return;
}
// Only the prescriber can send the prescription.
$prescriber_data = $sender->getPrescriberData();
if ($prescriber_data['send_rx_user_id'] != USERID) {
return;
}
// Send prescription.
if (!$sender->send(false)) {
return;
}
$field_name = $instrument . '_complete';
if ($patient_data[$event_id][$field_name] != 2) {
send_rx_save_record_field($project_id, $event_id, $record, $field_name, '2');
}
return;
}
// New PDF needs to be generated as changes are made to form after PDF is created.
// Reset pdf_is_updated flag to generate new PDF.
$field_name = 'send_rx_pdf_is_updated';
send_rx_save_record_field($project_id, $event_id, $record, $field_name, '0');
}
/**
* Includes a local CSS file.
*
* @param string $path
* The relative path to the css file.
*/
protected 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.
*/
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>sendRx.' . $key . ' = ' . json_encode($value) . ';</script>';
}
/**
* Auxiliary function to compare the order between two fields.
*
* @param string $a
* Name of the field to be compared.
* @param string $b
* Name of the field to compare with.
*
* @return
* TRUE if $a comes first than $b, FALSE otherwise.
*/
function _formFieldCmp($a, $b) {
global $Proj;
return $Proj->metadata[$a]['field_order'] >= $Proj->metadata[$b]['field_order'];
}
}