Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\task;
20
 
1441 ariadna 21
use core\{clock, di};
22
use core\task\adhoc_task;
1 efrain 23
use core_user;
1441 ariadna 24
use core_reportbuilder\local\helpers\{report, schedule as helper};
1 efrain 25
use core_reportbuilder\local\models\schedule;
26
use moodle_exception;
27
 
28
/**
29
 * Ad-hoc task for sending a single report schedule
30
 *
31
 * @package     core_reportbuilder
32
 * @copyright   2021 Paul Holden <paulh@moodle.com>
33
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class send_schedule extends adhoc_task {
36
 
37
    use \core\task\logging_trait;
38
 
39
    /**
40
     * Return name of the task
41
     *
42
     * @return string
43
     */
44
    public function get_name(): string {
45
        return get_string('tasksendschedule', 'core_reportbuilder');
46
    }
47
 
48
    /**
49
     * Execute the task
50
     */
51
    public function execute(): void {
52
        global $CFG, $USER, $DB;
53
 
54
        [
55
            'reportid' => $reportid,
56
            'scheduleid' => $scheduleid,
57
        ] = (array) $this->get_custom_data();
58
 
59
        // Custom reports are disabled.
60
        if (empty($CFG->enablecustomreports)) {
61
            return;
62
        }
63
 
64
        $schedule = schedule::get_record(['id' => $scheduleid, 'reportid' => $reportid]);
65
        if ($schedule === false) {
66
            $this->log('Invalid schedule', 0);
67
            return;
68
        }
69
 
70
        $this->log_start('Sending schedule: ' . $schedule->get_formatted_name());
71
 
72
        $scheduleattachment = null;
73
        $originaluser = $USER;
74
 
75
        // Get the schedule creator, ensure it's an active account.
76
        try {
77
            $schedulecreator = core_user::get_user($schedule->get('usercreated'), '*', MUST_EXIST);
78
            core_user::require_active_user($schedulecreator);
79
        } catch (moodle_exception $exception) {
80
            $this->log('Invalid schedule creator: ' . $exception->getMessage(), 0);
81
            return;
82
        }
83
 
84
        // Switch to schedule creator, and retrieve list of recipient users.
85
        \core\cron::setup_user($schedulecreator);
86
 
87
        $users = helper::get_schedule_report_users($schedule);
88
        if (count($users) > 0) {
89
 
90
            $scheduleuserviewas = $schedule->get('userviewas');
91
            $schedulereportempty = $schedule->get('reportempty');
92
 
93
            // Handle schedule configuration as to who the report should be viewed as.
94
            if ($scheduleuserviewas === schedule::REPORT_VIEWAS_CREATOR) {
95
                $scheduleattachment = helper::get_schedule_report_file($schedule);
96
            } else if ($scheduleuserviewas !== schedule::REPORT_VIEWAS_RECIPIENT) {
97
 
98
                // Get the user to view the schedule report as, ensure it's an active account.
99
                try {
100
                    $scheduleviewas = core_user::get_user($scheduleuserviewas, '*', MUST_EXIST);
101
                    core_user::require_active_user($scheduleviewas);
102
                } catch (moodle_exception $exception) {
103
                    $this->log('Invalid schedule view as user: ' . $exception->getMessage(), 0);
104
                    return;
105
                }
106
 
107
                \core\cron::setup_user($scheduleviewas);
108
                $scheduleattachment = helper::get_schedule_report_file($schedule);
109
            }
110
 
111
            // Apply special handling if report is empty (default is to send it anyway).
1441 ariadna 112
            if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND && $scheduleattachment !== null &&
113
                    report::get_report_row_count($schedule->get('reportid')) === 0) {
1 efrain 114
 
115
                $this->log('Empty report, skipping');
116
            } else {
117
 
118
                // Now iterate over recipient users, send the report to each.
119
                foreach ($users as $user) {
120
                    $this->log('Sending to: ' . fullname($user, true));
121
 
122
                    // If we already created the attachment, send that. Otherwise generate per recipient.
123
                    if ($scheduleattachment !== null) {
124
                        helper::send_schedule_message($schedule, $user, $scheduleattachment);
125
                    } else {
126
                        \core\cron::setup_user($user);
127
 
128
                        if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND &&
1441 ariadna 129
                                report::get_report_row_count($schedule->get('reportid')) === 0) {
1 efrain 130
 
131
                            $this->log('Empty report, skipping', 2);
132
                            continue;
133
                        }
134
 
135
                        $recipientattachment = helper::get_schedule_report_file($schedule);
136
                        helper::send_schedule_message($schedule, $user, $recipientattachment);
137
                        $recipientattachment->delete();
138
                    }
139
                }
140
            }
141
        }
142
 
143
        // Finish, clean up (set persistent property manually to avoid updating it's user/time modified data).
1441 ariadna 144
        $DB->set_field($schedule::TABLE, 'timelastsent', di::get(clock::class)->time(), [
145
            'id' => $schedule->get('id'),
146
        ]);
1 efrain 147
 
148
        if ($scheduleattachment !== null) {
149
            $scheduleattachment->delete();
150
        }
151
 
152
        $this->log_finish('Sending schedule complete');
153
 
154
        // Restore cron user to original state.
155
        \core\cron::setup_user($originaluser);
156
    }
157
}