2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * External backup API.
21 * @package core_backup
23 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die;
29 require_once("$CFG->libdir/externallib.php");
30 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
33 * Backup external functions.
35 * @package core_backup
37 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class core_backup_external
extends external_api
{
44 * Returns description of method parameters
46 * @return external_function_parameters
49 public static function get_async_backup_progress_parameters() {
50 return new external_function_parameters(
52 'backupids' => new external_multiple_structure(
53 new external_value(PARAM_ALPHANUM
, 'Backup id to get progress for', VALUE_REQUIRED
, null, NULL_ALLOWED
),
54 'Backup id to get progress for', VALUE_REQUIRED
56 'contextid' => new external_value(PARAM_INT
, 'Context id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
62 * Get asynchronous backup progress.
64 * @param string $backupids The ids of the backup to get progress for.
65 * @param int $contextid The context the backup relates to.
66 * @return array $results The array of results.
69 public static function get_async_backup_progress($backupids, $contextid) {
71 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
72 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
74 // Release session lock.
75 \core\session\manager
::write_close();
77 // Parameter validation.
78 self
::validate_parameters(
79 self
::get_async_backup_progress_parameters(),
81 'backupids' => $backupids,
82 'contextid' => $contextid
86 // Context validation.
87 list($context, $course, $cm) = get_context_info_array($contextid);
88 self
::validate_context($context);
91 require_capability('moodle/backup:backupactivity', $context);
93 require_capability('moodle/backup:backupcourse', $context);
94 $instanceid = $course->id
;
98 foreach ($backupids as $backupid) {
99 $results[] = backup_controller_dbops
::get_progress($backupid);
106 * Returns description of method result value
108 * @return external_description
111 public static function get_async_backup_progress_returns() {
112 return new external_multiple_structure(
113 new external_single_structure(
115 'status' => new external_value(PARAM_INT
, 'Backup Status'),
116 'progress' => new external_value(PARAM_FLOAT
, 'Backup progress'),
117 'backupid' => new external_value(PARAM_ALPHANUM
, 'Backup id'),
118 'operation' => new external_value(PARAM_ALPHANUM
, 'operation type'),
119 ), 'Backup completion status'
125 * Returns description of method parameters
127 * @return external_function_parameters
130 public static function get_async_backup_links_backup_parameters() {
131 return new external_function_parameters(
133 'filename' => new external_value(PARAM_FILE
, 'Backup filename', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
134 'contextid' => new external_value(PARAM_INT
, 'Context id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
140 * Get the data to be used when generating the table row for an asynchronous backup,
141 * the table row updates via ajax when backup is complete.
143 * @param string $filename The file name of the backup file.
144 * @param int $contextid The context the backup relates to.
147 public static function get_async_backup_links_backup($filename, $contextid) {
148 // Release session lock.
149 \core\session\manager
::write_close();
151 // Parameter validation.
152 self
::validate_parameters(
153 self
::get_async_backup_links_backup_parameters(),
155 'filename' => $filename,
156 'contextid' => $contextid
160 // Context validation.
161 list($context, $course, $cm) = get_context_info_array($contextid);
162 self
::validate_context($context);
163 require_capability('moodle/backup:backupcourse', $context);
166 $filearea = 'activity';
168 $filearea = 'course';
171 $results = \async_helper
::get_backup_file_info($filename, $filearea, $contextid);
177 * Returns description of method result value.
179 * @return external_description
182 public static function get_async_backup_links_backup_returns() {
183 return new external_single_structure(
185 'filesize' => new external_value(PARAM_TEXT
, 'Backup file size'),
186 'fileurl' => new external_value(PARAM_URL
, 'Backup file URL'),
187 'restoreurl' => new external_value(PARAM_URL
, 'Backup restore URL'),
188 ), 'Table row data.');
191 * Returns description of method parameters
193 * @return external_function_parameters
196 public static function get_async_backup_links_restore_parameters() {
197 return new external_function_parameters(
199 'backupid' => new external_value(PARAM_ALPHANUMEXT
, 'Backup id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
200 'contextid' => new external_value(PARAM_INT
, 'Context id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
206 * Get the data to be used when generating the table row for an asynchronous restore,
207 * the table row updates via ajax when restore is complete.
209 * @param string $backupid The id of the backup record.
210 * @param int $contextid The context the restore relates to.
211 * @return array $results The array of results.
214 public static function get_async_backup_links_restore($backupid, $contextid) {
215 // Release session lock.
216 \core\session\manager
::write_close();
218 // Parameter validation.
219 self
::validate_parameters(
220 self
::get_async_backup_links_restore_parameters(),
222 'backupid' => $backupid,
223 'contextid' => $contextid
227 // Context validation.
228 $context = context
::instance_by_id($contextid);
229 self
::validate_context($context);
230 require_capability('moodle/restore:restorecourse', $context);
232 $results = \async_helper
::get_restore_url($backupid);
238 * Returns description of method result value.
240 * @return external_description
243 public static function get_async_backup_links_restore_returns() {
244 return new external_single_structure(
246 'restoreurl' => new external_value(PARAM_URL
, 'Restore url'),
247 ), 'Table row data.');