Skip to content

Commit

Permalink
Add reset bounces action to bounces report
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl authored and brendanheywood committed Nov 28, 2024
1 parent 4b7e206 commit fea50e6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
24 changes: 23 additions & 1 deletion bounces.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

require(__DIR__.'/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/user/lib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/user/user_bulk_forms.php');

admin_externalpage_setup('tool_emailutils_bounces', '', [], '', ['pagelayout' => 'report']);

Expand All @@ -47,8 +49,28 @@
]), 'info');
}

$report = system_report_factory::create(email_bounces::class, context_system::instance());
echo html_writer::start_div('', ['data-region' => 'report-user-list-wrapper']);

// Exclude all actions besides reset bounces.
$actionames = array_keys((new user_bulk_action_form())->get_actions());
$excludeactions = array_diff($actionames, ['tool_emailutils_reset_bounces']);

$bulkactions = new user_bulk_action_form(new moodle_url('/admin/user/user_bulk.php'),
['excludeactions' => $excludeactions, 'passuserids' => true, 'hidesubmit' => true],
'post', '',
['id' => 'user-bulk-action-form']);
$bulkactions->set_data(['returnurl' => $PAGE->url->out_as_local_url(false)]);

$report = system_report_factory::create(email_bounces::class, context_system::instance(),
parameters: ['withcheckboxes' => $bulkactions->has_bulk_actions()]);

echo $report->output();

if ($bulkactions->has_bulk_actions()) {
$PAGE->requires->js_call_amd('core_admin/bulk_user_actions', 'init');
$bulkactions->display();
}

echo html_writer::end_div();

