forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MDL-69615-main' of https://github.com/HuongNV13/moodle
- Loading branch information
Showing
8 changed files
with
331 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace core\task; | ||
|
||
/** | ||
* Report task for core automation backup. | ||
* | ||
* @package core | ||
* @copyright 2024 Huong Nguyen <[email protected]> | ||
* @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', | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace core\task; | ||
|
||
/** | ||
* This file defines a trait to assist with unit tests in tasks. | ||
* | ||
* @package core | ||
* @copyright 2024 Huong Nguyen <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
trait task_trait { | ||
|
||
/** | ||
* Helper to execute a particular task. | ||
* | ||
* @param string $taskclass The task class. | ||
*/ | ||
protected function execute_task(string $taskclass): void { | ||
// Run the scheduled task. | ||
$this->start_output_buffering(); | ||
$task = manager::get_scheduled_task($taskclass); | ||
$task->execute(); | ||
$this->stop_output_buffering(); | ||
} | ||
|
||
/** | ||
* Helper to start output buffering. | ||
*/ | ||
protected function start_output_buffering(): void { | ||
ob_start(); | ||
} | ||
|
||
/** | ||
* Helper to stop output buffering. | ||
* | ||
* @return string|null The output buffer contents or null if output buffering is not active. | ||
*/ | ||
protected function stop_output_buffering(): ?string { | ||
$output = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.