-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSurveyDirector.php
147 lines (133 loc) · 6.67 KB
/
SurveyDirector.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
<?php
namespace INTERSECT\SurveyDirector;
use ExternalModules\AbstractExternalModule;
use REDCap;
use Piping;
use Project;
class SurveyDirector extends \ExternalModules\AbstractExternalModule {
function redcap_survey_complete($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance)
{
// Quit if the module is disabled in the project settings
if ($this -> getProjectSetting('director_enabled') == 0) return;
// Set debug mode
$debug = false;
if ($this -> getProjectSetting('debug') == 1)
{
$debug = true;
$proj = new Project($project_id);
$longitudinal = REDCap::isLongitudinal();
$repeating = $proj -> isRepeatingEvent($event_id) || $proj -> isRepeatingForm($event_id, $instrument);
$event_name = REDCap::getEventNames(true, true, $event_id);
}
// Parse each survey
$surveys = $this -> getProjectSetting('survey');
for ($s = 0; $s < count($surveys); $s++){
$survey_select = $this -> getProjectSetting('survey_select')[$s];
$event_select = $this -> getProjectSetting('event_select')[$s];
$directives = $this -> getProjectSetting('directive')[$s];
// Skip if we're not in this instrument, or if we're not in this event
if ($instrument != $survey_select || (!is_null($event_select) && $event_id != $event_select)) continue;
// Skip if this survey is disabled, logging that fact if we're in debug mode
if ($this -> getProjectSetting('survey_enabled')[$s] == 0) {
if ($debug) {
$logMsg = array();
$longitudinal ? $logMsg[] = "Survey: $instrument ($event_name)" : $logMsg[] = "Survey: $instrument";
if ($repeating) $logMsg[] = "Instance: $repeat_instance";
$logMsg[] = "All directives disabled";
REDCap::logEvent(
"Survey Director Debug",
implode("\n", $logMsg),
$sql = NULL,
$record = $record,
$event = $event_id
);
}
continue;
}
// Parse each directive
for ($d = 0; $d < count($directives); $d++) {
if ($debug) $directive_index = ($s + 1) . '.' . ($d + 1);
// Skip if this directive is disabled, and logging that fact if we're in debug mode
if ($this -> getProjectSetting('directive_enabled')[$s][$d] == 0) {
if ($debug) {
$logMsg = array();
$longitudinal ? $logMsg[] = "Survey: $instrument ($event_name)" : $logMsg[] = "Survey: $instrument";
if ($repeating) $logMsg[] = "Instance: $repeat_instance";
$logMsg[] = "Directive $directive_index disabled";
REDCap::logEvent(
"Survey Director Debug",
implode("\n", $logMsg),
$sql = NULL,
$record = $record,
$event = $event_id
);
}
continue;
}
$condition = $this -> getProjectSetting('condition')[$s][$d];
$target = $this -> getProjectSetting('target')[$s][$d];
// If the condition is met...
if (is_null($condition) || REDCap::evaluateLogic
(
$condition,
$project_id,
$record,
$event_id,
$repeat_instance,
null,
$instrument // $current_context_instrument, allows instance smart variables to function normally
)
)
// ...Perform piping on the target
{
$target = Piping::replaceVariablesInLabel(
$target, // $label='',
$record, // $record=null,
$event_id, // $event_id=null,
$repeat_instance, // $instance=1,
array(), // $record_data=array(),
false, // $replaceWithUnderlineIfMissing=true,
$project_id, // $project_id=null,
false, // $wrapValueInSpan=true
'', // $repeat_instrument=''
1, // $recursiveCount=1
false, // $simulation=false
false, //$applyDeIdExportRights=false
$instrument // Ensures we get the right instrument context for piping smart variables
);
if ($debug) {
$logMsg = array();
$longitudinal ? $logMsg[] = "Survey: $instrument ($event_name)" : $logMsg[] = "Survey: $instrument";
if ($repeating) $logMsg[] = "Instance: $repeat_instance";
$logMsg[] = "Directive $directive_index";
if (!is_null($condition)) $logMsg[] = "Condition: $condition (true)";
$logMsg[] = "Target: $target";
REDCap::logEvent(
"Survey Director Debug",
implode("\n", $logMsg),
$sql = NULL,
$record = $record,
$event = $event_id
);
}
header('Location: '.$target);
$this->exitAfterHook(); // Go there
// If we don't return from the function here, then all other directives will be evaluated, and the last one to be true will take effect.
return;
} elseif ($debug) {
$logMsg = array();
$longitudinal ? $logMsg[] = "Survey: $instrument ($event_name)" : $logMsg[] = "Survey: $instrument";
if ($repeating) $logMsg[] = "Instance: $repeat_instance";
$logMsg[] = "Directive $directive_index";
$logMsg[] = "Condition: $condition (false)";
REDCap::logEvent(
"Survey Director Debug",
implode("\n", $logMsg),
$sql = NULL,
$record = $record,
$event = $event_id);
}
}
}
}
}