echo $OUTPUT->footer();
2 changes: 1 addition & 1 deletion classes/hook_callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class hook_callbacks {
*/
public static function extend_bulk_user_actions(\core_user\hook\extend_bulk_user_actions $hook): void {
if (has_capability('moodle/site:config', \context_system::instance())) {
$hook->add_action('tool_ses_reset_bounces', new \action_link(
$hook->add_action('tool_emailutils_reset_bounces', new \action_link(
new \moodle_url('/admin/tool/emailutils/reset_bounces.php'),
get_string('resetbounces', 'tool_emailutils')
));
Expand Down
28 changes: 28 additions & 0 deletions classes/reportbuilder/local/systemreports/email_bounces.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use tool_emailutils\reportbuilder\local\entities\email_bounce;
use core_reportbuilder\system_report;
use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\report\action;

/**
* Email bounces report class implementation.
Expand All @@ -44,6 +45,16 @@ protected function initialise(): void {
$this->add_entity($entitymain);
$this->add_base_condition_simple("{$entitymainalias}.name", 'email_bounce_count');

// Any columns required by actions should be defined here to ensure they're always available.
$this->add_base_fields("{$entitymainalias}.userid");

if ($this->get_parameter('withcheckboxes', false, PARAM_BOOL)) {
$canviewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
$this->set_checkbox_toggleall(static function(\stdClass $row) use ($canviewfullnames): array {
return [$row->userid, fullname($row, $canviewfullnames)];
});
}

// We can join the "user" entity to our "main" entity using standard SQL JOIN.
$entityuser = new user();
$entityuseralias = $entityuser->get_table_alias('user');
Expand All @@ -54,6 +65,7 @@ protected function initialise(): void {
// Now we can call our helper methods to add the content we want to include in the report.
$this->add_columns();
$this->add_filters();
$this->add_actions();

// Set if report can be downloaded.
$this->set_downloadable(true, get_string('reportbounces', 'tool_emailutils'));
Expand Down Expand Up @@ -107,4 +119,20 @@ protected function add_filters(): void {

$this->add_filters_from_entities($filters);
}

/**
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
*
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
*/
protected function add_actions(): void {
// Action to reset the bounce count.
$this->add_action((new action(
new \moodle_url('/admin/tool/emailutils/reset_bounces.php', ['id' => ':userid']),
new \pix_icon('i/reload', ''),
[],
false,
new \lang_string('resetbounces', 'tool_emailutils'),
)));
}
}
5 changes: 3 additions & 2 deletions lang/en/tool_emailutils.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@
$string['privacy:metadata:tool_emailutils_list'] = 'Information.';
$string['privacy:metadata:tool_emailutils_list:updatedid'] = 'The ID of updated user.';
$string['privacy:metadata:tool_emailutils_list:userid'] = 'The ID of the user.';
$string['reportbounces'] = 'Email bounces report';
$string['resetbounces'] = 'Reset the number of bounces';
$string['reportbounces'] = 'Email bounces';
$string['resetbounceratio'] = 'A bounce ratio is being used, so resetting the bounce count will also reset the send count.';
$string['resetbounces'] = 'Reset bounce count';
$string['selectoractivate'] = 'Activate key pair';
$string['selectoractivateconfirm'] = 'This will set $CFG->emaildkimselector to this selector and it will be used for signing outgoing emails.';
$string['selectoractive'] = 'Active selector';
Expand Down
39 changes: 28 additions & 11 deletions reset_bounces.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');

admin_externalpage_setup('userbulk');
admin_externalpage_setup('tool_emailutils_bounces');
require_capability('moodle/user:update', context_system::instance());

$confirm = optional_param('confirm', 0, PARAM_BOOL);
$userid = optional_param('id', null, PARAM_INT);
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);

$return = new moodle_url('/admin/user/user_bulk.php');
$users = empty($userid) ? $SESSION->bulk_users : [$userid];
$return = new moodle_url($returnurl ?? '/admin/tool/emailutils/bounces.php');

if (empty($SESSION->bulk_users)) {
if (empty($users)) {
redirect($return);
}

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('resetbounces', 'tool_emailutils'));

if ($confirm && confirm_sesskey()) {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
list($in, $params) = $DB->get_in_or_equal($users);
$rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, ' . \tool_emailutils\helper::get_username_fields());
foreach ($rs as $user) {
\tool_emailutils\helper::reset_bounce_count($user);
Expand All @@ -50,16 +53,30 @@
echo $OUTPUT->box_start('generalbox', 'notice');
echo $OUTPUT->notification(get_string('bouncesreset', 'tool_emailutils'), 'notifysuccess');

$continue = new single_button(new moodle_url($return), get_string('continue'), 'post');
$continue = new single_button(new moodle_url($return), get_string('continue'));
echo $OUTPUT->render($continue);
echo $OUTPUT->box_end();
} else {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
list($in, $params) = $DB->get_in_or_equal($users);
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
$usernames = implode(', ', $userlist);
echo $OUTPUT->heading(get_string('confirmation', 'admin'));
$formcontinue = new single_button(new moodle_url('reset_bounces.php', ['confirm' => 1]), get_string('yes'));
$formcancel = new single_button(new moodle_url('/admin/user/user_bulk.php'), get_string('no'), 'get');
echo $OUTPUT->confirm(get_string('bouncecheckfull', 'tool_emailutils', $usernames), $formcontinue, $formcancel);

if (empty($userlist)) {
echo $OUTPUT->notification(get_string('invaliduserid', 'error'));
$continue = new single_button(new moodle_url($return), get_string('continue'));
echo $OUTPUT->render($continue);
} else {
if (\tool_emailutils\helper::use_bounce_ratio()) {
echo $OUTPUT->notification(get_string('resetbounceratio', 'tool_emailutils'), 'warning');
}
$usernames = implode(', ', $userlist);
$params = array_filter([
'confirm' => 1,
'id' => $userid,
'returnurl' => $returnurl,
]);
$formcontinue = new single_button(new moodle_url('reset_bounces.php', $params), get_string('yes'));
$formcancel = new single_button($return, get_string('no'));
echo $OUTPUT->confirm(get_string('bouncecheckfull', 'tool_emailutils', $usernames), $formcontinue, $formcancel);
}
}
echo $OUTPUT->footer();

0 comments on commit fea50e6

Please sign in to comment.