Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract getDiscountID() #27049

Merged
merged 1 commit into from
Aug 17, 2023
Merged

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Aug 11, 2023

Overview

Extract getDiscountID()

Before

CRM_Event_Form_Registration::initEventFee() does some weird magic checking if the property discountId is configured on the form & using that, if the property exists. This is a pretty clear code smell that we should pass in the value rather than have the function find it on the form.

CRM_Event_Form_Registration::initEventFee() is called from 3 places in universe :

CRM_Event_Form_ParticipantFeeSelection::buildQuickForm() (non-static, called only from the form)
CRM_Event_Form_Registration::preProcess() (non-static, called only from the form)
CRM_Event_Form_Participant::buildEventFeeForm()

This last one was made static/public to be used in unit tests. There are no core tests that do this anymore but @mlutfy wrote a test CRM_Taxcalculator_BackendEventWebinarTest that calls it from an extension. That test would be as likely to work after as before but it follows a pattern we ditched in core tests.

The function only handles one form CRM_Event_Form_Participant - and this is the only form that would ever have the $_discountId property

image

image

After

Each of the forms above

  1. passes the discount ID into CRM_Event_Form_Registration::initEventFee()
  2. implements a public function which is annotated with @api called getDiscountID()
  3. the function types the ID to an int, or to NULL

Technical Details

This is part of a general cleanup to stop passing the form into functions & then checking the form for a possible property.

I'm also trying add consistent functions to retrieve ids from the various forms and to support those functions for extension users (where we can be confident the value would have a consistent meaning over time - even if we changed the internals of the function at some point).

Note I was actually trying to give getPriceSetID this treatment but discountID was in the way so I've done it first...

In general I'm trying to make some consisent public functions available on forms for retrieving relevant entity IDs rather than the current property / form variable patchwork

Comments

@civibot
Copy link

civibot bot commented Aug 11, 2023

Thank you for contributing to CiviCRM! ❤️ We will need to test and review the PR. 👷

Introduction for new contributors
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers

@civibot civibot bot added the master label Aug 11, 2023
@eileenmcnaughton eileenmcnaughton force-pushed the discount branch 6 times, most recently from 93a1743 to 13fc461 Compare August 11, 2023 05:11
@eileenmcnaughton eileenmcnaughton changed the title Extract getDiscountPriceSetID() Extract getDiscountID() Aug 11, 2023
@eileenmcnaughton eileenmcnaughton force-pushed the discount branch 2 times, most recently from 3f0689a to afb392b Compare August 11, 2023 05:13
This is part of a general cleanup to stop passing the form into functions & then checking the form
for a possible property. Instead each form that calls the function has a consistent
(publicly supported) getDiscountID() function that it calls & discountID is passed in.

Note I was actually trying to give getPriceSetID this treatment but discountID was in the
way. In general I'm trying to make some consisent public functions available on forms
for retrieving relevant entity IDs rather than the current property / form
variable patchwork
$this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id');
if ($this->_discountId) {
if ($this->getDiscountID()) {
// This doesn't seem used....
Copy link
Contributor

@larssandergreen larssandergreen Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this doesn't seem to be used anywhere. Seems like the previous logic was to get the discount ID from the participant, but as it is here we'd be getting the discount ID from the event itself. Is that always the same? Not sure. (I was reading this wrong). Maybe cleaner just to get rid of it, since it does nothing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can just get rid of the the whole getting the discount ID from the participant thing entirely. It doesn't seem to do anything at all. Then we can simplify getDiscountID().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@larssandergreen I was referring to the set not being used - I think loading it from the participant makes sense in edit mode

@@ -390,4 +407,23 @@ private function emailReceipt(array $params): void {
CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like $this->_eventId would always be defined, so do we need to have a method to get it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if there is a reason to have it, I think we can just have it return $this->_eventId

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SO where I am going with these is the idea that you should be able to call getEventID() & similar getfunctions at any point & they don't rely on some order of events where things are set first, but you would always get ... the event ID.

Also - I've marked as supported for external use - with the goal of providing some consistency to extensions wanting to access basic information about what is being processed (without directly accessing the properties which could be inconsistent)

@@ -1436,8 +1436,8 @@ public function buildEventFeeForm($form) {

//retrieve custom information
$form->_values = [];
CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE);
CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->_discountId);
CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE, $form->getDiscountID());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton this function doesn't seem to have a 4th function param

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton
Copy link
Contributor Author

@seamuslee001 seamuslee001 merged commit e5cdc41 into civicrm:master Aug 17, 2023
@seamuslee001 seamuslee001 deleted the discount branch August 17, 2023 21:22
colemanw added a commit that referenced this pull request Aug 23, 2023
Builds on #27049 to extract getPriceSetID()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants