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 * Retrieve DB object and copy to defaults array.
76 * @param array $params
77 * Array of criteria values.
78 * @param array $defaults
79 * Array to be populated with found values.
82 * The DAO object, if found.
86 public static function retrieve($params, &$defaults) {
87 return self
::commonRetrieve(self
::class, $params, $defaults);
96 public static function setIsActive($id, $isActive) {
97 return CRM_Core_DAO
::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
101 * Checks if status_id (id or string (eg. 5 or "Pending from pay later") is allowed for class
103 * @param int|string $status_id
104 * @param string $class
108 public static function getIsValidStatusForClass($status_id, $class = 'Pending') {
109 $classParticipantStatuses = civicrm_api3('ParticipantStatusType', 'get', [
113 $allowedParticipantStatuses = [];
114 foreach ($classParticipantStatuses as $id => $detail) {
115 $allowedParticipantStatuses[$id] = $detail['name'];
117 if (in_array($status_id, $allowedParticipantStatuses) ||
array_key_exists($status_id, $allowedParticipantStatuses)) {
124 * @param array $params
128 public static function process($params) {
130 $returnMessages = [];
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'");
136 //build the required status ids.
137 $statusIds = '(' . implode(',', array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))) . ')';
139 $participantDetails = $fullEvents = [];
140 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
142 //get all participant who's status in class pending and waiting
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,
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
163 $dao = CRM_Core_DAO
::executeQuery($query);
164 while ($dao->fetch()) {
165 $participantDetails[$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
,
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'])) {
190 $expirationTime = $values['expiration_time'] ??
NULL;
191 if ($expirationTime && array_key_exists($values['status_id'], $pendingStatuses)) {
193 //get the expiration and registration pending time.
194 $expirationSeconds = $expirationTime * 3600;
195 $registrationPendingSeconds = CRM_Utils_Date
::unixTime($values['register_date']);
197 // expired registration since registration cross allow confirmation time.
198 if (($expirationSeconds +
$registrationPendingSeconds) < time()) {
200 //lets get the transaction mechanism.
201 $transaction = new CRM_Core_Transaction();
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();
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";
215 //mailed participants.
216 if (is_array($results['mailedParticipants']) &&
217 array_key_exists($processedId, $results['mailedParticipants'])
219 $returnMessages[] .= "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
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'])) {
237 if (array_key_exists($values['status_id'], $waitingStatuses) &&
238 !array_key_exists($values['event_id'], $fullEvents)
241 if ($waitingStatuses[$values['status_id']] == 'On waitlist' &&
242 CRM_Event_BAO_Event
::validRegistrationDate($values)
245 //check the target event having space.
246 $eventOpenSpaces = CRM_Event_BAO_Participant
::eventFull($values['event_id'], TRUE, FALSE);
248 if ($eventOpenSpaces && is_numeric($eventOpenSpaces) ||
($eventOpenSpaces === NULL)) {
250 //get the additional participant if any.
251 $additionalIds = CRM_Event_BAO_Participant
::getAdditionalParticipantIds($participantId);
253 $allIds = [$participantId];
254 if (!empty($additionalIds)) {
255 $allIds = array_merge($allIds, $additionalIds);
257 $pClause = ' participant.id IN ( ' . implode(' , ', $allIds) . ' )';
258 $requiredSpaces = CRM_Event_BAO_Event
::eventTotalSeats($values['event_id'], $pClause);
260 //need to check as to see if event has enough speces
261 if (($requiredSpaces <= $eventOpenSpaces) ||
($eventOpenSpaces === NULL)) {
262 $transaction = new CRM_Core_Transaction();
264 $ids = [$participantId];
265 $updateStatusId = array_search('Pending from waitlist', $pendingStatuses);
267 //lets take a call to make pending or need approval
268 if ($values['requires_approval']) {
269 $updateStatusId = array_search('Awaiting approval', $waitingStatuses);
271 $results = CRM_Event_BAO_Participant
::transitionParticipants($ids, $updateStatusId,
272 $values['status_id'], TRUE, TRUE
274 //commit the transaction.
275 $transaction->commit();
277 if (!empty($results)) {
278 //diaplay updated participants
279 if (is_array($results['updatedParticipantIds']) &&
280 !empty($results['updatedParticipantIds'])
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.";
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'])
294 $returnMessages[] .= "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
302 //target event is full.
303 $fullEvents[$values['event_id']] = $values['eventTitle'];
307 //target event is full.
308 $fullEvents[$values['event_id']] = $values['eventTitle'];
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 />";
325 return ['is_error' => 0, 'messages' => $returnMessages];