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