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