From a627504c32c8e2754419bd3c1e17f8b4a16f6736 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Thu, 18 Aug 2022 17:10:52 +0100 Subject: [PATCH] Add code hook to set civicrm_participant.created_id --- CRM/Event/BAO/Participant.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index 9ab5ab46e4..8459b31485 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -14,7 +14,7 @@ * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant { +class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant implements \Civi\Core\HookInterface { /** * Static field for all the participant information that we can potentially import. @@ -1904,4 +1904,36 @@ WHERE civicrm_participant.contact_id = {$contactID} AND return $details; } + /** + * Callback for hook_civicrm_pre(). + * @param \Civi\Core\Event\PreEvent $event + * @throws CRM_Core_Exception + */ + public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { + if ($event->entity === 'Participant' && $event->action === 'create' && empty($event->params['created_id'])) { + // Set the "created_id" field if not already set. + // The created_id should always be the person that actually did the registration. + // That might be the first participant, but it might be someone registering someone without registering themselves. + // 1. Prefer logged in contact id + // 2. Fall back to 'registered_by_id' param. + // 3. Fall back to participant contact_id (for anonymous person registering themselves) + $event->params['created_id'] = CRM_Core_Session::getLoggedInContactID(); + if (empty($event->params['created_id'])) { + if (!empty($event->params['registered_by_id'])) { + // No logged in contact but participant was registered by someone else. + // Look up the contact ID of that participant and record + $participant = \Civi\Api4\Participant::get(FALSE) + ->addSelect('contact_id') + ->addWhere('id', '=', $event->params['registered_by_id']) + ->execute() + ->first(); + $event->params['created_id'] = $participant['contact_id']; + } + else { + $event->params['created_id'] = $event->params['contact_id']; + } + } + } + } + } -- 2.25.1