-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormFill.php
More file actions
181 lines (157 loc) · 6.84 KB
/
Copy pathFormFill.php
File metadata and controls
181 lines (157 loc) · 6.84 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
<?php
namespace UWMadison\FormFill;
use ExternalModules\AbstractExternalModule;
use REDCap;
class FormFill extends AbstractExternalModule
{
public function redcap_every_page_top($project_id)
{
// Custom Config page
if ($this->isPage('ExternalModules/manager/project.php') && $project_id) {
$this->initGlobal();
$this->includeJs('config.js');
}
}
public function redcap_data_entry_form($project_id, $record, $instrument, $event_id)
{
$settings = $this->getProjectSettings();
$validSettings = [];
foreach ($settings['instrument'] as $index => $instrumentList) {
if (in_array($instrument, $instrumentList))
$validSettings[] = $index;
}
if (empty($validSettings))
return;
$this->initGlobal();
$dd = REDCap::getDataDictionary('array');
$files = [];
$parsedSettings = [];
// Pull timezone offset for this record if it exists
$tzOffset = 0;
if (isset($settings['tz-offset'])) {
$tzField = $settings['tz-offset'];
if (!empty($tzField)) {
$tzData = REDCap::getData($project_id, 'array', $record, $tzField)[$record];
// Flip through and find the first non-empty value for this record, if it exists
foreach ($tzData as $eventData) {
if (!empty($eventData[$tzField])) {
$tzOffset = (int)$eventData[$tzField];
break;
}
}
}
}
foreach ($validSettings as $settingIndex) {
$parsed = [];
foreach ($settings as $name => $valueArray) {
if (in_array($name, ['enabled', 'filable-instance', 'instrument', 'filable-field', 'event']))
continue;
if ($name == 'pdf') {
$doc_id = $valueArray[$settingIndex];
if (!empty($doc_id)) {
list($mimeType, $docName, $fileContent) = REDCap::getFile($doc_id);
$file = unpack("C*", $fileContent);
}
continue;
}
if ($name == 'fill-value') {
$fetched = [];
$defaultEvent = $settings['event'][$settingIndex];
foreach ($valueArray[$settingIndex] as $index => $field) {
$data = REDCap::getData($project_id, 'array', $record, $field)[$record];
$default = empty($data[$defaultEvent][$field]) ? reset($data)[$field] : $data[$defaultEvent][$field];
$data = empty($data[$event_id][$field]) ? $default : $data[$event_id][$field];
$type = $dd[$field]['field_type'];
$validation = $dd[$field]['text_validation_type_or_show_slider_number'];
if ($type == 'checkbox') // Only look at first check box
$data = reset($data) ? true : false;
if ($type == 'radio' || $type == 'yesno' || $type == 'dropdown') // Code blank, 0, and negatives as false
$data = $data == '' || $data == '0' ? false : !(intval($data) < 0);
$fetched[$index] = $this->escape($data);
}
$parsed[$name] = $fetched;
$parsed['redcap-fields'] = $valueArray[$settingIndex];
continue;
}
$parsed[$name] = $valueArray[$settingIndex];
}
if (!empty($file)) {
$files[] = $file;
$parsedSettings[] = $parsed;
}
}
if (!empty($files)) {
$this->passArgument('pdf_base64', $files);
$this->passArgument('settings', $parsedSettings);
$this->passArgument('tzOffset', $tzOffset);
$this->includeJs('pdf-lib.min.js');
$this->includeJs('formfill.js');
}
}
public function redcap_module_ajax($action, $payload, $project_id, $record, $instrument, $event_id)
{
$result = [];
if ($action == "log") {
$result = $this->projectLog($project_id, $record, $event_id, $payload['action'], $payload['changes']);
} elseif ($action == "email") {
$result = $this->sendEmail($payload['to'], $payload['from'], $payload['subject'], $payload['message'], $payload['attachment']);
}
return $result;
}
public function sendEmail($to, $from, $subject, $message, $attachment)
{
if (!isset($from) || !isset($to) || !isset($attachment)) {
return [
'text' => "Missing required parameters",
'sent' => false
];
}
// Set blank values if missing
if (!isset($subject)) $subject = '';
if (!isset($message)) $message = ' '; // Redcap requires a non-empty-string message
// Stash the PDF in the PHP tmp directory, send the email, and remove the file
$pdf = base64_decode($attachment);
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tmpRedcap.pdf';
$file = fopen($path, 'w');
fwrite($file, $pdf);
fclose($file);
$sent = REDCap::email($to, $from, $subject, $message, null, null, null, ['REDCap_Form.pdf' => $path]);
unlink($path);
return [
'text' => $sent ? "Email/Fax Sent" : "Issue sending Email/Fax",
'sent' => $sent
];
}
public function projectLog($project_id, $record, $event_id, $action, $changes)
{
$sql = NULL;
$action = empty($action) ? "No action logged" : $action;
$changes = empty($record) || empty($event_id) ? "Record Home Page\n{$changes}" : $changes;
REDCap::logEvent($action, $changes, $sql, $record, $event_id, $project_id);
return [
'text' => 'Action logged'
];
}
private function initGlobal()
{
global $project_contact_email;
global $from_email;
$this->initializeJavascriptModuleObject();
$data = json_encode([
"prefix" => $this->getPrefix(),
"from" => $from_email ? $from_email : $project_contact_email,
"fax" => $this->getProjectSetting('fax-fufiller'),
"format" => $this->getProjectSetting('date-format'),
"use12HourTime" => $this->getProjectSetting('time-12') == '1',
]);
echo "<script>Object.assign({$this->getJavascriptModuleObjectName()}, {$data});</script>";
}
private function passArgument($name, $value)
{
echo "<script>{$this->getJavascriptModuleObjectName()}.{$name} = " . json_encode($value) . ";</script>";
}
private function includeJs($path)
{
echo "<script src={$this->getUrl($path)}></script>";
}
}