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