Merge pull request #22675 from colemanw/customGroupNameIndex
[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 /**
c490a46a 74 * @param array $params
fa3fdebc 75 * @param array $defaults
0cf587a7
EM
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
62afd697
MW
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
0cf587a7 125 /**
c490a46a 126 * @param array $params
0cf587a7
EM
127 *
128 * @return array
129 */
7b60d5b9 130 public static function process($params) {
6a488035 131
be2fb01f 132 $returnMessages = [];
6a488035 133
6a488035
TO
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
be2fb01f 141 $participantDetails = $fullEvents = [];
6a488035
TO
142 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
143
144 //get all participant who's status in class pending and waiting
6a488035
TO
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
159LEFT 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)
03e04002 162 AND event.is_active = 1
163 ORDER BY participant.register_date, participant.id
6a488035
TO
164";
165 $dao = CRM_Core_DAO::executeQuery($query);
166 while ($dao->fetch()) {
be2fb01f 167 $participantDetails[$dao->id] = [
6a488035
TO
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,
be2fb01f 180 ];
6a488035
TO
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.
a7488080 188 if (!empty($values['registered_by_id'])) {
6a488035
TO
189 continue;
190 }
191
9c1bc317 192 $expirationTime = $values['expiration_time'] ?? NULL;
6a488035
TO
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
be2fb01f 205 $ids = [$participantId];
6a488035 206 $expiredId = array_search('Expired', $expiredStatuses);
353ffa53 207 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
6a488035
TO
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.
a7488080 235 if (!empty($values['registered_by_id'])) {
6a488035
TO
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
be2fb01f 255 $allIds = [$participantId];
6a488035
TO
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
be2fb01f 266 $ids = [$participantId];
6a488035
TO
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
be2fb01f 327 return ['is_error' => 0, 'messages' => $returnMessages];
6a488035 328 }
96025800 329
6a488035 330}