Skip to content

Commit

Permalink
Change suppression list download to report #55
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Nov 18, 2024
1 parent 7e49f1a commit 86ff4f7
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 117 deletions.
64 changes: 34 additions & 30 deletions aws/suppression_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,49 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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

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

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

$action = optional_param('action', '', PARAM_ALPHA);
admin_externalpage_setup('tool_emailutils_bounces', '', [], '', ['pagelayout' => 'report']);

if ($action === 'download') {
$content = \tool_emailutils\suppression_list::generate_csv();
$filename = 'email_suppression_list_' . date('Y-m-d') . '.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($content));
echo $content;
exit;
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('aws_suppressionlist', 'tool_emailutils'));

// Add warnings if the suppression list hasn't been set up or run recently.
if (empty(get_config('tool_emailutils', 'enable_suppression_list'))) {
echo $OUTPUT->notification(get_string('aws_suppressionlist_disabled', 'tool_emailutils'), 'warning');
} else {
// Display the download page.
admin_externalpage_setup('toolemailutilssuppressionlist');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('suppressionlist', 'tool_emailutils'));

echo html_writer::start_tag('div', ['class' => 'suppressionlist-download']);
echo html_writer::tag('p', get_string('suppressionlistdesc', 'tool_emailutils'));
echo html_writer::link(
new moodle_url('/admin/tool/emailutils/aws/suppression_list.php', ['action' => 'download']),
get_string('downloadsuppressionlist', 'tool_emailutils'),
['class' => 'btn btn-primary']
);
echo html_writer::end_tag('div');

echo $OUTPUT->footer();
// Check if the update task has run recently.
$task = \core\task\manager::get_scheduled_task('\tool_emailutils\task\update_suppression_list');
if ($task && $task->is_enabled()) {
$lastrun = $task->get_last_run_time();
if (empty($lastrun)) {
echo $OUTPUT->notification(get_string('aws_suppressionlist_tasknever', 'tool_emailutils'), 'warning');
} else if ($lastrun < (time() - DAYSECS)) {
echo $OUTPUT->notification(get_string('aws_suppressionlist_taskupdated', 'tool_emailutils', format_time(time() - $lastrun)), 'warning');
} else {
// Always show the last update time as info.
echo $OUTPUT->notification(get_string('aws_suppressionlist_taskupdated', 'tool_emailutils', format_time(time() - $lastrun)), 'info');
}
} else {
echo $OUTPUT->notification(get_string('aws_suppressionlist_taskdisabled', 'tool_emailutils'), 'warning');
}
}

$report = system_report_factory::create(suppression_list::class, context_system::instance());

echo $report->output();

echo $OUTPUT->footer();

171 changes: 171 additions & 0 deletions classes/reportbuilder/local/entities/suppressed_email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?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;

/**
* SES account level suppression 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 suppressed_email extends base {

/**
* Database tables that this entity uses
*
* @return string[]
*/
protected function get_default_tables(): array {
return [
'tool_emailutils_suppression',
];
}

/**
* The default title for this entity
*
* @return lang_string
*/
protected function get_default_entity_title(): lang_string {
return new lang_string('aws_suppressionlist', '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_suppression');

// 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);

// Reason column.
$columns[] = (new column(
'reason',
new lang_string('suppressionreason', 'tool_emailutils'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$tablealias}.reason")
->set_is_sortable(true);

// Created at column.
$columns[] = (new column(
'created',
new lang_string('suppressioncreated', 'tool_emailutils'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TIMESTAMP)
->add_fields("{$tablealias}.created_at")
->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_suppression');

// Email filter.
$filters[] = (new filter(
text::class,
'email',
new lang_string('email'),
$this->get_entity_name(),
"{$tablealias}.email"
))
->add_joins($this->get_joins());

// Reason filter.
$filters[] = (new filter(
text::class,
'reason',
new lang_string('suppressionreason', 'tool_emailutils'),
$this->get_entity_name(),
"{$tablealias}.reason"
))
->add_joins($this->get_joins());

// Created at filter.
$filters[] = (new filter(
date::class,
'created',
new lang_string('suppressioncreated', 'tool_emailutils'),
$this->get_entity_name(),
"{$tablealias}.created"
))
->add_joins($this->get_joins())
->set_limited_operators([
date::DATE_ANY,
date::DATE_RANGE,
date::DATE_PREVIOUS,
date::DATE_CURRENT,
]);

return $filters;
}
}
107 changes: 107 additions & 0 deletions classes/reportbuilder/local/systemreports/suppression_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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\systemreports;

use context_system;
use tool_emailutils\reportbuilder\local\entities\suppressed_email;
use core_reportbuilder\system_report;
use core_reportbuilder\local\entities\user;

/**
* SES account level suppression list class implementation.
*
* @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 suppression_list extends system_report {

/**
* Initialise report, we need to set the main table, load our entities and set columns/filters
*/
protected function initialise(): void {
// Our main entity, it contains all of the column definitions that we need.
$entitymain = new suppressed_email();
$entitymainalias = $entitymain->get_table_alias('tool_emailutils_suppression');

$this->set_main_table('tool_emailutils_suppression', $entitymainalias);
$this->add_entity($entitymain);

// We can join the "user" entity to our "main" entity using standard SQL JOIN.
$entityuser = new user();
$entityuseralias = $entityuser->get_table_alias('user');
$this->add_entity($entityuser
->add_join("LEFT JOIN {user} {$entityuseralias} ON {$entityuseralias}.email = {$entitymainalias}.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();

// Set if report can be downloaded.
$this->set_downloadable(true, 'email_suppression_list_' . date('Y-m-d'));
}

/**
* Validates access to view this report
*
* @return bool
*/
protected function can_view(): bool {
return has_capability('moodle/site:config', context_system::instance());
}

/**
* Adds the columns we want to display in the report
*
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
* unique identifier
*/
protected function add_columns(): void {
$columns = [
'suppressed_email:email',
'suppressed_email:reason',
'suppressed_email:created',
'user:fullnamewithlink',
];

$this->add_columns_from_entities($columns);

// Default sorting.
$this->set_initial_sort_column('suppressed_email:created', SORT_DESC);
}

/**
* Adds the filters we want to display in the report
*
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
* unique identifier
*/
protected function add_filters(): void {
$filters = [
'suppressed_email:email',
'suppressed_email:reason',
'suppressed_email:created',
'user:fullname',
'user:username',
];

$this->add_filters_from_entities($filters);
}
}
Loading

0 comments on commit 86ff4f7

Please sign in to comment.