Added admin notifications #185

This commit is contained in:
Brendan Heywood
2020-02-15 00:56:33 +11:00
parent c3a2faef08
commit c6cee3cd4f
8 changed files with 220 additions and 3 deletions

View File

@@ -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]);

View File

@@ -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']}'";
}
/**

View File

@@ -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.
*

View File

@@ -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.
*