Proyectos de Subversion Moodle

Rev

| 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
namespace core\task;
18
 
19
/**
20
 * Report task for core automation backup.
21
 *
22
 * @package    core
23
 * @copyright  2024 Huong Nguyen <huongnv13@gmail.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class automated_backup_report_task extends scheduled_task {
27
 
28
    /**
29
     * Get a descriptive name for the task (shown to admins).
30
     *
31
     * @return string
32
     */
33
    public function get_name(): string {
34
        return get_string('taskautomatedbackup_report', 'admin');
35
    }
36
 
37
    /**
38
     * Do the job.
39
     */
40
    public function execute(): void {
41
        global $DB, $CFG;
42
        $queuedtasks = [];
43
        if ($value = get_config('backup', 'backup_auto_adhoctasks')) {
44
            $queuedtasks = explode(',', $value);
45
        }
46
        if (!empty($queuedtasks)) {
47
            // Some automated backup tasks are still running.
48
            // Check the status for each task.
49
            foreach ($queuedtasks as $taskid) {
50
                if (!$DB->record_exists('task_adhoc', ['id' => $taskid])) {
51
                    // The task has been completed. Remove it from the queue.
52
                    if (($key = array_search($taskid, $queuedtasks)) !== false) {
53
                        unset($queuedtasks[$key]);
54
                    }
55
                }
56
            }
57
            // Update the queue.
58
            set_config(
59
                'backup_auto_adhoctasks',
60
                implode(',', $queuedtasks),
61
                'backup',
62
            );
63
        }
64
        if (empty($queuedtasks) && get_config('backup', 'backup_auto_emailpending')) {
65
            // All the automated backup tasks have been completed. Send the report.
66
            $admin = get_admin();
67
            if (!$admin) {
68
                mtrace("Error: No admin account was found");
69
                return;
70
            }
71
            // Send email to admin if necessary.
72
            require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
73
            require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php');
74
            \backup_cron_automated_helper::send_backup_status_to_admin($admin);
75
            // Remove the configs.
76
            unset_config(
77
                'backup_auto_adhoctasks',
78
                'backup',
79
            );
80
            unset_config(
81
                'backup_auto_emailpending',
82
                'backup',
83
            );
84
        }
85
    }
86
}