Enable api_v3_syntaxConformaceTest::testInvalidID_delete (#9068)
[civicrm-core.git] / CRM / Event / BAO / ParticipantStatusType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Event_BAO_ParticipantStatusType extends CRM_Event_DAO_ParticipantStatusType {
0cf587a7 36 /**
c490a46a 37 * @param array $params
0cf587a7 38 *
d5cc0fc2 39 * @return this|null
0cf587a7 40 */
00be9182 41 public static function add(&$params) {
6a488035
TO
42 if (empty($params)) {
43 return NULL;
44 }
d5cc0fc2 45 $dao = new CRM_Event_DAO_ParticipantStatusType();
6a488035
TO
46 $dao->copyValues($params);
47 return $dao->save();
48 }
49
0cf587a7 50 /**
c490a46a 51 * @param array $params
0cf587a7 52 *
d5cc0fc2 53 * @return this|null
0cf587a7 54 */
00be9182 55 public static function &create(&$params) {
6a488035
TO
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
0cf587a7 66 /**
100fef9d 67 * @param int $id
0cf587a7
EM
68 *
69 * @return bool
70 */
00be9182 71 public static function deleteParticipantStatusType($id) {
6a488035 72 // return early if there are participants with this status
d5cc0fc2 73 $participant = new CRM_Event_DAO_Participant();
6a488035
TO
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
d5cc0fc2 81 $dao = new CRM_Event_DAO_ParticipantStatusType();
6a488035 82 $dao->id = $id;
a60c0bc8
SL
83 if (!$dao->find()) {
84 return FALSE;
85 }
6a488035
TO
86 $dao->delete();
87 return TRUE;
88 }
89
0cf587a7 90 /**
c490a46a 91 * @param array $params
0cf587a7
EM
92 * @param $defaults
93 *
94 * @return CRM_Event_DAO_ParticipantStatusType|null
95 */
00be9182 96 public static function retrieve(&$params, &$defaults) {
6a488035
TO
97 $result = NULL;
98
d5cc0fc2 99 $dao = new CRM_Event_DAO_ParticipantStatusType();
6a488035
TO
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
0cf587a7 109 /**
100fef9d 110 * @param int $id
0cf587a7
EM
111 * @param $isActive
112 *
113 * @return bool
114 */
00be9182 115 public static function setIsActive($id, $isActive) {
6a488035
TO
116 return CRM_Core_DAO::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
117 }
118
0cf587a7 119 /**
c490a46a 120 * @param array $params
0cf587a7
EM
121 *
122 * @return array
123 */
7b60d5b9 124 public static function process($params) {
6a488035
TO
125
126 $returnMessages = array();
127
128 $participantRole = CRM_Event_PseudoConstant::participantRole();
129 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
130 $expiredStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
131 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
132
133 //build the required status ids.
134 $statusIds = '(' . implode(',', array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))) . ')';
135
136 $participantDetails = $fullEvents = array();
137 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
138
139 //get all participant who's status in class pending and waiting
140 $query = "SELECT * FROM civicrm_participant WHERE status_id IN {$statusIds} ORDER BY register_date";
141
142 $query = "
143 SELECT participant.id,
144 participant.contact_id,
145 participant.status_id,
146 participant.register_date,
147 participant.registered_by_id,
148 participant.event_id,
149 event.title as eventTitle,
150 event.registration_start_date,
151 event.registration_end_date,
152 event.end_date,
153 event.expiration_time,
154 event.requires_approval
155 FROM civicrm_participant participant
156LEFT JOIN civicrm_event event ON ( event.id = participant.event_id )
157 WHERE participant.status_id IN {$statusIds}
158 AND (event.end_date > now() OR event.end_date IS NULL)
03e04002 159 AND event.is_active = 1
160 ORDER BY participant.register_date, participant.id
6a488035
TO
161";
162 $dao = CRM_Core_DAO::executeQuery($query);
163 while ($dao->fetch()) {
164 $participantDetails[$dao->id] = array(
165 'id' => $dao->id,
166 'event_id' => $dao->event_id,
167 'status_id' => $dao->status_id,
168 'contact_id' => $dao->contact_id,
169 'register_date' => $dao->register_date,
170 'registered_by_id' => $dao->registered_by_id,
171 'eventTitle' => $dao->eventTitle,
172 'registration_start_date' => $dao->registration_start_date,
173 'registration_end_date' => $dao->registration_end_date,
174 'end_date' => $dao->end_date,
175 'expiration_time' => $dao->expiration_time,
176 'requires_approval' => $dao->requires_approval,
177 );
178 }
179
180 if (!empty($participantDetails)) {
181 //cron 1. move participant from pending to expire if needed
182 foreach ($participantDetails as $participantId => $values) {
183 //process the additional participant at the time of
184 //primary participant, don't process separately.
a7488080 185 if (!empty($values['registered_by_id'])) {
6a488035
TO
186 continue;
187 }
188
189 $expirationTime = CRM_Utils_Array::value('expiration_time', $values);
190 if ($expirationTime && array_key_exists($values['status_id'], $pendingStatuses)) {
191
192 //get the expiration and registration pending time.
193 $expirationSeconds = $expirationTime * 3600;
194 $registrationPendingSeconds = CRM_Utils_Date::unixTime($values['register_date']);
195
196 // expired registration since registration cross allow confirmation time.
197 if (($expirationSeconds + $registrationPendingSeconds) < time()) {
198
199 //lets get the transaction mechanism.
200 $transaction = new CRM_Core_Transaction();
201
353ffa53 202 $ids = array($participantId);
6a488035 203 $expiredId = array_search('Expired', $expiredStatuses);
353ffa53 204 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
6a488035
TO
205 $transaction->commit();
206
207 if (!empty($results)) {
208 //diaplay updated participants
209 if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) {
210 foreach ($results['updatedParticipantIds'] as $processedId) {
211 $expiredParticipantCount += 1;
212 $returnMessages[] .= "<br />Status updated to: Expired";
213
214 //mailed participants.
215 if (is_array($results['mailedParticipants']) &&
216 array_key_exists($processedId, $results['mailedParticipants'])
217 ) {
218 $returnMessages[] .= "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
219 }
220 }
221 }
222 }
223 }
224 }
225 }
226 //cron 1 end.
227
228 //cron 2. lets move participants from waiting list to pending status
229 foreach ($participantDetails as $participantId => $values) {
230 //process the additional participant at the time of
231 //primary participant, don't process separately.
a7488080 232 if (!empty($values['registered_by_id'])) {
6a488035
TO
233 continue;
234 }
235
236 if (array_key_exists($values['status_id'], $waitingStatuses) &&
237 !array_key_exists($values['event_id'], $fullEvents)
238 ) {
239
240 if ($waitingStatuses[$values['status_id']] == 'On waitlist' &&
241 CRM_Event_BAO_Event::validRegistrationDate($values)
242 ) {
243
244 //check the target event having space.
245 $eventOpenSpaces = CRM_Event_BAO_Participant::eventFull($values['event_id'], TRUE, FALSE);
246
247 if ($eventOpenSpaces && is_numeric($eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
248
249 //get the additional participant if any.
250 $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId);
251
252 $allIds = array($participantId);
253 if (!empty($additionalIds)) {
254 $allIds = array_merge($allIds, $additionalIds);
255 }
256 $pClause = ' participant.id IN ( ' . implode(' , ', $allIds) . ' )';
257 $requiredSpaces = CRM_Event_BAO_Event::eventTotalSeats($values['event_id'], $pClause);
258
259 //need to check as to see if event has enough speces
260 if (($requiredSpaces <= $eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
261 $transaction = new CRM_Core_Transaction();
262
263 $ids = array($participantId);
264 $updateStatusId = array_search('Pending from waitlist', $pendingStatuses);
265
266 //lets take a call to make pending or need approval
267 if ($values['requires_approval']) {
268 $updateStatusId = array_search('Awaiting approval', $waitingStatuses);
269 }
270 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $updateStatusId,
271 $values['status_id'], TRUE, TRUE
272 );
273 //commit the transaction.
274 $transaction->commit();
275
276 if (!empty($results)) {
277 //diaplay updated participants
278 if (is_array($results['updatedParticipantIds']) &&
279 !empty($results['updatedParticipantIds'])
280 ) {
281 foreach ($results['updatedParticipantIds'] as $processedId) {
282 if ($values['requires_approval']) {
283 $waitingApprovalCount += 1;
284 $returnMessages[] .= "<br /><br />- status updated to: Awaiting approval";
285 $returnMessages[] .= "<br />Will send you Confirmation Mail when registration gets approved.";
286 }
287 else {
288 $waitingConfirmCount += 1;
289 $returnMessages[] .= "<br /><br />- status updated to: Pending from waitlist";
290 if (is_array($results['mailedParticipants']) &&
291 array_key_exists($processedId, $results['mailedParticipants'])
292 ) {
293 $returnMessages[] .= "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
294 }
295 }
296 }
297 }
298 }
299 }
300 else {
301 //target event is full.
302 $fullEvents[$values['event_id']] = $values['eventTitle'];
303 }
304 }
305 else {
306 //target event is full.
307 $fullEvents[$values['event_id']] = $values['eventTitle'];
308 }
309 }
310 }
311 }
312 //cron 2 ends.
313 }
314
315 $returnMessages[] .= "<br /><br />Number of Expired registration(s) = {$expiredParticipantCount}";
316 $returnMessages[] .= "<br />Number of registration(s) require approval = {$waitingApprovalCount}";
317 $returnMessages[] .= "<br />Number of registration changed to Pending from waitlist = {$waitingConfirmCount}<br /><br />";
318 if (!empty($fullEvents)) {
319 foreach ($fullEvents as $eventId => $title) {
320 $returnMessages[] .= "Full Event : {$title}<br />";
321 }
322 }
323
324 return array('is_error' => 0, 'messages' => $returnMessages);
325 }
96025800 326
6a488035 327}