-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExternalModule.php
113 lines (94 loc) · 3.41 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
<?php
namespace HideChoiceByEvent\ExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
/**
* ExternalModule class for Project Ownership module.
*/
class ExternalModule extends AbstractExternalModule
{
/**
* @inheritdoc
*/
function redcap_every_page_top($project_id)
{
if ($project_id) {
$this->includeJs('js/modify_help_menu.js');
}
//if not on a data entry page or survey, then don't do anything
if (!in_array(PAGE, ['DataEntry/index.php', 'surveys/index.php'])) {
return;
}
global $Proj;
$settings = [];
//get the name of every field on this page.
$fields = empty($_GET['page']) ? [] : $Proj->forms[$_GET['page']]['fields'];
if (is_array($fields)) {
foreach (array_keys($fields) as $field) {
$field_info = $Proj->metadata[$field];
$misc = $field_info['misc'];
//continue if the field does not have any action_tags at all.
if (!$action_tags = str_replace("\n", '', $misc)) {
continue;
}
//check if @HIDE-CHOICE-BY-EVENT is among action tags.
//did not use \Form::getValueInActionTag methods because of nested
//quotes inside JSON.
if (preg_match('/@HIDE-CHOICE-BY-EVENT\s*=\s*(\[)/', $action_tags, $matches, PREG_OFFSET_CAPTURE)) {
$count = 1;
$json_start = $matches[1][1];
$json_len = 0;
$len = strlen($action_tags);
for ($i = $json_start + 1; $i < $len; $i++) {
if ($action_tags[$i] == '[') {
$count++;
} elseif ($action_tags[$i] == ']' && !--$count) {
$json_len = $i - $json_start + 1;
break;
}
}
if (!$json_len) {
continue;
}
//add to settings variable if it is a valid json
if ($json_config = json_decode(substr($action_tags, $json_start, $json_len), true)) {
$settings[$field] = $json_config;
}
}
}
}
if (empty($settings)) {
return;
}
$current_event = \Event::getEventNameById($_GET['pid'], $_GET['event_id']);
//make action tag configs available to js scripts
$js_settings = array(
"settings" => $settings,
"current_event" => $current_event
);
$this->setJsSettings($js_settings);
//run script to hide field choices
$this->includeJs('js/hide_choice_by_event.js');
}
/**
* 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 value in the global variable js variable
* called hideChoiceByEvent.
*
* @param array $settings
* an array of keys and values.
*/
protected function setJsSettings($settings)
{
echo '<script>var hideChoiceByEvent = ' . json_encode($settings) . ';</script>';
}
}