MDL-78364 reportbuilder: earlier calculation of schedule recipients.

This commit is contained in:
Paul Holden
2023-06-05 16:22:33 +01:00
parent 03b605f2eb
commit 6843ba0aad
2 changed files with 65 additions and 36 deletions
+41 -35
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) {
cron_setup_user(core_user::get_user($schedule->get('usercreated')));
$scheduleattachment = helper::get_schedule_report_file($schedule);
} else if ($scheduleuserviewas !== schedule::REPORT_VIEWAS_RECIPIENT) {
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.
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) {
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 {
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 {
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();
}
}