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