Merge pull request #23897 from agh1/5.51.0-releasenotes-initial
[civicrm-core.git] / CRM / Event / BAO / ParticipantStatusType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Event_BAO_ParticipantStatusType extends CRM_Event_DAO_ParticipantStatusType {
90b461f1 18
0cf587a7 19 /**
c490a46a 20 * @param array $params
0cf587a7 21 *
a2f24340 22 * @return self|null
0cf587a7 23 */
00be9182 24 public static function add(&$params) {
6a488035
TO
25 if (empty($params)) {
26 return NULL;
27 }
d5cc0fc2 28 $dao = new CRM_Event_DAO_ParticipantStatusType();
6a488035
TO
29 $dao->copyValues($params);
30 return $dao->save();
31 }
32
0cf587a7 33 /**
c490a46a 34 * @param array $params
0cf587a7 35 *
a2f24340 36 * @return self|null
0cf587a7 37 */
00be9182 38 public static function &create(&$params) {
6a488035
TO
39 $transaction = new CRM_Core_Transaction();
40 $statusType = self::add($params);
41 if (is_a($statusType, 'CRM_Core_Error')) {
42 $transaction->rollback();
43 return $statusType;
44 }
45 $transaction->commit();
46 return $statusType;
47 }
48
0cf587a7 49 /**
100fef9d 50 * @param int $id
0cf587a7
EM
51 *
52 * @return bool
53 */
00be9182 54 public static function deleteParticipantStatusType($id) {
6a488035 55 // return early if there are participants with this status
d5cc0fc2 56 $participant = new CRM_Event_DAO_Participant();
6a488035
TO
57 $participant->status_id = $id;
58 if ($participant->find()) {
59 return FALSE;
60 }
61
62 CRM_Utils_Weight::delWeight('CRM_Event_DAO_ParticipantStatusType', $id);
63
d5cc0fc2 64 $dao = new CRM_Event_DAO_ParticipantStatusType();
6a488035 65 $dao->id = $id;
a60c0bc8
SL
66 if (!$dao->find()) {
67 return FALSE;
68 }
6a488035
TO
69 $dao->delete();
70 return TRUE;
71 }
72
0cf587a7 73 /**
4f940304
CW
74 * Retrieve DB object and copy to defaults array.
75 *
c490a46a 76 * @param array $params
4f940304 77 * Array of criteria values.
fa3fdebc 78 * @param array $defaults
4f940304
CW
79 * Array to be populated with found values.
80 *
81 * @return self|null
82 * The DAO object, if found.
0cf587a7 83 *
4f940304 84 * @deprecated
0cf587a7 85 */
4f940304
CW
86 public static function retrieve($params, &$defaults) {
87 return self::commonRetrieve(self::class, $params, $defaults);
6a488035
TO
88 }
89
0cf587a7 90 /**
100fef9d 91 * @param int $id
0cf587a7
EM
92 * @param $isActive
93 *
94 * @return bool
95 */
00be9182 96 public static function setIsActive($id, $isActive) {
6a488035
TO
97 return CRM_Core_DAO::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
98 }
99
62afd697
MW
100 /**
101 * Checks if status_id (id or string (eg. 5 or "Pending from pay later") is allowed for class
102 *
103 * @param int|string $status_id
104 * @param string $class
105 *
106 * @return bool
107 */
108 public static function getIsValidStatusForClass($status_id, $class = 'Pending') {
109 $classParticipantStatuses = civicrm_api3('ParticipantStatusType', 'get', [
110 'class' => $class,
111 'is_active' => 1,
112 ])['values'];
113 $allowedParticipantStatuses = [];
114 foreach ($classParticipantStatuses as $id => $detail) {
115 $allowedParticipantStatuses[$id] = $detail['name'];
116 }
117 if (in_array($status_id, $allowedParticipantStatuses) || array_key_exists($status_id, $allowedParticipantStatuses)) {
118 return TRUE;
119 }
120 return FALSE;
121 }
122
0cf587a7 123 /**
c490a46a 124 * @param array $params
0cf587a7
EM
125 *
126 * @return array
127 */
7b60d5b9 128 public static function process($params) {
6a488035 129
be2fb01f 130 $returnMessages = [];
6a488035 131
6a488035
TO
132 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
133 $expiredStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
134 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
135
136 //build the required status ids.
137 $statusIds = '(' . implode(',', array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))) . ')';
138
be2fb01f 139 $participantDetails = $fullEvents = [];
6a488035
TO
140 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
141
142 //get all participant who's status in class pending and waiting
6a488035
TO
143 $query = "
144 SELECT participant.id,
145 participant.contact_id,
146 participant.status_id,
147 participant.register_date,
148 participant.registered_by_id,
149 participant.event_id,
150 event.title as eventTitle,
151 event.registration_start_date,
152 event.registration_end_date,
153 event.end_date,
154 event.expiration_time,
155 event.requires_approval
156 FROM civicrm_participant participant
157LEFT JOIN civicrm_event event ON ( event.id = participant.event_id )
158 WHERE participant.status_id IN {$statusIds}
159 AND (event.end_date > now() OR event.end_date IS NULL)
03e04002 160 AND event.is_active = 1
161 ORDER BY participant.register_date, participant.id
6a488035
TO
162";
163 $dao = CRM_Core_DAO::executeQuery($query);
164 while ($dao->fetch()) {
be2fb01f 165 $participantDetails[$dao->id] = [
6a488035
TO
166 'id' => $dao->id,
167 'event_id' => $dao->event_id,
168 'status_id' => $dao->status_id,
169 'contact_id' => $dao->contact_id,
170 'register_date' => $dao->register_date,
171 'registered_by_id' => $dao->registered_by_id,
172 'eventTitle' => $dao->eventTitle,
173 'registration_start_date' => $dao->registration_start_date,
174 'registration_end_date' => $dao->registration_end_date,
175 'end_date' => $dao->end_date,
176 'expiration_time' => $dao->expiration_time,
177 'requires_approval' => $dao->requires_approval,
be2fb01f 178 ];
6a488035
TO
179 }
180
181 if (!empty($participantDetails)) {
182 //cron 1. move participant from pending to expire if needed
183 foreach ($participantDetails as $participantId => $values) {
184 //process the additional participant at the time of
185 //primary participant, don't process separately.
a7488080 186 if (!empty($values['registered_by_id'])) {
6a488035
TO
187 continue;
188 }
189
9c1bc317 190 $expirationTime = $values['expiration_time'] ?? NULL;
6a488035
TO
191 if ($expirationTime && array_key_exists($values['status_id'], $pendingStatuses)) {
192
193 //get the expiration and registration pending time.
194 $expirationSeconds = $expirationTime * 3600;
195 $registrationPendingSeconds = CRM_Utils_Date::unixTime($values['register_date']);
196
197 // expired registration since registration cross allow confirmation time.
198 if (($expirationSeconds + $registrationPendingSeconds) < time()) {
199
200 //lets get the transaction mechanism.
201 $transaction = new CRM_Core_Transaction();
202
be2fb01f 203 $ids = [$participantId];
6a488035 204 $expiredId = array_search('Expired', $expiredStatuses);
353ffa53 205 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
6a488035
TO
206 $transaction->commit();
207
208 if (!empty($results)) {
209 //diaplay updated participants
210 if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) {
211 foreach ($results['updatedParticipantIds'] as $processedId) {
212 $expiredParticipantCount += 1;
213 $returnMessages[] .= "<br />Status updated to: Expired";
214
215 //mailed participants.
216 if (is_array($results['mailedParticipants']) &&
217 array_key_exists($processedId, $results['mailedParticipants'])
218 ) {
219 $returnMessages[] .= "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
220 }
221 }
222 }
223 }
224 }
225 }
226 }
227 //cron 1 end.
228
229 //cron 2. lets move participants from waiting list to pending status
230 foreach ($participantDetails as $participantId => $values) {
231 //process the additional participant at the time of
232 //primary participant, don't process separately.
a7488080 233 if (!empty($values['registered_by_id'])) {
6a488035
TO
234 continue;
235 }
236
237 if (array_key_exists($values['status_id'], $waitingStatuses) &&
238 !array_key_exists($values['event_id'], $fullEvents)
239 ) {
240
241 if ($waitingStatuses[$values['status_id']] == 'On waitlist' &&
242 CRM_Event_BAO_Event::validRegistrationDate($values)
243 ) {
244
245 //check the target event having space.
246 $eventOpenSpaces = CRM_Event_BAO_Participant::eventFull($values['event_id'], TRUE, FALSE);
247
248 if ($eventOpenSpaces && is_numeric($eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
249
250 //get the additional participant if any.
251 $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId);
252
be2fb01f 253 $allIds = [$participantId];
6a488035
TO
254 if (!empty($additionalIds)) {
255 $allIds = array_merge($allIds, $additionalIds);
256 }
257 $pClause = ' participant.id IN ( ' . implode(' , ', $allIds) . ' )';
258 $requiredSpaces = CRM_Event_BAO_Event::eventTotalSeats($values['event_id'], $pClause);
259
260 //need to check as to see if event has enough speces
261 if (($requiredSpaces <= $eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
262 $transaction = new CRM_Core_Transaction();
263
be2fb01f 264 $ids = [$participantId];
6a488035
TO
265 $updateStatusId = array_search('Pending from waitlist', $pendingStatuses);
266
267 //lets take a call to make pending or need approval
268 if ($values['requires_approval']) {
269 $updateStatusId = array_search('Awaiting approval', $waitingStatuses);
270 }
271 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $updateStatusId,
272 $values['status_id'], TRUE, TRUE
273 );
274 //commit the transaction.
275 $transaction->commit();
276
277 if (!empty($results)) {
278 //diaplay updated participants
279 if (is_array($results['updatedParticipantIds']) &&
280 !empty($results['updatedParticipantIds'])
281 ) {
282 foreach ($results['updatedParticipantIds'] as $processedId) {
283 if ($values['requires_approval']) {
284 $waitingApprovalCount += 1;
285 $returnMessages[] .= "<br /><br />- status updated to: Awaiting approval";
286 $returnMessages[] .= "<br />Will send you Confirmation Mail when registration gets approved.";
287 }
288 else {
289 $waitingConfirmCount += 1;
290 $returnMessages[] .= "<br /><br />- status updated to: Pending from waitlist";
291 if (is_array($results['mailedParticipants']) &&
292 array_key_exists($processedId, $results['mailedParticipants'])
293 ) {
294 $returnMessages[] .= "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
295 }
296 }
297 }
298 }
299 }
300 }
301 else {
302 //target event is full.
303 $fullEvents[$values['event_id']] = $values['eventTitle'];
304 }
305 }
306 else {
307 //target event is full.
308 $fullEvents[$values['event_id']] = $values['eventTitle'];
309 }
310 }
311 }
312 }
313 //cron 2 ends.
314 }
315
316 $returnMessages[] .= "<br /><br />Number of Expired registration(s) = {$expiredParticipantCount}";
317 $returnMessages[] .= "<br />Number of registration(s) require approval = {$waitingApprovalCount}";
318 $returnMessages[] .= "<br />Number of registration changed to Pending from waitlist = {$waitingConfirmCount}<br /><br />";
319 if (!empty($fullEvents)) {
320 foreach ($fullEvents as $eventId => $title) {
321 $returnMessages[] .= "Full Event : {$title}<br />";
322 }
323 }
324
be2fb01f 325 return ['is_error' => 0, 'messages' => $returnMessages];
6a488035 326 }
96025800 327
6a488035 328}