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