Merge branch 'MDL-65436-master' of git://github.com/sarjona/moodle
[moodle.git] / backup / externallib.php
blob98119ecaa4ee42949ad6fef2d9d3ab446b0adf4a
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.
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/>.
18 /**
19 * External backup API.
21 * @package core_backup
22 * @category external
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');
32 /**
33 * Backup external functions.
35 * @package core_backup
36 * @category external
37 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @since Moodle 3.7
41 class core_backup_external extends external_api {
43 /**
44 * Returns description of method parameters
46 * @return external_function_parameters
47 * @since Moodle 3.7
49 public static function get_async_backup_progress_parameters() {
50 return new external_function_parameters(
51 array(
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),
61 /**
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.
67 * @since Moodle 3.7
69 public static function get_async_backup_progress($backupids, $contextid) {
70 global $CFG;
71 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
72 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
74 // Parameter validation.
75 self::validate_parameters(
76 self::get_async_backup_progress_parameters(),
77 array(
78 'backupids' => $backupids,
79 'contextid' => $contextid
83 // Context validation.
84 list($context, $course, $cm) = get_context_info_array($contextid);
85 self::validate_context($context);
87 if ($cm) {
88 require_capability('moodle/backup:backupactivity', $context);
89 } else {
90 require_capability('moodle/backup:backupcourse', $context);
91 $instanceid = $course->id;
94 $results = array();
95 foreach ($backupids as $backupid) {
96 $results[] = backup_controller_dbops::get_progress($backupid);
99 return $results;
103 * Returns description of method result value
105 * @return external_description
106 * @since Moodle 3.7
108 public static function get_async_backup_progress_returns() {
109 return new external_multiple_structure(
110 new external_single_structure(
111 array(
112 'status' => new external_value(PARAM_INT, 'Backup Status'),
113 'progress' => new external_value(PARAM_FLOAT, 'Backup progress'),
114 'backupid' => new external_value(PARAM_ALPHANUM, 'Backup id'),
115 'operation' => new external_value(PARAM_ALPHANUM, 'operation type'),
116 ), 'Backup completion status'
117 ), 'Backup data'
122 * Returns description of method parameters
124 * @return external_function_parameters
125 * @since Moodle 3.7
127 public static function get_async_backup_links_backup_parameters() {
128 return new external_function_parameters(
129 array(
130 'filename' => new external_value(PARAM_FILE, 'Backup filename', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
131 'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
137 * Get the data to be used when generating the table row for an asynchronous backup,
138 * the table row updates via ajax when backup is complete.
140 * @param string $filename The file name of the backup file.
141 * @param int $contextid The context the backup relates to.
142 * @since Moodle 3.7
144 public static function get_async_backup_links_backup($filename, $contextid) {
145 // Parameter validation.
146 self::validate_parameters(
147 self::get_async_backup_links_backup_parameters(),
148 array(
149 'filename' => $filename,
150 'contextid' => $contextid
154 // Context validation.
155 list($context, $course, $cm) = get_context_info_array($contextid);
156 self::validate_context($context);
157 require_capability('moodle/backup:backupcourse', $context);
159 if ($cm) {
160 $filearea = 'activity';
161 } else {
162 $filearea = 'course';
165 $results = \async_helper::get_backup_file_info($filename, $filearea, $contextid);
167 return $results;
171 * Returns description of method result value.
173 * @return external_description
174 * @since Moodle 3.7
176 public static function get_async_backup_links_backup_returns() {
177 return new external_single_structure(
178 array(
179 'filesize' => new external_value(PARAM_TEXT, 'Backup file size'),
180 'fileurl' => new external_value(PARAM_URL, 'Backup file URL'),
181 'restoreurl' => new external_value(PARAM_URL, 'Backup restore URL'),
182 ), 'Table row data.');
185 * Returns description of method parameters
187 * @return external_function_parameters
188 * @since Moodle 3.7
190 public static function get_async_backup_links_restore_parameters() {
191 return new external_function_parameters(
192 array(
193 'backupid' => new external_value(PARAM_ALPHANUMEXT, 'Backup id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
194 'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
200 * Get the data to be used when generating the table row for an asynchronous restore,
201 * the table row updates via ajax when restore is complete.
203 * @param string $backupid The id of the backup record.
204 * @param int $contextid The context the restore relates to.
205 * @return array $results The array of results.
206 * @since Moodle 3.7
208 public static function get_async_backup_links_restore($backupid, $contextid) {
209 // Parameter validation.
210 self::validate_parameters(
211 self::get_async_backup_links_restore_parameters(),
212 array(
213 'backupid' => $backupid,
214 'contextid' => $contextid
218 // Context validation.
219 $context = context::instance_by_id($contextid);
220 self::validate_context($context);
221 require_capability('moodle/restore:restorecourse', $context);
223 $results = \async_helper::get_restore_url($backupid);
225 return $results;
229 * Returns description of method result value.
231 * @return external_description
232 * @since Moodle 3.7
234 public static function get_async_backup_links_restore_returns() {
235 return new external_single_structure(
236 array(
237 'restoreurl' => new external_value(PARAM_URL, 'Restore url'),
238 ), 'Table row data.');