-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUserProfile.php
258 lines (217 loc) · 7.48 KB
/
UserProfile.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
<?php
/**
* @file
* Provides User Profile class.
*/
namespace UserProfile;
use ExternalModules\ExternalModules;
use Project;
use Records;
use REDCap;
/**
* User Profile class.
*/
class UserProfile {
protected $projectId;
protected $username;
protected $usernameField;
protected $profileId;
protected $profileData;
/**
* Gets a list of existing profiles.
*
* @return array
* An array of UserProfile objects keyed by username.
*/
public static function getProfiles() {
$pid = ExternalModules::getSystemSetting('redcap_user_profile', 'project_id');
$field = ExternalModules::getSystemSetting('redcap_user_profile', 'username_field');
$data = REDCap::getData($pid, 'array', null, $field);
$profiles = array();
foreach ($data as $username) {
// Since the result is given a record-event-value nesting
// structure, we need to get rid of the first two array levels in
// order to get the username.
$username = reset($username);
$username = reset($username);
$profiles[$username] = new UserProfile($username);
}
return $profiles;
}
/**
* Creates a new user profile.
*
* @param mixed $data
* The user profile data array or the username.
*
* @return bool
* TRUE if success, FALSE otherwise.
*/
public static function createProfile($data) {
$module = ExternalModules::getModuleInstance('redcap_user_profile');
$username_field = $module->getSystemSetting('username_field');
if (is_string($data)) {
$data = array($username_field => $data);
}
elseif (!isset($data[$username_field])) {
REDCap::logEvent('User profile creation failed', 'No username provided.');
return false;
}
$username = db_escape($data[$username_field]);
$project_id = $module->getSystemSetting('project_id');
if (REDCap::getData($project_id, 'array', null, $username_field, null, null, false, false, false, '[' . $username_field . '] = "' . $username . '"')) {
REDCap::logEvent('User profile creation failed', 'User profile "' . $username . '" already exists.');
return false;
}
// Checking whether input fields are valid.
$project = new Project($project_id);
foreach (array_keys($data) as $field_name) {
if (!isset($project->metadata[$field_name])) {
unset($data[$field_name]);
}
}
$data = array(
$module->getAutoId() => array(
$project->firstEventId => $data + array(
$project->firstForm . '_complete' => 2,
),
),
);
$result = Records::saveData($project_id, 'array', $data);
if (!is_array($result) || !empty($result['errors']) || empty($result['ids'])) {
$msg = !is_array($result) || empty($result['errors']) ? 'Data could not be saved.' : json_encode($result['errors']);
REDCap::logEvent('User profile creation failed', $msg);
return false;
}
REDCap::logEvent('User profile created', 'Username: "' . $username . '"');
return true;
}
/**
* Constructor.
*/
public function __construct($username, $set_profile_data = true) {
$module_name = 'redcap_user_profile';
$this->username = $username;
$this->usernameField = ExternalModules::getSystemSetting($module_name, 'username_field');
$this->projectId = ExternalModules::getSystemSetting($module_name, 'project_id');
$this->setProfileId();
if ($set_profile_data) {
$this->setProfileData();
}
}
/**
* Gets profile username.
*
* @return string
* The profile username.
*/
public function getUsername() {
return $this->username;
}
/**
* Gets profile ID.
*
* @return int
* The profile record ID.
*/
public function getProfileId() {
return $this->profileId;
}
/**
* Gets profile data.
*
* @return array
* A keyed array containing the profile information.
*/
public function getProfileData() {
return $this->profileData;
}
/**
* Gets project ID.
*
* @return int
* The profile project ID.
*/
public function getProjectId() {
return $this->projectId;
}
/**
* Gets username field name.
*
* @return string
* The username field name.
*/
public function getUsernameField() {
return $this->usernameField;
}
/**
* Sets profile ID.
*/
protected function setProfileId() {
$sql = '
SELECT record FROM redcap_data
WHERE
field_name = "' . db_real_escape_string($this->usernameField) . '" AND
project_id = "' . intval($this->projectId) . '" AND
value = "' . db_real_escape_string($this->username) . '"
LIMIT 1';
$q = db_query($sql);
if (!db_num_rows($q)) {
return;
}
$result = db_fetch_assoc($q);
$this->profileId = $result['record'];
}
/**
* Sets profile data.
*/
protected function setProfileData() {
if (empty($this->profileId)) {
return;
}
$data = REDCap::getData($this->projectId, 'array', $this->profileId);
$project = new Project($this->projectId);
// Taking the first and only event (the concept of events does not apply
// to user profiles.
$data = reset($data);
// Getting repeat instruments.
$repeat_instances = array();
if (isset($data['repeat_instances']) && isset($data['repeat_instances'][$project->firstEventId])) {
$repeat_instances = $data['repeat_instances'][$project->firstEventId];
}
$data = $data[$project->firstEventId];
$user_profile = array();
foreach ($project->forms as $form_key => $form) {
if (!in_array($form_key, $project->eventsForms[$project->firstEventId])) {
continue;
}
$user_profile[$form_key] = array();
if (isset($repeat_instances[$form_key])) {
// Handling repeat instruments.
foreach ($repeat_instances[$form_key] as $instance_data) {
$row = array();
foreach (array_keys($form['fields']) as $field) {
$row[$field] = $instance_data[$field];
}
// Removing "complete?" field from repeat instrument data.
unset($row[$form_key . '_complete']);
$user_profile[$form_key][] = $row;
}
}
else {
foreach (array_keys($form['fields']) as $field) {
$user_profile[$form_key][$field] = $data[$field];
}
}
// Removing "complete?" field from profile data.
unset($user_profile[$form_key][$form_key . '_complete']);
}
// Removing record ID from profile data.
unset($user_profile[$project->firstForm][$project->table_pk]);
if (count($user_profile) == 1) {
// If there is only one instrument, remove the first tree level.
$user_profile = reset($user_profile);
}
$this->profileData = $user_profile;
}
}