/
survey.php
413 lines (368 loc) · 11.8 KB
/
survey.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* This implements the portion of Loris which is user-facing for the survey
* module. Survey participants should have received an email with a URL
* pointing to this page with a unique one-time key, and this page will handle
* loading the correct instrument, displaying it to them in a user-friendly
* manner and saving the data to the database.
*
* PHP Version 5
*
* @category Survey
* @package Loris
* @author Dave MacFarlane <driusan@bic.mni.mcgill.ca>
* @license Loris license
* @link https://www.github.com/aces/Loris-Trunk/
*/
set_include_path(get_include_path().":../project/libraries:../php/libraries:");
ini_set('default_charset', 'utf-8');
ini_set('session.use_strict_mode', '1');
require_once __DIR__ . "/../vendor/autoload.php";
require_once 'NDB_Config.class.inc';
require_once 'Smarty_hook.class.inc';
require_once 'NDB_Caller.class.inc';
require_once 'NDB_Client.class.inc';
require_once 'NDB_BVL_Instrument.class.inc';
/**
* Implements the survey page
*
* @category Survey
* @package Loris
* @author Dave MacFarlane <driusan@bic.mni.mcgill.ca>
* @license Loris license
* @link https://www.github.com/aces/Loris-Trunk/
*/
class DirectDataEntryMainPage
{
var $key;
var $TestName;
/**
* The instrument subpage being accessed
*
* @var mixed
*/
var $Subtest;
var $NumPages;
var $NextPageNum;
var $PrevPageNum;
var $tpl_data;
var $caller;
/**
* Initialize all of the class variables and things required from the
* REQUEST.
*
* @return void (but as a side-effect modifies class)
*/
function initialize()
{
ob_start('ob_gzhandler');
$client = new NDB_Client();
$client->makeCommandLine();
$client->initialize();
$config =& NDB_Config::singleton();
$this->caller =& NDB_Caller::singleton();
$this->caller->setDataEntryType('Direct');
if (empty($_REQUEST['key'])) {
throw new Exception("Missing parameter", 403);
}
$this->key = $_REQUEST['key'];
$DB = Database::singleton();
$this->TestName = $DB->pselectOne(
"SELECT Test_name FROM participant_accounts
WHERE OneTimePassword=:key AND Status <> 'Complete'",
array('key' => $this->key)
);
$this->CommentID = $DB->pselectOne(
"SELECT CommentID FROM participant_accounts
WHERE OneTimePassword=:key AND Status <> 'Complete'",
array('key' => $this->key)
);
$this->NumPages = $DB->pselectOne(
"SELECT COUNT(*) FROM instrument_subtests WHERE Test_name=:TN",
array('TN' => $this->TestName)
);
if (empty($this->TestName) && empty($this->CommentID)) {
throw new Exception("Data has already been submitted.", 403);
}
$pageNum = null;
if (!empty($_REQUEST['pageNum'])) {
$pageNum = $_REQUEST['pageNum'];
}
if ($pageNum === 'finalpage') {
$this->Subtest = 'finalpage';
} else {
$this->Subtest = $DB->pselectOne(
"SELECT Subtest_name
FROM instrument_subtests
WHERE Test_name=:TN AND Order_number=:PN",
array(
'TN' => $this->TestName,
'PN' => $pageNum,
)
);
}
$totalPages = $DB->pselectOne(
"SELECT COUNT(*)+1 from instrument_subtests WHERE Test_name=:TN",
array('TN' => $this->TestName)
);
$this->NextPageNum = $this->getNextPageNum($pageNum);
$this->PrevPageNum = $this->getPrevPageNum($pageNum);
$this->CommentID = $this->getCommentID();
$this->tpl_data = array(
'nextpage' => $this->NextPageNum,
'prevpage' => $this->PrevPageNum,
'pageNum' =>
$pageNum && is_numeric($pageNum) ?
intval($pageNum) + 1 : 1,
'totalPages' => $totalPages,
'key' => $this->key,
'study_title' => $config->getSetting('title'),
);
}
/**
* Get the page which follows this page
*
* @param integer $currentPage The current page number
*
* @return int The page which preceeded this one
*/
function getNextPageNum($currentPage): int
{
if ($currentPage === null) {
return 1;
}
if (!is_numeric($currentPage)) {
return -1;
}
$nextPage = $currentPage+1;
return intval(
\Database::singleton()->pselectOne(
"SELECT Order_number FROM instrument_subtests
WHERE Test_name=:TN AND Order_number=:PN",
array(
'TN' => $this->TestName,
'PN' => $nextPage,
)
)
);
}
/**
* Get the previous page number
*
* @param integer $currentPage The current page number
*
* @return string|null the previous page number or "top" if the user is on
* the top page
*/
function getPrevPageNum($currentPage): ?string
{
$DB = Database::singleton();
if ($currentPage === null) {
// On the top page or no page specified, do not include link
return null;
}
if ($currentPage == 1) {
// on the first subtest page the previous page is the top page
return 'top';
}
if ($currentPage === 'finalpage') {
return $DB->pselectOne(
"SELECT MAX(Order_number)
FROM instrument_subtests
WHERE Test_name=:TN",
array('TN' => $this->TestName)
);
}
$prevPage = $currentPage-1;
return $DB->pselectOne(
"SELECT Order_number FROM instrument_subtests
WHERE Test_name=:TN AND Order_number=:PN",
array(
'TN' => $this->TestName,
'PN' => $prevPage,
)
);
}
/**
* Run the current page, consists of initializing and then displaying the page
*
* @return void
*/
function run()
{
try {
$this->initialize();
$this->display();
} catch(Exception $e) {
$this->displayError($e);
}
}
/**
* Get the CommentID associated with the current page's key
*
* @return string a valid CommentID for this page
*/
function getCommentID()
{
$DB = Database::singleton();
return $DB->pselectOne(
"SELECT CommentID FROM participant_accounts
WHERE OneTimePassword=:key AND Status <> 'Complete'",
array(
'key' => $this->key,
)
);
}
/**
* Display an error page in the event of an exception.
*
* @param Exception $e The exception which was thrown by the code
*
* @return void (but as a side-effect changes the HTTP return code)
*/
function displayError($e)
{
switch($e->getCode())
{
case 404:
header("HTTP/1.1 404 Not Found");
break;
case 403:
header("HTTP/1.1 403 Forbidden");
break;
}
$this->tpl_data['workspace'] = $e->getMessage();
$this->tpl_data['complete'] = false;
$smarty = new Smarty_neurodb;
$smarty->assign($this->tpl_data);
$smarty->display('directentry.tpl');
}
/**
* Updates the status of the current key
*
* @param string $status The status to be updated to
*
* @return bool True on success, false on failure
*/
function updateStatus($status): bool
{
$DB = Database::singleton();
$currentStatus = $DB->pselectOne(
'SELECT Status FROM participant_accounts
WHERE OneTimePassword=:key',
array('key' => $this->key)
);
if ($currentStatus === 'Complete') {
// Already completed, don't want to accidentally change it back to
// started or some other status..
return false;
}
$DB->update(
"participant_accounts",
array('Status' => $status),
array('OneTimePassword' => $this->key)
);
return true;
}
/**
* Saves the comments that the user gave on the final page
* to the database
*
* @param integer $ease Integer rating of how easy the user found the
* survey to use
* @param string $comments Comments entered by survey user on review page
*
* @return void
*/
function updateComments($ease, $comments)
{
$DB = Database::singleton();
$DB->update(
"participant_accounts",
array(
'UserEaseRating' => $ease,
'UserComments' => $comments,
),
array('OneTimePassword' => $this->key)
);
}
/**
* Loads the correct page and renders it to the user
*
* @return void
*/
function display()
{
$DB = Database::singleton();
$nextpage = null;
if (isset($_REQUEST['nextpage'])) {
$nextpage = "survey.php?key=$_REQUEST[key]&pageNum=$_REQUEST[nextpage]";
}
if (isset($_POST['ease'])) {
// Comments is too comment of an instrument fieldname,
// so just check if ease is set
$this->updateComments(
$_POST['ease'],
$_POST['comments']
);
}
$workspace = $this->caller->load(
$this->TestName,
$this->Subtest ?? '',
$this->CommentID,
$nextpage
);
// Caller calls instrument's save function and might have errors,
// so we still need to call it. But if nextpage is 'complete',
// then after that override with a "Thank you" message
if (isset($_REQUEST['pageNum']) && $_REQUEST['pageNum'] === 'finalpage') {
if (isset($_POST['FinalPageSubmission'])
&& $_POST['FinalPageSubmission'] == "Yes"
) {
// Data was submitted on the last page.
$this->tpl_data['workspace'] = $workspace;
} else {
// We're just getting to the last page for the first time
$this->tpl_data['workspace'] = '';
$this->tpl_data['review'] = $this->caller->instrument->getReview();
}
$this->tpl_data['lastpage'] = "survey.php?key=$_REQUEST[key]";
$this->tpl_data['finalpage'] = true;
} else if (isset($_REQUEST['pageNum'])
&& $_REQUEST['pageNum'] === 'complete'
) {
$this->tpl_data['workspace'] = "Thank you for completing this survey.";
$this->tpl_data['complete'] = true;
$this->updateStatus('Complete');
$DB->update(
$this->TestName,
array(
'Date_taken' => date('Y-m-d'),
),
array(
'CommentID' => $this->CommentID,
)
);
$DB->update(
'flag',
array(
'Data_entry' => 'Complete',
'Administration' => 'All',
),
array(
'CommentID' => $this->CommentID,
)
);
} else {
$this->updateStatus('In Progress');
$this->tpl_data['workspace'] = $workspace;
}
$smarty = new Smarty_neurodb;
$smarty->assign($this->tpl_data);
$smarty->display('directentry.tpl');
}
}
if (!class_exists('UnitTestCase')) {
$Runner = new DirectDataEntryMainPage();
$Runner->run();
}