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};
1 efrain 22
use core\task\scheduled_task;
23
use core_reportbuilder\local\helpers\schedule;
24
use core_reportbuilder\local\models\schedule as model;
25
 
26
/**
27
 * Scheduled task for sending queued report schedules
28
 *
29
 * @package     core_reportbuilder
30
 * @copyright   2021 Paul Holden <paulh@moodle.com>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class send_schedules extends scheduled_task {
34
 
35
    /**
36
     * Return name of the task
37
     *
38
     * @return string
39
     */
40
    public function get_name(): string {
41
        return get_string('tasksendschedules', 'core_reportbuilder');
42
    }
43
 
44
    /**
45
     * Execute the task, request all pending schedules to be sent
46
     */
47
    public function execute(): void {
48
        global $DB;
49
 
1441 ariadna 50
        $schedules = model::get_records_select('enabled = 1 AND timenextsend <= :time', [
51
            'time' => di::get(clock::class)->time(),
52
        ]);
1 efrain 53
        $schedules = array_filter($schedules, [schedule::class, 'should_send_schedule']);
54
 
55
        // Loop over all schedules for sending, execute corresponding task to send each individually.
56
        foreach ($schedules as $schedule) {
57
            $sendschedule = new send_schedule();
58
            $sendschedule->set_custom_data([
59
                'reportid' => $schedule->get('reportid'),
60
                'scheduleid' => $schedule->get('id'),
61
            ]);
62
            $sendschedule->execute();
63
 
64
            // Calculate next send time (set persistent property manually to avoid updating it's user/time modified data).
65
            $DB->set_field($schedule::TABLE, 'timenextsend', schedule::calculate_next_send_time($schedule->read()),
66
                ['id' => $schedule->get('id')]);
67
        }
68
    }
69
}