/
getData.php
371 lines (332 loc) · 10.3 KB
/
getData.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
<?php
/**
* Publication data retriever
*
* This retrieves data for publication uploads & editing
*
* PHP Version 7
*
* @category Loris
* @package Publication
* @author David <dblader.mcin@gmail.com>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://github.com/aces/Loris-Trunk
*/
if (isset($_REQUEST['action'])) {
$db = \Database::singleton();
$user = \User::singleton();
$action = $_REQUEST['action'];
$message = array('message' => null);
if ($action === 'getData') {
if (userCanGetData($db, $user)) {
header('Content-Type: application/json');
exit(json_encode(getData($db)));
} else {
http_response_code(403);
$message['message']
= 'You do not have access to the publication module.';
exit(json_encode($message));
}
} elseif ($action === 'getProjectData') {
$id = $_REQUEST['id'];
if (userCanGetData($db, $user, $id)) {
header('Content-Type: application/json');
exit(json_encode(getProjectData($db, $user, $id)));
} else {
http_response_code(403);
$message['message'] = 'You do not have access to this project.';
exit(json_encode($message));
}
} else {
http_response_code(400);
exit;
}
}
/**
* Gets publication and parameter_type data from database
*
* @param Database $db Database instance
*
* @return array Array of general publication data
*/
function getData($db) : array
{
$data = array();
$titles = $db->pselectCol(
'SELECT Title FROM publication',
array()
);
// for selecting behavioral variables of interest
$bvlVOIs = $db->pselect(
"SELECT pt.Name, pt.SourceFrom FROM parameter_type pt ".
"JOIN test_names tn ON tn.Test_name=pt.SourceFrom ORDER BY pt.SourceFrom",
array()
);
// merge variables and test names into one array
$bvlVOIs = array_merge(
array_column($bvlVOIs, 'Name'),
array_unique(array_column($bvlVOIs, 'SourceFrom'))
);
sort($bvlVOIs);
// sets keys and values to be equal
$allVOIs = array();
$allVOIs['Behavioral'] = array_combine($bvlVOIs, $bvlVOIs);
// imaging VoIs -- filter out non-human readable DICOM tags
$imgVOIs = $db->pselectCol(
"SELECT DISTINCT p.Name FROM parameter_type p
JOIN parameter_type_category_rel ptcr
ON p.ParameterTypeID=ptcr.ParameterTypeID
JOIN parameter_type_category ptc
ON ptc.ParameterTypeCategoryID=ptcr.ParameterTypeCategoryID
WHERE ptc.Name='MRI Variables'",
array()
);
sort($imgVOIs);
// sets keys and values to be equal
$allVOIs['Imaging'] = array_combine($imgVOIs, $imgVOIs);
$users = $db->pselectColWithIndexKey(
"SELECT ID, Real_name FROM users ".
"WHERE Active='Y' AND Pending_approval='N' ".
"ORDER BY Real_name",
array(),
'ID'
);
$kws = $db->pselectCol(
'SELECT Label FROM publication_keyword',
array()
);
$kws = array_combine($kws, $kws);
$collabs = $db->pselectCol(
'SELECT Name FROM publication_collaborator',
array()
);
$collabs = array_combine($collabs, $collabs);
$data['users'] = $users;
$data['uploadTypes'] = getUploadTypes();
$data['existingTitles'] = $titles;
$data['allVOIs'] = $allVOIs;
$data['allKWs'] = $kws;
$data['allCollabs'] = $collabs;
return $data;
}
/**
* Gets Data for a specific PublicationID
*
* @param Database $db Database instance
* @param User $user User instance
* @param int $id Publication ID
*
* @return array Array of data for a specific project
*/
function getProjectData($db, $user, $id) : array
{
$query = 'SELECT Title, Description, DateProposed, '.
'pc.Name as LeadInvestigator, pc.Email as LeadInvestigatorEmail, '.
'PublicationStatusID, UserID, RejectedReason '.
'FROM publication p '.
'LEFT JOIN publication_collaborator pc '.
'ON p.LeadInvestigatorID = pc.PublicationCollaboratorID '.
'WHERE p.PublicationID=:pid ';
$result = $db->pselectRow(
$query,
array('pid' => $id)
);
if (!$result) {
throw new LorisException('Invalid publication ID!');
} else {
$result['VOIs'] = getVOIs($id);
$result['files'] = getFiles($id);
$result['Keywords'] = getKeywords($id);
$result['collaborators'] = getCollaborators($id);
// allow edit access for user if user is original proposer
$userIDs = $db->pselectCol(
'SELECT pu.UserID as ID '.
'FROM publication_users_edit_perm_rel pu '.
'WHERE PublicationID=:p',
array('p' => $id)
);
$userCanEdit = (
$user->getId() === $result['UserID'] ||
in_array($user->getId(), $userIDs)
);
$usersWithEditPerm = $userIDs;
$title = htmlspecialchars_decode($result['Title']);
$description = htmlspecialchars_decode($result['Description']);
$rejectedReason = htmlspecialchars_decode($result['RejectedReason']);
$pubData = array(
'title' => $title,
'description' => $description,
'leadInvestigator' => $result['LeadInvestigator'],
'leadInvestigatorEmail' => $result['LeadInvestigatorEmail'],
'status' => $result['PublicationStatusID'],
'rejectedReason' => $rejectedReason,
'voi' => $result['VOIs'],
'keywords' => $result['Keywords'],
'collaborators' => $result['collaborators'],
'files' => $result['files'],
'usersWithEditPerm' => $usersWithEditPerm,
'userCanEdit' => $userCanEdit,
'statusOpts' => getStatusOptions(),
'uploadTypes' => getUploadTypes(),
);
// if user can edit, retrieve getData() options to allow modifications
if ($userCanEdit) {
return array_merge($pubData, getData($db));
} else {
return $pubData;
}
}
}
/**
* Gets Variables of Interest for a given publication ID
*
* @param int $id the PublicationID
*
* @return array Array of VoIs
*/
function getVOIs($id) : array
{
$db = \Database::singleton();
$fields = $db->pselectCol(
'SELECT pt.Name AS field ' .
'FROM parameter_type pt '.
'LEFT JOIN publication_parameter_type_rel pptr '.
'ON pptr.ParameterTypeID=pt.ParameterTypeID '.
'WHERE pptr.PublicationID=:pid',
array('pid' => $id)
);
$testNames = $db->pselectCol(
'SELECT Test_name '.
'FROM publication_test_names_rel ptnr '.
'LEFT JOIN test_names tn '.
'ON tn.ID=ptnr.TestNameID '.
'WHERE PublicationID=:pid',
array('pid' => $id)
);
$vois = array_merge($testNames, $fields);
return $vois;
}
/**
* Gets Keywords for a given publication ID
*
* @param int $id the PublicationID
*
* @return array Array of keywords
*/
function getKeywords($id) : array
{
$db = \Database::singleton();
$kws = $db->pselectCol(
'SELECT pk.Label FROM publication_keyword pk '.
'LEFT JOIN publication_keyword_rel pkr '.
'ON pkr.PublicationKeywordID=pk.PublicationKeywordID '.
'WHERE pkr.PublicationID=:pid',
array('pid' => $id)
);
return $kws;
}
/**
* Gets Collaborators for a given publication ID
*
* @param int $id the PublicationID
*
* @return array Array of collaborators
*/
function getCollaborators($id) : array
{
$db = \Database::singleton();
$collaborators = $db->pselect(
'SELECT Name as name, Email as email FROM publication_collaborator pc '.
'LEFT JOIN publication_collaborator_rel pcr '.
'ON pc.PublicationCollaboratorID=pcr.PublicationCollaboratorID '.
'WHERE pcr.PublicationID=:pid',
array('pid' => $id)
);
return $collaborators;
}
/**
* Gets file uploads for a given publication ID
*
* @param int $id the PublicationID
*
* @return array Array of file (meta) data
*/
function getFiles($id) : array
{
$db = \Database::singleton();
$files = $db->pselect(
'SELECT * FROM publication_upload WHERE PublicationID=:pid',
array('pid' => $id)
);
foreach ($files as $key => $f) {
$files[$key]['Citation'] = htmlspecialchars_decode($f['Citation']);
$files[$key]['Version'] = htmlspecialchars_decode($f['Version']);
}
return $files;
}
/**
* Gets options for setting the approval status for projects
*
* @return array Array of status options
*/
function getStatusOptions() : array
{
$db = \Database::singleton();
$rawStatus = $db->pselect(
'SELECT * FROM publication_status',
array()
);
$statusOpts = array();
foreach ($rawStatus as $rs) {
$statusOpts[$rs['PublicationStatusID']] = $rs['Label'];
}
return $statusOpts;
}
/**
* Gets different types of uploads
*
* @return array Array of upload types
*/
function getUploadTypes() : array
{
$db = \Database::singleton();
return $db->pselectColWithIndexKey(
'SELECT PublicationUploadTypeID, Label FROM publication_upload_type',
array(),
'PublicationUploadTypeID'
);
}
/**
* Permission check
*
* @param Database $db database
* @param User $user user
* @param int $pubID publication ID
*
* @return bool
*/
function userCanGetData($db, $user, $pubID = null) : bool
{
$retVal = true;
if (is_null($pubID) && !$user->hasPermission('publication_view')) {
$retVal = false;
}
$origUser = $db->pselectOne(
'SELECT UserID FROM publication WHERE PublicationID=:p',
array('p' => $pubID)
);
$userIDs = $db->pselectCol(
'SELECT pu.UserID as ID '.
'FROM publication_users_edit_perm_rel pu '.
'WHERE PublicationID=:p',
array('p' => $pubID)
);
$userCanEdit = (
$user->getId() === $origUser ||
in_array($user->getId(), $userIDs)
);
if (!$userCanEdit && !$user->hasPermission('publication_view')) {
$retVal = false;
}
return $retVal;
}