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