Skip to content

Commit 8e431c6

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

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
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/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

+26-17
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,49 @@
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');
35-
36-
if (empty($SESSION->bulk_users)) {
36+
$users = empty($userid) ? $SESSION->bulk_users : [$userid];
37+
$return = new moodle_url($returnurl ?? '/admin/tool/emailutils/bounces.php');
38+
if (empty($users)) {
3739
redirect($return);
3840
}
3941

40-
echo $OUTPUT->header();
41-
echo $OUTPUT->heading(get_string('resetbounces', 'tool_emailutils'));
42-
4342
if ($confirm && confirm_sesskey()) {
44-
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
43+
list($in, $params) = $DB->get_in_or_equal($users);
4544
$rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, ' . \tool_emailutils\helper::get_username_fields());
4645
foreach ($rs as $user) {
4746
\tool_emailutils\helper::reset_bounce_count($user);
4847
}
4948
$rs->close();
50-
echo $OUTPUT->box_start('generalbox', 'notice');
51-
echo $OUTPUT->notification(get_string('bouncesreset', 'tool_emailutils'), 'notifysuccess');
49+
\core\notification::success(get_string('bouncesreset', 'tool_emailutils'));;
50+
redirect($return);
51+
}
52+
53+
echo $OUTPUT->header();
54+
echo $OUTPUT->heading(get_string('resetbounces', 'tool_emailutils'));
55+
56+
list($in, $params) = $DB->get_in_or_equal($users);
57+
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
5258

53-
$continue = new single_button(new moodle_url($return), get_string('continue'), 'post');
59+
if (empty($userlist)) {
60+
echo $OUTPUT->notification(get_string('invaliduserid', 'error'));
61+
$continue = new single_button(new moodle_url($return), get_string('continue'));
5462
echo $OUTPUT->render($continue);
55-
echo $OUTPUT->box_end();
5663
} else {
57-
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
58-
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
5964
$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');
65+
$params = array_filter([
66+
'confirm' => 1,
67+
'id' => $userid,
68+
'returnurl' => $returnurl,
69+
]);
70+
$formcontinue = new single_button(new moodle_url('reset_bounces.php', $params), get_string('yes'));
71+
$formcancel = new single_button($return, get_string('no'));
6372
echo $OUTPUT->confirm(get_string('bouncecheckfull', 'tool_emailutils', $usernames), $formcontinue, $formcancel);
6473
}
6574
echo $OUTPUT->footer();

0 commit comments

Comments
 (0)