Skip to content

Commit

Permalink
Merge pull request #911 from jitendrapurohit/primary_participant
Browse files Browse the repository at this point in the history
D9/10 - Add setting to disable primary participant for Contact 1
  • Loading branch information
KarinG authored Sep 24, 2023
2 parents a4c965e + 94d3032 commit 957cc00
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/AdminForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,15 @@ private function buildParticipantTab() {
'#title' => t('Allow events to be autoloaded from URL'),
'#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:allow_url_load'),
];
$this->form['participant']['reg_options']['disable_primary_participant'] = [
'#type' => 'checkbox',
'#title' => t('Disable Contact 1 to be stored as Primary Participant'),
'#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:disable_primary_participant'),
];
$this->help($this->form['participant']['reg_options']['block_form'], 'reg_options_block_form');
$this->help($this->form['participant']['reg_options']['disable_unregister'], 'reg_options_disable_unregister');
$this->help($this->form['participant']['reg_options']['allow_url_load'], 'reg_options_allow_url_load');
$this->help($this->form['participant']['reg_options']['disable_primary_participant'], 'reg_options_disable_primary_participant');
$this->addAjaxItem('participant', 'participant_reg_type', 'participants');
$this->addAjaxItem('participant', 'event_type', 'participants');
$this->addAjaxItem('participant', 'show_past_events', 'participants');
Expand Down
9 changes: 9 additions & 0 deletions src/AdminHelp.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ protected function reg_options_allow_url_load() {
'/{node.nid}?c1event1={event1.event_id},{event2.event_id}&amp;c2event1={event3.event_id}</code></p>';
}

/**
* Help text for disable primary setting.
*/
protected function reg_options_disable_primary_participant() {
return '<p>' .
t('If enabled, Contact 1 will not be stored as primary participant for multiple registrations.') .
'</p>';
}

protected function reg_options_show_past_events() {
return '<p>' .
t('To also display events that have ended, choose an option for how far in the past to search.') .
Expand Down
6 changes: 4 additions & 2 deletions src/WebformCivicrmPostProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,9 @@ private function processParticipants($c, $cid) {
unset($params['status_id']);
}
// Set the currency of the result to the currency type that was submitted.
$params['fee_currency'] = $this->data['contribution'][$n]['currency'];
if (isset($this->data['contribution'][$n]['currency'])) {
$params['fee_currency'] = $this->data['contribution'][$n]['currency'];
}
$result = $this->utils->wf_civicrm_api('participant', 'create', $params);
$this->ent['participant'][$n]['id'] = $result['id'];

Expand All @@ -1205,7 +1207,7 @@ private function processParticipants($c, $cid) {
}
}
// When registering contact 1, store id to apply to other contacts
if ($c == 1) {
if ($c == 1 && empty($this->data['reg_options']['disable_primary_participant'])) {
$registered_by_id[$e][$i] = $result['id'];
}
}
Expand Down
96 changes: 94 additions & 2 deletions tests/src/FunctionalJavascript/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,99 @@ function testParticipantContactReference() {
$this->assertEquals($contactRef['id'], $participant["{$customKey}_id"]);
}

/**
* Submit the form with values.
*/
function submitWebform() {
$this->drupalGet($this->webform->toUrl('canonical'));
$this->assertPageNoErrorMessages();
$edit = [
'civicrm_1_contact_1_contact_first_name' => 'Frederick',
'civicrm_1_contact_1_contact_last_name' => 'Pabst',
'civicrm_2_contact_1_contact_first_name' => 'Mark',
'civicrm_2_contact_1_contact_last_name' => 'Anthony'
];
$this->postSubmission($this->webform, $edit);
}

/**
* Verify submission results.
*
* @param boolean $primary
* false if primary participant setting is disabled on the webform.
*/
function verifyResults($primary = true) {
// Ensure both contacts are added to the event.
$api_result = $this->utils->wf_civicrm_api('participant', 'get', [
'sequential' => 1,
]);
$this->assertEquals(0, $api_result['is_error']);
$this->assertEquals(2, $api_result['count']);

$values = $api_result['values'];
$this->assertEquals($this->_event['id'], $values[0]['event_id']);
$this->assertEquals($this->_event['id'], $values[1]['event_id']);
if ($primary) {
$this->assertEquals($values[0]['id'], $values[1]['participant_registered_by_id']);
}
else {
$this->assertEmpty($values[0]['participant_registered_by_id']);
$this->assertEmpty($values[1]['participant_registered_by_id']);
}
// Delete participants.
$this->utils->wf_civicrm_api('participant', 'delete', [
'id' => $values[0]['id'],
]);
$this->utils->wf_civicrm_api('participant', 'delete', [
'id' => $values[1]['id'],
]);
}

/**
* Verify the submission of multiple participants.
*/
function testMultipleParticipants() {
$this->drupalLogin($this->adminUser);
$this->drupalGet(Url::fromRoute('entity.webform.civicrm', [
'webform' => $this->webform->id(),
]));
$this->enableCivicrmOnWebform();

$this->getSession()->getPage()->selectFieldOption('number_of_contacts', 2);
$this->assertSession()->assertWaitOnAjaxRequest();
$this->htmlOutput();

$this->getSession()->getPage()->clickLink('Event Registration');

// Configure Event tab.
$this->getSession()->getPage()->selectFieldOption('participant_reg_type', 'all');
$this->assertSession()->assertWaitOnAjaxRequest();
$this->htmlOutput();
$this->getSession()->getPage()->selectFieldOption('participant_1_number_of_participant', 1);
$this->assertSession()->assertWaitOnAjaxRequest();
$this->htmlOutput();
$this->getSession()->getPage()->selectFieldOption('civicrm_1_participant_1_participant_event_id[]', 'Test Event');

$this->saveCiviCRMSettings();

$this->submitWebform();

// Ensure both contacts are added to the event.
$this->verifyResults();

// Disable primary participant on the webform.
$this->drupalGet(Url::fromRoute('entity.webform.civicrm', [
'webform' => $this->webform->id(),
]));
$this->getSession()->getPage()->clickLink('Event Registration');
$this->getSession()->getPage()->checkField('Disable Contact 1 to be stored as Primary Participant');
$this->saveCiviCRMSettings();

// Resubmit the form and verify the results.
$this->submitWebform();
$this->verifyResults(false);
}

/**
* Event Participant submission.
*/
Expand Down Expand Up @@ -207,8 +300,7 @@ function testSubmitEventParticipant() {
$this->assertSession()->pageTextContains('New submission added to CiviCRM Webform Test.');

//Assert if recur is attached to the created membership.
$utils = \Drupal::service('webform_civicrm.utils');
$api_result = $utils->wf_civicrm_api('participant', 'get', [
$api_result = $this->utils->wf_civicrm_api('participant', 'get', [
'sequential' => 1,
]);
$this->assertEquals(0, $api_result['is_error']);
Expand Down

0 comments on commit 957cc00

Please sign in to comment.