Skip to content

Commit

Permalink
Send new email alerts when new users are added to research entities
Browse files Browse the repository at this point in the history
  • Loading branch information
paul121 committed Jan 17, 2024
1 parent a8c027d commit 83e00d8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ function farm_rothamsted_experiment_research_entity_update(EntityInterface $enti
return;
}

// Send new entity alert emails to notify if new researchers were added.
/** @var \Drupal\farm_rothamsted_experiment_research\ResearchNotificationHandler $notification_handler */
$notification_handler = \Drupal::classResolver(ResearchNotificationHandler::class);
$notification_handler->buildNewEntityAlert($entity, TRUE);

// Include email logic.
$emails = [];
if ($entity_type_id === 'rothamsted_proposal') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,39 @@ public static function create(ContainerInterface $container) {
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to create an alert for.
* @param bool $new_researcher
* Boolean if emails should only send to new researchers.
*/
public function buildNewEntityAlert(EntityInterface $entity) {
public function buildNewEntityAlert(EntityInterface $entity, bool $new_researcher = FALSE) {
$entity_type = $entity->getEntityTypeId();
$function_name = lcfirst(str_replace('_', '', ucwords("build_new_{$entity_type}_alert", '_')));
if (!is_callable([$this, $function_name])) {
return;
}
$this->$function_name($entity);
$this->$function_name($entity, $new_researcher);
}

/**
* Build a new alert for Rothamsted Proposal.
*
* @param \Drupal\Core\Entity\EntityInterface $proposal
* The research proposal entity.
* @param bool $new_researcher
* Boolean if emails should only send to new researchers.
*/
protected function buildNewRothamstedProposalAlert(EntityInterface $proposal) {
protected function buildNewRothamstedProposalAlert(EntityInterface $proposal, bool $new_researcher = FALSE) {

// Get the researchers from the research proposal entity.
$researchLeads = $this->getResearcherEmails($proposal->get('contact'));
$statisticians = $this->getResearcherEmails($proposal->get('statistician'));
$dataStewards = $this->getResearcherEmails($proposal->get('data_steward'));

if ($new_researcher && $proposal->isNew()) {
$researchLeads = array_diff($researchLeads, $this->getResearcherEmails($proposal->original->get('contact')));
$statisticians = array_diff($statisticians, $this->getResearcherEmails($proposal->original->get('statistician')));
$dataStewards = array_diff($dataStewards, $this->getResearcherEmails($proposal->original->get('data_steward')));
}

// Merge all the emails into an array, limiting to non-duplicate values.
$emails = array_unique(array_merge($researchLeads, $statisticians, $dataStewards));

Expand All @@ -115,11 +125,17 @@ protected function buildNewRothamstedProposalAlert(EntityInterface $proposal) {
*
* @param \Drupal\Core\Entity\EntityInterface $program
* The program entity.
* @param bool $new_researcher
* Boolean if emails should only send to new researchers.
*/
protected function buildNewRothamstedProgramAlert(EntityInterface $program) {
protected function buildNewRothamstedProgramAlert(EntityInterface $program, bool $new_researcher = FALSE) {

// Get principal investigator emails.
$emails = $this->getResearcherEmails($program->get('principal_investigator'));
if ($new_researcher && !$program->isNew()) {
$old_emails = $this->getResearcherEmails($program->original->get('principal_investigator'));
$emails = array_diff($emails, $old_emails);
}

// Build email content.
$entity_type_id = $program->getEntityTypeId();
Expand All @@ -140,9 +156,11 @@ protected function buildNewRothamstedProgramAlert(EntityInterface $program) {
*
* @param \Drupal\farm_rothamsted_experiment_research\Entity\RothamstedExperimentInterface $experiment
* The experiment entity.
* @param bool $new_researcher
* Boolean if emails should only send to new researchers.
*/
protected function buildNewRothamstedExperimentAlert(RothamstedExperimentInterface $experiment) {
$emails = $this->getExperimentResearcherEmails($experiment);
protected function buildNewRothamstedExperimentAlert(RothamstedExperimentInterface $experiment, bool $new_researcher = FALSE) {
$emails = $this->getExperimentResearcherEmails($experiment, $new_researcher);

$entity_type_id = $experiment->getEntityTypeId();

Expand All @@ -161,10 +179,12 @@ protected function buildNewRothamstedExperimentAlert(RothamstedExperimentInterfa
*
* @param \Drupal\Core\Entity\EntityInterface $design
* The design entity.
* @param bool $new_researcher
* Boolean if emails should only send to new researchers.
*/
protected function buildNewRothamstedDesignAlert(EntityInterface $design) {
protected function buildNewRothamstedDesignAlert(EntityInterface $design, bool $new_researcher = FALSE) {

$emails = $this->getDesignResearcherEmails($design);
$emails = $this->getDesignResearcherEmails($design, $new_researcher);

// Build email content.
$entity_type_id = $design->getEntityTypeId();
Expand Down Expand Up @@ -206,14 +226,20 @@ protected function buildNewPlanAlert(EntityInterface $plan) {
*
* @param \Drupal\farm_rothamsted_experiment_research\Entity\RothamstedDesignInterface $design
* The design entity.
* @param bool $new_researcher
* Boolean if emails should only send to new researchers.
*
* @return array
* An array of researcher emails.
*/
protected function getDesignResearcherEmails(RothamstedDesignInterface $design) {
protected function getDesignResearcherEmails(RothamstedDesignInterface $design, bool $new_researcher = FALSE) {
// Get the researcher and statistician from the research design entity.
$researchers = $this->getExperimentResearcherEmails($design->get('experiment')->entity);
$statisticians = $this->getResearcherEmails($design->get('statistician'));
if ($new_researcher && !$design->isNew()) {
$old_stats = $this->getResearcherEmails($design->original->get('statistician'));
$statisticians = array_diff($statisticians, $old_stats);
}

// Merge all the emails into an array, limiting to non-duplicate values.
return array_unique(array_merge($researchers, $statisticians));
Expand All @@ -224,12 +250,21 @@ protected function getDesignResearcherEmails(RothamstedDesignInterface $design)
*
* @param \Drupal\farm_rothamsted_experiment_research\Entity\RothamstedExperimentInterface $experiment
* The experiment entity.
* @param bool $new_researcher
* Boolean if emails should only send to new researchers.
*
* @return array
* An array of researcher emails.
*/
protected function getExperimentResearcherEmails(RothamstedExperimentInterface $experiment) {
return $this->getResearcherEmails($experiment->get('researcher'));
protected function getExperimentResearcherEmails(RothamstedExperimentInterface $experiment, bool $new_researcher = FALSE) {
$current_emails = $this->getResearcherEmails($experiment->get('researcher'));

if ($new_researcher && !$experiment->isNew()) {
$old_emails = $this->getResearcherEmails($experiment->original->get('researcher'));
return array_diff($current_emails, $old_emails);
}

return $current_emails;
}

/**
Expand Down Expand Up @@ -265,6 +300,11 @@ protected function sendMail(EntityInterface $entity, array $emails, array $param
}
$emails = array_unique(array_filter($emails));

// Bail if there is no one to send an email to.
if (empty($emails)) {
return;
}

// Set the entity param.
$params['entity'] = $entity;

Expand Down

0 comments on commit 83e00d8

Please sign in to comment.