This commit is contained in:
Andrew Nicols 2023-06-08 12:17:03 +08:00
commit 8538341bc8
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
2 changed files with 65 additions and 36 deletions

View File

@ -65,51 +65,57 @@ class send_schedule extends adhoc_task {
return;
}
$originaluser = $USER;
$scheduleuserviewas = $schedule->get('userviewas');
$schedulereportempty = $schedule->get('reportempty');
$scheduleattachment = null;
$this->log_start('Sending schedule: ' . $schedule->get_formatted_name());
// Handle schedule configuration as to who the report should be viewed as.
if ($scheduleuserviewas === schedule::REPORT_VIEWAS_CREATOR) {
\core\cron::setup_user(core_user::get_user($schedule->get('usercreated')));
$scheduleattachment = helper::get_schedule_report_file($schedule);
} else if ($scheduleuserviewas !== schedule::REPORT_VIEWAS_RECIPIENT) {
\core\cron::setup_user(core_user::get_user($scheduleuserviewas));
$scheduleattachment = helper::get_schedule_report_file($schedule);
}
$scheduleattachment = null;
$originaluser = $USER;
// Apply special handling if report is empty (default is to send it anyway).
if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND &&
$scheduleattachment !== null && helper::get_schedule_report_count($schedule) === 0) {
$this->log('Empty report, skipping');
return;
}
// Switch to schedule creator, and retrieve list of recipient users.
\core\cron::setup_user(core_user::get_user($schedule->get('usercreated')));
$users = helper::get_schedule_report_users($schedule);
foreach ($users as $user) {
$this->log('Sending to: ' . fullname($user, true));
if (count($users) > 0) {
// If we already created the attachment, send that. Otherwise generate per recipient.
if ($scheduleattachment !== null) {
helper::send_schedule_message($schedule, $user, $scheduleattachment);
$scheduleuserviewas = $schedule->get('userviewas');
$schedulereportempty = $schedule->get('reportempty');
// Handle schedule configuration as to who the report should be viewed as.
if ($scheduleuserviewas === schedule::REPORT_VIEWAS_CREATOR) {
$scheduleattachment = helper::get_schedule_report_file($schedule);
} else if ($scheduleuserviewas !== schedule::REPORT_VIEWAS_RECIPIENT) {
\core\cron::setup_user(core_user::get_user($scheduleuserviewas));
$scheduleattachment = helper::get_schedule_report_file($schedule);
}
// Apply special handling if report is empty (default is to send it anyway).
if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND &&
$scheduleattachment !== null && helper::get_schedule_report_count($schedule) === 0) {
$this->log('Empty report, skipping');
} else {
\core\cron::setup_user($user);
if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND &&
helper::get_schedule_report_count($schedule) === 0) {
// Now iterate over recipient users, send the report to each.
foreach ($users as $user) {
$this->log('Sending to: ' . fullname($user, true));
$this->log('Empty report, skipping', 2);
continue;
// If we already created the attachment, send that. Otherwise generate per recipient.
if ($scheduleattachment !== null) {
helper::send_schedule_message($schedule, $user, $scheduleattachment);
} else {
\core\cron::setup_user($user);
if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND &&
helper::get_schedule_report_count($schedule) === 0) {
$this->log('Empty report, skipping', 2);
continue;
}
$recipientattachment = helper::get_schedule_report_file($schedule);
helper::send_schedule_message($schedule, $user, $recipientattachment);
$recipientattachment->delete();
}
}
$recipientattachment = helper::get_schedule_report_file($schedule);
helper::send_schedule_message($schedule, $user, $recipientattachment);
$recipientattachment->delete();
}
}

View File

@ -165,14 +165,37 @@ class send_schedule_test extends advanced_testcase {
'user:username_value' => 'baconlettucetomato',
]);
$audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
$schedule = $generator->create_schedule([
'reportid' => $report->get('id'),
'name' => 'My schedule',
'audiences' => json_encode([$audience->get_persistent()->get('id')]),
'reportempty' => schedule::REPORT_EMPTY_DONT_SEND,
]);
$this->expectOutputString("Sending schedule: My schedule\n" .
" Empty report, skipping\n");
" Empty report, skipping\n" .
"Sending schedule complete\n");
$sendschedule = new send_schedule();
$sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
$sendschedule->execute();
}
/**
* Test executing task for a schedule that contains no recipients
*/
public function test_execute_schedule_no_recipients(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$this->expectOutputString("Sending schedule: My schedule\n" .
"Sending schedule complete\n");
$sendschedule = new send_schedule();
$sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
$sendschedule->execute();