Skip to content

Commit

Permalink
Merge pull request #24304 from mattwire/participantcontacthook
Browse files Browse the repository at this point in the history
dev/core#3778 Add code hook to set civicrm_participant.created_id
  • Loading branch information
seamuslee001 committed Sep 5, 2022
2 parents e000468 + 7462110 commit f7e48e9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
34 changes: 33 additions & 1 deletion CRM/Event/BAO/Participant.php
Expand Up @@ -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.
Expand Down Expand Up @@ -1904,4 +1904,36 @@ public static function getSelfServiceEligibility(int $participantId, string $url
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 changes: 1 addition & 1 deletion tests/phpunit/CRM/Event/BAO/ParticipantTest.php
Expand Up @@ -102,7 +102,7 @@ public function testgetValuesWithValidParams() {
'cart_id' => NULL,
'must_wait' => NULL,
'transferred_to_contact_id' => NULL,
'created_id' => NULL,
'created_id' => $this->_contactId,
];

foreach ($compareValues as $key => $value) {
Expand Down
12 changes: 6 additions & 6 deletions tests/phpunit/CRM/Utils/TokenConsistencyTest.php
Expand Up @@ -577,16 +577,16 @@ protected function getMembershipID(): int {
*
* @return string
*/
protected function getExpectedParticipantTokenOutput(): string {
return 'participant.status_id :2
protected function getExpectedParticipantTokenOutput(int $participantCreatedID = NULL): string {
return "participant.status_id :2
participant.role_id :1
participant.register_date :February 19th, 2007
participant.source :Wimbeldon
participant.fee_level :steep
participant.fee_amount :$50.00
participant.registered_by_id :
participant.transferred_to_contact_id :
participant.created_id :
participant.created_id :{$participantCreatedID}
participant.role_id:label :Attendee
participant.balance :
participant.custom_2 :99999
Expand All @@ -598,7 +598,7 @@ protected function getExpectedParticipantTokenOutput(): string {
participant.role_id:name :Attendee
participant.is_test:label :No
participant.must_wait :
';
";
}

/**
Expand Down Expand Up @@ -666,7 +666,7 @@ public function testParticipantTokenConsistency(): void {
$this->assertEquals(array_merge($tokens, $this->getEventTokens(), $this->getDomainTokens()), $tokenProcessor->listTokens());

$this->callAPISuccess('job', 'send_reminder', []);
$expected = $this->getExpectedParticipantTokenOutput();
$expected = $this->getExpectedParticipantTokenOutput(3);
$mut->checkMailLog([$expected]);

$tokenProcessor->addMessage('html', $this->getTokenString(array_keys($this->getParticipantTokens())), 'text/plain');
Expand Down Expand Up @@ -836,7 +836,7 @@ public function testEventTokenConsistency(): void {

$expectedEventString = $this->getExpectedEventTokenOutput();
$this->callAPISuccess('job', 'send_reminder', []);
$expectedParticipantString = $this->getExpectedParticipantTokenOutput();
$expectedParticipantString = $this->getExpectedParticipantTokenOutput(5);
$toCheck = array_merge(explode("\n", $expectedEventString), explode("\n", $expectedParticipantString));
$toCheck[] = $expectedEventString;
$toCheck[] = $expectedParticipantString;
Expand Down

0 comments on commit f7e48e9

Please sign in to comment.