From a9266455578dcea5cc3a4c2a43d93d98b9b17c49 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 31 May 2024 15:58:41 +1000 Subject: [PATCH] feat: allow multiple emails in Notify on abort to fix #881 --- classes/local/execution/engine.php | 55 ++++++++++++++++-------------- lang/en/tool_dataflows.php | 2 +- version.php | 4 +-- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/classes/local/execution/engine.php b/classes/local/execution/engine.php index 5902b11a..4cc459ba 100644 --- a/classes/local/execution/engine.php +++ b/classes/local/execution/engine.php @@ -866,34 +866,39 @@ private function setup_logging() { */ public function notify_on_abort(?\Throwable $reason) { // If configured to send email, attempt to notify of the abort reason. - $notifyemail = $this->dataflow->get('notifyonabort'); - if (empty($notifyemail)) { - return; - } + $notifyemails = $this->dataflow->get('notifyonabort'); - $this->log('Sending abort notification email.', [], Logger::NOTICE); - $context = [ - 'flowname' => $this->dataflow->get('name'), - 'run' => $this->run->get('id'), - 'reason' => isset($reason) ? $reason->getMessage() : '' - ]; - $message = get_string('notifyonabort_message', 'tool_dataflows', $context); + foreach (explode(',', $notifyemails) as $notifyemail) { + $email = trim($notifyemail); - // First try to match the email with a Moodle user. - $to = \core_user::get_user_by_email($notifyemail); + if (empty($email)) { + continue; + } - // Otherwise send it with a dummy account. - if (!$to) { - $to = \core_user::get_noreply_user(); - $to->email = $notifyemail; - $to->firstname = $this->dataflow->get('name'); - $to->emailstop = 0; - $to->maildisplay = true; - $to->mailformat = 1; - } - $from = \core_user::get_noreply_user(); - $subject = get_string('notifyonabort_subject', 'tool_dataflows', $this->dataflow->get('name')); + $this->log('Sending abort notification email.', [], Logger::NOTICE); + $context = [ + 'flowname' => $this->dataflow->get('name'), + 'run' => $this->run->get('id'), + 'reason' => isset($reason) ? $reason->getMessage() : '', + ]; + $message = get_string('notifyonabort_message', 'tool_dataflows', $context); + + // First try to match the email with a Moodle user. + $to = \core_user::get_user_by_email($email); + + // Otherwise send it with a dummy account. + if (!$to) { + $to = \core_user::get_noreply_user(); + $to->email = $email; + $to->firstname = $this->dataflow->get('name'); + $to->emailstop = 0; + $to->maildisplay = true; + $to->mailformat = 1; + } + $from = \core_user::get_noreply_user(); + $subject = get_string('notifyonabort_subject', 'tool_dataflows', $this->dataflow->get('name')); - email_to_user($to, $from, $subject, $message); + email_to_user($to, $from, $subject, $message); + } } } diff --git a/lang/en/tool_dataflows.php b/lang/en/tool_dataflows.php index 9c2b3d36..9060e3a0 100644 --- a/lang/en/tool_dataflows.php +++ b/lang/en/tool_dataflows.php @@ -102,7 +102,7 @@ $string['error:vars_not_object'] = 'Vars must form a YAML object (Define each var as <var>: <value>)'; $string['error:invalid_yaml'] = 'Invalid YAML (Try quoting your value(s)): {$a}'; $string['notifyonabort'] = 'Notify on abort'; -$string['notifyonabort_desc'] = 'Enter an email address to be notified on for dataflow aborts.'; +$string['notifyonabort_desc'] = 'Enter a list of email addresses separated by comma to be notified on for dataflow aborts.'; $string['notifyonabort_message'] = 'The dataflow {$a->flowname} was aborted on run {$a->run}. Reason: {$a->reason} diff --git a/version.php b/version.php index d3de945f..34c10bc9 100644 --- a/version.php +++ b/version.php @@ -25,8 +25,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024030201; -$plugin->release = 2024030201; +$plugin->version = 2024053100; +$plugin->release = 2024053100; $plugin->requires = 2022112800; // Our lowest supported Moodle (3.3.0). $plugin->supported = [400, 402]; // TODO $plugin->incompatible = ; // Available as of Moodle 3.9.0 or later.