Newer
Older
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
namespace Drupal\recurring_events_registration;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\recurring_events\Entity\EventInstance;
use Drupal\Core\Messenger\Messenger;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\Core\Entity\EntityTypeManager;
/**
* RegistrationCreationService class.
*/
class RegistrationCreationService {
/**
* The translation interface.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
private $translation;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
private $database;
/**
* Logger Factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* The entity storage for registrants.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* Event instance entity.
*
* @var \Drupal\recurring_events\Entity\EventInstance
*/
protected $eventInstance;
/**
* Event series entity.
*
* @var \Drupal\recurring_events\Entity\EventSeries
*/
protected $eventSeries;
/**
* Class constructor.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The translation interface.
* @param \Drupal\Core\Database\Connection $database
* The database connection.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
* The logger factory.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The messenger service.
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* The entity type manager service.
*/
public function __construct(TranslationInterface $translation, Connection $database, LoggerChannelFactoryInterface $logger, Messenger $messenger, EntityTypeManager $entity_type_manager) {
$this->translation = $translation;
$this->database = $database;
$this->loggerFactory = $logger->get('recurring_events_registration');
$this->messenger = $messenger;
$this->storage = $entity_type_manager->getStorage('registrant');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('string_translation'),
$container->get('database'),
$container->get('logger.factory'),
$container->get('messenger'),
$container->get('entity_type.manager')
);
}
/**
* Set the event entities.
*
* @param Drupal\recurring_events\Entity\EventInstance $event_instance
* The event instance.
*/
public function setEvents(EventInstance $event_instance) {
$this->eventInstance = $event_instance;
$this->eventSeries = $event_instance->getEventSeries();
}
/**
* Retreive all registered parties.
*
* @param bool $include_nonwaitlisted
* Whether or not to include non-waitlisted registrants.
* @param bool $include_waitlisted
* Whether or not to include waitlisted registrants.
* @param int $uid
* The user ID for whom to retrieve registrants.
*
* @return array
* An array of email contacts.
*/
public function retrieveRegisteredParties($include_nonwaitlisted = TRUE, $include_waitlisted = TRUE, $uid = FALSE) {
$parties = [];
$properties = [];
if ($include_nonwaitlisted && !$include_waitlisted) {
$properties['waitlist'] = 0;
}
elseif (!$include_nonwaitlisted && $include_waitlisted) {
$properties['waitlist'] = 1;
}
if (!$include_waitlisted) {
$properties['waitlist'] = 0;
}
if ($uid) {
$properties['user_id'] = $uid;
}
switch ($this->getRegistrationType()) {
case 'series':
$properties['eventseries_id'] = $this->eventSeries->id();
break;
case 'instance':
$properties['eventinstance_id'] = $this->eventInstance->id();
break;
}
$results = $this->storage->loadByProperties($properties);
if (!empty($results)) {
$parties = $results;
}
return $parties;
}
/**
* Get registration availability.
*
* @return int
* The number of spaces available for registration.
*/
public function retrieveAvailability() {
$availability = 0;
$parties = $this->retrieveRegisteredParties(TRUE, FALSE);
$capacity = $this->eventSeries->event_registration->capacity;
if (empty($capacity)) {
$capacity = 0;
}
$availability = $capacity - count($parties);
if ($availability < 0) {
$availability = 0;
}
return $availability;
}
/**
* Get whether this event has a waitlist.
*
* @return bool
* Whether or not there is a waitlist for this event.
*/
public function hasWaitlist() {
$waitlist = FALSE;
if (!empty($this->eventSeries->event_registration->waitlist)) {
$waitlist = (bool) $this->eventSeries->event_registration->waitlist;
}
return $waitlist;
}
/**
* Get whether this event has registration.
*
* @return bool
* Whether or not registration is open for this event.
*/
public function hasRegistration() {
$registration = FALSE;
if (!empty($this->eventSeries->event_registration->registration)) {
$registration = (bool) $this->eventSeries->event_registration->registration;
}
return $registration;
}
/**
* Get registration date range.
*
* @return array
* The registration date range array.
*/
public function getRegistrationDateRange() {
$date_range = [];
$value = $this->eventSeries->event_registration->getValue();
if (!empty($value)) {
$date_range['value'] = $value['value'];
$date_range['end_value'] = $value['end_value'];
}
return $date_range;
}
/**
* Has the user registered for this event before.
*
* @param int $uid
* The ID of the user.
*
* @return bool
* Whether this user has already registered for this event.
*/
public function hasUserRegisteredById($uid) {
$properties = [];
$registrants = $this->retrieveRegisteredParties(TRUE, TRUE, $uid);
return !empty($registrants);
}
/**
* Retreive all waitlisted users.
*
* @return array
* An array of waitlist users
*/
public function retrieveWaitlistedParties() {
$parties = [];
$registrants = $this->retrieveRegisteredParties(FALSE, TRUE);
if (!empty($registrants)) {
$parties = $registrants;
}
return $parties;
}
/**
* Retreive first user on the waitlist.
*
* @return object
* A fully loaded registrant entity.
*/
public function retrieveFirstWaitlistParty() {
$waitlisted_users = $this->retrieveWaitlistedParties();
if (!empty($waitlisted_users)) {
$first = reset($waitlisted_users);
return $first;
}
return NULL;
}
/**
* Get registration type.
*
* @return string
* The type of registration: series, or instance.
*/
public function getRegistrationType() {
$type = FALSE;
if (!empty($this->eventSeries->event_registration->registration_type)) {
$type = $this->eventSeries->event_registration->registration_type;
}
return $type;
}
/**
* Get registration dates type.
*
* @return string
* The type of registration dates: open, or scheduled.
*/
public function getRegistrationDatesType() {
$type = FALSE;
if (!empty($this->eventSeries->event_registration->registration_dates)) {
$type = $this->eventSeries->event_registration->registration_dates;
}
return $type;
}
/**
* Get registration time.
*
* @return string
* The time before each event that registration opens.
*/
public function getRegistrationTime() {
$time = FALSE;
if (!empty($this->eventSeries->event_registration->time_amount) && !empty($this->getRegistrationTimeUnit())) {
$time = $this->eventSeries->event_registration->time_amount . ' ' . $this->getRegistrationTimeUnit();
}
return $time;
}
/**
* Get registration time unit.
*
* @return string
* The unit used to define the registration time, days or hours.
*/
public function getRegistrationTimeUnit() {
$unit = FALSE;
if (!empty($this->eventSeries->event_registration->time_type)) {
$unit = $this->eventSeries->event_registration->time_type;
}
return $unit;
}
/**
* Is registration open for this event?
*
* @return bool
* Whether or not registration is open for this event.
*/
public function registrationIsOpen() {
$registration = FALSE;
if ($this->hasRegistration()) {
$now = new DrupalDateTime();
$reg_open_close_dates = $this->registrationOpeningClosingTime();
if (!empty($reg_open_close_dates)) {
$registration = (
$now->getTimestamp() >= $reg_open_close_dates['reg_open']->getTimestamp()
&& $now->getTimestamp() < $reg_open_close_dates['reg_close']->getTimestamp()
);
}
}
return $registration;
}
/**
* Get registration opening date and time.
*
* @return array
* An array of drupal date time objects for when registration opens/closes.
*/
public function registrationOpeningClosingTime() {
$reg_dates = FALSE;
// Does this event even have registration?
if ($this->hasRegistration()) {
// Grab the type of registration and the type of dates.
$reg_type = $this->getRegistrationType();
$reg_dates_type = $this->getRegistrationDatesType();
$timezone = new \DateTimeZone(drupal_get_user_timezone());
$utc_timezone = new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE);
$now = new DrupalDateTime();
switch ($reg_dates_type) {
case 'open':
// For series, the event registration should close when the first
// event in that series begins. For instance registration the event
// registration should close when that instance begins.
switch ($reg_type) {
case 'series':
$event_date = $this->eventSeries->getSeriesStart();
break;
case 'instance':
$event_date = $this->eventInstance->date->start_date;
break;
}
$event_date->setTimezone($timezone);
$reg_dates = [
'reg_open' => $now,
'reg_close' => $event_date,
];
break;
case 'scheduled':
// The two registration types are 'series' or 'instance'.
switch ($reg_type) {
case 'series':
$reg_date_range = $this->getRegistrationDateRange();
if (!empty($reg_date_range)) {
$reg_start = DrupalDateTime::createFromFormat(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $reg_date_range['value'], $utc_timezone);
$reg_end = DrupalDateTime::createFromFormat(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $reg_date_range['end_value'], $utc_timezone);
$reg_start->setTimezone($timezone);
$reg_end->setTimezone($timezone);
}
break;
case 'instance':
$reg_time_string = $this->getRegistrationTime();
if (!empty($reg_time_string)) {
$event_date = $this->eventInstance->date->start_date;
$reg_end = clone $event_date;
$reg_start = clone $event_date;
// Subtract the number of days/hours from the event start date.
$reg_start->modify('-' . $reg_time_string);
}
break;
}
$reg_dates = [
'reg_open' => $reg_start,
'reg_close' => $reg_end,
];
break;
}
}
return $reg_dates;
}
}