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