forked from grezniczek/redcap_javascript_injector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalModule.php
More file actions
139 lines (115 loc) · 4.27 KB
/
ExternalModule.php
File metadata and controls
139 lines (115 loc) · 4.27 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
<?php
namespace RUB\JSInjector\ExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
/**
* ExternalModule class for Javascript Injector.
*/
class ExternalModule extends AbstractExternalModule {
function redcap_module_configure_button_display() {
if ($this->getSystemSetting("su_only") && !SUPER_USER) return null;
return true;
}
function redcap_data_entry_form_top($project_id, $record = null, $instrument, $event_id, $group_id = null, $repeat_instance = 1) {
$this->injectJS("data_entry", $instrument);
}
function redcap_survey_page_top($project_id, $record = null, $instrument, $event_id, $group_id = null, $survey_hash, $response_id = null, $repeat_instance = 1) {
$this->injectJS("survey", $instrument);
}
function redcap_project_home_page ($project_id) {
$this->injectJS("php", null);
}
function redcap_every_page_top ($project_id) {
if (PAGE === "DataEntry/record_status_dashboard.php") {
$this->injectJS("rsd", null);
}
else if (PAGE === "DataEntry/record_home.php" && !isset($_GET["id"])) {
$this->injectJS("aer", null);
}
else if (PAGE === "DataEntry/record_home.php" && isset($_GET["id"])) {
$this->injectJS("rhp", null);
}
// All project pages.
if ($project_id !== null) {
$this->injectJS("all", null);
}
}
/**
* Inject JS code.
*
* @param string $type
* Accepted types: 'data_entry' or 'survey'.
* @param string $instrument
* The instrument name.
*/
function injectJS($type, $instrument) {
$settings = $this->getFormattedSettings(PROJECT_ID);
if (empty($settings["js"])) {
return;
}
foreach ($settings["js"] as $row) {
if (empty($row["js_enabled"])) continue;
$inject = strpos($row["js_type"], $type) !== false;
if (strpos("survey,data_entry", $row["js_type"]) !== false) {
// Check instrument.
$inject = $inject && (!array_filter($row["js_instruments"]) || in_array($instrument, $row["js_instruments"], true));
}
if ($inject) {
echo "<script>" . $row["js_code"] . "</script>";
}
}
}
/**
* The code for getFormattedSettings and _getFormattedSettings
* originates from https://github.com/ctsit/redcap_css_injector
* as of March 27, 2019.
*/
/**
* Formats settings into a hierarchical key-value pair array.
*
* @param int $project_id
* Enter a project ID to get project settings.
* Leave blank to get system settings.
*
* @return array
* The formatted settings.
*/
function getFormattedSettings($project_id = null) {
$settings = $this->getConfig();
if ($project_id) {
$settings = $settings['project-settings'];
$values = ExternalModules::getProjectSettingsAsArray($this->PREFIX, $project_id);
}
else {
$settings = $settings['system-settings'];
$values = ExternalModules::getSystemSettingsAsArray($this->PREFIX);
}
return $this->_getFormattedSettings($settings, $values);
}
/**
* Auxiliary function for getFormattedSettings().
*/
protected function _getFormattedSettings($settings, $values, $inherited_deltas = []) {
$formatted = [];
foreach ($settings as $setting) {
$key = $setting['key'];
$value = $values[$key]['value'];
foreach ($inherited_deltas as $delta) {
$value = $value[$delta];
}
if ($setting['type'] == 'sub_settings') {
$deltas = array_keys($value);
$value = [];
foreach ($deltas as $delta) {
$sub_deltas = array_merge($inherited_deltas, [$delta]);
$value[$delta] = $this->_getFormattedSettings($setting['sub_settings'], $values, $sub_deltas);
}
if (empty($setting['repeatable'])) {
$value = $value[0];
}
}
$formatted[$key] = $value;
}
return $formatted;
}
}