Skip to content

Commit 2ee4a25

Browse files
committed
Add reset bounces action to bounces report
1 parent 8a4c8d1 commit 2ee4a25

File tree

6 files changed

+84
-15
lines changed

6 files changed

+84
-15
lines changed

bounces.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
require(__DIR__.'/../../../config.php');
3232
require_once($CFG->libdir.'/adminlib.php');
33+
require_once($CFG->dirroot.'/'.$CFG->admin.'/user/lib.php');
34+
require_once($CFG->dirroot.'/'.$CFG->admin.'/user/user_bulk_forms.php');
3335

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

@@ -47,8 +49,28 @@
4749
]), 'info');
4850
}
4951

50-
$report = system_report_factory::create(email_bounces::class, context_system::instance());
52+
echo html_writer::start_div('', ['data-region' => 'report-user-list-wrapper']);
53+
54+
// Exclude all actions besides reset bounces.
55+
$actionames = array_keys((new user_bulk_action_form())->get_actions());
56+
$excludeactions = array_diff($actionames, ['tool_emailutils_reset_bounces']);
57+
58+
$bulkactions = new user_bulk_action_form(new moodle_url('/admin/user/user_bulk.php'),
59+
['excludeactions' => $excludeactions, 'passuserids' => true, 'hidesubmit' => true],
60+
'post', '',
61+
['id' => 'user-bulk-action-form']);
62+
$bulkactions->set_data(['returnurl' => $PAGE->url->out_as_local_url(false)]);
63+
64+
$report = system_report_factory::create(email_bounces::class, context_system::instance(),
65+
parameters: ['withcheckboxes' => $bulkactions->has_bulk_actions()]);
5166

5267
echo $report->output();
5368

69+
if ($bulkactions->has_bulk_actions()) {
70+
$PAGE->requires->js_call_amd('core_admin/bulk_user_actions', 'init');
71+
$bulkactions->display();
72+
}
73+
74+
echo html_writer::end_div();
75+
5476
echo $OUTPUT->footer();

classes/helper.php

