Remove instances of fatal
[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 * $Id$
17 *
18 */
19 class CRM_Event_BAO_ParticipantStatusType extends CRM_Event_DAO_ParticipantStatusType {
20
21 /**
22 * @param array $params
23 *
24 * @return this|null
25 */
26 public static function add(&$params) {
27 if (empty($params)) {
28 return NULL;
29 }
30 $dao = new CRM_Event_DAO_ParticipantStatusType();
31 $dao->copyValues($params);
32 return $dao->save();
33 }
34
35 /**
36 * @param array $params
37 *
38 * @return this|null
39 */
40 public static function &create(&$params) {
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
51 /**
52 * @param int $id
53 *
54 * @return bool
55 */
56 public static function deleteParticipantStatusType($id) {
57 // return early if there are participants with this status
58 $participant = new CRM_Event_DAO_Participant();
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
66 $dao = new CRM_Event_DAO_ParticipantStatusType();
67 $dao->id = $id;
68 if (!$dao->find()) {
69 return FALSE;
70 }
71 $dao->delete();
72 return TRUE;
73 }
74
75 /**
76 * @param array $params
77 * @param $defaults
78 *
79 * @return CRM_Event_DAO_ParticipantStatusType|null
80 */
81 public static function retrieve(&$params, &$defaults) {
82 $result = NULL;
83
84 $dao = new CRM_Event_DAO_ParticipantStatusType();
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
94 /**
95 * @param int $id
96 * @param $isActive
97 *
98 * @return bool
99 */
100 public static function setIsActive($id, $isActive) {
101 return CRM_Core_DAO::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
102 }
103
104 /**
105 * @param array $params
106 *
107 * @return array
108 */
109 public static function process($params) {
110
111 $returnMessages = [];
112
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
120 $participantDetails = $fullEvents = [];
121 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
122
123 //get all participant who's status in class pending and waiting
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
138 LEFT 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)
141 AND event.is_active = 1
142 ORDER BY participant.register_date, participant.id
143 ";
144 $dao = CRM_Core_DAO::executeQuery($query);
145 while ($dao->fetch()) {
146 $participantDetails[$dao->id] = [
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,
159 ];
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.
167 if (!empty($values['registered_by_id'])) {
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
184 $ids = [$participantId];
185 $expiredId = array_search('Expired', $expiredStatuses);
186 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
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.
214 if (!empty($values['registered_by_id'])) {
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
234 $allIds = [$participantId];
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
245 $ids = [$participantId];
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
306 return ['is_error' => 0, 'messages' => $returnMessages];
307 }
308
309 }