Merge pull request #18777 from civicrm/5.31
[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 *
d5cc0fc2 22 * @return this|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 *
d5cc0fc2 36 * @return this|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 /**
c490a46a 74 * @param array $params
0cf587a7
EM
75 * @param $defaults
76 *
77 * @return CRM_Event_DAO_ParticipantStatusType|null
78 */
00be9182 79 public static function retrieve(&$params, &$defaults) {
6a488035
TO
80 $result = NULL;
81
d5cc0fc2 82 $dao = new CRM_Event_DAO_ParticipantStatusType();
6a488035
TO
83 $dao->copyValues($params);
84 if ($dao->find(TRUE)) {
85 CRM_Core_DAO::storeValues($dao, $defaults);
86 $result = $dao;
87 }
88
89 return $result;
90 }
91
0cf587a7 92 /**
100fef9d 93 * @param int $id
0cf587a7
EM
94 * @param $isActive
95 *
96 * @return bool
97 */
00be9182 98 public static function setIsActive($id, $isActive) {
6a488035
TO
99 return CRM_Core_DAO::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
100 }
101
0cf587a7 102 /**
c490a46a 103 * @param array $params
0cf587a7
EM
104 *
105 * @return array
106 */
7b60d5b9 107 public static function process($params) {
6a488035 108
be2fb01f 109 $returnMessages = [];
6a488035 110
6a488035
TO
111 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
112 $expiredStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
113 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
114
115 //build the required status ids.
116 $statusIds = '(' . implode(',', array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))) . ')';
117
be2fb01f 118 $participantDetails = $fullEvents = [];
6a488035
TO
119 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
120
121 //get all participant who's status in class pending and waiting
6a488035
TO
122 $query = "
123 SELECT participant.id,
124 participant.contact_id,
125 participant.status_id,
126 participant.register_date,
127 participant.registered_by_id,
128 participant.event_id,
129 event.title as eventTitle,
130 event.registration_start_date,
131 event.registration_end_date,
132 event.end_date,
133 event.expiration_time,
134 event.requires_approval
135 FROM civicrm_participant participant
136LEFT JOIN civicrm_event event ON ( event.id = participant.event_id )
137 WHERE participant.status_id IN {$statusIds}
138 AND (event.end_date > now() OR event.end_date IS NULL)
03e04002 139 AND event.is_active = 1
140 ORDER BY participant.register_date, participant.id
6a488035
TO
141";
142 $dao = CRM_Core_DAO::executeQuery($query);
143 while ($dao->fetch()) {
be2fb01f 144 $participantDetails[$dao->id] = [
6a488035
TO
145 'id' => $dao->id,
146 'event_id' => $dao->event_id,
147 'status_id' => $dao->status_id,
148 'contact_id' => $dao->contact_id,
149 'register_date' => $dao->register_date,
150 'registered_by_id' => $dao->registered_by_id,
151 'eventTitle' => $dao->eventTitle,
152 'registration_start_date' => $dao->registration_start_date,
153 'registration_end_date' => $dao->registration_end_date,
154 'end_date' => $dao->end_date,
155 'expiration_time' => $dao->expiration_time,
156 'requires_approval' => $dao->requires_approval,
be2fb01f 157 ];
6a488035
TO
158 }
159
160 if (!empty($participantDetails)) {
161 //cron 1. move participant from pending to expire if needed
162 foreach ($participantDetails as $participantId => $values) {
163 //process the additional participant at the time of
164 //primary participant, don't process separately.
a7488080 165 if (!empty($values['registered_by_id'])) {
6a488035
TO
166 continue;
167 }
168
9c1bc317 169 $expirationTime = $values['expiration_time'] ?? NULL;
6a488035
TO
170 if ($expirationTime && array_key_exists($values['status_id'], $pendingStatuses)) {
171
172 //get the expiration and registration pending time.
173 $expirationSeconds = $expirationTime * 3600;
174 $registrationPendingSeconds = CRM_Utils_Date::unixTime($values['register_date']);
175
176 // expired registration since registration cross allow confirmation time.
177 if (($expirationSeconds + $registrationPendingSeconds) < time()) {
178
179 //lets get the transaction mechanism.
180 $transaction = new CRM_Core_Transaction();
181
be2fb01f 182 $ids = [$participantId];
6a488035 183 $expiredId = array_search('Expired', $expiredStatuses);
353ffa53 184 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
6a488035
TO
185 $transaction->commit();
186
187 if (!empty($results)) {
188 //diaplay updated participants
189 if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) {
190 foreach ($results['updatedParticipantIds'] as $processedId) {
191 $expiredParticipantCount += 1;
192 $returnMessages[] .= "<br />Status updated to: Expired";
193
194 //mailed participants.
195 if (is_array($results['mailedParticipants']) &&
196 array_key_exists($processedId, $results['mailedParticipants'])
197 ) {
198 $returnMessages[] .= "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
199 }
200 }
201 }
202 }
203 }
204 }
205 }
206 //cron 1 end.
207
208 //cron 2. lets move participants from waiting list to pending status
209 foreach ($participantDetails as $participantId => $values) {
210 //process the additional participant at the time of
211 //primary participant, don't process separately.
a7488080 212 if (!empty($values['registered_by_id'])) {
6a488035
TO
213 continue;
214 }
215
216 if (array_key_exists($values['status_id'], $waitingStatuses) &&
217 !array_key_exists($values['event_id'], $fullEvents)
218 ) {
219
220 if ($waitingStatuses[$values['status_id']] == 'On waitlist' &&
221 CRM_Event_BAO_Event::validRegistrationDate($values)
222 ) {
223
224 //check the target event having space.
225 $eventOpenSpaces = CRM_Event_BAO_Participant::eventFull($values['event_id'], TRUE, FALSE);
226
227 if ($eventOpenSpaces && is_numeric($eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
228
229 //get the additional participant if any.
230 $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId);
231
be2fb01f 232 $allIds = [$participantId];
6a488035
TO
233 if (!empty($additionalIds)) {
234 $allIds = array_merge($allIds, $additionalIds);
235 }
236 $pClause = ' participant.id IN ( ' . implode(' , ', $allIds) . ' )';
237 $requiredSpaces = CRM_Event_BAO_Event::eventTotalSeats($values['event_id'], $pClause);
238
239 //need to check as to see if event has enough speces
240 if (($requiredSpaces <= $eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
241 $transaction = new CRM_Core_Transaction();
242
be2fb01f 243 $ids = [$participantId];
6a488035
TO
244 $updateStatusId = array_search('Pending from waitlist', $pendingStatuses);
245
246 //lets take a call to make pending or need approval
247 if ($values['requires_approval']) {
248 $updateStatusId = array_search('Awaiting approval', $waitingStatuses);
249 }
250 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $updateStatusId,
251 $values['status_id'], TRUE, TRUE
252 );
253 //commit the transaction.
254 $transaction->commit();
255
256 if (!empty($results)) {
257 //diaplay updated participants
258 if (is_array($results['updatedParticipantIds']) &&
259 !empty($results['updatedParticipantIds'])
260 ) {
261 foreach ($results['updatedParticipantIds'] as $processedId) {
262 if ($values['requires_approval']) {
263 $waitingApprovalCount += 1;
264 $returnMessages[] .= "<br /><br />- status updated to: Awaiting approval";
265 $returnMessages[] .= "<br />Will send you Confirmation Mail when registration gets approved.";
266 }
267 else {
268 $waitingConfirmCount += 1;
269 $returnMessages[] .= "<br /><br />- status updated to: Pending from waitlist";
270 if (is_array($results['mailedParticipants']) &&
271 array_key_exists($processedId, $results['mailedParticipants'])
272 ) {
273 $returnMessages[] .= "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
274 }
275 }
276 }
277 }
278 }
279 }
280 else {
281 //target event is full.
282 $fullEvents[$values['event_id']] = $values['eventTitle'];
283 }
284 }
285 else {
286 //target event is full.
287 $fullEvents[$values['event_id']] = $values['eventTitle'];
288 }
289 }
290 }
291 }
292 //cron 2 ends.
293 }
294
295 $returnMessages[] .= "<br /><br />Number of Expired registration(s) = {$expiredParticipantCount}";
296 $returnMessages[] .= "<br />Number of registration(s) require approval = {$waitingApprovalCount}";
297 $returnMessages[] .= "<br />Number of registration changed to Pending from waitlist = {$waitingConfirmCount}<br /><br />";
298 if (!empty($fullEvents)) {
299 foreach ($fullEvents as $eventId => $title) {
300 $returnMessages[] .= "Full Event : {$title}<br />";
301 }
302 }
303
be2fb01f 304 return ['is_error' => 0, 'messages' => $returnMessages];
6a488035 305 }
96025800 306
6a488035 307}