Skip to content

Commit 4c0760d

Browse files
bwalkerlbrendanheywood
authored andcommitted
Change suppression list download to report #55
1 parent 7e49f1a commit 4c0760d

11 files changed

+345
-117
lines changed

aws/suppression_list.php

+34-30
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,49 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Email suppression list download page.
18+
* Email suppression list report page.
1919
*
2020
* @package tool_emailutils
2121
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
* @author Waleed ul hassan <[email protected]>
2424
*/
2525

26-
require_once(__DIR__ . '/../../../../config.php');
27-
require_once($CFG->libdir . '/adminlib.php');
26+
use core_reportbuilder\system_report_factory;
27+
use tool_emailutils\reportbuilder\local\systemreports\suppression_list;
2828

29-
// Ensure the user is logged in and has the necessary permissions.
30-
require_login();
31-
require_capability('moodle/site:config', context_system::instance());
29+
require(__DIR__.'/../../../../config.php');
30+
require_once($CFG->libdir.'/adminlib.php');
3231

33-
$action = optional_param('action', '', PARAM_ALPHA);
32+
admin_externalpage_setup('tool_emailutils_bounces', '', [], '', ['pagelayout' => 'report']);
3433

35-
if ($action === 'download') {
36-
$content = \tool_emailutils\suppression_list::generate_csv();
37-
$filename = 'email_suppression_list_' . date('Y-m-d') . '.csv';
38-
header('Content-Type: text/csv');
39-
header('Content-Disposition: attachment; filename="' . $filename . '"');
40-
header('Content-Length: ' . strlen($content));
41-
echo $content;
42-
exit;
34+
echo $OUTPUT->header();
35+
echo $OUTPUT->heading(get_string('aws_suppressionlist', 'tool_emailutils'));
36+
37+
// Add warnings if the suppression list hasn't been set up or run recently.
38+
if (empty(get_config('tool_emailutils', 'enable_suppression_list'))) {
39+
echo $OUTPUT->notification(get_string('aws_suppressionlist_disabled', 'tool_emailutils'), 'warning');
4340
} else {
44-
// Display the download page.
45-
admin_externalpage_setup('toolemailutilssuppressionlist');
46-
echo $OUTPUT->header();
47-
echo $OUTPUT->heading(get_string('suppressionlist', 'tool_emailutils'));
48-
49-
echo html_writer::start_tag('div', ['class' => 'suppressionlist-download']);
50-
echo html_writer::tag('p', get_string('suppressionlistdesc', 'tool_emailutils'));
51-
echo html_writer::link(
52-
new moodle_url('/admin/tool/emailutils/aws/suppression_list.php', ['action' => 'download']),
53-
get_string('downloadsuppressionlist', 'tool_emailutils'),
54-
['class' => 'btn btn-primary']
55-
);
56-
echo html_writer::end_tag('div');
57-
58-
echo $OUTPUT->footer();
41+
// Check if the update task has run recently.
42+
$task = \core\task\manager::get_scheduled_task('\tool_emailutils\task\update_suppression_list');
43+
if ($task && $task->is_enabled()) {
44+
$lastrun = $task->get_last_run_time();
45+
if (empty($lastrun)) {
46+
echo $OUTPUT->notification(get_string('aws_suppressionlist_tasknever', 'tool_emailutils'), 'warning');
47+
} else if ($lastrun < (time() - DAYSECS)) {
48+
echo $OUTPUT->notification(get_string('aws_suppressionlist_taskupdated', 'tool_emailutils', format_time(time() - $lastrun)), 'warning');
49+
} else {
50+
// Always show the last update time as info.
51+
echo $OUTPUT->notification(get_string('aws_suppressionlist_taskupdated', 'tool_emailutils', format_time(time() - $lastrun)), 'info');
52+
}
53+
} else {
54+
echo $OUTPUT->notification(get_string('aws_suppressionlist_taskdisabled', 'tool_emailutils'), 'warning');
55+
}
5956
}
57+
58+
$report = system_report_factory::create(suppression_list::class, context_system::instance());
59+
60+
echo $report->output();
61+
62+
echo $OUTPUT->footer();
63+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
* SES account level suppression 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 suppressed_email 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_suppression',
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('aws_suppressionlist', '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_suppression');
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+
// Reason column.
100+
$columns[] = (new column(
101+
'reason',
102+
new lang_string('suppressionreason', 'tool_emailutils'),
103+
$this->get_entity_name()
104+
))
105+
->add_joins($this->get_joins())
106+
->set_type(column::TYPE_TEXT)
107+
->add_fields("{$tablealias}.reason")
108+
->set_is_sortable(true);
109+
110+
// Created at column.
111+
$columns[] = (new column(
112+
'created',
113+
new lang_string('suppressioncreated', 'tool_emailutils'),
114+
$this->get_entity_name()
115+
))
116+
->add_joins($this->get_joins())
117+
->set_type(column::TYPE_TIMESTAMP)
118+
->add_fields("{$tablealias}.created_at")
119+
->set_is_sortable(true)
120+
->add_callback([format::class, 'userdate'], get_string('strftimedatetimeshortaccurate', 'core_langconfig'));
121+
122+
return $columns;
123+
}
124+
125+
/**
126+
* Return list of all available filters
127+
*
128+
* @return filter[]
129+
*/
130+
protected function get_all_filters(): array {
131+
$tablealias = $this->get_table_alias('tool_emailutils_suppression');
132+
133+
// Email filter.
134+
$filters[] = (new filter(
135+
text::class,
136+
'email',
137+
new lang_string('email'),
138+
$this->get_entity_name(),
139+
"{$tablealias}.email"
140+
))
141+
->add_joins($this->get_joins());
142+
143+
// Reason filter.
144+
$filters[] = (new filter(
145+
text::class,
146+
'reason',
147+
new lang_string('suppressionreason', 'tool_emailutils'),
148+
$this->get_entity_name(),
149+
"{$tablealias}.reason"
150+
))
151+
->add_joins($this->get_joins());
152+
153+
// Created at filter.
154+
$filters[] = (new filter(
155+
date::class,
156+
'created',
157+
new lang_string('suppressioncreated', 'tool_emailutils'),
158+
$this->get_entity_name(),
159+
"{$tablealias}.created"
160+
))
161+
->add_joins($this->get_joins())
162+
->set_limited_operators([
163+
date::DATE_ANY,
164+
date::DATE_RANGE,
165+
date::DATE_PREVIOUS,
166+
date::DATE_CURRENT,
167+
]);
168+
169+
return $filters;
170+
}
171+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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\systemreports;
18+
19+
use context_system;
20+
use tool_emailutils\reportbuilder\local\entities\suppressed_email;
21+
use core_reportbuilder\system_report;
22+
use core_reportbuilder\local\entities\user;
23+
24+
/**
25+
* SES account level suppression list class implementation.
26+
*
27+
* @package tool_emailutils
28+
* @author Benjamin Walker <[email protected]>
29+
* @copyright Catalyst IT 2024
30+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31+
*
32+
*/
33+
class suppression_list extends system_report {
34+
35+
/**
36+
* Initialise report, we need to set the main table, load our entities and set columns/filters
37+
*/
38+
protected function initialise(): void {
39+
// Our main entity, it contains all of the column definitions that we need.
40+
$entitymain = new suppressed_email();
41+
$entitymainalias = $entitymain->get_table_alias('tool_emailutils_suppression');
42+
43+
$this->set_main_table('tool_emailutils_suppression', $entitymainalias);
44+
$this->add_entity($entitymain);
45+
46+
// We can join the "user" entity to our "main" entity using standard SQL JOIN.
47+
$entityuser = new user();
48+
$entityuseralias = $entityuser->get_table_alias('user');
49+
$this->add_entity($entityuser
50+
->add_join("LEFT JOIN {user} {$entityuseralias} ON {$entityuseralias}.email = {$entitymainalias}.email")
51+
);
52+
53+
// Now we can call our helper methods to add the content we want to include in the report.
54+
$this->add_columns();
55+
$this->add_filters();
56+
57+
// Set if report can be downloaded.
58+
$this->set_downloadable(true, 'email_suppression_list_' . date('Y-m-d'));
59+
}
60+
61+
/**
62+
* Validates access to view this report
63+
*
64+
* @return bool
65+
*/
66+
protected function can_view(): bool {
67+
return has_capability('moodle/site:config', context_system::instance());
68+
}
69+
70+
/**
71+
* Adds the columns we want to display in the report
72+
*
73+
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
74+
* unique identifier
75+
*/
76+
protected function add_columns(): void {
77+
$columns = [
78+
'suppressed_email:email',
79+
'suppressed_email:reason',
80+
'suppressed_email:created',
81+
'user:fullnamewithlink',
82+
];
83+
84+
$this->add_columns_from_entities($columns);
85+
86+
// Default sorting.
87+
$this->set_initial_sort_column('suppressed_email:created', SORT_DESC);
88+
}
89+
90+
/**
91+
* Adds the filters we want to display in the report
92+
*
93+
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
94+
* unique identifier
95+
*/
96+
protected function add_filters(): void {
97+
$filters = [
98+
'suppressed_email:email',
99+
'suppressed_email:reason',
100+
'suppressed_email:created',
101+
'user:fullname',
102+
'user:username',
103+
];
104+
105+
$this->add_filters_from_entities($filters);
106+
}
107+
}

0 commit comments

Comments
 (0)