Skip to content

Commit 872cd93

Browse files
committed
Add bounce types to bounces table
1 parent 9d81c12 commit 872cd93

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tool_emailutils\reportbuilder\local\entities;
18+
19+
use lang_string;
20+
use core_reportbuilder\local\entities\base;
21+
use core_reportbuilder\local\filters\date;
22+
use core_reportbuilder\local\filters\text;
23+
use core_reportbuilder\local\helpers\format;
24+
use core_reportbuilder\local\report\column;
25+
use core_reportbuilder\local\report\filter;
26+
27+
/**
28+
* Notification log list entity class class implementation.
29+
*
30+
* Defines all the columns and filters that can be added to reports that use this entity.
31+
*
32+
* @package tool_emailutils
33+
* @author Benjamin Walker <[email protected]>
34+
* @copyright Catalyst IT 2024
35+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36+
*
37+
*/
38+
class notification_log extends base {
39+
40+
/**
41+
* Database tables that this entity uses
42+
*
43+
* @return string[]
44+
*/
45+
protected function get_default_tables(): array {
46+
return [
47+
'tool_emailutils_log',
48+
];
49+
}
50+
51+
/**
52+
* The default title for this entity
53+
*
54+
* @return lang_string
55+
*/
56+
protected function get_default_entity_title(): lang_string {
57+
return new lang_string('notificationlog', 'tool_emailutils');
58+
}
59+
60+
/**
61+
* Initialize the entity
62+
*
63+
* @return base
64+
*/
65+
public function initialise(): base {
66+
$columns = $this->get_all_columns();
67+
foreach ($columns as $column) {
68+
$this->add_column($column);
69+
}
70+
71+
$filters = $this->get_all_filters();
72+
foreach ($filters as $filter) {
73+
$this->add_filter($filter);
74+
$this->add_condition($filter);
75+
}
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* Returns list of all available columns
82+
*
83+
* @return column[]
84+
*/
85+
protected function get_all_columns(): array {
86+
$tablealias = $this->get_table_alias('tool_emailutils_log');
87+
88+
// Email column.
89+
$columns[] = (new column(
90+
'email',
91+
new lang_string('email'),
92+
$this->get_entity_name()
93+
))
94+
->add_joins($this->get_joins())
95+
->set_type(column::TYPE_TEXT)
96+
->add_fields("{$tablealias}.email")
97+
->set_is_sortable(true);
98+
99+
// Type column.
100+
$columns[] = (new column(
101+
'type',
102+
new lang_string('type', 'tool_emailutils'),
103+
$this->get_entity_name()
104+
))
105+
->add_joins($this->get_joins())
106+
->set_type(column::TYPE_TEXT)
107+
->add_fields("{$tablealias}.type")
108+
->set_is_sortable(true);
109+
110+
// Subtypes column.
111+
$columns[] = (new column(
112+
'subtypes',
113+
new lang_string('subtypes', 'tool_emailutils'),
114+
$this->get_entity_name()
115+
))
116+
->add_joins($this->get_joins())
117+
->set_type(column::TYPE_TEXT)
118+
->add_fields("{$tablealias}.subtypes")
119+
->set_is_sortable(true);
120+
121+
// Time column.
122+
$columns[] = (new column(
123+
'time',
124+
new lang_string('time'),
125+
$this->get_entity_name()
126+
))
127+
->add_joins($this->get_joins())
128+
->set_type(column::TYPE_TIMESTAMP)
129+
->add_fields("{$tablealias}.time")
130+
->set_is_sortable(true)
131+
->add_callback([format::class, 'userdate'], get_string('strftimedatetimeshortaccurate', 'core_langconfig'));
132+
133+
return $columns;
134+
}
135+
136+
/**
137+
* Return list of all available filters
138+
*
139+
* @return filter[]
140+
*/
141+
protected function get_all_filters(): array {
142+
$tablealias = $this->get_table_alias('tool_emailutils_log');
143+
144+
// Email filter.
145+
$filters[] = (new filter(
146+
text::class,
147+
'email',
148+
new lang_string('email'),
149+
$this->get_entity_name(),
150+
"{$tablealias}.email"
151+
))
152+
->add_joins($this->get_joins());
153+
154+
// Type filter.
155+
$filters[] = (new filter(
156+
text::class,
157+
'type',
158+
new lang_string('type', 'tool_emailutils'),
159+
$this->get_entity_name(),
160+
"{$tablealias}.type"
161+
))
162+
->add_joins($this->get_joins());
163+
164+
// Subtypes filter.
165+
$filters[] = (new filter(
166+
text::class,
167+
'subtypes',
168+
new lang_string('subtypes', 'tool_emailutils'),
169+
$this->get_entity_name(),
170+
"{$tablealias}.subtypes"
171+
))
172+
->add_joins($this->get_joins());
173+
174+
// Time filter.
175+
$filters[] = (new filter(
176+
date::class,
177+
'time',
178+
new lang_string('time'),
179+
$this->get_entity_name(),
180+
"{$tablealias}.time"
181+
))
182+
->add_joins($this->get_joins())
183+
->set_limited_operators([
184+
date::DATE_ANY,
185+
date::DATE_RANGE,
186+
date::DATE_PREVIOUS,
187+
date::DATE_CURRENT,
188+
]);
189+
190+
return $filters;
191+
}
192+
}

classes/reportbuilder/local/systemreports/email_bounces.php

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use context_system;
2020
use tool_emailutils\reportbuilder\local\entities\email_bounce;
21+
use tool_emailutils\reportbuilder\local\entities\notification_log;
2122
use core_reportbuilder\system_report;
2223
use core_reportbuilder\local\entities\user;
2324
use core_reportbuilder\local\report\action;
@@ -62,6 +63,21 @@ protected function initialise(): void {
6263
->add_join("LEFT JOIN {user} {$entityuseralias} ON {$entityuseralias}.id = {$entitymainalias}.userid")
6364
);
6465

66+
// Join with the latest entry in the notification log for each user.
67+
$entitylog = new notification_log();
68+
$entitylogalias = $entitylog->get_table_alias('tool_emailutils_log');
69+
$this->add_entity($entitylog
70+
->add_join("LEFT JOIN (
71+
SELECT l1.*
72+
FROM {tool_emailutils_log} l1
73+
WHERE l1.id = (
74+
SELECT MAX(l2.id)
75+
FROM {tool_emailutils_log} l2
76+
WHERE l2.email = l1.email
77+
)
78+
) {$entitylogalias} ON {$entitylogalias}.email = {$entityuseralias}.email")
79+
);
80+
6581
// Now we can call our helper methods to add the content we want to include in the report.
6682
$this->add_columns();
6783
$this->add_filters();
@@ -94,6 +110,7 @@ protected function add_columns(): void {
94110
'email_bounce:bounces',
95111
'email_bounce:send',
96112
'email_bounce:ratio',
113+
'notification_log:subtypes',
97114
];
98115

99116
$this->add_columns_from_entities($columns);

lang/en/tool_emailutils.php

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
$string['mxrecords'] = 'MX records';
8585
$string['mxtoolbox'] = 'MXtoolbox links';
8686
$string['not_implemented'] = 'Not implemented yet. Search the user report for emails ending with ".b.invalid" and ".c.invalid".';
87+
$string['notificationlog'] = 'SNS notification log';
8788
$string['password'] = 'Password';
8889
$string['password_help'] = 'HTTP Basic Auth Password - Leave empty if you\'re not changing the password';
8990
$string['pluginname'] = 'Email utilities';
@@ -114,9 +115,11 @@
114115
$string['selectornotblank'] = 'Selector cannot be empty';
115116
$string['sendcount'] = 'Send count';
116117
$string['settings'] = 'AWS SES settings';
118+
$string['subtypes'] = 'Reason';
117119
$string['suppressionreason'] = 'Reason';
118120
$string['suppressioncreated'] = 'Created at';
119121
$string['task_update_suppression_list'] = 'Update email suppression list';
122+
$string['type'] = 'Type';
120123
$string['username'] = 'Username';
121124
$string['username_help'] = 'HTTP Basic Auth Username';
122125
$string['userdomains'] = 'Most common user email domains';

0 commit comments

Comments
 (0)