Merge pull request #18400 from aydun/class_api_tweak_2
[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 * @param array $params
104 *
105 * @return array
106 */
107 public static function process($params) {
108
109 $returnMessages = [];
110
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
118 $participantDetails = $fullEvents = [];
119 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
120
121 //get all participant who's status in class pending and waiting
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
136 LEFT 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)
139 AND event.is_active = 1
140 ORDER BY participant.register_date, participant.id
141 ";
142 $dao = CRM_Core_DAO::executeQuery($query);
143 while ($dao->fetch()) {
144 $participantDetails[$dao->id] = [
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,
157 ];
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.
165 if (!empty($values['registered_by_id'])) {
166 continue;
167 }
168
169 $expirationTime = $values['expiration_time'] ?? NULL;
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
182 $ids = [$participantId];
183 $expiredId = array_search('Expired', $expiredStatuses);
184 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
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.
212 if (!empty($values['registered_by_id'])) {
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
232 $allIds = [$participantId];
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
243 $ids = [$participantId];
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
304 return ['is_error' => 0, 'messages' => $returnMessages];
305 }
306
307 }