From c6cee3cd4f4667938a63af721b80e62391d4952f Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Sat, 15 Feb 2020 00:56:33 +1100 Subject: [PATCH] Added admin notifications #185 --- classes/dml/outagedb.php | 76 ++++++++++++++++++++++++++++++++ classes/event/outage_created.php | 12 ++++- classes/event/outage_deleted.php | 10 +++++ classes/event/outage_updated.php | 10 +++++ db/access.php | 37 ++++++++++++++++ db/messages.php | 32 ++++++++++++++ lang/en/auth_outage.php | 42 ++++++++++++++++++ version.php | 4 +- 8 files changed, 220 insertions(+), 3 deletions(-) create mode 100644 db/access.php create mode 100644 db/messages.php diff --git a/classes/dml/outagedb.php b/classes/dml/outagedb.php index 963ac3f..bb046e9 100644 --- a/classes/dml/outagedb.php +++ b/classes/dml/outagedb.php @@ -96,6 +96,79 @@ 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); + $fields = [ + 'site_shortname' => $SITE->shortname, + 'site_fullname' => $SITE->fullname, + 'site_wwwroot' => $CFG->wwwroot, + + 'outage_id' => $outage->id, + 'outage_title' => $outage->get_title(), + 'outage_desc' => $outage->get_description(), + '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'; + $messageid = message_send($message); + + } + /** * Saves an outage to the database. * @@ -126,6 +199,7 @@ class outagedb { ]); $event->add_record_snapshot('auth_outage', (object)(array) $outage); $event->trigger(); + self::notify($outage, $event); // Create calendar entry. calendar::create($outage); @@ -140,6 +214,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); @@ -183,6 +258,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]); diff --git a/classes/event/outage_created.php b/classes/event/outage_created.php index d2593e3..7840cd1 100644 --- a/classes/event/outage_created.php +++ b/classes/event/outage_created.php @@ -39,13 +39,23 @@ defined('MOODLE_INTERNAL') || die(); * @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']}'"; } /** diff --git a/classes/event/outage_deleted.php b/classes/event/outage_deleted.php index 663c7f4..f01cff9 100644 --- a/classes/event/outage_deleted.php +++ b/classes/event/outage_deleted.php @@ -38,6 +38,16 @@ defined('MOODLE_INTERNAL') || die(); * @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. * diff --git a/classes/event/outage_updated.php b/classes/event/outage_updated.php index 06feda7..e0047d6 100644 --- a/classes/event/outage_updated.php +++ b/classes/event/outage_updated.php @@ -39,6 +39,16 @@ defined('MOODLE_INTERNAL') || die(); * @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. * diff --git a/db/access.php b/db/access.php new file mode 100644 index 0000000..9685d4f --- /dev/null +++ b/db/access.php @@ -0,0 +1,37 @@ +. + +/** + * Defines capabilities + * + * @package auth_outage + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$capabilities = [ + 'auth/outage:updatenotify' => [ + 'captype' => 'write', + 'riskbitmask' => RISK_XSS, + 'contextlevel' => CONTEXT_SYSTEM, + 'archetypes' => [ + 'manager' => CAP_ALLOW, + ] + ], +]; + diff --git a/db/messages.php b/db/messages.php new file mode 100644 index 0000000..91dd480 --- /dev/null +++ b/db/messages.php @@ -0,0 +1,32 @@ +. + +/** + * Defines message providers for outage + * + * @package auth_outage + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$messageproviders = [ + 'updatenotify' => [ + 'capability' => 'auth/outage:updatenotify', + ] +]; + diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 98fc5d1..c1dbcf4 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -88,6 +88,9 @@ $string['defaultdescriptiondescription'] = 'Default warning message for outages. $string['defaultdescriptionvalue'] = 'There is an scheduled maintenance from {{start}} to {{stop}} and our system will not be available during that time.'; $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'; @@ -108,6 +111,45 @@ $string['messageoutagebackonline'] = 'We are back online!'; $string['messageoutagebackonlinedescription'] = 'You may resume browsing safely.'; $string['messageoutageongoing'] = 'Back online at {$a->stop}.'; $string['messageoutagewarning'] = 'Shutting down in {{countdown}}'; +$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'] = ' +

{$a->event_name}

+ +

{$a->site_fullname} ({$a->site_wwwroot})

+ +

{$a->event_link}

+ +

Name:

+

{$a->outage_title}

+

Start:

+

{$a->outage_start}

+

Duration:

+

{$a->outage_duration}

+

Description:

+

{$a->outage_desc}

+ +
+

To unsubscribe visit:
+{$a->prefs_link}

+'; $string['na'] = 'n/a'; $string['notfound'] = 'No outages found.'; $string['outageedit'] = 'Edit outage'; diff --git a/version.php b/version.php index b07af31..2a572fd 100644 --- a/version.php +++ b/version.php @@ -28,7 +28,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = "auth_outage"; -$plugin->version = 2020021300; // The current plugin version (Date: YYYYMMDDXX). -$plugin->release = '1.0.11'; // Human-readable release information. +$plugin->version = 2020021500; // The current plugin version (Date: YYYYMMDDXX). +$plugin->release = 2020021500; // Human-readable release information. $plugin->requires = 2017051500; // Requires 3.3 and higher. $plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments!