Merge pull request #13548 from civicrm/5.10
[civicrm-core.git] / CRM / Event / BAO / ParticipantStatusType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35 class CRM_Event_BAO_ParticipantStatusType extends CRM_Event_DAO_ParticipantStatusType {
36 /**
37 * @param array $params
38 *
39 * @return this|null
40 */
41 public static function add(&$params) {
42 if (empty($params)) {
43 return NULL;
44 }
45 $dao = new CRM_Event_DAO_ParticipantStatusType();
46 $dao->copyValues($params);
47 return $dao->save();
48 }
49
50 /**
51 * @param array $params
52 *
53 * @return this|null
54 */
55 public static function &create(&$params) {
56 $transaction = new CRM_Core_Transaction();
57 $statusType = self::add($params);
58 if (is_a($statusType, 'CRM_Core_Error')) {
59 $transaction->rollback();
60 return $statusType;
61 }
62 $transaction->commit();
63 return $statusType;
64 }
65
66 /**
67 * @param int $id
68 *
69 * @return bool
70 */
71 public static function deleteParticipantStatusType($id) {
72 // return early if there are participants with this status
73 $participant = new CRM_Event_DAO_Participant();
74 $participant->status_id = $id;
75 if ($participant->find()) {
76 return FALSE;
77 }
78
79 CRM_Utils_Weight::delWeight('CRM_Event_DAO_ParticipantStatusType', $id);
80
81 $dao = new CRM_Event_DAO_ParticipantStatusType();
82 $dao->id = $id;
83 if (!$dao->find()) {
84 return FALSE;
85 }
86 $dao->delete();
87 return TRUE;
88 }
89
90 /**
91 * @param array $params
92 * @param $defaults
93 *
94 * @return CRM_Event_DAO_ParticipantStatusType|null
95 */
96 public static function retrieve(&$params, &$defaults) {
97 $result = NULL;
98
99 $dao = new CRM_Event_DAO_ParticipantStatusType();
100 $dao->copyValues($params);
101 if ($dao->find(TRUE)) {
102 CRM_Core_DAO::storeValues($dao, $defaults);
103 $result = $dao;
104 }
105
106 return $result;
107 }
108
109 /**
110 * @param int $id
111 * @param $isActive
112 *
113 * @return bool
114 */
115 public static function setIsActive($id, $isActive) {
116 return CRM_Core_DAO::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
117 }
118
119 /**
120 * @param array $params
121 *
122 * @return array
123 */
124 public static function process($params) {
125
126 $returnMessages = array();
127
128 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
129 $expiredStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
130 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
131
132 //build the required status ids.
133 $statusIds = '(' . implode(',', array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))) . ')';
134
135 $participantDetails = $fullEvents = array();
136 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
137
138 //get all participant who's status in class pending and waiting
139 $query = "
140 SELECT participant.id,
141 participant.contact_id,
142 participant.status_id,
143 participant.register_date,
144 participant.registered_by_id,
145 participant.event_id,
146 event.title as eventTitle,
147 event.registration_start_date,
148 event.registration_end_date,
149 event.end_date,
150 event.expiration_time,
151 event.requires_approval
152 FROM civicrm_participant participant
153 LEFT JOIN civicrm_event event ON ( event.id = participant.event_id )
154 WHERE participant.status_id IN {$statusIds}
155 AND (event.end_date > now() OR event.end_date IS NULL)
156 AND event.is_active = 1
157 ORDER BY participant.register_date, participant.id
158 ";
159 $dao = CRM_Core_DAO::executeQuery($query);
160 while ($dao->fetch()) {
161 $participantDetails[$dao->id] = array(
162 'id' => $dao->id,
163 'event_id' => $dao->event_id,
164 'status_id' => $dao->status_id,
165 'contact_id' => $dao->contact_id,
166 'register_date' => $dao->register_date,
167 'registered_by_id' => $dao->registered_by_id,
168 'eventTitle' => $dao->eventTitle,
169 'registration_start_date' => $dao->registration_start_date,
170 'registration_end_date' => $dao->registration_end_date,
171 'end_date' => $dao->end_date,
172 'expiration_time' => $dao->expiration_time,
173 'requires_approval' => $dao->requires_approval,
174 );
175 }
176
177 if (!empty($participantDetails)) {
178 //cron 1. move participant from pending to expire if needed
179 foreach ($participantDetails as $participantId => $values) {
180 //process the additional participant at the time of
181 //primary participant, don't process separately.
182 if (!empty($values['registered_by_id'])) {
183 continue;
184 }
185
186 $expirationTime = CRM_Utils_Array::value('expiration_time', $values);
187 if ($expirationTime && array_key_exists($values['status_id'], $pendingStatuses)) {
188
189 //get the expiration and registration pending time.
190 $expirationSeconds = $expirationTime * 3600;
191 $registrationPendingSeconds = CRM_Utils_Date::unixTime($values['register_date']);
192
193 // expired registration since registration cross allow confirmation time.
194 if (($expirationSeconds + $registrationPendingSeconds) < time()) {
195
196 //lets get the transaction mechanism.
197 $transaction = new CRM_Core_Transaction();
198
199 $ids = array($participantId);
200 $expiredId = array_search('Expired', $expiredStatuses);
201 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
202 $transaction->commit();
203
204 if (!empty($results)) {
205 //diaplay updated participants
206 if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) {
207 foreach ($results['updatedParticipantIds'] as $processedId) {
208 $expiredParticipantCount += 1;
209 $returnMessages[] .= "<br />Status updated to: Expired";
210
211 //mailed participants.
212 if (is_array($results['mailedParticipants']) &&
213 array_key_exists($processedId, $results['mailedParticipants'])
214 ) {
215 $returnMessages[] .= "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
216 }
217 }
218 }
219 }
220 }
221 }
222 }
223 //cron 1 end.
224
225 //cron 2. lets move participants from waiting list to pending status
226 foreach ($participantDetails as $participantId => $values) {
227 //process the additional participant at the time of
228 //primary participant, don't process separately.
229 if (!empty($values['registered_by_id'])) {
230 continue;
231 }
232
233 if (array_key_exists($values['status_id'], $waitingStatuses) &&
234 !array_key_exists($values['event_id'], $fullEvents)
235 ) {
236
237 if ($waitingStatuses[$values['status_id']] == 'On waitlist' &&
238 CRM_Event_BAO_Event::validRegistrationDate($values)
239 ) {
240
241 //check the target event having space.
242 $eventOpenSpaces = CRM_Event_BAO_Participant::eventFull($values['event_id'], TRUE, FALSE);
243
244 if ($eventOpenSpaces && is_numeric($eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
245
246 //get the additional participant if any.
247 $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId);
248
249 $allIds = array($participantId);
250 if (!empty($additionalIds)) {
251 $allIds = array_merge($allIds, $additionalIds);
252 }
253 $pClause = ' participant.id IN ( ' . implode(' , ', $allIds) . ' )';
254 $requiredSpaces = CRM_Event_BAO_Event::eventTotalSeats($values['event_id'], $pClause);
255
256 //need to check as to see if event has enough speces
257 if (($requiredSpaces <= $eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
258 $transaction = new CRM_Core_Transaction();
259
260 $ids = array($participantId);
261 $updateStatusId = array_search('Pending from waitlist', $pendingStatuses);
262
263 //lets take a call to make pending or need approval
264 if ($values['requires_approval']) {
265 $updateStatusId = array_search('Awaiting approval', $waitingStatuses);
266 }
267 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $updateStatusId,
268 $values['status_id'], TRUE, TRUE
269 );
270 //commit the transaction.
271 $transaction->commit();
272
273 if (!empty($results)) {
274 //diaplay updated participants
275 if (is_array($results['updatedParticipantIds']) &&
276 !empty($results['updatedParticipantIds'])
277 ) {
278 foreach ($results['updatedParticipantIds'] as $processedId) {
279 if ($values['requires_approval']) {
280 $waitingApprovalCount += 1;
281 $returnMessages[] .= "<br /><br />- status updated to: Awaiting approval";
282 $returnMessages[] .= "<br />Will send you Confirmation Mail when registration gets approved.";
283 }
284 else {
285 $waitingConfirmCount += 1;
286 $returnMessages[] .= "<br /><br />- status updated to: Pending from waitlist";
287 if (is_array($results['mailedParticipants']) &&
288 array_key_exists($processedId, $results['mailedParticipants'])
289 ) {
290 $returnMessages[] .= "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
291 }
292 }
293 }
294 }
295 }
296 }
297 else {
298 //target event is full.
299 $fullEvents[$values['event_id']] = $values['eventTitle'];
300 }
301 }
302 else {
303 //target event is full.
304 $fullEvents[$values['event_id']] = $values['eventTitle'];
305 }
306 }
307 }
308 }
309 //cron 2 ends.
310 }
311
312 $returnMessages[] .= "<br /><br />Number of Expired registration(s) = {$expiredParticipantCount}";
313 $returnMessages[] .= "<br />Number of registration(s) require approval = {$waitingApprovalCount}";
314 $returnMessages[] .= "<br />Number of registration changed to Pending from waitlist = {$waitingConfirmCount}<br /><br />";
315 if (!empty($fullEvents)) {
316 foreach ($fullEvents as $eventId => $title) {
317 $returnMessages[] .= "Full Event : {$title}<br />";
318 }
319 }
320
321 return array('is_error' => 0, 'messages' => $returnMessages);
322 }
323
324 }