+5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public static function use_consecutive_bounces(): bool {
102102
public static function reset_bounce_count(\stdClass $resetuser): void {
103103
global $USER;
104104

105+
$bouncecount = get_user_preferences('email_bounce_count', null, $resetuser);
106+
if (!isset($bouncecount)) {
107+
return;
108+
}
109+
105110
// Swap to set_bounce_count($resetuser, true) once MDL-73798 is integrated.
106111
unset_user_preference('email_bounce_count', $resetuser);
107112

classes/hook_callbacks.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class hook_callbacks {
3333
*/
3434
public static function extend_bulk_user_actions(\core_user\hook\extend_bulk_user_actions $hook): void {
3535
if (has_capability('moodle/site:config', \context_system::instance())) {
36-
$hook->add_action('tool_ses_reset_bounces', new \action_link(
36+
$hook->add_action('tool_emailutils_reset_bounces', new \action_link(
3737
new \moodle_url('/admin/tool/emailutils/reset_bounces.php'),
3838
get_string('resetbounces', 'tool_emailutils')
3939
));

classes/reportbuilder/local/systemreports/email_bounces.php

+28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use tool_emailutils\reportbuilder\local\entities\email_bounce;
2121
use core_reportbuilder\system_report;
2222
use core_reportbuilder\local\entities\user;
23+
use core_reportbuilder\local\report\action;
2324

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

48+
// Any columns required by actions should be defined here to ensure they're always available.
49+
$this->add_base_fields("{$entitymainalias}.userid");
50+
51+
if ($this->get_parameter('withcheckboxes', false, PARAM_BOOL)) {
52+
$canviewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
53+
$this->set_checkbox_toggleall(static function(\stdClass $row) use ($canviewfullnames): array {
54+
return [$row->userid, fullname($row, $canviewfullnames)];
55+
});
56+
}
57+
4758
// We can join the "user" entity to our "main" entity using standard SQL JOIN.
4859
$entityuser = new user();
4960
$entityuseralias = $entityuser->get_table_alias('user');
@@ -54,6 +65,7 @@ protected function initialise(): void {
5465
// Now we can call our helper methods to add the content we want to include in the report.
5566
$this->add_columns();
5667
$this->add_filters();
68+
$this->add_actions();
5769

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

108120
$this->add_filters_from_entities($filters);
109121
}
122+
123+
/**
124+
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
125+
*
126+
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
127+
*/
128+
protected function add_actions(): void {
129+
// Action to reset the bounce count.
130+
$this->add_action((new action(
131+
new \moodle_url('/admin/tool/emailutils/reset_bounces.php', ['id' => ':userid']),
132+
new \pix_icon('i/reload', ''),
133+
[],
134+
false,
135+
new \lang_string('resetbounces', 'tool_emailutils'),
136+
)));
137+
}
110138
}

lang/en/tool_emailutils.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
$string['privacy:metadata:tool_emailutils_list'] = 'Information.';
9494
$string['privacy:metadata:tool_emailutils_list:updatedid'] = 'The ID of updated user.';
9595
$string['privacy:metadata:tool_emailutils_list:userid'] = 'The ID of the user.';
96-
$string['reportbounces'] = 'Email bounces report';
97-
$string['resetbounces'] = 'Reset the number of bounces';
96+
$string['reportbounces'] = 'Email bounces';
97+
$string['resetbounces'] = 'Reset bounce count';
9898
$string['selectoractivate'] = 'Activate key pair';
9999
$string['selectoractivateconfirm'] = 'This will set $CFG->emaildkimselector to this selector and it will be used for signing outgoing emails.';
100100
$string['selectoractive'] = 'Active selector';

reset_bounces.php

+25-11
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,25 @@
2626
require_once(__DIR__ . '/../../../config.php');
2727
require_once($CFG->libdir . '/adminlib.php');
2828

29-
admin_externalpage_setup('userbulk');
29+
admin_externalpage_setup('tool_emailutils_bounces');
3030
require_capability('moodle/user:update', context_system::instance());
3131

3232
$confirm = optional_param('confirm', 0, PARAM_BOOL);
33+
$userid = optional_param('id', null, PARAM_INT);
34+
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
3335

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

36-
if (empty($SESSION->bulk_users)) {
39+
if (empty($users)) {
3740
redirect($return);
3841
}
3942

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

4346
if ($confirm && confirm_sesskey()) {
44-
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
47+
list($in, $params) = $DB->get_in_or_equal($users);
4548
$rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, ' . \tool_emailutils\helper::get_username_fields());
4649
foreach ($rs as $user) {
4750
\tool_emailutils\helper::reset_bounce_count($user);
@@ -50,16 +53,27 @@
5053
echo $OUTPUT->box_start('generalbox', 'notice');
5154
echo $OUTPUT->notification(get_string('bouncesreset', 'tool_emailutils'), 'notifysuccess');
5255

53-
$continue = new single_button(new moodle_url($return), get_string('continue'), 'post');
56+
$continue = new single_button(new moodle_url($return), get_string('continue'));
5457
echo $OUTPUT->render($continue);
5558
echo $OUTPUT->box_end();
5659
} else {
57-
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
60+
list($in, $params) = $DB->get_in_or_equal($users);
5861
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
59-
$usernames = implode(', ', $userlist);
60-
echo $OUTPUT->heading(get_string('confirmation', 'admin'));
61-
$formcontinue = new single_button(new moodle_url('reset_bounces.php', ['confirm' => 1]), get_string('yes'));
62-
$formcancel = new single_button(new moodle_url('/admin/user/user_bulk.php'), get_string('no'), 'get');
63-
echo $OUTPUT->confirm(get_string('bouncecheckfull', 'tool_emailutils', $usernames), $formcontinue, $formcancel);
62+
63+
if (empty($userlist)) {
64+
echo $OUTPUT->notification(get_string('invaliduserid', 'error'));
65+
$continue = new single_button(new moodle_url($return), get_string('continue'));
66+
echo $OUTPUT->render($continue);
67+
} else {
68+
$usernames = implode(', ', $userlist);
69+
$params = array_filter([
70+
'confirm' => 1,
71+
'id' => $userid,
72+
'returnurl' => $returnurl,
73+
]);
74+
$formcontinue = new single_button(new moodle_url('reset_bounces.php', $params), get_string('yes'));
75+
$formcancel = new single_button($return, get_string('no'));
76+
echo $OUTPUT->confirm(get_string('bouncecheckfull', 'tool_emailutils', $usernames), $formcontinue, $formcancel);
77+
}
6478
}
6579
echo $OUTPUT->footer();

0 commit comments

Comments
 (0)