-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSurveyLinkLookup.php
276 lines (253 loc) · 15.5 KB
/
SurveyLinkLookup.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
<?php
/**
* REDCap External Module: Survey Link Lookup
* Look up the project / survey / record / event / instance corresponding to
* an individual survey link or hash.
* @author Luke Stevens, Murdoch Children's Research Institute
*/
namespace MCRI\SurveyLinkLookup;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use REDCap;
use HtmlPage;
use RCView;
/**
* REDCap External Module: Survey Link Lookup
*/
class SurveyLinkLookup extends AbstractExternalModule
{
public function __construct()
{
parent::__construct();
}
/**
* Print the module plugin page html content
* @global array $lang
* @param string $link Optional value to be written to the plugin page
* input text box and searched for on document ready.
*/
public function printPage($link='') {
global $lang;
if (!SUPER_USER) {
displayMsg($lang['global_05'], 'errorMsg','center','red','exclamation_frame.png', 600);
return;
}
$moduleJS = $this->getUrl('survey_link_lookup.js');
?>
<style type="text/css">
#pagecontent { margin-top: 70px; }
</style>
<script type="text/javascript" src="<?php echo $moduleJS;?>"></script>
<?php
$link = REDCap::escapeHtml($link);
$instructionText = $this->getSystemSetting('page-instruction-text');
$inputLabelText = $this->getSystemSetting('page-input-label');
include APP_PATH_VIEWS . 'HomeTabs.php';
renderPageTitle('Survey Link Lookup');
print RCView::div(
array('id'=>'lookup_form'),
RCView::div(
array(),
$instructionText
).
RCView::form(
array('name'=>'form', 'onsubmit'=>'return false;'),
RCView::div(
array('class'=>'form-group', 'style'=>'margin:10px 0 20px 0'),
RCView::label(array('for'=>'hash'),$inputLabelText).
RCView::input(
array('type'=>'text','class'=>'form-control', 'style'=>'display:inline;width:200px;margin:0 5px',
'id'=>'lookup_val','value'=>$link)
).
RCView::button(
array('id'=>'btnFind', 'class'=>'btn btn-primaryrc','type'=>'button'),
'<span class="fa f-search"></span> '.$lang['control_center_439'].' '
)
)
)
);
print RCView::div(
array(
'id'=>'results', 'class'=>'container well', 'style'=>'width:100%; font-size:120%; display:none;'
),
RCView::div(
array('id'=>'results_spin', 'class'=>'row', 'style'=>'display:block;text-align:center;'),
RCView::img(array('src'=>'progress_circle.gif'))
).
RCView::div(
array('id'=>'results_error', 'class'=>'row', 'style'=>'display:block;text-align:center;'),
RCView::span(array('id'=>'result_error_msg', 'class'=>'text-danger'), 'error')
).
RCView::div(
array('id'=>'results_detail', 'style'=>'display:block;'),
RCView::div(
array('class'=>'row'),
RCView::div(
array('class'=>'col-sm-2 col-md-2 col-lg-2', 'style'=>'color:#888'),
$lang['global_65'] //Project
).
RCView::div(
array('class'=>'col-sm-6 col-md-6 col-lg-6'),
'<span id="result_app_title"></span> <span class="text-muted">(pid=<span id="result_project_id"]."></span>)</span>'
).
RCView::div(
array('class'=>'col-sm-3 col-md-3 col-lg-3'),
RCView::a(
array('class'=>'btn btn-xs btn-defaultrc', 'target'=>'_blank', 'style'=>'text-align:center;min-width:12em;',
'id'=>'result_link_setup_page', 'href'=>'#'),
'<span class="fa fa-link"></span> '.$lang['app_17'].' <span class="fa fa-external-link-alt"></span>' //Project Setup
)
)
).
RCView::div(
array('class'=>'row', 'style'=>'margin-top:20px;margin-bottom:20px;'),
RCView::div(
array('class'=>'col-sm-2 col-md-2 col-lg-2', 'style'=>'color:#888'),
$lang['survey_437'] //Survey'
).
RCView::div(
array('class'=>'col-sm-6 col-md-6 col-lg-6'),
'<span id="result_survey_title"></span>'
).
RCView::div(
array('class'=>'col-sm-3 col-md-3 col-lg-3'),
RCView::a(
array('class'=>'btn btn-xs btn-defaultrc', 'target'=>'_blank', 'style'=>'text-align:center;min-width:12em;',
'id'=>'result_link_designer_page', 'href'=>'#'),
'<span class="fa fa-link"></span> '.$lang['design_25'].' <span class="fa fa-external-link-alt"></span>' //Online Designer
)
)
).
RCView::div(
array('class'=>'row'),
RCView::div(
array('class'=>'col-sm-2 col-md-2 col-lg-2', 'style'=>'color:#888'),
$lang['global_49'].'<br>'.$lang['global_141'].'<br>'.$lang['data_entry_246'] //Record<br>Event<br>Instance
).
RCView::div(
array('class'=>'col-sm-6 col-md-6 col-lg-6'),
'<span id="result_record"></span><br><span id="result_event_name"></span><br><span id="result_instance"></span>'
).
RCView::div(
array('class'=>'col-sm-3 col-md-3 col-lg-3'),
RCView::a(
array('class'=>'btn btn-xs btn-defaultrc', 'target'=>'_blank', 'style'=>'text-align:center;min-width:12em;',
'id'=>'result_link_data_entry_page', 'href'=>'#'),
'<span class="fa fa-link"></span> '.$lang['global_35'].' <span class="fa fa-external-link-alt"></span>' //Data Collection Instrument
).
RCView::a(
array('class'=>'btn btn-xs btn-defaultrc', 'target'=>'_blank', 'style'=>'text-align:center;min-width:12em;display:none;',
'id'=>'result_link_public_survey_page', 'href'=>'#'),
'<span class="fa fa-link"></span> '.$lang['app_22'].' <span class="fa fa-external-link-alt"></span>' //Manage Survey Participants (Public Survey Link)
)
)
)
)
);
return;
}
/**
* Extract the section of the input string that looks like a survey hash
* @param string $lookup_val The string from which the survey hash value
* will be extracted.
* @return array Array with two elements: 1) lookup_success (bool),
* indicating whether a valid hash was found in $lookup_val;
* 2) lookup_result (mixed), array of survey details or error message
*/
public function lookup($lookup_val) {
$resultArray = array(
'lookup_success' => false,
'lookup_result' => ''
);
if (!isset($lookup_val) || $lookup_val=='') {
$resultArray['lookup_result'] = 'No link or survey hash provided';
} else {
$hash = $this->extractHash($lookup_val);
if (!isset($hash) || $hash=='') {
$resultArray['lookup_result'] = "Could not extract survey hash value (?s=hash) from '$lookup_val'";
} else {
try {
$details = $this->readSurveyDetailsFromHash($hash);
if (count($details) > 0) {
$resultArray['lookup_success'] = true;
$resultArray['lookup_result'] = $details;
} else {
$resultArray['lookup_result'] = "Survey hash '$hash' not found";
}
} catch (Exception $ex) {
$resultArray['lookup_result'] = $ex->getMessage();
}
}
}
return $resultArray;
}
/**
* Extract the section of the input string that looks like a survey hash
* @param string $lookup_val The string from which the survey hash value
* will be extracted.
* @return string Hash value (generally 10 characters), or empty string
* if no hash value found.
*/
private function extractHash($lookup_val) {
$hash = '';
$matches = array();
if (strpos($lookup_val, 's=')!==false) {
if (preg_match('/(?<=s=)[^\&]*/', $lookup_val, $matches)) {
$hashPart = $matches[0];
}
} else {
$hashPart = $lookup_val;
}
if (preg_match('/^\w{6,10}$/', $hashPart, $matches)) {
$hash = $matches[0];
}
return $hash;
}
/**
* Look up details of survey corresponding to the hash value provided
* @param string $hash A (generally) 10-character value identifying an
* individual participant survey.
* @return array
*/
private function readSurveyDetailsFromHash($hash) {
global $lang;
$details = array();
if (isset($hash) && $hash!=='') {
$sql = "SELECT s.survey_id,s.project_id,s.form_name,s.title as survey_title".
",pr.app_title,pr.repeatforms".
",p.participant_id,p.event_id,p.hash,IF(p.participant_email IS NULL,1,0) as is_public_survey_link".
",em.descrip".
",ea.arm_id,ea.arm_num,ea.arm_name".
",proj_ea.num_project_arms".
",r.response_id,r.record,r.instance,r.start_time,r.first_submit_time,r.completion_time,r.return_code,r.results_code ".
"FROM redcap_surveys s ".
"INNER JOIN redcap_projects pr ON s.project_id = pr.project_id ".
"INNER JOIN redcap_surveys_participants p ON s.survey_id = p.survey_id ".
"INNER JOIN redcap_events_metadata em ON em.event_id = p.event_id ".
"INNER JOIN redcap_events_arms ea ON ea.arm_id = em.arm_id ".
"INNER JOIN (SELECT project_id, COUNT(arm_id) as num_project_arms FROM redcap_events_arms GROUP BY project_id) proj_ea ON proj_ea.project_id = pr.project_id ".
"LEFT OUTER JOIN redcap_surveys_response r ON p.participant_id = r.participant_id ".
"WHERE hash = '".db_real_escape_string($hash)."' LIMIT 1";
$result = db_query($sql);
$details = db_fetch_assoc($result);
db_free_result($result);
// get event name (with arm ref, if multiple)
if (isset($details['project_id']) && intval($details['project_id']) > 0) {
$event_name = '';
if ($details['is_public_survey_link']) {
$details['record'] = $lang['survey_279']; // Public Survey Link
if (intval($details['num_project_arms']) > 1) { $event_name = $details['descrip']." (".$details['arm_name'].")"; }
$details['instance'] = '';
} else if (!$details['repeatforms']) {
$event_name = $lang['control_center_149']; // N/A (not a longitudinal project)
} else {
$event_name = (intval($details['num_project_arms']) > 1)
? $event_name = $details['descrip']." (".$details['arm_name'].")"
: $event_name = $details['descrip'];
}
$details['event_name'] = $event_name;
}
}
return $details;
}
}