Merge branch 'phpunit-ob-fix' of https://github.com/giant-rabbit/civicrm-core into...
[civicrm-core.git] / bin / deprecated / ParticipantProcessor.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 * This file check and updates the status of all participant records.
31 *
32 * EventParticipantion.php prior to running this script.
33 */
34
35 require_once '../civicrm.config.php';
36 require_once 'CRM/Core/Config.php';
37
38 /**
39 * Class CRM_ParticipantProcessor
40 */
41 class CRM_ParticipantProcessor {
42 /**
43 *
44 */
45 function __construct() {
46 $config = CRM_Core_Config::singleton();
47
48 //this does not return on failure
49 require_once 'CRM/Utils/System.php';
50 require_once 'CRM/Utils/Hook.php';
51
52 CRM_Utils_System::authenticateScript(TRUE);
53
54 //log the execution time of script
55 CRM_Core_Error::debug_log_message('ParticipantProcessor.php');
56 }
57
58 public function updateParticipantStatus() {
59 require_once 'CRM/Event/PseudoConstant.php';
60 $participantRole = CRM_Event_PseudoConstant::participantRole();
61 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
62 $expiredStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
63 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
64
65 //build the required status ids.
66 $statusIds = '(' . implode(',', array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))) . ')';
67
68 $participantDetails = $fullEvents = array();
69 $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
70
71 //get all participant who's status in class pending and waiting
72 $query = "SELECT * FROM civicrm_participant WHERE status_id IN {$statusIds} ORDER BY register_date";
73
74 $query = "
75 SELECT participant.id,
76 participant.contact_id,
77 participant.status_id,
78 participant.register_date,
79 participant.registered_by_id,
80 participant.event_id,
81 event.title as eventTitle,
82 event.registration_start_date,
83 event.registration_end_date,
84 event.end_date,
85 event.expiration_time,
86 event.requires_approval
87 FROM civicrm_participant participant
88 LEFT JOIN civicrm_event event ON ( event.id = participant.event_id )
89 WHERE participant.status_id IN {$statusIds}
90 AND (event.end_date > now() OR event.end_date IS NULL)
91 AND event.is_active = 1
92 ORDER BY participant.register_date, participant.id
93 ";
94 $dao = CRM_Core_DAO::executeQuery($query);
95 while ($dao->fetch()) {
96 $participantDetails[$dao->id] = array(
97 'id' => $dao->id,
98 'event_id' => $dao->event_id,
99 'status_id' => $dao->status_id,
100 'contact_id' => $dao->contact_id,
101 'register_date' => $dao->register_date,
102 'registered_by_id' => $dao->registered_by_id,
103 'eventTitle' => $dao->eventTitle,
104 'registration_start_date' => $dao->registration_start_date,
105 'registration_end_date' => $dao->registration_end_date,
106 'end_date' => $dao->end_date,
107 'expiration_time' => $dao->expiration_time,
108 'requires_approval' => $dao->requires_approval,
109 );
110 }
111
112 if (!empty($participantDetails)) {
113 //cron 1. move participant from pending to expire if needed
114 foreach ($participantDetails as $participantId => $values) {
115 //process the additional participant at the time of
116 //primary participant, don't process separately.
117 if (!empty($values['registered_by_id'])) {
118 continue;
119 }
120
121 $expirationTime = CRM_Utils_Array::value('expiration_time', $values);
122 if ($expirationTime && array_key_exists($values['status_id'], $pendingStatuses)) {
123
124 //get the expiration and registration pending time.
125 $expirationSeconds = $expirationTime * 3600;
126 $registrationPendingSeconds = CRM_Utils_Date::unixTime($values['register_date']);
127
128 // expired registration since registration cross allow confirmation time.
129 if (($expirationSeconds + $registrationPendingSeconds) < time()) {
130
131 //lets get the transaction mechanism.
132 require_once 'CRM/Core/Transaction.php';
133 $transaction = new CRM_Core_Transaction();
134
135 require_once 'CRM/Event/BAO/Participant.php';
136 $ids = array($participantId);
137 $expiredId = array_search('Expired', $expiredStatuses);
138 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $expiredId, $values['status_id'], TRUE, TRUE);
139 $transaction->commit();
140
141 if (!empty($results)) {
142 //diaplay updated participants
143 if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) {
144 foreach ($results['updatedParticipantIds'] as $processedId) {
145 $expiredParticipantCount += 1;
146 echo "<br /><br />- status updated to: Expired";
147
148 //mailed participants.
149 if (is_array($results['mailedParticipants']) &&
150 array_key_exists($processedId, $results['mailedParticipants'])
151 ) {
152 echo "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
153 }
154 }
155 }
156 }
157 }
158 }
159 }
160 //cron 1 end.
161
162 //cron 2. lets move participants from waiting list to pending status
163 foreach ($participantDetails as $participantId => $values) {
164 //process the additional participant at the time of
165 //primary participant, don't process separately.
166 if (!empty($values['registered_by_id'])) {
167 continue;
168 }
169
170 if (array_key_exists($values['status_id'], $waitingStatuses) &&
171 !array_key_exists($values['event_id'], $fullEvents)
172 ) {
173
174 if ($waitingStatuses[$values['status_id']] == 'On waitlist' &&
175 CRM_Event_BAO_Event::validRegistrationDate($values)
176 ) {
177
178 //check the target event having space.
179 require_once 'CRM/Event/BAO/Participant.php';
180 $eventOpenSpaces = CRM_Event_BAO_Participant::eventFull($values['event_id'], TRUE, FALSE);
181
182 if ($eventOpenSpaces && is_numeric($eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
183
184 //get the additional participant if any.
185 $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId);
186
187 $allIds = array($participantId);
188 if (!empty($additionalIds)) {
189 $allIds = array_merge($allIds, $additionalIds);
190 }
191 $pClause = ' participant.id IN ( ' . implode(' , ', $allIds) . ' )';
192 $requiredSpaces = CRM_Event_BAO_Event::eventTotalSeats($values['event_id'], $pClause);
193
194 //need to check as to see if event has enough speces
195 if (($requiredSpaces <= $eventOpenSpaces) || ($eventOpenSpaces === NULL)) {
196 require_once 'CRM/Core/Transaction.php';
197 $transaction = new CRM_Core_Transaction();
198
199 require_once 'CRM/Event/BAO/Participant.php';
200 $ids = array($participantId);
201 $updateStatusId = array_search('Pending from waitlist', $pendingStatuses);
202
203 //lets take a call to make pending or need approval
204 if ($values['requires_approval']) {
205 $updateStatusId = array_search('Awaiting approval', $waitingStatuses);
206 }
207 $results = CRM_Event_BAO_Participant::transitionParticipants($ids, $updateStatusId,
208 $values['status_id'], TRUE, TRUE
209 );
210 //commit the transaction.
211 $transaction->commit();
212
213 if (!empty($results)) {
214 //diaplay updated participants
215 if (is_array($results['updatedParticipantIds']) &&
216 !empty($results['updatedParticipantIds'])
217 ) {
218 foreach ($results['updatedParticipantIds'] as $processedId) {
219 if ($values['requires_approval']) {
220 $waitingApprovalCount += 1;
221 echo "<br /><br />- status updated to: Awaiting approval";
222 echo "<br />Will send you Confirmation Mail when registration get approved.";
223 }
224 else {
225 $waitingConfirmCount += 1;
226 echo "<br /><br />- status updated to: Pending from waitlist";
227 if (is_array($results['mailedParticipants']) &&
228 array_key_exists($processedId, $results['mailedParticipants'])
229 ) {
230 echo "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
231 }
232 }
233 }
234 }
235 }
236 }
237 else {
238 //target event is full.
239 $fullEvents[$values['event_id']] = $values['eventTitle'];
240 }
241 }
242 else {
243 //target event is full.
244 $fullEvents[$values['event_id']] = $values['eventTitle'];
245 }
246 }
247 }
248 }
249 //cron 2 ends.
250 }
251
252 echo "<br /><br />Number of Expired registration(s) = {$expiredParticipantCount}";
253 echo "<br />Number of registration(s) require approval = {$waitingApprovalCount}";
254 echo "<br />Number of registration changed to Pending from waitlist = {$waitingConfirmCount}<br /><br />";
255 if (!empty($fullEvents)) {
256 foreach ($fullEvents as $eventId => $title) {
257 echo "Full Event : {$title}<br />";
258 }
259 }
260 }
261 }
262
263 $obj = new CRM_ParticipantProcessor();
264 echo "Updating..";
265 $obj->updateParticipantStatus();
266 echo "<br />Participant records updated. (Done)";
267
268