From 872cd93ccbdbdb1d2186ca3f32c6c2af7c415176 Mon Sep 17 00:00:00 2001 From: Benjamin Walker Date: Thu, 28 Nov 2024 16:13:26 +1000 Subject: [PATCH] Add bounce types to bounces table --- .../local/entities/notification_log.php | 192 ++++++++++++++++++ .../local/systemreports/email_bounces.php | 17 ++ lang/en/tool_emailutils.php | 3 + 3 files changed, 212 insertions(+) create mode 100644 classes/reportbuilder/local/entities/notification_log.php diff --git a/classes/reportbuilder/local/entities/notification_log.php b/classes/reportbuilder/local/entities/notification_log.php new file mode 100644 index 0000000..1f2fc29 --- /dev/null +++ b/classes/reportbuilder/local/entities/notification_log.php @@ -0,0 +1,192 @@ +. + +namespace tool_emailutils\reportbuilder\local\entities; + +use lang_string; +use core_reportbuilder\local\entities\base; +use core_reportbuilder\local\filters\date; +use core_reportbuilder\local\filters\text; +use core_reportbuilder\local\helpers\format; +use core_reportbuilder\local\report\column; +use core_reportbuilder\local\report\filter; + +/** + * Notification log list entity class class implementation. + * + * Defines all the columns and filters that can be added to reports that use this entity. + * + * @package tool_emailutils + * @author Benjamin Walker + * @copyright Catalyst IT 2024 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ +class notification_log extends base { + + /** + * Database tables that this entity uses + * + * @return string[] + */ + protected function get_default_tables(): array { + return [ + 'tool_emailutils_log', + ]; + } + + /** + * The default title for this entity + * + * @return lang_string + */ + protected function get_default_entity_title(): lang_string { + return new lang_string('notificationlog', 'tool_emailutils'); + } + + /** + * Initialize the entity + * + * @return base + */ + public function initialise(): base { + $columns = $this->get_all_columns(); + foreach ($columns as $column) { + $this->add_column($column); + } + + $filters = $this->get_all_filters(); + foreach ($filters as $filter) { + $this->add_filter($filter); + $this->add_condition($filter); + } + + return $this; + } + + /** + * Returns list of all available columns + * + * @return column[] + */ + protected function get_all_columns(): array { + $tablealias = $this->get_table_alias('tool_emailutils_log'); + + // Email column. + $columns[] = (new column( + 'email', + new lang_string('email'), + $this->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_TEXT) + ->add_fields("{$tablealias}.email") + ->set_is_sortable(true); + + // Type column. + $columns[] = (new column( + 'type', + new lang_string('type', 'tool_emailutils'), + $this->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_TEXT) + ->add_fields("{$tablealias}.type") + ->set_is_sortable(true); + + // Subtypes column. + $columns[] = (new column( + 'subtypes', + new lang_string('subtypes', 'tool_emailutils'), + $this->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_TEXT) + ->add_fields("{$tablealias}.subtypes") + ->set_is_sortable(true); + + // Time column. + $columns[] = (new column( + 'time', + new lang_string('time'), + $this->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_TIMESTAMP) + ->add_fields("{$tablealias}.time") + ->set_is_sortable(true) + ->add_callback([format::class, 'userdate'], get_string('strftimedatetimeshortaccurate', 'core_langconfig')); + + return $columns; + } + + /** + * Return list of all available filters + * + * @return filter[] + */ + protected function get_all_filters(): array { + $tablealias = $this->get_table_alias('tool_emailutils_log'); + + // Email filter. + $filters[] = (new filter( + text::class, + 'email', + new lang_string('email'), + $this->get_entity_name(), + "{$tablealias}.email" + )) + ->add_joins($this->get_joins()); + + // Type filter. + $filters[] = (new filter( + text::class, + 'type', + new lang_string('type', 'tool_emailutils'), + $this->get_entity_name(), + "{$tablealias}.type" + )) + ->add_joins($this->get_joins()); + + // Subtypes filter. + $filters[] = (new filter( + text::class, + 'subtypes', + new lang_string('subtypes', 'tool_emailutils'), + $this->get_entity_name(), + "{$tablealias}.subtypes" + )) + ->add_joins($this->get_joins()); + + // Time filter. + $filters[] = (new filter( + date::class, + 'time', + new lang_string('time'), + $this->get_entity_name(), + "{$tablealias}.time" + )) + ->add_joins($this->get_joins()) + ->set_limited_operators([ + date::DATE_ANY, + date::DATE_RANGE, + date::DATE_PREVIOUS, + date::DATE_CURRENT, + ]); + + return $filters; + } +} diff --git a/classes/reportbuilder/local/systemreports/email_bounces.php b/classes/reportbuilder/local/systemreports/email_bounces.php index 6854519..b5c3b0c 100644 --- a/classes/reportbuilder/local/systemreports/email_bounces.php +++ b/classes/reportbuilder/local/systemreports/email_bounces.php @@ -18,6 +18,7 @@ use context_system; use tool_emailutils\reportbuilder\local\entities\email_bounce; +use tool_emailutils\reportbuilder\local\entities\notification_log; use core_reportbuilder\system_report; use core_reportbuilder\local\entities\user; use core_reportbuilder\local\report\action; @@ -62,6 +63,21 @@ protected function initialise(): void { ->add_join("LEFT JOIN {user} {$entityuseralias} ON {$entityuseralias}.id = {$entitymainalias}.userid") ); + // Join with the latest entry in the notification log for each user. + $entitylog = new notification_log(); + $entitylogalias = $entitylog->get_table_alias('tool_emailutils_log'); + $this->add_entity($entitylog + ->add_join("LEFT JOIN ( + SELECT l1.* + FROM {tool_emailutils_log} l1 + WHERE l1.id = ( + SELECT MAX(l2.id) + FROM {tool_emailutils_log} l2 + WHERE l2.email = l1.email + ) + ) {$entitylogalias} ON {$entitylogalias}.email = {$entityuseralias}.email") + ); + // Now we can call our helper methods to add the content we want to include in the report. $this->add_columns(); $this->add_filters(); @@ -94,6 +110,7 @@ protected function add_columns(): void { 'email_bounce:bounces', 'email_bounce:send', 'email_bounce:ratio', + 'notification_log:subtypes', ]; $this->add_columns_from_entities($columns); diff --git a/lang/en/tool_emailutils.php b/lang/en/tool_emailutils.php index 14a0162..0f2a6b5 100644 --- a/lang/en/tool_emailutils.php +++ b/lang/en/tool_emailutils.php @@ -84,6 +84,7 @@ $string['mxrecords'] = 'MX records'; $string['mxtoolbox'] = 'MXtoolbox links'; $string['not_implemented'] = 'Not implemented yet. Search the user report for emails ending with ".b.invalid" and ".c.invalid".'; +$string['notificationlog'] = 'SNS notification log'; $string['password'] = 'Password'; $string['password_help'] = 'HTTP Basic Auth Password - Leave empty if you\'re not changing the password'; $string['pluginname'] = 'Email utilities'; @@ -114,9 +115,11 @@ $string['selectornotblank'] = 'Selector cannot be empty'; $string['sendcount'] = 'Send count'; $string['settings'] = 'AWS SES settings'; +$string['subtypes'] = 'Reason'; $string['suppressionreason'] = 'Reason'; $string['suppressioncreated'] = 'Created at'; $string['task_update_suppression_list'] = 'Update email suppression list'; +$string['type'] = 'Type'; $string['username'] = 'Username'; $string['username_help'] = 'HTTP Basic Auth Username'; $string['userdomains'] = 'Most common user email domains';