Skip to content

Commit

Permalink
Add bounce types to bounces table
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Nov 28, 2024
1 parent 9d81c12 commit 872cd93
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
192 changes: 192 additions & 0 deletions classes/reportbuilder/local/entities/notification_log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?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/>.

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 <[email protected]>
* @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;
}
}
17 changes: 17 additions & 0 deletions classes/reportbuilder/local/systemreports/email_bounces.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions lang/en/tool_emailutils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 872cd93

Please sign in to comment.