MDL-79131 core: Hook listener callback for failed_task_max_delay

This commit is contained in:
raortegar 2024-01-26 15:38:49 +01:00
parent 10ecfa731d
commit 5488908538
3 changed files with 79 additions and 0 deletions

View File

@ -893,6 +893,11 @@ $string['explanationdigitalminor'] = 'This information is required to determine
$string['extendperiod'] = 'Extended period';
$string['favourites'] = 'Starred';
$string['failedloginattempts'] = '{$a->attempts} failed logins since your last login';
$string['failedtaskbody'] = '<p>Hi {$a->firstname},</p>
<p>The task <b>{$a->taskname}</b> has failed multiple times and requires attention.</p>
<p><a href="{$a->link}">See task</a></p>';
$string['failedtasksubject'] = 'Task failed: {$a}';
$string['failedtaskcontexturlname'] = 'Status report';
$string['feedback'] = 'Feedback';
$string['file'] = 'File';
$string['fileexists'] = 'There is already a file called {$a}';

View File

@ -0,0 +1,70 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\task;
use core\hook\task\after_failed_task_max_delay;
use core_user;
use stdClass;
/**
* Hook listener callbacks for tasks in core
*
* @package core
* @category task
* @copyright 2024 Raquel Ortega <raquel.ortega@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class failed_task_callbacks {
/**
* Callback to send a notification when the max fail delay of a task has been reached.
*
* @param after_failed_task_max_delay $hook
*/
public static function send_failed_task_max_delay_message(after_failed_task_max_delay $hook): void {
$task = $hook->get_task();
$admins = get_admins();
if (empty($admins)) {
return;
}
foreach ($admins as $admin) {
$a = new stdClass();
$a->firstname = $admin->firstname;
$a->taskname = $task->get_name();
$a->link = new \moodle_url('/report/status/index.php', ['detail' => 'tool_task_maxfaildelay']);
$messagetxt = get_string('failedtaskbody', 'moodle', $a);
// Create message.
$message = new \core\message\message();
$message->component = 'moodle';
$message->name = 'failedtaskmaxdelay';
$message->userfrom = core_user::get_noreply_user();
$message->userto = $admin;
$message->subject = get_string('failedtasksubject', 'moodle', $task->get_name());
$message->fullmessage = html_to_text($messagetxt);
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = text_to_html($messagetxt);
$message->smallmessage = get_string('failedtasksubject', 'moodle', $task->get_name());
$message->notification = 1;
$message->contexturl = (
new \moodle_url('/report/status/index.php', ['detail' => 'tool_task_maxfaildelay']))->out(false);
$message->contexturlname = get_string('failedtaskcontexturlname', 'moodle');
// Actually send the message.
message_send($message);
}
}
}

View File

@ -93,4 +93,8 @@ $callbacks = [
'hook' => \core_enrol\hook\before_user_enrolment_remove::class,
'callback' => \core_communication\hook_listener::class . '::remove_communication_membership_for_unenrolled_user',
],
[
'hook' => \core\hook\task\after_failed_task_max_delay::class,
'callback' => core\task\failed_task_callbacks::class . '::send_failed_task_max_delay_message',
],
];