Compare commits

...

2 Commits

Author SHA1 Message Date
Brendan Heywood 5756583514 Added admin notifications #185 2026-06-05 12:14:17 +10:00
Benjamin Walker 1fa22a4ea3 Display creation info on manage outage page 2026-01-25 13:44:11 +11:00
11 changed files with 243 additions and 7 deletions
+79
View File
@@ -84,6 +84,82 @@ class outagedb {
return new outage($outage);
}
/**
* Also sends all admins the event as a message
*
* @param $outage
* @param $event
*/
private static function notify($outage, $event) {
$admins = get_admins();
foreach ($admins as $admin) {
self::notify_user($outage, $event, $admin);
}
}
/**
* Send outage info to one user
*
* @param $outage outage
* @param $event event
* @param $to user object
*/
private static function notify_user($outage, $event, $to) {
global $SITE, $CFG;
$from = \core_user::get_user($event->userid);
if (!$from) {
return;
}
$fields = [
'site_shortname' => $SITE->shortname,
'site_fullname' => $SITE->fullname,
'site_wwwroot' => $CFG->wwwroot,
'outage_id' => $outage->id,
'outage_title' => $outage->get_title(),
'outage_desc' => clean_text($outage->get_description(), FORMAT_HTML),
'outage_start' => userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')),
'outage_stop' => userdate($outage->stoptime, get_string('datetimeformat', 'auth_outage')),
'outage_duration' => format_time($outage->get_duration_planned()),
'event_name' => $event->get_name(),
'event_desc' => $event->get_description(),
'event_link' => $event->get_url()->out(),
'from_name' => fullname($from),
'to_name' => fullname($to),
'prefs_link' => (new \moodle_url('/message/notificationpreferences.php'))->out(),
];
$message = new \core\message\message();
$message->component = 'auth_outage';
$message->name = 'updatenotify';
$message->userto = $to;
$message->subject = get_string('messagesubject', 'auth_outage', $fields);
$message->fullmessage = get_string('messagetext', 'auth_outage', $fields);
$message->fullmessagehtml = get_string('messagehtml', 'auth_outage', $fields);
$message->fullmessageformat = FORMAT_HTML;
$threadid = generate_email_messageid('outage' . $outage->id);
$message->userfrom = $from;
$message->userfrom->customheaders = [
"In-Reply-To: $threadid",
"References: $threadid",
"Thread-Topic: " . $message->subject,
"Thread-Index: $threadid",
];
$message->notification = 1;
message_send($message);
}
/**
* Saves an outage to the database.
*
@@ -114,6 +190,7 @@ class outagedb {
]);
$event->add_record_snapshot('auth_outage', (object)(array) $outage);
$event->trigger();
self::notify($outage, $event);
// Create calendar entry.
calendar::create($outage);
@@ -127,6 +204,7 @@ class outagedb {
$event->add_record_snapshot('auth_outage', (object)(array) $outage);
$event->trigger();
self::notify($outage, $event);
// Remove the createdby field so it does not get updated.
unset($outage->createdby);
@@ -170,6 +248,7 @@ class outagedb {
$event->add_record_snapshot('auth_outage', $previous);
$event->trigger();
self::notify($outage, $event);
// Delete it and remove from calendar.
$DB->delete_records('auth_outage', ['id' => $id]);
+11 -1
View File
@@ -28,13 +28,23 @@ use moodle_url;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outage_created extends base {
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventoutagecreated', 'auth_outage');
}
/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return "The user with the id '{$this->userid}' created outage {$this->other['id']} '{$this->other['title']}'";
return "The user with the id '{$this->userid}' scheduled outage {$this->other['id']} '{$this->other['title']}'";
}
/**
+10
View File
@@ -28,6 +28,16 @@ use moodle_url;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outage_deleted extends base {
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventoutagedeleted', 'auth_outage');
}
/**
* Returns non-localised event description with id's for admin use only.
*
+10
View File
@@ -28,6 +28,16 @@ use moodle_url;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outage_updated extends base {
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventoutageupdated', 'auth_outage');
}
/**
* Returns non-localised event description with id's for admin use only.
*
+35
View File
@@ -64,6 +64,41 @@ class base_table extends flexible_table {
$this->set_attribute('class', 'generaltable admintable');
}
/**
* Displays a user by their fullname with a link to a profile.
* @param int $userid
* @return string HTML link to user profile
*/
private function format_user(int $userid): string {
if ($userid == 0 || !$user = \core_user::get_user($userid)) {
return get_string('na', 'auth_outage');
}
$url = new moodle_url('/user/profile.php', ['id' => $userid]);
return html_writer::link($url, fullname($user));
}
/**
* Formats created by column.
* @param outage $outage
* @return string The user who created the outage.
*/
protected function format_created(outage $outage): string {
return $this->format_user($outage->createdby);
}
/**
* Formats modified by column.
* @param outage $outage
* @return string The user who last modiifed the outage and the last modified time.
*/
protected function format_modified(outage $outage): string {
$timestamp = html_writer::div(
userdate($outage->lastmodified, get_string('datetimeformat', 'auth_outage')),
'small text-muted'
);
return $this->format_user($outage->modifiedby) . $timestamp;
}
/**
* Create the action buttons HTML code for a specific outage.
* @param outage $outage The outage to generate the buttons.
+5 -1
View File
@@ -36,7 +36,7 @@ class history_table extends base_table {
public function __construct() {
parent::__construct();
$this->define_columns(['warning', 'starts', 'durationplanned', 'durationactual', 'title', 'actions']);
$this->define_columns(['warning', 'starts', 'duration', 'durationactual', 'title', 'created', 'modified', 'actions']);
$this->define_headers([
get_string('tableheaderwarnbefore', 'auth_outage'),
@@ -44,6 +44,8 @@ class history_table extends base_table {
get_string('tableheaderdurationplanned', 'auth_outage'),
get_string('tableheaderdurationactual', 'auth_outage'),
get_string('tableheadertitle', 'auth_outage'),
get_string('tableheadercreatedby', 'auth_outage'),
get_string('tableheadermodifiedby', 'auth_outage'),
get_string('actions'),
]);
@@ -64,6 +66,8 @@ class history_table extends base_table {
format_time($outage->get_duration_planned()),
$finished,
$outage->get_title(),
$this->format_created($outage),
$this->format_modified($outage),
$this->create_data_buttons($outage, false),
]);
}
+5 -1
View File
@@ -38,13 +38,15 @@ class planned_table extends base_table {
public function __construct() {
parent::__construct();
$this->define_columns(['warning', 'starts', 'duration', 'title', 'actions']);
$this->define_columns(['warning', 'starts', 'duration', 'title', 'created', 'modified', 'actions']);
$this->define_headers([
get_string('tableheaderwarnbefore', 'auth_outage'),
get_string('tableheaderstarttime', 'auth_outage'),
get_string('tableheaderduration', 'auth_outage'),
get_string('tableheadertitle', 'auth_outage'),
get_string('tableheadercreatedby', 'auth_outage'),
get_string('tableheadermodifiedby', 'auth_outage'),
get_string('actions'),
]);
@@ -68,6 +70,8 @@ class planned_table extends base_table {
self::create_starttime_string($outage->starttime),
format_time($outage->get_duration_planned()),
$title,
$this->format_created($outage),
$this->format_modified($outage),
$this->create_data_buttons($outage, true),
]);
}
+9
View File
@@ -34,4 +34,13 @@ $capabilities = [
'user' => CAP_ALLOW,
],
],
'auth/outage:updatenotify' => [
'captype' => 'write',
'riskbitmask' => RISK_XSS,
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => [
'manager' => CAP_ALLOW,
]
],
];
+32
View File
@@ -0,0 +1,32 @@
<?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/>.
/**
* Defines message providers for outage
*
* @package auth_outage
* @copyright 2020 Brendan Heywood <brendan@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$messageproviders = [
'updatenotify' => [
'capability' => 'auth/outage:updatenotify',
]
];
+45 -1
View File
@@ -100,6 +100,9 @@ $string['defaultwarningduration'] = 'Warning duration';
$string['defaultwarningdurationdescription'] = 'Default warning time (in minutes) for outages.';
$string['description'] = 'Public Description';
$string['description_help'] = 'A full description of the outage, publicly visible by all users.';
$string['eventoutagecreated'] = 'Outage scheduled';
$string['eventoutagedeleted'] = 'Outage cancelled';
$string['eventoutageupdated'] = 'Outage updated';
$string['finish'] = 'Finish';
$string['info15secondsbefore'] = '15 seconds before';
$string['infoendofoutage'] = 'end of outage';
@@ -121,9 +124,48 @@ $string['messageoutageongoing'] = 'Back online at {$a->stop}.';
$string['messageoutagewarning'] = 'Shutting down in {{countdown}}';
$string['metadata'] = 'Outage metadata';
$string['metadata_help'] = 'Data here will be output in the outage page as a meta tag in the header of the outage page.';
$string['messageprovider:updatenotify'] = 'Changes to planned outages';
$string['messagesubject'] = '[{$a->site_shortname}] {$a->event_name} #{$a->outage_id}: {$a->outage_start}';
$string['messagetext'] = '{$a->event_name}
{$a->site_fullname} ({$a->site_wwwroot})
{$a->event_link}
ID: {$a->outage_id}
NAME: {$a->outage_title}
START: {$a->outage_start}
DURATION: {$a->outage_duration}
DESCRIPTION:
{$a->outage_desc}
--
To unsubscribe visit:
{$a->prefs_link}';
$string['messagehtml'] = '
<h3>{$a->event_name}</h3>
<p>{$a->site_fullname} (<a href="{$a->site_wwwroot}">{$a->site_wwwroot}</a>)</p>
<p><a href="{$a->event_link}">{$a->event_link}</a></p>
<h4>Name:</h4>
<p>{$a->outage_title}</p>
<h4>Start:</h4>
<p>{$a->outage_start}</p>
<h4>Duration:</h4>
<p>{$a->outage_duration}</p>
<h4>Description:</h4>
<p>{$a->outage_desc}</p>
<hr>
<p>To unsubscribe visit:<br>
<a href="{$a->prefs_link}">{$a->prefs_link}</a></p>
';
$string['na'] = 'n/a';
$string['notfound'] = 'No outages found.';
$string['outage:updatenotify'] = '';
$string['outage:updatenotify'] = 'Receive outage update notifications';
$string['outage:viewinfo'] = 'View outage info';
$string['outageclone'] = 'Clone outage';
$string['outageclonecrumb'] = 'Clone';
@@ -150,9 +192,11 @@ $string['settingssectionplugin'] = 'Plugin Configuration';
$string['settingssectionplugindescription'] = 'General outage management plugin settings.';
$string['starttime'] = 'Start date and time';
$string['starttime_help'] = 'At which date and time the outage starts, preventing general access to the system.';
$string['tableheadercreatedby'] = 'Created by';
$string['tableheaderduration'] = 'Duration';
$string['tableheaderdurationactual'] = 'Actual duration';
$string['tableheaderdurationplanned'] = 'Planned duration';
$string['tableheadermodifiedby'] = 'Last modified by';
$string['tableheaderstartedtime'] = 'Started on';
$string['tableheaderstarttime'] = 'Starts on';
$string['tableheadertitle'] = 'Title';
+2 -3
View File
@@ -28,8 +28,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = "auth_outage";
$plugin->version = 2026011302; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2026011302; // Human-readable release information.
$plugin->version = 2026011304; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2026011304; // Human-readable release information.
$plugin->requires = 2025100600; // Moodle 5.1.
$plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments!
$plugin->supported = [501, 501]; // A range of branch numbers of supported moodle versions.