Moodle release 4.4rc1
[moodle.git] / reportbuilder / classes / task / send_schedule.php
blob79eda5a95271f29367accf81924932d36ec1505c
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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;
21 use core_user;
22 use core\task\adhoc_task;
23 use core_reportbuilder\local\helpers\schedule as helper;
24 use core_reportbuilder\local\models\schedule;
25 use moodle_exception;
27 /**
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;
38 /**
39 * Return name of the task
41 * @return string
43 public function get_name(): string {
44 return get_string('tasksendschedule', 'core_reportbuilder');
47 /**
48 * Execute the task
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)) {
60 return;
63 $schedule = schedule::get_record(['id' => $scheduleid, 'reportid' => $reportid]);
64 if ($schedule === false) {
65 $this->log('Invalid schedule', 0);
66 return;
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.
75 try {
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);
80 return;
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.
98 try {
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);
103 return;
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');
115 } else {
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);
124 } else {
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);
131 continue;
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);