Skip to content

Commit

Permalink
feat: allow multiple emails in Notify on abort to fix #881
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed May 31, 2024
1 parent eb25aa3 commit a926645
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
55 changes: 30 additions & 25 deletions classes/local/execution/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a926645

Please sign in to comment.