Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | ||
e70a7fc0 | 11 | */ |
6a488035 TO |
12 | |
13 | /** | |
6a488035 | 14 | * @package CRM |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant { | |
18 | ||
19 | /** | |
66f9e52b | 20 | * Static field for all the participant information that we can potentially import. |
6a488035 TO |
21 | * |
22 | * @var array | |
6a488035 | 23 | */ |
90b461f1 | 24 | public static $_importableFields = NULL; |
6a488035 TO |
25 | |
26 | /** | |
66f9e52b | 27 | * Static field for all the participant information that we can potentially export. |
6a488035 TO |
28 | * |
29 | * @var array | |
6a488035 | 30 | */ |
90b461f1 | 31 | public static $_exportableFields = NULL; |
6a488035 TO |
32 | |
33 | /** | |
66f9e52b | 34 | * Static array for valid status transitions rules. |
6a488035 TO |
35 | * |
36 | * @var array | |
6a488035 | 37 | */ |
90b461f1 | 38 | public static $_statusTransitionsRules = [ |
be2fb01f CW |
39 | 'Pending from pay later' => ['Registered', 'Cancelled'], |
40 | 'Pending from incomplete transaction' => ['Registered', 'Cancelled'], | |
41 | 'On waitlist' => ['Cancelled', 'Pending from waitlist'], | |
42 | 'Pending from waitlist' => ['Registered', 'Cancelled'], | |
43 | 'Awaiting approval' => ['Cancelled', 'Pending from approval'], | |
44 | 'Pending from approval' => ['Registered', 'Cancelled'], | |
45 | ]; | |
0cf587a7 EM |
46 | |
47 | /** | |
0cf587a7 | 48 | */ |
00be9182 | 49 | public function __construct() { |
6a488035 TO |
50 | parent::__construct(); |
51 | } | |
52 | ||
53 | /** | |
66f9e52b | 54 | * Takes an associative array and creates a participant object. |
6a488035 TO |
55 | * |
56 | * the function extract all the params it needs to initialize the create a | |
57 | * participant object. the params array could contain additional unused name/value | |
58 | * pairs | |
59 | * | |
d4dd1e85 TO |
60 | * @param array $params |
61 | * (reference ) an assoc array of name/value pairs. | |
77035e4b | 62 | * |
16b10e64 | 63 | * @return CRM_Event_BAO_Participant |
6a488035 | 64 | */ |
00be9182 | 65 | public static function &add(&$params) { |
6a488035 | 66 | |
a7488080 | 67 | if (!empty($params['id'])) { |
6a488035 TO |
68 | CRM_Utils_Hook::pre('edit', 'Participant', $params['id'], $params); |
69 | } | |
70 | else { | |
71 | CRM_Utils_Hook::pre('create', 'Participant', NULL, $params); | |
72 | } | |
73 | ||
74 | // converting dates to mysql format | |
a7488080 | 75 | if (!empty($params['register_date'])) { |
6a488035 TO |
76 | $params['register_date'] = CRM_Utils_Date::isoToMysql($params['register_date']); |
77 | } | |
78 | ||
a7488080 | 79 | if (!empty($params['participant_fee_amount'])) { |
6a488035 TO |
80 | $params['participant_fee_amount'] = CRM_Utils_Rule::cleanMoney($params['participant_fee_amount']); |
81 | } | |
82 | ||
a7488080 | 83 | if (!empty($params['fee_amount'])) { |
6a488035 TO |
84 | $params['fee_amount'] = CRM_Utils_Rule::cleanMoney($params['fee_amount']); |
85 | } | |
86 | ||
7577274a | 87 | // ensure that role ids are encoded as a string |
3cef9954 | 88 | if (isset($params['role_id']) && is_array($params['role_id'])) { |
653cbe46 | 89 | if (in_array(key($params['role_id']), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
e51b7372 | 90 | $op = key($params['role_id']); |
91 | $params['role_id'] = $params['role_id'][$op]; | |
92 | } | |
774e09ad | 93 | else { |
94 | $params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']); | |
95 | } | |
7577274a KJ |
96 | } |
97 | ||
2177d15c | 98 | $participantBAO = new CRM_Event_BAO_Participant(); |
a7488080 | 99 | if (!empty($params['id'])) { |
6a488035 TO |
100 | $participantBAO->id = CRM_Utils_Array::value('id', $params); |
101 | $participantBAO->find(TRUE); | |
102 | $participantBAO->register_date = CRM_Utils_Date::isoToMysql($participantBAO->register_date); | |
103 | } | |
104 | ||
105 | $participantBAO->copyValues($params); | |
106 | ||
107 | //CRM-6910 | |
108 | //1. If currency present, it should be valid one. | |
109 | //2. We should have currency when amount is not null. | |
110 | $currency = $participantBAO->fee_currency; | |
111 | if ($currency || | |
112 | !CRM_Utils_System::isNull($participantBAO->fee_amount) | |
113 | ) { | |
114 | if (!CRM_Utils_Rule::currencyCode($currency)) { | |
115 | $config = CRM_Core_Config::singleton(); | |
116 | $currency = $config->defaultCurrency; | |
117 | } | |
118 | } | |
119 | $participantBAO->fee_currency = $currency; | |
120 | ||
121 | $participantBAO->save(); | |
122 | ||
2b68a50c | 123 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
6a488035 | 124 | |
a7488080 | 125 | if (!empty($params['id'])) { |
6a488035 TO |
126 | CRM_Utils_Hook::post('edit', 'Participant', $participantBAO->id, $participantBAO); |
127 | } | |
128 | else { | |
129 | CRM_Utils_Hook::post('create', 'Participant', $participantBAO->id, $participantBAO); | |
130 | } | |
131 | ||
132 | return $participantBAO; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Given the list of params in the params array, fetch the object | |
137 | * and store the values in the values array | |
138 | * | |
d4dd1e85 TO |
139 | * @param array $params |
140 | * Input parameters to find object. | |
141 | * @param array $values | |
142 | * Output values of the object. | |
6a488035 | 143 | * |
77035e4b EM |
144 | * @param $ids |
145 | * | |
6a488035 | 146 | * @return CRM_Event_BAO_Participant|null the found object or null |
6a488035 | 147 | */ |
db62d3a5 | 148 | public static function getValues(&$params, &$values = [], &$ids = []) { |
6a488035 TO |
149 | if (empty($params)) { |
150 | return NULL; | |
151 | } | |
152 | $participant = new CRM_Event_BAO_Participant(); | |
153 | $participant->copyValues($params); | |
154 | $participant->find(); | |
be2fb01f | 155 | $participants = []; |
6a488035 TO |
156 | while ($participant->fetch()) { |
157 | $ids['participant'] = $participant->id; | |
158 | CRM_Core_DAO::storeValues($participant, $values[$participant->id]); | |
159 | $participants[$participant->id] = $participant; | |
160 | } | |
161 | return $participants; | |
162 | } | |
163 | ||
164 | /** | |
66f9e52b | 165 | * Takes an associative array and creates a participant object. |
6a488035 | 166 | * |
d4dd1e85 TO |
167 | * @param array $params |
168 | * (reference ) an assoc array of name/value pairs. | |
77035e4b | 169 | * |
16b10e64 | 170 | * @return CRM_Event_BAO_Participant |
6a488035 | 171 | */ |
00be9182 | 172 | public static function create(&$params) { |
6a488035 TO |
173 | |
174 | $transaction = new CRM_Core_Transaction(); | |
175 | $status = NULL; | |
176 | ||
a7488080 | 177 | if (!empty($params['id'])) { |
6a488035 TO |
178 | $status = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $params['id'], 'status_id'); |
179 | } | |
180 | ||
181 | $participant = self::add($params); | |
182 | ||
183 | if (is_a($participant, 'CRM_Core_Error')) { | |
184 | $transaction->rollback(); | |
185 | return $participant; | |
186 | } | |
187 | ||
d41da727 CW |
188 | // Log activity when creating new participant or changing status |
189 | if (empty($params['id']) || | |
9af2925b | 190 | (isset($params['status_id']) && $params['status_id'] != $status) |
6a488035 | 191 | ) { |
d41da727 CW |
192 | // Default status if not specified |
193 | $participant->status_id = $participant->status_id ?: self::fields()['participant_status_id']['default']; | |
6a488035 TO |
194 | CRM_Activity_BAO_Activity::addActivity($participant); |
195 | } | |
196 | ||
197 | //CRM-5403 | |
198 | //for update mode | |
199 | if (self::isPrimaryParticipant($participant->id) && $status) { | |
200 | self::updateParticipantStatus($participant->id, $status, $participant->status_id); | |
201 | } | |
202 | ||
203 | $session = CRM_Core_Session::singleton(); | |
204 | $id = $session->get('userID'); | |
205 | if (!$id) { | |
edf1b210 | 206 | $id = CRM_Utils_Array::value('contact_id', $params); |
6a488035 TO |
207 | } |
208 | ||
209 | // add custom field values | |
a7488080 | 210 | if (!empty($params['custom']) && |
6a488035 TO |
211 | is_array($params['custom']) |
212 | ) { | |
213 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_participant', $participant->id); | |
214 | } | |
215 | ||
216 | //process note, CRM-7634 | |
217 | $noteId = NULL; | |
a7488080 | 218 | if (!empty($params['id'])) { |
6a488035 TO |
219 | $note = CRM_Core_BAO_Note::getNote($params['id'], 'civicrm_participant'); |
220 | $noteId = key($note); | |
221 | } | |
222 | $noteValue = NULL; | |
223 | $hasNoteField = FALSE; | |
be2fb01f | 224 | foreach ([ |
90b461f1 SL |
225 | 'note', |
226 | 'participant_note', | |
227 | ] as $noteFld) { | |
6a488035 TO |
228 | if (array_key_exists($noteFld, $params)) { |
229 | $noteValue = $params[$noteFld]; | |
230 | $hasNoteField = TRUE; | |
231 | break; | |
232 | } | |
233 | } | |
234 | if ($noteId || $noteValue) { | |
235 | if ($noteValue) { | |
be2fb01f | 236 | $noteParams = [ |
6a488035 TO |
237 | 'entity_table' => 'civicrm_participant', |
238 | 'note' => $noteValue, | |
239 | 'entity_id' => $participant->id, | |
240 | 'contact_id' => $id, | |
241 | 'modified_date' => date('Ymd'), | |
be2fb01f CW |
242 | ]; |
243 | $noteIDs = []; | |
6a488035 TO |
244 | if ($noteId) { |
245 | $noteIDs['id'] = $noteId; | |
246 | } | |
247 | CRM_Core_BAO_Note::add($noteParams, $noteIDs); | |
248 | } | |
249 | elseif ($noteId && $hasNoteField) { | |
250 | CRM_Core_BAO_Note::del($noteId, FALSE); | |
251 | } | |
252 | } | |
253 | ||
254 | // Log the information on successful add/edit of Participant data. | |
be2fb01f | 255 | $logParams = [ |
6a488035 TO |
256 | 'entity_table' => 'civicrm_participant', |
257 | 'entity_id' => $participant->id, | |
258 | 'data' => CRM_Event_PseudoConstant::participantStatus($participant->status_id), | |
259 | 'modified_id' => $id, | |
260 | 'modified_date' => date('Ymd'), | |
be2fb01f | 261 | ]; |
6a488035 TO |
262 | |
263 | CRM_Core_BAO_Log::add($logParams); | |
264 | ||
265 | $params['participant_id'] = $participant->id; | |
266 | ||
267 | $transaction->commit(); | |
268 | ||
269 | // do not add to recent items for import, CRM-4399 | |
a7488080 | 270 | if (empty($params['skipRecentView'])) { |
6a488035 TO |
271 | |
272 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', | |
273 | "action=view&reset=1&id={$participant->id}&cid={$participant->contact_id}&context=home" | |
274 | ); | |
275 | ||
be2fb01f | 276 | $recentOther = []; |
6a488035 TO |
277 | if (CRM_Core_Permission::check('edit event participants')) { |
278 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant', | |
279 | "action=update&reset=1&id={$participant->id}&cid={$participant->contact_id}&context=home" | |
280 | ); | |
281 | } | |
282 | if (CRM_Core_Permission::check('delete in CiviEvent')) { | |
283 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant', | |
284 | "action=delete&reset=1&id={$participant->id}&cid={$participant->contact_id}&context=home" | |
285 | ); | |
286 | } | |
287 | ||
288 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); | |
289 | ||
290 | if ($participant->role_id) { | |
291 | $role = explode(CRM_Core_DAO::VALUE_SEPARATOR, $participant->role_id); | |
292 | ||
293 | foreach ($role as & $roleValue) { | |
294 | if (isset($roleValue)) { | |
295 | $roleValue = $participantRoles[$roleValue]; | |
296 | } | |
297 | } | |
298 | $roles = implode(', ', $role); | |
299 | } | |
300 | ||
301 | $roleString = empty($roles) ? '' : $roles; | |
302 | $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $participant->event_id, 'title'); | |
303 | $title = CRM_Contact_BAO_Contact::displayName($participant->contact_id) . ' (' . $roleString . ' - ' . $eventTitle . ')'; | |
304 | ||
305 | // add the recently created Participant | |
306 | CRM_Utils_Recent::add($title, | |
307 | $url, | |
308 | $participant->id, | |
309 | 'Participant', | |
310 | $participant->contact_id, | |
311 | NULL, | |
312 | $recentOther | |
313 | ); | |
314 | } | |
315 | ||
316 | return $participant; | |
317 | } | |
318 | ||
319 | /** | |
66f9e52b | 320 | * Check whether the event is full for participation and return as. |
6a488035 TO |
321 | * per requirements. |
322 | * | |
d4dd1e85 TO |
323 | * @param int $eventId |
324 | * Event id. | |
325 | * @param bool $returnEmptySeats | |
326 | * Are we require number if empty seats. | |
327 | * @param bool $includeWaitingList | |
328 | * Consider waiting list in event full. | |
6a488035 TO |
329 | * calculation or not. (it is for cron job purpose) |
330 | * | |
77035e4b EM |
331 | * @param bool $returnWaitingCount |
332 | * @param bool $considerTestParticipant | |
6a488035 | 333 | * |
72b3a70c CW |
334 | * @return bool|int|null|string |
335 | * 1. false => If event having some empty spaces. | |
6a488035 | 336 | */ |
2da40d21 | 337 | public static function eventFull( |
6a488035 TO |
338 | $eventId, |
339 | $returnEmptySeats = FALSE, | |
340 | $includeWaitingList = TRUE, | |
341 | $returnWaitingCount = FALSE, | |
342 | $considerTestParticipant = FALSE | |
343 | ) { | |
344 | $result = NULL; | |
345 | if (!$eventId) { | |
346 | return $result; | |
347 | } | |
348 | ||
349 | // consider event is full when. | |
350 | // 1. (count(is_counted) >= event_size) or | |
351 | // 2. (count(participants-with-status-on-waitlist) > 0) | |
352 | // It might be case there are some empty spaces and still event | |
353 | // is full, as waitlist might represent group require spaces > empty. | |
354 | ||
ddca8f33 TO |
355 | $participantRoles = CRM_Event_PseudoConstant::participantRole(NULL, 'filter = 1'); |
356 | $countedStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); | |
357 | $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); | |
6a488035 TO |
358 | $onWaitlistStatusId = array_search('On waitlist', $waitingStatuses); |
359 | ||
360 | //when we do require only waiting count don't consider counted. | |
361 | if (!$returnWaitingCount && !empty($countedStatuses)) { | |
362 | $allStatusIds = array_keys($countedStatuses); | |
363 | } | |
364 | ||
be2fb01f | 365 | $where = [' event.id = %1 ']; |
6a488035 TO |
366 | if (!$considerTestParticipant) { |
367 | $where[] = ' ( participant.is_test = 0 OR participant.is_test IS NULL ) '; | |
368 | } | |
369 | if (!empty($participantRoles)) { | |
be2fb01f | 370 | $escapedRoles = []; |
0906de17 MM |
371 | foreach (array_keys($participantRoles) as $participantRole) { |
372 | $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String'); | |
373 | } | |
374 | ||
a7aa4454 CW |
375 | $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', $escapedRoles) . "([[:cntrl:]]|$)"; |
376 | $where[] = " participant.role_id REGEXP '{$regexp}'"; | |
6a488035 TO |
377 | } |
378 | ||
be2fb01f | 379 | $eventParams = [1 => [$eventId, 'Positive']]; |
6a488035 TO |
380 | |
381 | //in case any waiting, straight forward event is full. | |
382 | if ($includeWaitingList && $onWaitlistStatusId) { | |
383 | ||
384 | //build the where clause. | |
ddca8f33 TO |
385 | $whereClause = ' WHERE ' . implode(' AND ', $where); |
386 | $whereClause .= " AND participant.status_id = $onWaitlistStatusId "; | |
6a488035 TO |
387 | $eventSeatsWhere = implode(' AND ', $where) . " AND ( participant.status_id = $onWaitlistStatusId )"; |
388 | ||
389 | $query = " | |
390 | SELECT participant.id id, | |
391 | event.event_full_text as event_full_text | |
392 | FROM civicrm_participant participant | |
393 | INNER JOIN civicrm_event event ON ( event.id = participant.event_id ) | |
394 | {$whereClause}"; | |
395 | ||
92d6c5f5 | 396 | $eventFullText = ts('This event is full.'); |
6a488035 TO |
397 | $participants = CRM_Core_DAO::executeQuery($query, $eventParams); |
398 | while ($participants->fetch()) { | |
399 | //oops here event is full and we don't want waiting count. | |
400 | if ($returnWaitingCount) { | |
401 | return CRM_Event_BAO_Event::eventTotalSeats($eventId, $eventSeatsWhere); | |
402 | } | |
403 | else { | |
404 | return ($participants->event_full_text) ? $participants->event_full_text : $eventFullText; | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
409 | //consider only counted participants. | |
ddca8f33 TO |
410 | $where[] = ' participant.status_id IN ( ' . implode(', ', array_keys($countedStatuses)) . ' ) '; |
411 | $whereClause = ' WHERE ' . implode(' AND ', $where); | |
6a488035 TO |
412 | $eventSeatsWhere = implode(' AND ', $where); |
413 | ||
414 | $query = " | |
415 | SELECT participant.id id, | |
416 | event.event_full_text as event_full_text, | |
417 | event.max_participants as max_participants | |
418 | FROM civicrm_participant participant | |
419 | INNER JOIN civicrm_event event ON ( event.id = participant.event_id ) | |
420 | {$whereClause}"; | |
421 | ||
422 | $eventMaxSeats = NULL; | |
92d6c5f5 | 423 | $eventFullText = ts('This event is full.'); |
ddca8f33 | 424 | $participants = CRM_Core_DAO::executeQuery($query, $eventParams); |
6a488035 TO |
425 | while ($participants->fetch()) { |
426 | if ($participants->event_full_text) { | |
427 | $eventFullText = $participants->event_full_text; | |
428 | } | |
429 | $eventMaxSeats = $participants->max_participants; | |
430 | //don't have limit for event seats. | |
431 | if ($participants->max_participants == NULL) { | |
432 | return $result; | |
433 | } | |
434 | } | |
435 | ||
436 | //get the total event seats occupied by these participants. | |
437 | $eventRegisteredSeats = CRM_Event_BAO_Event::eventTotalSeats($eventId, $eventSeatsWhere); | |
438 | ||
439 | if ($eventRegisteredSeats) { | |
440 | if ($eventRegisteredSeats >= $eventMaxSeats) { | |
441 | $result = $eventFullText; | |
442 | } | |
443 | elseif ($returnEmptySeats) { | |
444 | $result = $eventMaxSeats - $eventRegisteredSeats; | |
445 | } | |
446 | return $result; | |
447 | } | |
448 | else { | |
449 | $query = ' | |
450 | SELECT event.event_full_text, | |
451 | event.max_participants | |
452 | FROM civicrm_event event | |
453 | WHERE event.id = %1'; | |
454 | $event = CRM_Core_DAO::executeQuery($query, $eventParams); | |
455 | while ($event->fetch()) { | |
456 | $eventFullText = $event->event_full_text; | |
457 | $eventMaxSeats = $event->max_participants; | |
458 | } | |
459 | } | |
460 | ||
461 | // no limit for registration. | |
462 | if ($eventMaxSeats == NULL) { | |
463 | return $result; | |
464 | } | |
465 | if ($eventMaxSeats) { | |
466 | return ($returnEmptySeats) ? (int) $eventMaxSeats : FALSE; | |
467 | } | |
468 | ||
31312bdc | 469 | return $eventFullText; |
6a488035 TO |
470 | } |
471 | ||
472 | /** | |
473 | * Return the array of all price set field options, | |
474 | * with total participant count that field going to carry. | |
475 | * | |
d4dd1e85 TO |
476 | * @param int $eventId |
477 | * Event id. | |
478 | * @param array $skipParticipantIds | |
479 | * An array of participant ids those we should skip. | |
77035e4b EM |
480 | * @param bool $considerCounted |
481 | * @param bool $considerWaiting | |
482 | * @param bool $considerTestParticipants | |
483 | * | |
a6c01b45 CW |
484 | * @return array |
485 | * an array of each option id and total count | |
6a488035 | 486 | */ |
2da40d21 | 487 | public static function priceSetOptionsCount( |
6a488035 | 488 | $eventId, |
be2fb01f | 489 | $skipParticipantIds = [], |
6a488035 TO |
490 | $considerCounted = TRUE, |
491 | $considerWaiting = TRUE, | |
492 | $considerTestParticipants = FALSE | |
493 | ) { | |
be2fb01f | 494 | $optionsCount = []; |
6a488035 TO |
495 | if (!$eventId) { |
496 | return $optionsCount; | |
497 | } | |
498 | ||
be2fb01f | 499 | $allStatusIds = []; |
6a488035 TO |
500 | if ($considerCounted) { |
501 | $countedStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); | |
502 | $allStatusIds = array_merge($allStatusIds, array_keys($countedStatuses)); | |
503 | } | |
504 | if ($considerWaiting) { | |
505 | $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); | |
506 | $allStatusIds = array_merge($allStatusIds, array_keys($waitingStatuses)); | |
507 | } | |
508 | $statusIdClause = NULL; | |
509 | if (!empty($allStatusIds)) { | |
510 | $statusIdClause = ' AND participant.status_id IN ( ' . implode(', ', array_values($allStatusIds)) . ')'; | |
511 | } | |
512 | ||
513 | $isTestClause = NULL; | |
514 | if (!$considerTestParticipants) { | |
515 | $isTestClause = ' AND ( participant.is_test IS NULL OR participant.is_test = 0 )'; | |
516 | } | |
517 | ||
518 | $skipParticipantClause = NULL; | |
519 | if (is_array($skipParticipantIds) && !empty($skipParticipantIds)) { | |
520 | $skipParticipantClause = ' AND participant.id NOT IN ( ' . implode(', ', $skipParticipantIds) . ')'; | |
521 | } | |
522 | ||
523 | $sql = " | |
524 | SELECT line.id as lineId, | |
525 | line.entity_id as entity_id, | |
526 | line.qty, | |
527 | value.id as valueId, | |
528 | value.count, | |
529 | field.html_type | |
530 | FROM civicrm_line_item line | |
531 | INNER JOIN civicrm_participant participant ON ( line.entity_table = 'civicrm_participant' | |
532 | AND participant.id = line.entity_id ) | |
533 | INNER JOIN civicrm_price_field_value value ON ( value.id = line.price_field_value_id ) | |
534 | INNER JOIN civicrm_price_field field ON ( value.price_field_id = field.id ) | |
535 | WHERE participant.event_id = %1 | |
0d9c7c6d | 536 | AND line.qty > 0 |
6a488035 TO |
537 | {$statusIdClause} |
538 | {$isTestClause} | |
539 | {$skipParticipantClause}"; | |
540 | ||
be2fb01f | 541 | $lineItem = CRM_Core_DAO::executeQuery($sql, [1 => [$eventId, 'Positive']]); |
6a488035 TO |
542 | while ($lineItem->fetch()) { |
543 | $count = $lineItem->count; | |
544 | if (!$count) { | |
545 | $count = 1; | |
546 | } | |
547 | if ($lineItem->html_type == 'Text') { | |
548 | $count *= $lineItem->qty; | |
549 | } | |
550 | $optionsCount[$lineItem->valueId] = $count + CRM_Utils_Array::value($lineItem->valueId, $optionsCount, 0); | |
551 | } | |
552 | ||
553 | return $optionsCount; | |
554 | } | |
555 | ||
556 | /** | |
557 | * Get the empty spaces for event those we can allocate | |
558 | * to pending participant to become confirm. | |
559 | * | |
d4dd1e85 TO |
560 | * @param int $eventId |
561 | * Event id. | |
6a488035 | 562 | * |
a6c01b45 CW |
563 | * @return int |
564 | * $spaces Number of Empty Seats/null. | |
6a488035 | 565 | */ |
00be9182 | 566 | public static function pendingToConfirmSpaces($eventId) { |
6a488035 TO |
567 | $emptySeats = 0; |
568 | if (!$eventId) { | |
569 | return $emptySeats; | |
570 | } | |
571 | ||
572 | $positiveStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Positive'"); | |
573 | $statusIds = '(' . implode(',', array_keys($positiveStatuses)) . ')'; | |
574 | ||
575 | $query = " | |
576 | SELECT count(participant.id) as registered, | |
577 | civicrm_event.max_participants | |
578 | FROM civicrm_participant participant, civicrm_event | |
579 | WHERE participant.event_id = {$eventId} | |
580 | AND civicrm_event.id = participant.event_id | |
581 | AND participant.status_id IN {$statusIds} | |
582 | GROUP BY participant.event_id | |
583 | "; | |
584 | $dao = CRM_Core_DAO::executeQuery($query); | |
585 | if ($dao->fetch()) { | |
586 | ||
587 | //unlimited space. | |
588 | if ($dao->max_participants == NULL || $dao->max_participants <= 0) { | |
589 | return NULL; | |
590 | } | |
591 | ||
592 | //no space. | |
593 | if ($dao->registered >= $dao->max_participants) { | |
594 | return $emptySeats; | |
595 | } | |
596 | ||
597 | //difference. | |
598 | return $dao->max_participants - $dao->registered; | |
599 | } | |
600 | ||
601 | //space in case no registeration yet. | |
602 | return CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'max_participants'); | |
603 | } | |
604 | ||
605 | /** | |
66f9e52b | 606 | * Combine all the importable fields from the lower levels object. |
6a488035 | 607 | * |
77035e4b EM |
608 | * @param string $contactType |
609 | * @param bool $status | |
610 | * @param bool $onlyParticipant | |
e9ff5391 | 611 | * @param bool $checkPermission |
612 | * Is this a permissioned retrieval? | |
77035e4b | 613 | * |
a6c01b45 CW |
614 | * @return array |
615 | * array of importable Fields | |
6a488035 | 616 | */ |
e9ff5391 | 617 | public static function &importableFields($contactType = 'Individual', $status = TRUE, $onlyParticipant = FALSE, $checkPermission = TRUE) { |
6a488035 TO |
618 | if (!self::$_importableFields) { |
619 | if (!$onlyParticipant) { | |
620 | if (!$status) { | |
be2fb01f | 621 | $fields = ['' => ['title' => ts('- do not import -')]]; |
6a488035 TO |
622 | } |
623 | else { | |
be2fb01f | 624 | $fields = ['' => ['title' => ts('- Participant Fields -')]]; |
6a488035 TO |
625 | } |
626 | } | |
627 | else { | |
be2fb01f | 628 | $fields = []; |
6a488035 TO |
629 | } |
630 | ||
631 | $tmpFields = CRM_Event_DAO_Participant::import(); | |
632 | ||
be2fb01f CW |
633 | $note = [ |
634 | 'participant_note' => [ | |
3cb8f9b2 | 635 | 'title' => ts('Participant Note'), |
6a488035 TO |
636 | 'name' => 'participant_note', |
637 | 'headerPattern' => '/(participant.)?note$/i', | |
672b875d | 638 | 'data_type' => CRM_Utils_Type::T_TEXT, |
be2fb01f CW |
639 | ], |
640 | ]; | |
6a488035 | 641 | |
3cb8f9b2 CW |
642 | // Split status and status id into 2 fields |
643 | // Fixme: it would be better to leave as 1 field and intelligently handle both during import | |
be2fb01f CW |
644 | $participantStatus = [ |
645 | 'participant_status' => [ | |
3cb8f9b2 | 646 | 'title' => ts('Participant Status'), |
6a488035 TO |
647 | 'name' => 'participant_status', |
648 | 'data_type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
649 | ], |
650 | ]; | |
3cb8f9b2 | 651 | $tmpFields['participant_status_id']['title'] = ts('Participant Status Id'); |
6a488035 | 652 | |
3cb8f9b2 CW |
653 | // Split role and role id into 2 fields |
654 | // Fixme: it would be better to leave as 1 field and intelligently handle both during import | |
be2fb01f CW |
655 | $participantRole = [ |
656 | 'participant_role' => [ | |
3cb8f9b2 | 657 | 'title' => ts('Participant Role'), |
6a488035 TO |
658 | 'name' => 'participant_role', |
659 | 'data_type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
660 | ], |
661 | ]; | |
3cb8f9b2 | 662 | $tmpFields['participant_role_id']['title'] = ts('Participant Role Id'); |
6a488035 | 663 | |
be2fb01f CW |
664 | $eventType = [ |
665 | 'event_type' => [ | |
3cb8f9b2 | 666 | 'title' => ts('Event Type'), |
6a488035 TO |
667 | 'name' => 'event_type', |
668 | 'data_type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
669 | ], |
670 | ]; | |
6a488035 | 671 | |
be2fb01f CW |
672 | $tmpContactField = $contactFields = []; |
673 | $contactFields = []; | |
6a488035 TO |
674 | if (!$onlyParticipant) { |
675 | $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL); | |
676 | ||
677 | // Using new Dedupe rule. | |
be2fb01f | 678 | $ruleParams = [ |
6a488035 | 679 | 'contact_type' => $contactType, |
ddca8f33 | 680 | 'used' => 'Unsupervised', |
be2fb01f | 681 | ]; |
6a488035 TO |
682 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
683 | ||
684 | if (is_array($fieldsArray)) { | |
685 | foreach ($fieldsArray as $value) { | |
686 | $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
687 | $value, | |
688 | 'id', | |
689 | 'column_name' | |
690 | ); | |
691 | $value = $customFieldId ? 'custom_' . $customFieldId : $value; | |
692 | $tmpContactField[trim($value)] = CRM_Utils_Array::value(trim($value), $contactFields); | |
693 | if (!$status) { | |
694 | $title = $tmpContactField[trim($value)]['title'] . ' (match to contact)'; | |
695 | } | |
696 | else { | |
697 | $title = $tmpContactField[trim($value)]['title']; | |
698 | } | |
699 | ||
700 | $tmpContactField[trim($value)]['title'] = $title; | |
701 | } | |
702 | } | |
703 | } | |
704 | $extIdentifier = CRM_Utils_Array::value('external_identifier', $contactFields); | |
705 | if ($extIdentifier) { | |
706 | $tmpContactField['external_identifier'] = $extIdentifier; | |
6c552737 | 707 | $tmpContactField['external_identifier']['title'] = CRM_Utils_Array::value('title', $extIdentifier) . ' (match to contact)'; |
6a488035 | 708 | } |
6c552737 | 709 | $tmpFields['participant_contact_id']['title'] = $tmpFields['participant_contact_id']['title'] . ' (match to contact)'; |
6a488035 | 710 | |
6a488035 TO |
711 | $fields = array_merge($fields, $tmpContactField); |
712 | $fields = array_merge($fields, $tmpFields); | |
713 | $fields = array_merge($fields, $note, $participantStatus, $participantRole, $eventType); | |
e9ff5391 | 714 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Participant', FALSE, FALSE, FALSE, $checkPermission)); |
6a488035 TO |
715 | |
716 | self::$_importableFields = $fields; | |
717 | } | |
718 | ||
719 | return self::$_importableFields; | |
720 | } | |
721 | ||
722 | /** | |
e9ff5391 | 723 | * Combine all the exportable fields from the lower level objects. |
724 | * | |
725 | * @param bool $checkPermission | |
6a488035 | 726 | * |
a6c01b45 CW |
727 | * @return array |
728 | * array of exportable Fields | |
6a488035 | 729 | */ |
e9ff5391 | 730 | public static function &exportableFields($checkPermission = TRUE) { |
6a488035 TO |
731 | if (!self::$_exportableFields) { |
732 | if (!self::$_exportableFields) { | |
be2fb01f | 733 | self::$_exportableFields = []; |
6a488035 TO |
734 | } |
735 | ||
6a488035 | 736 | $participantFields = CRM_Event_DAO_Participant::export(); |
e51b7372 | 737 | $eventFields = CRM_Event_DAO_Event::export(); |
be2fb01f CW |
738 | $noteField = [ |
739 | 'participant_note' => [ | |
e300cf31 | 740 | 'title' => ts('Participant Note'), |
6a488035 | 741 | 'name' => 'participant_note', |
e51b7372 | 742 | 'type' => CRM_Utils_Type::T_STRING, |
be2fb01f CW |
743 | ], |
744 | ]; | |
6a488035 | 745 | |
be2fb01f CW |
746 | $participantStatus = [ |
747 | 'participant_status' => [ | |
e300cf31 | 748 | 'title' => ts('Participant Status (label)'), |
6a488035 | 749 | 'name' => 'participant_status', |
e51b7372 | 750 | 'type' => CRM_Utils_Type::T_STRING, |
be2fb01f CW |
751 | ], |
752 | ]; | |
6a488035 | 753 | |
be2fb01f CW |
754 | $participantRole = [ |
755 | 'participant_role' => [ | |
e300cf31 | 756 | 'title' => ts('Participant Role (label)'), |
6a488035 | 757 | 'name' => 'participant_role', |
e51b7372 | 758 | 'type' => CRM_Utils_Type::T_STRING, |
be2fb01f CW |
759 | ], |
760 | ]; | |
8b8fa582 | 761 | |
1fbc48b0 CW |
762 | $participantFields['participant_status_id']['title'] .= ' (ID)'; |
763 | $participantFields['participant_role_id']['title'] .= ' (ID)'; | |
764 | ||
a1a2a83d | 765 | $discountFields = CRM_Core_DAO_Discount::export(); |
6a488035 | 766 | |
a1a2a83d | 767 | $fields = array_merge($participantFields, $participantStatus, $participantRole, $eventFields, $noteField, $discountFields); |
6a488035 | 768 | |
a1a2a83d | 769 | // add custom data |
e9ff5391 | 770 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Participant', FALSE, FALSE, FALSE, $checkPermission)); |
a1a2a83d | 771 | self::$_exportableFields = $fields; |
6a488035 TO |
772 | } |
773 | ||
774 | return self::$_exportableFields; | |
775 | } | |
776 | ||
777 | /** | |
100fef9d | 778 | * Get the event name/sort name for a particular participation / participant |
6a488035 | 779 | * |
d4dd1e85 TO |
780 | * @param int $participantId |
781 | * Id of the participant. | |
6a488035 | 782 | * |
a6c01b45 CW |
783 | * @return array |
784 | * associated array with sort_name and event title | |
6a488035 | 785 | */ |
00be9182 | 786 | public static function participantDetails($participantId) { |
6a488035 TO |
787 | $query = " |
788 | SELECT civicrm_contact.sort_name as name, civicrm_event.title as title, civicrm_contact.id as cid | |
789 | FROM civicrm_participant | |
790 | LEFT JOIN civicrm_event ON (civicrm_participant.event_id = civicrm_event.id) | |
791 | LEFT JOIN civicrm_contact ON (civicrm_participant.contact_id = civicrm_contact.id) | |
792 | WHERE civicrm_participant.id = {$participantId} | |
793 | "; | |
33621c4f | 794 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 | 795 | |
be2fb01f | 796 | $details = []; |
6a488035 | 797 | while ($dao->fetch()) { |
ddca8f33 | 798 | $details['name'] = $dao->name; |
6a488035 | 799 | $details['title'] = $dao->title; |
ddca8f33 | 800 | $details['cid'] = $dao->cid; |
6a488035 TO |
801 | } |
802 | ||
803 | return $details; | |
804 | } | |
805 | ||
806 | /** | |
807 | * Get the values for pseudoconstants for name->value and reverse. | |
808 | * | |
d4dd1e85 TO |
809 | * @param array $defaults |
810 | * (reference) the default values, some of which need to be resolved. | |
811 | * @param bool $reverse | |
812 | * True if we want to resolve the values in the reverse direction (value -> name). | |
6a488035 | 813 | */ |
00be9182 | 814 | public static function resolveDefaults(&$defaults, $reverse = FALSE) { |
6a488035 TO |
815 | self::lookupValue($defaults, 'event', CRM_Event_PseudoConstant::event(), $reverse); |
816 | self::lookupValue($defaults, 'status', CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'), $reverse); | |
817 | self::lookupValue($defaults, 'role', CRM_Event_PseudoConstant::participantRole(), $reverse); | |
818 | } | |
819 | ||
820 | /** | |
3bdf1f3a | 821 | * Convert associative array names to values and vice-versa. |
6a488035 TO |
822 | * |
823 | * This function is used by both the web form layer and the api. Note that | |
824 | * the api needs the name => value conversion, also the view layer typically | |
825 | * requires value => name conversion | |
ad37ac8e | 826 | * |
827 | * @param array $defaults | |
828 | * @param string $property | |
829 | * @param string $lookup | |
830 | * @param bool $reverse | |
831 | * | |
832 | * @return bool | |
6a488035 | 833 | */ |
00be9182 | 834 | public static function lookupValue(&$defaults, $property, $lookup, $reverse) { |
6a488035 TO |
835 | $id = $property . '_id'; |
836 | ||
837 | $src = $reverse ? $property : $id; | |
838 | $dst = $reverse ? $id : $property; | |
839 | ||
840 | if (!array_key_exists($src, $defaults)) { | |
841 | return FALSE; | |
842 | } | |
843 | ||
844 | $look = $reverse ? array_flip($lookup) : $lookup; | |
845 | ||
846 | if (is_array($look)) { | |
847 | if (!array_key_exists($defaults[$src], $look)) { | |
848 | return FALSE; | |
849 | } | |
850 | } | |
851 | $defaults[$dst] = $look[$defaults[$src]]; | |
852 | return TRUE; | |
853 | } | |
854 | ||
855 | /** | |
3bdf1f3a | 856 | * Delete the records that are associated with this participation. |
6a488035 | 857 | * |
d4dd1e85 TO |
858 | * @param int $id |
859 | * Id of the participation to delete. | |
6a488035 | 860 | * |
3bdf1f3a | 861 | * @return \CRM_Event_DAO_Participant |
6a488035 | 862 | */ |
00be9182 | 863 | public static function deleteParticipant($id) { |
a60c0bc8 SL |
864 | $participant = new CRM_Event_DAO_Participant(); |
865 | $participant->id = $id; | |
866 | if (!$participant->find()) { | |
867 | return FALSE; | |
868 | } | |
6a488035 TO |
869 | CRM_Utils_Hook::pre('delete', 'Participant', $id, CRM_Core_DAO::$_nullArray); |
870 | ||
871 | $transaction = new CRM_Core_Transaction(); | |
872 | ||
873 | //delete activity record | |
be2fb01f | 874 | $params = [ |
6a488035 TO |
875 | 'source_record_id' => $id, |
876 | // activity type id for event registration | |
877 | 'activity_type_id' => 5, | |
be2fb01f | 878 | ]; |
6a488035 TO |
879 | |
880 | CRM_Activity_BAO_Activity::deleteActivity($params); | |
881 | ||
882 | // delete the participant payment record | |
883 | // we need to do this since the cascaded constraints | |
884 | // dont work with join tables | |
be2fb01f | 885 | $p = ['participant_id' => $id]; |
6a488035 TO |
886 | CRM_Event_BAO_ParticipantPayment::deleteParticipantPayment($p); |
887 | ||
888 | // cleanup line items. | |
be2fb01f | 889 | $participantsId = []; |
ddca8f33 | 890 | $participantsId = self::getAdditionalParticipantIds($id); |
6a488035 TO |
891 | $participantsId[] = $id; |
892 | CRM_Price_BAO_LineItem::deleteLineItems($participantsId, 'civicrm_participant'); | |
893 | ||
894 | //delete note when participant deleted. | |
895 | $note = CRM_Core_BAO_Note::getNote($id, 'civicrm_participant'); | |
896 | $noteId = key($note); | |
897 | if ($noteId) { | |
898 | CRM_Core_BAO_Note::del($noteId, FALSE); | |
899 | } | |
900 | ||
6a488035 TO |
901 | $participant->delete(); |
902 | ||
903 | $transaction->commit(); | |
904 | ||
905 | CRM_Utils_Hook::post('delete', 'Participant', $participant->id, $participant); | |
906 | ||
907 | // delete the recently created Participant | |
be2fb01f | 908 | $participantRecent = [ |
6a488035 TO |
909 | 'id' => $id, |
910 | 'type' => 'Participant', | |
be2fb01f | 911 | ]; |
6a488035 TO |
912 | |
913 | CRM_Utils_Recent::del($participantRecent); | |
914 | ||
915 | return $participant; | |
916 | } | |
917 | ||
918 | /** | |
66f9e52b | 919 | * Checks duplicate participants. |
6a488035 | 920 | * |
d4dd1e85 TO |
921 | * @param array $input |
922 | * An assosiative array of name /value pairs. | |
16b10e64 | 923 | * from other function |
a1a2a83d TO |
924 | * @param array $duplicates |
925 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 926 | * |
16b10e64 | 927 | * @return CRM_Contribute_BAO_Contribution |
6a488035 | 928 | */ |
00be9182 | 929 | public static function checkDuplicate($input, &$duplicates) { |
6a488035 TO |
930 | $eventId = CRM_Utils_Array::value('event_id', $input); |
931 | $contactId = CRM_Utils_Array::value('contact_id', $input); | |
932 | ||
be2fb01f CW |
933 | $clause = []; |
934 | $input = []; | |
6a488035 TO |
935 | |
936 | if ($eventId) { | |
937 | $clause[] = "event_id = %1"; | |
be2fb01f | 938 | $input[1] = [$eventId, 'Integer']; |
6a488035 TO |
939 | } |
940 | ||
941 | if ($contactId) { | |
942 | $clause[] = "contact_id = %2"; | |
be2fb01f | 943 | $input[2] = [$contactId, 'Integer']; |
6a488035 TO |
944 | } |
945 | ||
946 | if (empty($clause)) { | |
947 | return FALSE; | |
948 | } | |
949 | ||
950 | $clause = implode(' AND ', $clause); | |
951 | ||
ddca8f33 TO |
952 | $query = "SELECT id FROM civicrm_participant WHERE $clause"; |
953 | $dao = CRM_Core_DAO::executeQuery($query, $input); | |
6a488035 TO |
954 | $result = FALSE; |
955 | while ($dao->fetch()) { | |
956 | $duplicates[] = $dao->id; | |
957 | $result = TRUE; | |
958 | } | |
959 | return $result; | |
960 | } | |
961 | ||
962 | /** | |
66f9e52b | 963 | * Fix the event level. |
6a488035 TO |
964 | * |
965 | * When price sets are used as event fee, fee_level is set as ^A | |
77035e4b | 966 | * separated string. We need to change that string to comma |
6a488035 TO |
967 | * separated string before using fee_level in view mode. |
968 | * | |
d4dd1e85 | 969 | * @param string $eventLevel |
3bdf1f3a | 970 | * Event_level string from db. |
6a488035 | 971 | */ |
00be9182 | 972 | public static function fixEventLevel(&$eventLevel) { |
6a488035 TO |
973 | if ((substr($eventLevel, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) && |
974 | (substr($eventLevel, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) | |
975 | ) { | |
9747cc7e | 976 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($eventLevel, 1, -1))); |
0cca0bf4 J |
977 | $pos = strrpos($eventLevel, '(multiple participants)', 0); |
978 | if ($pos) { | |
6a488035 TO |
979 | $eventLevel = substr_replace($eventLevel, "", $pos - 3, 1); |
980 | } | |
981 | } | |
982 | elseif ((substr($eventLevel, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR)) { | |
983 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
ddca8f33 TO |
984 | substr($eventLevel, 0, 1) |
985 | )); | |
6a488035 TO |
986 | } |
987 | elseif ((substr($eventLevel, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)) { | |
988 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
ddca8f33 TO |
989 | substr($eventLevel, 0, -1) |
990 | )); | |
6a488035 TO |
991 | } |
992 | } | |
993 | ||
994 | /** | |
100fef9d | 995 | * Get the additional participant ids. |
6a488035 | 996 | * |
d4dd1e85 TO |
997 | * @param int $primaryParticipantId |
998 | * Primary partycipant Id. | |
999 | * @param bool $excludeCancel | |
1000 | * Do not include participant those are cancelled. | |
654ca41c | 1001 | * |
100fef9d | 1002 | * @param int $oldStatusId |
6a488035 | 1003 | * |
a6c01b45 | 1004 | * @return array |
6a488035 | 1005 | */ |
00be9182 | 1006 | public static function getAdditionalParticipantIds($primaryParticipantId, $excludeCancel = TRUE, $oldStatusId = NULL) { |
be2fb01f | 1007 | $additionalParticipantIds = []; |
6a488035 TO |
1008 | if (!$primaryParticipantId) { |
1009 | return $additionalParticipantIds; | |
1010 | } | |
1011 | ||
1012 | $where = "participant.registered_by_id={$primaryParticipantId}"; | |
1013 | if ($excludeCancel) { | |
ddca8f33 | 1014 | $cancelStatusId = 0; |
6a488035 | 1015 | $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"); |
ddca8f33 | 1016 | $cancelStatusId = array_search('Cancelled', $negativeStatuses); |
6a488035 TO |
1017 | $where .= " AND participant.status_id != {$cancelStatusId}"; |
1018 | } | |
1019 | ||
1020 | if ($oldStatusId) { | |
1021 | $where .= " AND participant.status_id = {$oldStatusId}"; | |
1022 | } | |
1023 | ||
1024 | $query = " | |
1025 | SELECT participant.id | |
1026 | FROM civicrm_participant participant | |
1027 | WHERE {$where}"; | |
1028 | ||
1029 | $dao = CRM_Core_DAO::executeQuery($query); | |
1030 | while ($dao->fetch()) { | |
1031 | $additionalParticipantIds[$dao->id] = $dao->id; | |
1032 | } | |
1033 | return $additionalParticipantIds; | |
1034 | } | |
1035 | ||
362bd1b7 | 1036 | /** |
1037 | * Get the amount for the undiscounted version of the field. | |
1038 | * | |
1039 | * Note this function is part of the refactoring process rather than the best approach. | |
1040 | * | |
1041 | * @param int $eventID | |
1042 | * @param int $discountedPriceFieldOptionID | |
1043 | * @param string $feeLevel (deprecated) | |
1044 | * | |
1045 | * @return null|string | |
1046 | */ | |
1047 | public static function getUnDiscountedAmountForEventPriceSetFieldValue($eventID, $discountedPriceFieldOptionID, $feeLevel) { | |
1048 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID, NULL); | |
be2fb01f CW |
1049 | $params = [ |
1050 | 1 => [$priceSetId, 'Integer'], | |
1051 | ]; | |
362bd1b7 | 1052 | if ($discountedPriceFieldOptionID) { |
1053 | $query = "SELECT cpfv.amount FROM `civicrm_price_field_value` cpfv | |
1054 | LEFT JOIN civicrm_price_field cpf ON cpfv.price_field_id = cpf.id | |
1055 | WHERE cpf.price_set_id = %1 AND cpfv.label = (SELECT label from civicrm_price_field_value WHERE id = %2)"; | |
be2fb01f | 1056 | $params[2] = [$discountedPriceFieldOptionID, 'Integer']; |
362bd1b7 | 1057 | } |
1058 | else { | |
1059 | $feeLevel = current($feeLevel); | |
1060 | $query = "SELECT cpfv.amount FROM `civicrm_price_field_value` cpfv | |
1061 | LEFT JOIN civicrm_price_field cpf ON cpfv.price_field_id = cpf.id | |
1062 | WHERE cpf.price_set_id = %1 AND cpfv.label LIKE %2"; | |
be2fb01f | 1063 | $params[2] = [$feeLevel, 'String']; |
362bd1b7 | 1064 | } |
1065 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
1066 | } | |
1067 | ||
6a488035 TO |
1068 | /** |
1069 | * Get the event fee info for given participant ids | |
1070 | * either from line item table / participant table. | |
1071 | * | |
d4dd1e85 TO |
1072 | * @param array $participantIds |
1073 | * Participant ids. | |
1074 | * @param bool $hasLineItems | |
1075 | * Do fetch from line items. | |
6a488035 | 1076 | * |
a6c01b45 | 1077 | * @return array |
6a488035 | 1078 | */ |
00be9182 | 1079 | public function getFeeDetails($participantIds, $hasLineItems = FALSE) { |
be2fb01f | 1080 | $feeDetails = []; |
6a488035 TO |
1081 | if (!is_array($participantIds) || empty($participantIds)) { |
1082 | return $feeDetails; | |
1083 | } | |
1084 | ||
1085 | $select = ' | |
1086 | SELECT participant.id as id, | |
1087 | participant.fee_level as fee_level, | |
1088 | participant.fee_amount as fee_amount'; | |
1089 | $from = 'FROM civicrm_participant participant'; | |
1090 | if ($hasLineItems) { | |
1091 | $select .= ' , | |
1092 | lineItem.id as lineId, | |
1093 | lineItem.label as label, | |
1094 | lineItem.qty as qty, | |
1095 | lineItem.unit_price as unit_price, | |
1096 | lineItem.line_total as line_total, | |
1097 | field.label as field_title, | |
1098 | field.html_type as html_type, | |
1099 | field.id as price_field_id, | |
1100 | value.id as price_field_value_id, | |
1101 | value.description as description, | |
1102 | IF( value.count, value.count, 0 ) as participant_count'; | |
1103 | $from .= " | |
1104 | INNER JOIN civicrm_line_item lineItem ON ( lineItem.entity_table = 'civicrm_participant' | |
1105 | AND lineItem.entity_id = participant.id ) | |
1106 | INNER JOIN civicrm_price_field field ON ( field.id = lineItem.price_field_id ) | |
1107 | INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_value_id ) | |
1108 | "; | |
1109 | } | |
1110 | $where = 'WHERE participant.id IN ( ' . implode(', ', $participantIds) . ' )'; | |
1111 | $query = "$select $from $where"; | |
1112 | ||
ddca8f33 | 1113 | $feeInfo = CRM_Core_DAO::executeQuery($query); |
be2fb01f CW |
1114 | $feeProperties = ['fee_level', 'fee_amount']; |
1115 | $lineProperties = [ | |
ddca8f33 TO |
1116 | 'lineId', |
1117 | 'label', | |
1118 | 'qty', | |
1119 | 'unit_price', | |
1120 | 'line_total', | |
1121 | 'field_title', | |
1122 | 'html_type', | |
1123 | 'price_field_id', | |
1124 | 'participant_count', | |
1125 | 'price_field_value_id', | |
1126 | 'description', | |
be2fb01f | 1127 | ]; |
6a488035 TO |
1128 | while ($feeInfo->fetch()) { |
1129 | if ($hasLineItems) { | |
1130 | foreach ($lineProperties as $property) { | |
1131 | $feeDetails[$feeInfo->id][$feeInfo->lineId][$property] = $feeInfo->$property; | |
1132 | } | |
1133 | } | |
1134 | else { | |
ddca8f33 TO |
1135 | foreach ($feeProperties as $property) { |
1136 | $feeDetails[$feeInfo->id][$property] = $feeInfo->$property; | |
0479b4c8 | 1137 | } |
6a488035 TO |
1138 | } |
1139 | } | |
1140 | ||
1141 | return $feeDetails; | |
1142 | } | |
1143 | ||
1144 | /** | |
1145 | * Retrieve additional participants display-names and URL to view their participant records. | |
1146 | * (excludes cancelled participants automatically) | |
1147 | * | |
d4dd1e85 TO |
1148 | * @param int $primaryParticipantID |
1149 | * Id of primary participant record. | |
6a488035 | 1150 | * |
a6c01b45 CW |
1151 | * @return array |
1152 | * $displayName => $viewUrl | |
6a488035 | 1153 | */ |
00be9182 | 1154 | public static function getAdditionalParticipants($primaryParticipantID) { |
be2fb01f | 1155 | $additionalParticipantIDs = []; |
6a488035 TO |
1156 | $additionalParticipantIDs = self::getAdditionalParticipantIds($primaryParticipantID); |
1157 | if (!empty($additionalParticipantIDs)) { | |
1158 | foreach ($additionalParticipantIDs as $additionalParticipantID) { | |
1159 | $additionalContactID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
1160 | $additionalParticipantID, | |
1161 | 'contact_id', 'id' | |
1162 | ); | |
1163 | $additionalContactName = CRM_Contact_BAO_Contact::displayName($additionalContactID); | |
1164 | $pViewURL = CRM_Utils_System::url('civicrm/contact/view/participant', | |
1165 | "action=view&reset=1&id={$additionalParticipantID}&cid={$additionalContactID}" | |
1166 | ); | |
1167 | ||
1168 | $additionalParticipants[$additionalContactName] = $pViewURL; | |
1169 | } | |
1170 | } | |
1171 | return $additionalParticipants; | |
1172 | } | |
1173 | ||
1174 | /** | |
66f9e52b | 1175 | * Function for update primary and additional participant status. |
6a488035 | 1176 | * |
d4dd1e85 TO |
1177 | * @param int $participantID |
1178 | * Primary participant's id. | |
100fef9d CW |
1179 | * @param int $oldStatusID |
1180 | * @param int $newStatusID | |
654ca41c EM |
1181 | * @param bool $updatePrimaryStatus |
1182 | * | |
a1a2a83d | 1183 | * @return bool|NULL |
6a488035 | 1184 | */ |
00be9182 | 1185 | public static function updateParticipantStatus($participantID, $oldStatusID, $newStatusID = NULL, $updatePrimaryStatus = FALSE) { |
6a488035 | 1186 | if (!$participantID || !$oldStatusID) { |
a1a2a83d | 1187 | return NULL; |
6a488035 TO |
1188 | } |
1189 | ||
1190 | if (!$newStatusID) { | |
1191 | $newStatusID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id'); | |
1192 | } | |
1193 | elseif ($updatePrimaryStatus) { | |
1194 | CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id', $newStatusID); | |
1195 | } | |
1196 | ||
1197 | $cascadeAdditionalIds = self::getValidAdditionalIds($participantID, $oldStatusID, $newStatusID); | |
1198 | ||
1199 | if (!empty($cascadeAdditionalIds)) { | |
f7f14b5c JP |
1200 | try { |
1201 | foreach ($cascadeAdditionalIds as $id) { | |
be2fb01f | 1202 | $participantParams = [ |
f7f14b5c JP |
1203 | 'id' => $id, |
1204 | 'status_id' => $newStatusID, | |
be2fb01f | 1205 | ]; |
f7f14b5c JP |
1206 | civicrm_api3('Participant', 'create', $participantParams); |
1207 | } | |
1208 | return TRUE; | |
1209 | } | |
1210 | catch (CiviCRM_API3_Exception $e) { | |
1211 | throw new CRM_Core_Exception('Failed to update additional participant status in database'); | |
29a59599 | 1212 | } |
6a488035 TO |
1213 | } |
1214 | return FALSE; | |
1215 | } | |
1216 | ||
1217 | /** | |
66f9e52b | 1218 | * Function for update status for given participant ids. |
6a488035 | 1219 | * |
d4dd1e85 TO |
1220 | * @param int $participantIds |
1221 | * Array of participant ids. | |
1222 | * @param int $statusId | |
1223 | * Status id for participant. | |
654ca41c | 1224 | * @param bool $updateRegisterDate |
6a488035 | 1225 | */ |
00be9182 | 1226 | public static function updateStatus($participantIds, $statusId, $updateRegisterDate = FALSE) { |
6a488035 TO |
1227 | if (!is_array($participantIds) || empty($participantIds) || !$statusId) { |
1228 | return; | |
1229 | } | |
1230 | ||
1231 | //lets update register date as we update status to keep track | |
1232 | //when we did update status, useful for moving participant | |
1233 | //from pending to expired. | |
1234 | $setClause = "status_id = {$statusId}"; | |
1235 | if ($updateRegisterDate) { | |
1236 | $setClause .= ", register_date = NOW()"; | |
1237 | } | |
1238 | ||
1239 | $participantIdClause = '( ' . implode(',', $participantIds) . ' )'; | |
1240 | ||
1241 | $query = " | |
1242 | UPDATE civicrm_participant | |
1243 | SET {$setClause} | |
1244 | WHERE id IN {$participantIdClause}"; | |
1245 | ||
1246 | $dao = CRM_Core_DAO::executeQuery($query); | |
1247 | } | |
1248 | ||
654ca41c | 1249 | /** |
6a488035 TO |
1250 | * Function takes participant ids and statuses |
1251 | * update status from $fromStatusId to $toStatusId | |
1252 | * and send mail + create activities. | |
1253 | * | |
d4dd1e85 TO |
1254 | * @param array $participantIds |
1255 | * Participant ids. | |
1256 | * @param int $toStatusId | |
1257 | * Update status id. | |
1258 | * @param int $fromStatusId | |
1259 | * From status id. | |
654ca41c EM |
1260 | * @param bool $returnResult |
1261 | * @param bool $skipCascadeRule | |
1262 | * | |
a1a2a83d | 1263 | * @return array|NULL |
6a488035 | 1264 | */ |
2da40d21 | 1265 | public static function transitionParticipants( |
ddca8f33 | 1266 | $participantIds, $toStatusId, |
6a488035 TO |
1267 | $fromStatusId = NULL, $returnResult = FALSE, $skipCascadeRule = FALSE |
1268 | ) { | |
1269 | if (!is_array($participantIds) || empty($participantIds) || !$toStatusId) { | |
a1a2a83d | 1270 | return NULL; |
6a488035 TO |
1271 | } |
1272 | ||
1273 | //thumb rule is if we triggering primary participant need to triggered additional | |
be2fb01f | 1274 | $allParticipantIds = $primaryANDAdditonalIds = []; |
6a488035 TO |
1275 | foreach ($participantIds as $id) { |
1276 | $allParticipantIds[] = $id; | |
1277 | if (self::isPrimaryParticipant($id)) { | |
1278 | //filter additional as per status transition rules, CRM-5403 | |
1279 | if ($skipCascadeRule) { | |
1280 | $additionalIds = self::getAdditionalParticipantIds($id); | |
1281 | } | |
1282 | else { | |
1283 | $additionalIds = self::getValidAdditionalIds($id, $fromStatusId, $toStatusId); | |
1284 | } | |
1285 | if (!empty($additionalIds)) { | |
1286 | $allParticipantIds = array_merge($allParticipantIds, $additionalIds); | |
1287 | $primaryANDAdditonalIds[$id] = $additionalIds; | |
1288 | } | |
1289 | } | |
1290 | } | |
1291 | ||
1292 | //get the unique participant ids, | |
1293 | $allParticipantIds = array_unique($allParticipantIds); | |
1294 | ||
1295 | //pull required participants, contacts, events data, if not in hand | |
be2fb01f CW |
1296 | static $eventDetails = []; |
1297 | static $domainValues = []; | |
1298 | static $contactDetails = []; | |
6a488035 | 1299 | |
be2fb01f | 1300 | $contactIds = $eventIds = $participantDetails = []; |
6a488035 | 1301 | |
ddca8f33 | 1302 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(); |
6a488035 | 1303 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); |
ddca8f33 | 1304 | $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, |
6a488035 TO |
1305 | "class = 'Pending'" |
1306 | ); | |
1307 | ||
1308 | //first thing is pull all necessory data from db. | |
1309 | $participantIdClause = '(' . implode(',', $allParticipantIds) . ')'; | |
1310 | ||
1311 | //get all participants data. | |
1312 | $query = "SELECT * FROM civicrm_participant WHERE id IN {$participantIdClause}"; | |
1313 | $dao = CRM_Core_DAO::executeQuery($query); | |
1314 | while ($dao->fetch()) { | |
be2fb01f | 1315 | $participantDetails[$dao->id] = [ |
6a488035 TO |
1316 | 'id' => $dao->id, |
1317 | 'role' => $participantRoles[$dao->role_id], | |
1318 | 'is_test' => $dao->is_test, | |
1319 | 'event_id' => $dao->event_id, | |
1320 | 'status_id' => $dao->status_id, | |
1321 | 'fee_amount' => $dao->fee_amount, | |
1322 | 'contact_id' => $dao->contact_id, | |
1323 | 'register_date' => $dao->register_date, | |
1324 | 'registered_by_id' => $dao->registered_by_id, | |
be2fb01f | 1325 | ]; |
6a488035 TO |
1326 | if (!array_key_exists($dao->contact_id, $contactDetails)) { |
1327 | $contactIds[$dao->contact_id] = $dao->contact_id; | |
1328 | } | |
1329 | ||
1330 | if (!array_key_exists($dao->event_id, $eventDetails)) { | |
1331 | $eventIds[$dao->event_id] = $dao->event_id; | |
1332 | } | |
1333 | } | |
1334 | ||
1335 | //get the domain values. | |
1336 | if (empty($domainValues)) { | |
1337 | // making all tokens available to templates. | |
1338 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
be2fb01f CW |
1339 | $tokens = [ |
1340 | 'domain' => ['name', 'phone', 'address', 'email'], | |
6a488035 | 1341 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
be2fb01f | 1342 | ]; |
6a488035 TO |
1343 | |
1344 | foreach ($tokens['domain'] as $token) { | |
1345 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | //get all required contacts detail. | |
1350 | if (!empty($contactIds)) { | |
1351 | // get the contact details. | |
1352 | list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, | |
1353 | FALSE, FALSE, NULL, | |
be2fb01f | 1354 | [], |
6a488035 TO |
1355 | 'CRM_Event_BAO_Participant' |
1356 | ); | |
1357 | foreach ($currentContactDetails as $contactId => $contactValues) { | |
1358 | $contactDetails[$contactId] = $contactValues; | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | //get all required events detail. | |
1363 | if (!empty($eventIds)) { | |
1364 | foreach ($eventIds as $eventId) { | |
1365 | //retrieve event information | |
be2fb01f | 1366 | $eventParams = ['id' => $eventId]; |
6a488035 TO |
1367 | CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$eventId]); |
1368 | ||
1369 | //get default participant role. | |
1370 | $eventDetails[$eventId]['participant_role'] = CRM_Utils_Array::value($eventDetails[$eventId]['default_role_id'], $participantRoles); | |
1371 | ||
1372 | //get the location info | |
be2fb01f | 1373 | $locParams = ['entity_id' => $eventId, 'entity_table' => 'civicrm_event']; |
6a488035 TO |
1374 | $eventDetails[$eventId]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE); |
1375 | } | |
1376 | } | |
1377 | ||
1378 | //now we are ready w/ all required data. | |
1379 | //take a decision as per statuses. | |
1380 | ||
ddca8f33 TO |
1381 | $emailType = NULL; |
1382 | $toStatus = $statusTypes[$toStatusId]; | |
6a488035 TO |
1383 | $fromStatus = CRM_Utils_Array::value($fromStatusId, $statusTypes); |
1384 | ||
1385 | switch ($toStatus) { | |
1386 | case 'Pending from waitlist': | |
1387 | case 'Pending from approval': | |
1388 | switch ($fromStatus) { | |
1389 | case 'On waitlist': | |
1390 | case 'Awaiting approval': | |
1391 | $emailType = 'Confirm'; | |
1392 | break; | |
1393 | } | |
1394 | break; | |
1395 | ||
1396 | case 'Expired': | |
1397 | //no matter from where u come send expired mail. | |
1398 | $emailType = $toStatus; | |
1399 | break; | |
1400 | ||
1401 | case 'Cancelled': | |
1402 | //no matter from where u come send cancel mail. | |
1403 | $emailType = $toStatus; | |
1404 | break; | |
1405 | } | |
1406 | ||
1407 | //as we process additional w/ primary, there might be case if user | |
1408 | //select primary as well as additionals, so avoid double processing. | |
be2fb01f CW |
1409 | $processedParticipantIds = []; |
1410 | $mailedParticipants = []; | |
6a488035 TO |
1411 | |
1412 | //send mails and update status. | |
1413 | foreach ($participantDetails as $participantId => $participantValues) { | |
be2fb01f | 1414 | $updateParticipantIds = []; |
6a488035 TO |
1415 | if (in_array($participantId, $processedParticipantIds)) { |
1416 | continue; | |
1417 | } | |
1418 | ||
1419 | //check is it primary and has additional. | |
1420 | if (array_key_exists($participantId, $primaryANDAdditonalIds)) { | |
a0d6db1e | 1421 | foreach ($primaryANDAdditonalIds[$participantId] as $additionalId) { |
6a488035 TO |
1422 | |
1423 | if ($emailType) { | |
a0d6db1e | 1424 | $mail = self::sendTransitionParticipantMail($additionalId, |
1425 | $participantDetails[$additionalId], | |
1426 | $eventDetails[$participantDetails[$additionalId]['event_id']], | |
1427 | $contactDetails[$participantDetails[$additionalId]['contact_id']], | |
6a488035 TO |
1428 | $domainValues, |
1429 | $emailType | |
1430 | ); | |
1431 | ||
1432 | //get the mail participant ids | |
1433 | if ($mail) { | |
a0d6db1e | 1434 | $mailedParticipants[$additionalId] = $contactDetails[$participantDetails[$additionalId]['contact_id']]['display_name']; |
6a488035 TO |
1435 | } |
1436 | } | |
a0d6db1e | 1437 | $updateParticipantIds[] = $additionalId; |
1438 | $processedParticipantIds[] = $additionalId; | |
6a488035 TO |
1439 | } |
1440 | } | |
1441 | ||
1442 | //now send email appropriate mail to primary. | |
1443 | if ($emailType) { | |
1444 | $mail = self::sendTransitionParticipantMail($participantId, | |
1445 | $participantValues, | |
1446 | $eventDetails[$participantValues['event_id']], | |
1447 | $contactDetails[$participantValues['contact_id']], | |
1448 | $domainValues, | |
1449 | $emailType | |
1450 | ); | |
1451 | ||
1452 | //get the mail participant ids | |
1453 | if ($mail) { | |
1454 | $mailedParticipants[$participantId] = $contactDetails[$participantValues['contact_id']]['display_name']; | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | //now update status of group/one at once. | |
1459 | $updateParticipantIds[] = $participantId; | |
1460 | ||
1461 | //update the register date only when we, | |
1462 | //move participant to pending class, CRM-6496 | |
1463 | $updateRegisterDate = FALSE; | |
1464 | if (array_key_exists($toStatusId, $pendingStatuses)) { | |
1465 | $updateRegisterDate = TRUE; | |
1466 | } | |
1467 | self::updateStatus($updateParticipantIds, $toStatusId, $updateRegisterDate); | |
1468 | $processedParticipantIds[] = $participantId; | |
1469 | } | |
1470 | ||
1471 | //return result for cron. | |
1472 | if ($returnResult) { | |
be2fb01f | 1473 | $results = [ |
6a488035 TO |
1474 | 'mailedParticipants' => $mailedParticipants, |
1475 | 'updatedParticipantIds' => $processedParticipantIds, | |
be2fb01f | 1476 | ]; |
6a488035 TO |
1477 | |
1478 | return $results; | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | /** | |
100fef9d | 1483 | * Send mail and create activity |
6a488035 TO |
1484 | * when participant status changed. |
1485 | * | |
d4dd1e85 TO |
1486 | * @param int $participantId |
1487 | * Participant id. | |
1488 | * @param array $participantValues | |
1489 | * Participant detail values. status id for participants. | |
1490 | * @param array $eventDetails | |
1491 | * Required event details. | |
1492 | * @param array $contactDetails | |
1493 | * Required contact details. | |
1494 | * @param array $domainValues | |
1495 | * Required domain values. | |
1496 | * @param string $mailType | |
1497 | * (eg 'approval', 'confirm', 'expired' ). | |
6a488035 | 1498 | * |
654ca41c | 1499 | * @return bool |
6a488035 | 1500 | */ |
2da40d21 | 1501 | public static function sendTransitionParticipantMail( |
6a488035 TO |
1502 | $participantId, |
1503 | $participantValues, | |
1504 | $eventDetails, | |
1505 | $contactDetails, | |
1506 | &$domainValues, | |
1507 | $mailType | |
1508 | ) { | |
1509 | //send emails. | |
1510 | $mailSent = FALSE; | |
1511 | ||
1512 | //don't send confirmation mail to additional | |
1513 | //since only primary able to confirm registration. | |
a7488080 | 1514 | if (!empty($participantValues['registered_by_id']) && |
6a488035 TO |
1515 | $mailType == 'Confirm' |
1516 | ) { | |
1517 | return $mailSent; | |
1518 | } | |
0cca0bf4 J |
1519 | $toEmail = CRM_Utils_Array::value('email', $contactDetails); |
1520 | if ($toEmail) { | |
6a488035 TO |
1521 | |
1522 | $contactId = $participantValues['contact_id']; | |
1523 | $participantName = $contactDetails['display_name']; | |
1524 | ||
1525 | //calculate the checksum value. | |
1526 | $checksumValue = NULL; | |
1527 | if ($mailType == 'Confirm' && !$participantValues['registered_by_id']) { | |
1528 | $checksumLife = 'inf'; | |
0cca0bf4 J |
1529 | $endDate = CRM_Utils_Array::value('end_date', $eventDetails); |
1530 | if ($endDate) { | |
6a488035 TO |
1531 | $checksumLife = (CRM_Utils_Date::unixTime($endDate) - time()) / (60 * 60); |
1532 | } | |
1533 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactId, NULL, $checksumLife); | |
1534 | } | |
1535 | ||
1536 | //take a receipt from as event else domain. | |
1537 | $receiptFrom = $domainValues['name'] . ' <' . $domainValues['email'] . '>'; | |
8cc574cf | 1538 | if (!empty($eventDetails['confirm_from_name']) && !empty($eventDetails['confirm_from_email'])) { |
6a488035 TO |
1539 | $receiptFrom = $eventDetails['confirm_from_name'] . ' <' . $eventDetails['confirm_from_email'] . '>'; |
1540 | } | |
1541 | ||
c6327d7d | 1542 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
be2fb01f | 1543 | [ |
6a488035 TO |
1544 | 'groupName' => 'msg_tpl_workflow_event', |
1545 | 'valueName' => 'participant_' . strtolower($mailType), | |
1546 | 'contactId' => $contactId, | |
be2fb01f | 1547 | 'tplParams' => [ |
6a488035 TO |
1548 | 'contact' => $contactDetails, |
1549 | 'domain' => $domainValues, | |
1550 | 'participant' => $participantValues, | |
1551 | 'event' => $eventDetails, | |
1552 | 'paidEvent' => CRM_Utils_Array::value('is_monetary', $eventDetails), | |
1553 | 'isShowLocation' => CRM_Utils_Array::value('is_show_location', $eventDetails), | |
1554 | 'isAdditional' => $participantValues['registered_by_id'], | |
1555 | 'isExpired' => $mailType == 'Expired', | |
1556 | 'isConfirm' => $mailType == 'Confirm', | |
1557 | 'checksumValue' => $checksumValue, | |
be2fb01f | 1558 | ], |
6a488035 TO |
1559 | 'from' => $receiptFrom, |
1560 | 'toName' => $participantName, | |
1561 | 'toEmail' => $toEmail, | |
1562 | 'cc' => CRM_Utils_Array::value('cc_confirm', $eventDetails), | |
1563 | 'bcc' => CRM_Utils_Array::value('bcc_confirm', $eventDetails), | |
be2fb01f | 1564 | ] |
6a488035 TO |
1565 | ); |
1566 | ||
1567 | // 3. create activity record. | |
1568 | if ($mailSent) { | |
ddca8f33 TO |
1569 | $now = date('YmdHis'); |
1570 | $activityType = 'Event Registration'; | |
be2fb01f | 1571 | $activityParams = [ |
6a488035 TO |
1572 | 'subject' => $subject, |
1573 | 'source_contact_id' => $contactId, | |
1574 | 'source_record_id' => $participantId, | |
593dbb07 | 1575 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', $activityType), |
6a488035 TO |
1576 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), |
1577 | 'due_date_time' => CRM_Utils_Date::isoToMysql($participantValues['register_date']), | |
1578 | 'is_test' => $participantValues['is_test'], | |
1579 | 'status_id' => 2, | |
be2fb01f | 1580 | ]; |
6a488035 TO |
1581 | |
1582 | if (is_a(CRM_Activity_BAO_Activity::create($activityParams), 'CRM_Core_Error')) { | |
1583 | CRM_Core_Error::fatal('Failed creating Activity for expiration mail'); | |
1584 | } | |
1585 | } | |
1586 | } | |
1587 | ||
1588 | return $mailSent; | |
1589 | } | |
1590 | ||
1591 | /** | |
100fef9d | 1592 | * Get participant status change message. |
6a488035 | 1593 | * |
100fef9d | 1594 | * @param int $participantId |
654ca41c | 1595 | * @param $statusChangeTo |
100fef9d | 1596 | * @param int $fromStatusId |
654ca41c | 1597 | * |
6a488035 | 1598 | * @return string |
6a488035 | 1599 | */ |
00be9182 | 1600 | public function updateStatusMessage($participantId, $statusChangeTo, $fromStatusId) { |
6a488035 | 1601 | $statusMsg = NULL; |
be2fb01f | 1602 | $results = self::transitionParticipants([$participantId], |
6a488035 TO |
1603 | $statusChangeTo, $fromStatusId, TRUE |
1604 | ); | |
1605 | ||
1606 | $allStatuses = CRM_Event_PseudoConstant::participantStatus(); | |
1607 | //give user message only when mail has sent. | |
1608 | if (is_array($results) && !empty($results)) { | |
1609 | if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) { | |
1610 | foreach ($results['updatedParticipantIds'] as $processedId) { | |
1611 | if (is_array($results['mailedParticipants']) && | |
1612 | array_key_exists($processedId, $results['mailedParticipants']) | |
1613 | ) { | |
1614 | $statusMsg .= '<br /> ' . ts("Participant status has been updated to '%1'. An email has been sent to %2.", | |
be2fb01f | 1615 | [ |
ddca8f33 TO |
1616 | 1 => $allStatuses[$statusChangeTo], |
1617 | 2 => $results['mailedParticipants'][$processedId], | |
be2fb01f | 1618 | ] |
ddca8f33 | 1619 | ); |
6a488035 TO |
1620 | } |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | return $statusMsg; | |
1626 | } | |
1627 | ||
1628 | /** | |
100fef9d | 1629 | * Get event full and waiting list message. |
6a488035 | 1630 | * |
100fef9d CW |
1631 | * @param int $eventId |
1632 | * @param int $participantId | |
654ca41c | 1633 | * |
6a488035 | 1634 | * @return string |
6a488035 | 1635 | */ |
00be9182 | 1636 | public static function eventFullMessage($eventId, $participantId = NULL) { |
6a488035 TO |
1637 | $eventfullMsg = $dbStatusId = NULL; |
1638 | $checkEventFull = TRUE; | |
1639 | if ($participantId) { | |
1640 | $dbStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantId, 'status_id'); | |
1641 | if (array_key_exists($dbStatusId, CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'))) { | |
1642 | //participant already in counted status no need to check for event full messages. | |
1643 | $checkEventFull = FALSE; | |
1644 | } | |
1645 | } | |
1646 | ||
1647 | //early return. | |
1648 | if (!$eventId || !$checkEventFull) { | |
1649 | return $eventfullMsg; | |
1650 | } | |
1651 | ||
1652 | //event is truly full. | |
1653 | $emptySeats = self::eventFull($eventId, FALSE, FALSE); | |
1654 | if (is_string($emptySeats) && $emptySeats !== NULL) { | |
1655 | $maxParticipants = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'max_participants'); | |
be2fb01f | 1656 | $eventfullMsg = ts("This event currently has the maximum number of participants registered (%1). However, you can still override this limit and register additional participants using this form.", [ |
90b461f1 SL |
1657 | 1 => $maxParticipants, |
1658 | ]) . '<br />'; | |
6a488035 TO |
1659 | } |
1660 | ||
1661 | $hasWaiting = FALSE; | |
1662 | $waitListedCount = self::eventFull($eventId, FALSE, TRUE, TRUE); | |
1663 | if (is_numeric($waitListedCount)) { | |
1664 | $hasWaiting = TRUE; | |
1665 | //only current processing participant is on waitlist. | |
1666 | if ($waitListedCount == 1 && CRM_Event_PseudoConstant::participantStatus($dbStatusId) == 'On waitlist') { | |
1667 | $hasWaiting = FALSE; | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | if ($hasWaiting) { | |
1672 | $waitingStatusId = array_search('On waitlist', | |
1673 | CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'") | |
1674 | ); | |
1675 | $viewWaitListUrl = CRM_Utils_System::url('civicrm/event/search', | |
1676 | "reset=1&force=1&event={$eventId}&status={$waitingStatusId}" | |
1677 | ); | |
1678 | ||
1679 | $eventfullMsg .= ts("There are %2 people currently on the waiting list for this event. You can <a href='%1'>view waitlisted registrations here</a>, or you can continue and register additional participants using this form.", | |
be2fb01f | 1680 | [ |
6a488035 TO |
1681 | 1 => $viewWaitListUrl, |
1682 | 2 => $waitListedCount, | |
be2fb01f | 1683 | ] |
6a488035 TO |
1684 | ); |
1685 | } | |
1686 | ||
1687 | return $eventfullMsg; | |
1688 | } | |
1689 | ||
1690 | /** | |
66f9e52b | 1691 | * Check for whether participant is primary or not. |
6a488035 | 1692 | * |
100fef9d | 1693 | * @param int $participantId |
6a488035 | 1694 | * |
72b3a70c CW |
1695 | * @return bool |
1696 | * true if participant is primary | |
6a488035 | 1697 | */ |
00be9182 | 1698 | public static function isPrimaryParticipant($participantId) { |
6a488035 TO |
1699 | |
1700 | $participant = new CRM_Event_DAO_Participant(); | |
1701 | $participant->registered_by_id = $participantId; | |
1702 | ||
1703 | if ($participant->find(TRUE)) { | |
1704 | return TRUE; | |
1705 | } | |
1706 | return FALSE; | |
1707 | } | |
1708 | ||
1709 | /** | |
66f9e52b | 1710 | * Get additional participant Ids for cascading with primary participant status. |
6a488035 | 1711 | * |
d4dd1e85 TO |
1712 | * @param int $participantId |
1713 | * Participant id. | |
1714 | * @param int $oldStatusId | |
1715 | * Previous status. | |
1716 | * @param int $newStatusId | |
1717 | * New status. | |
6a488035 | 1718 | * |
72b3a70c CW |
1719 | * @return bool |
1720 | * true if allowed | |
6a488035 | 1721 | */ |
00be9182 | 1722 | public static function getValidAdditionalIds($participantId, $oldStatusId, $newStatusId) { |
6a488035 | 1723 | |
be2fb01f | 1724 | $additionalParticipantIds = []; |
6a488035 | 1725 | |
be2fb01f | 1726 | static $participantStatuses = []; |
6a488035 TO |
1727 | |
1728 | if (empty($participantStatuses)) { | |
1729 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); | |
1730 | } | |
1731 | ||
1732 | if (CRM_Utils_Array::value($participantStatuses[$oldStatusId], self::$_statusTransitionsRules) && | |
1733 | in_array($participantStatuses[$newStatusId], self::$_statusTransitionsRules[$participantStatuses[$oldStatusId]]) | |
1734 | ) { | |
1735 | $additionalParticipantIds = self::getAdditionalParticipantIds($participantId, TRUE, $oldStatusId); | |
1736 | } | |
1737 | ||
1738 | return $additionalParticipantIds; | |
1739 | } | |
1740 | ||
1741 | /** | |
66f9e52b | 1742 | * Get participant record count for a Contact. |
6a488035 | 1743 | * |
c490a46a | 1744 | * @param int $contactID |
6a488035 | 1745 | * |
a6c01b45 CW |
1746 | * @return int |
1747 | * count of participant records | |
6a488035 | 1748 | */ |
00be9182 | 1749 | public static function getContactParticipantCount($contactID) { |
6a488035 TO |
1750 | $query = "SELECT count(*) |
1751 | FROM civicrm_participant | |
1752 | WHERE civicrm_participant.contact_id = {$contactID} AND | |
1753 | civicrm_participant.is_test = 0"; | |
1754 | return CRM_Core_DAO::singleValueQuery($query); | |
1755 | } | |
1756 | ||
1757 | /** | |
66f9e52b | 1758 | * Get participant ids by contribution id. |
6a488035 | 1759 | * |
d4dd1e85 TO |
1760 | * @param int $contributionId |
1761 | * Contribution Id. | |
1762 | * @param bool $excludeCancelled | |
1763 | * Exclude cancelled additional participant. | |
6a488035 | 1764 | * |
a6c01b45 | 1765 | * @return array |
6a488035 | 1766 | */ |
00be9182 | 1767 | public static function getParticipantIds($contributionId, $excludeCancelled = FALSE) { |
6a488035 | 1768 | |
be2fb01f | 1769 | $ids = []; |
6a488035 TO |
1770 | if (!$contributionId) { |
1771 | return $ids; | |
1772 | } | |
1773 | ||
1774 | // get primary participant id | |
b677b23b JP |
1775 | $query = "SELECT participant_id |
1776 | FROM civicrm_participant cp | |
1777 | LEFT JOIN civicrm_participant_payment cpp ON cp.id = cpp.participant_id | |
1778 | WHERE cpp.contribution_id = {$contributionId} | |
1779 | AND cp.registered_by_id IS NULL"; | |
1780 | $participantPayment = CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
1781 | |
1782 | // get additional participant ids (including cancelled) | |
b677b23b | 1783 | while ($participantPayment->fetch()) { |
be2fb01f | 1784 | $ids = array_merge($ids, array_merge([ |
b677b23b | 1785 | $participantPayment->participant_id, |
be2fb01f | 1786 | ], self::getAdditionalParticipantIds($participantPayment->participant_id, |
ddca8f33 | 1787 | $excludeCancelled |
b677b23b | 1788 | ))); |
6a488035 TO |
1789 | } |
1790 | ||
1791 | return $ids; | |
1792 | } | |
1793 | ||
1794 | /** | |
100fef9d | 1795 | * Get additional Participant edit & view url . |
6a488035 | 1796 | * |
d4dd1e85 TO |
1797 | * @param array $participantIds |
1798 | * An array of additional participant ids. | |
6a488035 | 1799 | * |
a6c01b45 | 1800 | * @return array |
16b10e64 | 1801 | * Array of Urls. |
6a488035 | 1802 | */ |
00be9182 | 1803 | public static function getAdditionalParticipantUrl($participantIds) { |
6a488035 | 1804 | foreach ($participantIds as $value) { |
be2fb01f | 1805 | $links = []; |
6a488035 TO |
1806 | $details = self::participantDetails($value); |
1807 | $viewUrl = CRM_Utils_System::url('civicrm/contact/view/participant', | |
1808 | "action=view&reset=1&id={$value}&cid={$details['cid']}" | |
1809 | ); | |
1810 | $editUrl = CRM_Utils_System::url('civicrm/contact/view/participant', | |
1811 | "action=update&reset=1&id={$value}&cid={$details['cid']}" | |
1812 | ); | |
1813 | $links[] = "<td><a href='{$viewUrl}'>" . $details['name'] . "</a></td><td></td><td><a href='{$editUrl}'>" . ts('Edit') . "</a></td>"; | |
1814 | $links = "<table><tr>" . implode("</tr><tr>", $links) . "</tr></table>"; | |
1815 | return $links; | |
1816 | } | |
1817 | } | |
1818 | ||
1819 | /** | |
dc195289 | 1820 | * create trxn entry if an event has discount. |
6a488035 | 1821 | * |
d4dd1e85 TO |
1822 | * @param int $eventID |
1823 | * Event id. | |
1824 | * @param array $contributionParams | |
1825 | * Contribution params. | |
654ca41c | 1826 | * |
362bd1b7 | 1827 | * @param string $feeLevel (deprecated) |
1828 | * @param int $discountedPriceFieldOptionID | |
1829 | * ID of the civicrm_price_field_value field for the discount id. | |
6a488035 | 1830 | */ |
9a05ea52 | 1831 | public static function createDiscountTrxn($eventID, $contributionParams, $feeLevel, $discountedPriceFieldOptionID = NULL) { |
362bd1b7 | 1832 | $financialTypeID = $contributionParams['contribution']->financial_type_id; |
1833 | $total_amount = $contributionParams['total_amount']; | |
1834 | ||
0479b4c8 | 1835 | $checkDiscount = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event'); |
6a488035 | 1836 | if (!empty($checkDiscount)) { |
362bd1b7 | 1837 | $mainAmount = self::getUnDiscountedAmountForEventPriceSetFieldValue($eventID, $discountedPriceFieldOptionID, $feeLevel); |
876b8ab0 PN |
1838 | $transactionParams['from_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount( |
1839 | $financialTypeID, 'Discounts Account is'); | |
362bd1b7 | 1840 | if (!empty($transactionParams['trxnParams']['from_financial_account_id'])) { |
1841 | $transactionParams['trxnParams']['total_amount'] = $mainAmount - $total_amount; | |
1842 | $transactionParams['trxnParams']['payment_processor_id'] = NULL; | |
1843 | $transactionParams['trxnParams']['payment_instrument_id'] = NULL; | |
1844 | $transactionParams['trxnParams']['check_number'] = NULL; | |
1845 | $transactionParams['trxnParams']['trxn_id'] = NULL; | |
1846 | $transactionParams['trxnParams']['net_amount'] = NULL; | |
1847 | $transactionParams['trxnParams']['fee_amount'] = NULL; | |
1848 | CRM_Core_BAO_FinancialTrxn::create($transactionParams); | |
6a488035 TO |
1849 | } |
1850 | } | |
6a488035 | 1851 | } |
c3d24ba7 PN |
1852 | |
1853 | /** | |
66f9e52b | 1854 | * Delete participants of contact. |
c3d24ba7 PN |
1855 | * |
1856 | * CRM-12155 | |
1857 | * | |
d4dd1e85 TO |
1858 | * @param int $contactId |
1859 | * Contact id. | |
c3d24ba7 | 1860 | * |
c3d24ba7 | 1861 | */ |
00be9182 | 1862 | public static function deleteContactParticipant($contactId) { |
c3d24ba7 PN |
1863 | $participant = new CRM_Event_DAO_Participant(); |
1864 | $participant->contact_id = $contactId; | |
1865 | $participant->find(); | |
1866 | while ($participant->fetch()) { | |
1867 | self::deleteParticipant($participant->id); | |
1868 | } | |
1869 | } | |
6a488035 | 1870 | |
0cf587a7 | 1871 | /** |
100fef9d | 1872 | * @param int $participantId |
0cf587a7 EM |
1873 | * @param $activityType |
1874 | * | |
1875 | * @throws CRM_Core_Exception | |
1876 | */ | |
00be9182 | 1877 | public static function addActivityForSelection($participantId, $activityType) { |
0aaf8fe9 PJ |
1878 | $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantId, 'event_id'); |
1879 | $contactId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantId, 'contact_id'); | |
1880 | ||
1881 | $date = CRM_Utils_Date::currentDBDate(); | |
1882 | $event = CRM_Event_BAO_Event::getEvents(0, $eventId); | |
28c64535 | 1883 | $subject = sprintf("Registration selections changed for %s", CRM_Utils_Array::value($eventId, $event)); |
0aaf8fe9 PJ |
1884 | |
1885 | // activity params | |
be2fb01f | 1886 | $activityParams = [ |
a0d6db1e | 1887 | 'source_contact_id' => $contactId, |
1888 | 'source_record_id' => $participantId, | |
d66c61b6 | 1889 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', $activityType), |
0aaf8fe9 PJ |
1890 | 'subject' => $subject, |
1891 | 'activity_date_time' => $date, | |
d66c61b6 | 1892 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'), |
0aaf8fe9 | 1893 | 'skipRecentView' => TRUE, |
be2fb01f | 1894 | ]; |
0aaf8fe9 PJ |
1895 | |
1896 | // create activity with target contacts | |
fe7f4414 | 1897 | $id = CRM_Core_Session::singleton()->getLoggedInContactID(); |
0aaf8fe9 PJ |
1898 | if ($id) { |
1899 | $activityParams['source_contact_id'] = $id; | |
a0d6db1e | 1900 | $activityParams['target_contact_id'][] = $contactId; |
0aaf8fe9 | 1901 | } |
d66c61b6 | 1902 | // @todo use api & also look at duplication of similar methods. |
0aaf8fe9 PJ |
1903 | CRM_Activity_BAO_Activity::create($activityParams); |
1904 | } | |
f76b27fe CW |
1905 | |
1906 | /** | |
1907 | * Get options for a given field. | |
1908 | * @see CRM_Core_DAO::buildOptions | |
1909 | * | |
d4dd1e85 TO |
1910 | * @param string $fieldName |
1911 | * @param string $context | |
a1a2a83d | 1912 | * @see CRM_Core_DAO::buildOptionsContext |
d4dd1e85 | 1913 | * @param array $props |
16b10e64 | 1914 | * whatever is known about this dao object. |
f76b27fe | 1915 | * |
5c766a0b | 1916 | * @return array|bool |
f76b27fe | 1917 | */ |
be2fb01f CW |
1918 | public static function buildOptions($fieldName, $context = NULL, $props = []) { |
1919 | $params = ['condition' => []]; | |
f76b27fe CW |
1920 | |
1921 | if ($fieldName == 'status_id' && $context != 'validate') { | |
1922 | // Get rid of cart-related option if disabled | |
1923 | // FIXME: Why does this option even exist if cart is disabled? | |
aaffa79f | 1924 | if (!Civi::settings()->get('enable_cart')) { |
f76b27fe CW |
1925 | $params['condition'][] = "name <> 'Pending in cart'"; |
1926 | } | |
1927 | } | |
1928 | ||
1929 | return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); | |
1930 | } | |
96025800 | 1931 | |
c7d52684 | 1932 | /** |
c50429ca | 1933 | * CRM-17797 -- Format fields and setDefaults for primary and additional participants profile |
1934 | * @param int $contactId | |
1935 | * @param CRM_Core_Form $form | |
c7d52684 | 1936 | */ |
1937 | public static function formatFieldsAndSetProfileDefaults($contactId, &$form) { | |
1938 | if (!$contactId) { | |
1939 | return; | |
1940 | } | |
be2fb01f | 1941 | $fields = []; |
c7d52684 | 1942 | if (!empty($form->_fields)) { |
be2fb01f | 1943 | $removeCustomFieldTypes = ['Participant']; |
c7d52684 | 1944 | |
1945 | foreach ($form->_fields as $name => $dontCare) { | |
1946 | if ((substr($name, 0, 7) == 'custom_' && !$form->_allowConfirmation | |
1947 | && !CRM_Core_BAO_CustomGroup::checkCustomField(substr($name, 7), $removeCustomFieldTypes)) | |
1948 | || substr($name, 0, 12) == 'participant_') { | |
1949 | continue; | |
1950 | } | |
1951 | $fields[$name] = 1; | |
1952 | } | |
1953 | ||
1954 | if (!empty($fields)) { | |
1955 | CRM_Core_BAO_UFGroup::setProfileDefaults($contactId, $fields, $form->_defaults); | |
1956 | } | |
1957 | } | |
1958 | } | |
1959 | ||
37d5fc55 | 1960 | } |