2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 declare(strict_types
=1);
19 namespace core_reportbuilder\task
;
22 use core\task\adhoc_task
;
23 use core_reportbuilder\local\helpers\schedule
as helper
;
24 use core_reportbuilder\local\models\schedule
;
28 * Ad-hoc task for sending a single report schedule
30 * @package core_reportbuilder
31 * @copyright 2021 Paul Holden <paulh@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class send_schedule
extends adhoc_task
{
36 use \core\task\logging_trait
;
39 * Return name of the task
43 public function get_name(): string {
44 return get_string('tasksendschedule', 'core_reportbuilder');
50 public function execute(): void
{
51 global $CFG, $USER, $DB;
54 'reportid' => $reportid,
55 'scheduleid' => $scheduleid,
56 ] = (array) $this->get_custom_data();
58 // Custom reports are disabled.
59 if (empty($CFG->enablecustomreports
)) {
63 $schedule = schedule
::get_record(['id' => $scheduleid, 'reportid' => $reportid]);
64 if ($schedule === false) {
65 $this->log('Invalid schedule', 0);
69 $this->log_start('Sending schedule: ' . $schedule->get_formatted_name());
71 $scheduleattachment = null;
72 $originaluser = $USER;
74 // Get the schedule creator, ensure it's an active account.
76 $schedulecreator = core_user
::get_user($schedule->get('usercreated'), '*', MUST_EXIST
);
77 core_user
::require_active_user($schedulecreator);
78 } catch (moodle_exception
$exception) {
79 $this->log('Invalid schedule creator: ' . $exception->getMessage(), 0);
83 // Switch to schedule creator, and retrieve list of recipient users.
84 \core\cron
::setup_user($schedulecreator);
86 $users = helper
::get_schedule_report_users($schedule);
87 if (count($users) > 0) {
89 $scheduleuserviewas = $schedule->get('userviewas');
90 $schedulereportempty = $schedule->get('reportempty');
92 // Handle schedule configuration as to who the report should be viewed as.
93 if ($scheduleuserviewas === schedule
::REPORT_VIEWAS_CREATOR
) {
94 $scheduleattachment = helper
::get_schedule_report_file($schedule);
95 } else if ($scheduleuserviewas !== schedule
::REPORT_VIEWAS_RECIPIENT
) {
97 // Get the user to view the schedule report as, ensure it's an active account.
99 $scheduleviewas = core_user
::get_user($scheduleuserviewas, '*', MUST_EXIST
);
100 core_user
::require_active_user($scheduleviewas);
101 } catch (moodle_exception
$exception) {
102 $this->log('Invalid schedule view as user: ' . $exception->getMessage(), 0);
106 \core\cron
::setup_user($scheduleviewas);
107 $scheduleattachment = helper
::get_schedule_report_file($schedule);
110 // Apply special handling if report is empty (default is to send it anyway).
111 if ($schedulereportempty === schedule
::REPORT_EMPTY_DONT_SEND
&&
112 $scheduleattachment !== null && helper
::get_schedule_report_count($schedule) === 0) {
114 $this->log('Empty report, skipping');
117 // Now iterate over recipient users, send the report to each.
118 foreach ($users as $user) {
119 $this->log('Sending to: ' . fullname($user, true));
121 // If we already created the attachment, send that. Otherwise generate per recipient.
122 if ($scheduleattachment !== null) {
123 helper
::send_schedule_message($schedule, $user, $scheduleattachment);
125 \core\cron
::setup_user($user);
127 if ($schedulereportempty === schedule
::REPORT_EMPTY_DONT_SEND
&&
128 helper
::get_schedule_report_count($schedule) === 0) {
130 $this->log('Empty report, skipping', 2);
134 $recipientattachment = helper
::get_schedule_report_file($schedule);
135 helper
::send_schedule_message($schedule, $user, $recipientattachment);
136 $recipientattachment->delete();
142 // Finish, clean up (set persistent property manually to avoid updating it's user/time modified data).
143 $DB->set_field($schedule::TABLE
, 'timelastsent', time(), ['id' => $schedule->get('id')]);
145 if ($scheduleattachment !== null) {
146 $scheduleattachment->delete();
149 $this->log_finish('Sending schedule complete');
151 // Restore cron user to original state.
152 \core\cron
::setup_user($originaluser);