. namespace core\task; /** * Report task for core automation backup. * * @package core * @copyright 2024 Huong Nguyen * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class automated_backup_report_task extends scheduled_task { /** * Get a descriptive name for the task (shown to admins). * * @return string */ public function get_name(): string { return get_string('taskautomatedbackup_report', 'admin'); } /** * Do the job. */ public function execute(): void { global $DB, $CFG; $queuedtasks = []; if ($value = get_config('backup', 'backup_auto_adhoctasks')) { $queuedtasks = explode(',', $value); } if (!empty($queuedtasks)) { // Some automated backup tasks are still running. // Check the status for each task. foreach ($queuedtasks as $taskid) { if (!$DB->record_exists('task_adhoc', ['id' => $taskid])) { // The task has been completed. Remove it from the queue. if (($key = array_search($taskid, $queuedtasks)) !== false) { unset($queuedtasks[$key]); } } } // Update the queue. set_config( 'backup_auto_adhoctasks', implode(',', $queuedtasks), 'backup', ); } if (empty($queuedtasks) && get_config('backup', 'backup_auto_emailpending')) { // All the automated backup tasks have been completed. Send the report. $admin = get_admin(); if (!$admin) { mtrace("Error: No admin account was found"); return; } // Send email to admin if necessary. require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php'); \backup_cron_automated_helper::send_backup_status_to_admin($admin); // Remove the configs. unset_config( 'backup_auto_adhoctasks', 'backup', ); unset_config( 'backup_auto_emailpending', 'backup', ); } } }