-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticipant.php
245 lines (220 loc) · 7.32 KB
/
Participant.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
<?php
namespace Stanford\AppointmentScheduler;
class Participant
{
/**
* @param string $email
* @param string $date
* @param int $project_id
* @param int $event_id
* @return bool
*/
public function isUserBookedSlotForThatDay($email, $date, $project_id, $event_id)
{
/**
* Let see if user booked something else for same date, we will validate via email
* TODO we can verify via user_id or SUNet ID if we decided to do so
*/
$range = "rd.value > '" . date('Y-m-d', strtotime($date)) . "' AND " . "rd.value < '" . date('Y-m-d',
strtotime($date . ' + 1 DAY')) . "'";
$data_table = method_exists('\REDCap', 'getDataTable') ? \REDCap::getDataTable($project_id) : "redcap_data";
$sql = sprintf("SELECT id from redcap_appointment_participant ra JOIN $data_table rd ON ra.record_id = rd.record WHERE rd.project_id = $project_id AND event_id = $event_id AND $range AND ra.email = '$email'");
$r = db_query($sql);
$count = db_num_rows($r);
if ($count > 0) {
return true;
}
return false;
}
/**
* @param int $event_id
* @param int $record_id
* @return array
*/
public function getParticipationSlotData($recodId, $projectId, $primary)
{
try {
$filter = "[$primary] = '" . $recodId . "'";
$param = array(
'project_id' => $projectId,
'filterLogic' => $filter,
'return_format' => 'array',
);
$record = \REDCap::getData($param);
return $record[$recodId];
} catch (\LogicException $e) {
echo $e->getMessage();
}
}
public static function isSuperUser()
{
return defined('SUPER_USER');
}
public static function canUserUpdateReservations($sunetId)
{
if ((defined('USERID') && USERID == $sunetId) || AppointmentScheduler::isUserHasManagePermission()) {
return true;
}
return false;
}
/**
* @param int $record_id
* @return int
*/
public function getSlotActualCountReservedSpots($slotId, $eventId, $suffix, $projectId)
{
try {
//this flag will determine if logged in user booked this slot
$userBookThisSlot = false;
$counter = 0;
$param = array(
'project_id' => $projectId,
'return_format' => 'array',
'events' => $eventId
);
$records = \REDCap::getData($param);
foreach ($records as $id => $record) {
if ($record[$eventId]["slot_id$suffix"] == $slotId && $record[$eventId]["participant_status$suffix"] == RESERVED) {
if (self::canUserUpdateReservations($record[$eventId]["sunet_id"])) {
//capture record id for cancellation
$record[$eventId]['record_id'] = $id;
$userBookThisSlot[] = $record[$eventId];
}
$counter++;
}
}
return array('counter' => $counter, 'userBookThisSlot' => $userBookThisSlot);
} catch (\LogicException $e) {
echo $e->getMessage();
}
}
/**
* @param int $record_id
* @return bool|\mysqli_result
*/
public function getSlotActualReservedSpots($slotId, $eventId, $projectId)
{
try {
$filter = "[slot_id] = '" . $slotId . "' AND [participant_status] ='" . RESERVED . "'";
$param = array(
'project_id' => $projectId,
'filterLogic' => $filter,
'return_format' => 'array',
'events' => $eventId
);
$record = \REDCap::getData($param);
return $record;
} catch (\LogicException $e) {
echo $e->getMessage();
}
}
/**
* @param int $record_id
* @return bool|\mysqli_result
*/
public function getAllReservedSlots($projectId)
{
try {
$filter = "[participant_status] ='" . RESERVED . "'";
$param = array(
'project_id' => $projectId,
'filterLogic' => $filter,
'return_format' => 'array'
);
$records = \REDCap::getData($param);
return $records;
} catch (\LogicException $e) {
echo $e->getMessage();
}
}
/**
* @param int $record_id
* @return bool|\mysqli_result
*/
public function getSlotParticipants($recordId, $eventId, $suffix, $projectId)
{
try {
$filter = "[slot_id$suffix] = '" . $recordId . "'";
$param = array(
'project_id' => $projectId,
'filterLogic' => $filter,
'return_format' => 'array',
'events' => $eventId
);
$record = \REDCap::getData($param);
return $record;
} catch (\LogicException $e) {
echo $e->getMessage();
}
}
/**
* @param int $event_id
* @param int $record_id
* @return bool
*/
public function isThereAvailableSpotsInAppointment($event_id, $record_id, $projectId, $primary)
{
$slot = AppointmentScheduler::getSlot($record_id, $event_id, $projectId, $primary);
if ($slot['number_of_participants'] > $this->getSlotActualCountReservedSpots($record_id, $event_id, '',
$projectId)) {
return true;
} else {
return false;
}
}
/**
* @param $sunetID
* @param $suffix
* @param $projectId
* @param null $status
* @return mixed
*/
public function getUserParticipation($sunetID, $suffix, $projectId, $status = null)
{
try {
if (is_null($status)) {
$filter = "[sunet_id$suffix] = '" . $sunetID . "'";
} else {
$filter = "[sunet_id$suffix] = '" . $sunetID . "' AND [participant_status$suffix] = $status";
}
$param = array(
'project_id' => $projectId,
'filterLogic' => $filter,
'return_format' => 'array'
);
$records = \REDCap::getData($param);
return $records;
} catch (\LogicException $e) {
echo $e->getMessage();
}
}
/**
* @param array $data
* @param int $id
*/
public function updateParticipation($data, $id)
{
$filters = '';
foreach ($data as $key => $value) {
$filters = " $key = '$value' ,";
}
$filters = rtrim($filters, ",");
$sql = sprintf("UPDATE redcap_appointment_participant SET $filters WHERE id = $id");
if (!db_query($sql)) {
throw new \LogicException('cant update participant');
}
}
public function getUserParticipationViaStatus($records, $status, $suffix)
{
$result = array();
foreach ($records as $record) {
$participation = end($record);
$eventId = key($record);
$participation['event_id'] = $eventId;
if ($participation['participant_status' . $suffix] == $status) {
$result[] = $participation;
}
}
return $result;
}
}