3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Event_BAO_ParticipantStatusType
extends CRM_Event_DAO_ParticipantStatusType
{
20 * @param array $params
24 public static function add(&$params) {
28 $dao = new CRM_Event_DAO_ParticipantStatusType();
29 $dao->copyValues($params);
34 * @param array $params
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();
45 $transaction->commit();
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()) {
62 CRM_Utils_Weight
::delWeight('CRM_Event_DAO_ParticipantStatusType', $id);
64 $dao = new CRM_Event_DAO_ParticipantStatusType();
74 * @param array $params
77 * @return CRM_Event_DAO_ParticipantStatusType|null
79 public static function retrieve(&$params, &$defaults) {
82 $dao = new CRM_Event_DAO_ParticipantStatusType();
83 $dao->copyValues($params);
84 if ($dao->find(TRUE)) {
85 CRM_Core_DAO
::storeValues($dao, $defaults);
98 public static function setIsActive($id, $isActive) {
99 return CRM_Core_DAO
::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
103 * Checks if status_id (id or string (eg. 5 or "Pending from pay later") is allowed for class
105 * @param int|string $status_id
106 * @param string $class
110 public static function getIsValidStatusForClass($status_id, $class = 'Pending') {
111 $classParticipantStatuses = civicrm_api3('ParticipantStatusType', 'get', [
115 $allowedParticipantStatuses = [];
116 foreach ($classParticipantStatuses as $id => $detail) {
117 $allowedParticipantStatuses[$id] = $detail['name'];
119 if (in_array($status_id, $allowedParticipantStatuses) ||
array_key_exists($status_id, $allowedParticipantStatuses)) {
126 * @param array $params
130 public static function process($params) {
132 $returnMessages = [];
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'");
138 //build the required status ids.
139 $statusIds = '(' . implode(',', array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))) . ')';
141 $participantDetails = $fullEvents = [];
142 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
144 //get all participant who's status in class pending and waiting
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,
156 event.expiration_time,
157 event.requires_approval
158 FROM civicrm_participant participant
159 LEFT 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)
162 AND event.is_active = 1
163 ORDER BY participant.register_date, participant.id
165 $dao = CRM_Core_DAO
::executeQuery($query);
166 while ($dao->fetch()) {
167 $participantDetails[$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
,
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.
188 if (!empty($values['registered_by_id'])) {
192 $expirationTime = $values['expiration_time'] ??
NULL;
193 if ($expirationTime && array_key_exists($values['status_id'], $pendingStatuses)) {
195 //get the expiration and registration pending time.
196 $expirationSeconds = $expirationTime * 3600;
197 $registrationPendingSeconds = CRM_Utils_Date
::unixTime($values['register_date']);
199 // expired registration since registration cross allow confirmation time.
200 if (($expirationSeconds +
$registrationPendingSeconds) < time()) {
202 //lets get the transaction mechanism.
203 $transaction = new CRM_Core_Transaction();
205 $ids = [$participantId];
206 $expiredId = array_search('Expired', $expiredStatuses);
207 $results = CRM_Event_BAO_Participant
::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
208 $transaction->commit();
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";
217 //mailed participants.
218 if (is_array($results['mailedParticipants']) &&
219 array_key_exists($processedId, $results['mailedParticipants'])
221 $returnMessages[] .= "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
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.
235 if (!empty($values['registered_by_id'])) {
239 if (array_key_exists($values['status_id'], $waitingStatuses) &&
240 !array_key_exists($values['event_id'], $fullEvents)
243 if ($waitingStatuses[$values['status_id']] == 'On waitlist' &&
244 CRM_Event_BAO_Event
::validRegistrationDate($values)
247 //check the target event having space.
248 $eventOpenSpaces = CRM_Event_BAO_Participant
::eventFull($values['event_id'], TRUE, FALSE);
250 if ($eventOpenSpaces && is_numeric($eventOpenSpaces) ||
($eventOpenSpaces === NULL)) {
252 //get the additional participant if any.
253 $additionalIds = CRM_Event_BAO_Participant
::getAdditionalParticipantIds($participantId);
255 $allIds = [$participantId];
256 if (!empty($additionalIds)) {
257 $allIds = array_merge($allIds, $additionalIds);
259 $pClause = ' participant.id IN ( ' . implode(' , ', $allIds) . ' )';
260 $requiredSpaces = CRM_Event_BAO_Event
::eventTotalSeats($values['event_id'], $pClause);
262 //need to check as to see if event has enough speces
263 if (($requiredSpaces <= $eventOpenSpaces) ||
($eventOpenSpaces === NULL)) {
264 $transaction = new CRM_Core_Transaction();
266 $ids = [$participantId];
267 $updateStatusId = array_search('Pending from waitlist', $pendingStatuses);
269 //lets take a call to make pending or need approval
270 if ($values['requires_approval']) {
271 $updateStatusId = array_search('Awaiting approval', $waitingStatuses);
273 $results = CRM_Event_BAO_Participant
::transitionParticipants($ids, $updateStatusId,
274 $values['status_id'], TRUE, TRUE
276 //commit the transaction.
277 $transaction->commit();
279 if (!empty($results)) {
280 //diaplay updated participants
281 if (is_array($results['updatedParticipantIds']) &&
282 !empty($results['updatedParticipantIds'])
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.";
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'])
296 $returnMessages[] .= "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
304 //target event is full.
305 $fullEvents[$values['event_id']] = $values['eventTitle'];
309 //target event is full.
310 $fullEvents[$values['event_id']] = $values['eventTitle'];
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 />";
327 return ['is_error' => 0, 'messages' => $returnMessages];