From 101ca4d21b4c601fa8007ca30f3fde691b6d3241 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Wed, 23 Feb 2022 12:32:57 +1100 Subject: [PATCH] MDL-73939 tool_moodlenet: Send enable notification via an adhoc task --- .../classes/task/send_enable_notification.php | 47 +++++++++++++++++++ admin/tool/moodlenet/db/upgrade.php | 20 ++------ 2 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 admin/tool/moodlenet/classes/task/send_enable_notification.php diff --git a/admin/tool/moodlenet/classes/task/send_enable_notification.php b/admin/tool/moodlenet/classes/task/send_enable_notification.php new file mode 100644 index 00000000000..97255473b9f --- /dev/null +++ b/admin/tool/moodlenet/classes/task/send_enable_notification.php @@ -0,0 +1,47 @@ +. + +declare(strict_types=1); + +namespace tool_moodlenet\task; + +/** + * Ad-hoc task to send the notification to admin stating MoodleNet is automatically enabled after upgrade. + * + * @package tool_moodlenet + * @copyright 2022 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class send_enable_notification extends \core\task\adhoc_task { + public function execute(): void { + $message = new \core\message\message(); + $message->component = 'moodle'; + $message->name = 'notices'; + $message->userfrom = \core_user::get_noreply_user(); + $message->userto = get_admin(); + $message->notification = 1; + $message->contexturl = (new \moodle_url('/admin/settings.php', + ['section' => 'optionalsubsystems'], 'admin-enablemoodlenet'))->out(false); + $message->contexturlname = get_string('advancedfeatures', 'admin'); + $message->subject = get_string('autoenablenotification_subject', 'tool_moodlenet'); + $message->fullmessageformat = FORMAT_HTML; + $message->fullmessagehtml = get_string('autoenablenotification', 'tool_moodlenet', (object) [ + 'settingslink' => (new \moodle_url('/admin/settings.php', ['section' => 'tool_moodlenet']))->out(false), + ]); + $message->smallmessage = strip_tags($message->fullmessagehtml); + message_send($message); + } +} diff --git a/admin/tool/moodlenet/db/upgrade.php b/admin/tool/moodlenet/db/upgrade.php index 5090778c3bf..289e4c11e4d 100644 --- a/admin/tool/moodlenet/db/upgrade.php +++ b/admin/tool/moodlenet/db/upgrade.php @@ -137,23 +137,9 @@ function xmldb_tool_moodlenet_upgrade(int $oldversion) { set_config('enablemoodlenet', '1', 'tool_moodlenet'); set_config('activitychooseractivefooter', 'tool_moodlenet'); - // Send notification. - $message = new \core\message\message(); - $message->component = 'moodle'; - $message->name = 'notices'; - $message->userfrom = \core_user::get_noreply_user(); - $message->userto = get_admin(); - $message->notification = 1; - $message->contexturl = (new moodle_url('/admin/settings.php', - ['section' => 'optionalsubsystems'], 'admin-enablemoodlenet'))->out(false); - $message->contexturlname = get_string('advancedfeatures', 'admin'); - $message->subject = get_string('autoenablenotification_subject', 'tool_moodlenet'); - $message->fullmessageformat = FORMAT_HTML; - $message->fullmessagehtml = get_string('autoenablenotification', 'tool_moodlenet', (object) [ - 'settingslink' => (new moodle_url('/admin/settings.php', ['section' => 'tool_moodlenet']))->out(false), - ]); - $message->smallmessage = strip_tags($message->fullmessagehtml); - message_send($message); + // Use an adhoc task to send a notification to admin stating MoodleNet is automatically enabled after upgrade. + $notificationtask = new tool_moodlenet\task\send_enable_notification(); + core\task\manager::queue_adhoc_task($notificationtask); } upgrade_plugin_savepoint(true, 2022021600, 'tool', 'moodlenet');