Merge branch 'MDL-81399' of https://github.com/paulholden/moodle
[moodle.git] / backup / externallib.php
blobfb747965d8cec29f49a8c8370c1afbd6533fe534
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/>.
17 /**
18 * External backup API.
20 * @package core_backup
21 * @category external
22 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 use core_external\external_api;
27 use core_external\external_function_parameters;
28 use core_external\external_multiple_structure;
29 use core_external\external_single_structure;
30 use core_external\external_value;
32 defined('MOODLE_INTERNAL') || die;
34 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
35 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
37 /**
38 * Backup external functions.
40 * @package core_backup
41 * @category external
42 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 * @since Moodle 3.7
46 class core_backup_external extends external_api {
48 /**
49 * Returns description of method parameters
51 * @return external_function_parameters
52 * @since Moodle 3.7
54 public static function get_async_backup_progress_parameters() {
55 return new external_function_parameters(
56 array(
57 'backupids' => new external_multiple_structure(
58 new external_value(PARAM_ALPHANUM, 'Backup id to get progress for', VALUE_REQUIRED, null, NULL_ALLOWED),
59 'Backup id to get progress for', VALUE_REQUIRED
61 'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
66 /**
67 * Get asynchronous backup progress.
69 * @param string $backupids The ids of the backup to get progress for.
70 * @param int $contextid The context the backup relates to.
71 * @return array $results The array of results.
72 * @since Moodle 3.7
74 public static function get_async_backup_progress($backupids, $contextid) {
75 // Release session lock.
76 \core\session\manager::write_close();
78 // Parameter validation.
79 self::validate_parameters(
80 self::get_async_backup_progress_parameters(),
81 array(
82 'backupids' => $backupids,
83 'contextid' => $contextid
87 // Context validation.
88 list($context, $course, $cm) = get_context_info_array($contextid);
89 self::validate_context($context);
91 if ($cm) {
92 require_capability('moodle/backup:backupactivity', $context);
93 } else {
94 require_capability('moodle/backup:backupcourse', $context);
97 $results = array();
98 foreach ($backupids as $backupid) {
99 $results[] = backup_controller_dbops::get_progress($backupid);
102 return $results;
106 * Returns description of method result value
108 * @return \core_external\external_description
109 * @since Moodle 3.7
111 public static function get_async_backup_progress_returns() {
112 return new external_multiple_structure(
113 new external_single_structure(
114 array(
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'
120 ), 'Backup data'
125 * Returns description of method parameters
127 * @return external_function_parameters
128 * @since Moodle 3.7
130 public static function get_async_backup_links_backup_parameters() {
131 return new external_function_parameters(
132 array(
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),
135 'backupid' => new external_value(PARAM_ALPHANUMEXT, 'Backup id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
141 * Get the data to be used when generating the table row for an asynchronous backup,
142 * the table row updates via ajax when backup is complete.
144 * @param string $filename The file name of the backup file.
145 * @param int $contextid The context the backup relates to.
146 * @param string $backupid The backup ID to get the backup settings.
147 * @since Moodle 3.7
149 public static function get_async_backup_links_backup($filename, $contextid, $backupid) {
150 // Release session lock.
151 \core\session\manager::write_close();
153 // Parameter validation.
154 self::validate_parameters(
155 self::get_async_backup_links_backup_parameters(),
156 array(
157 'filename' => $filename,
158 'contextid' => $contextid,
159 'backupid' => $backupid,
163 // Context validation.
164 list($context, $course, $cm) = get_context_info_array($contextid);
165 self::validate_context($context);
166 require_capability('moodle/backup:backupcourse', $context);
168 // Backups without user info or with the anonymise functionality enabled are sent
169 // to user's "user_backup" file area.
170 $filearea = 'backup';
171 // Get useful info to render async status in correct area.
172 $bc = \backup_controller::load_controller($backupid);
173 list($hasusers, $isannon) = \async_helper::get_userdata_backup_settings($bc);
174 if ($hasusers && !$isannon) {
175 if ($cm) {
176 $filearea = 'activity';
177 } else {
178 $filearea = 'course';
182 $results = \async_helper::get_backup_file_info($filename, $filearea, $contextid);
184 return $results;
188 * Returns description of method result value.
190 * @return \core_external\external_description
191 * @since Moodle 3.7
193 public static function get_async_backup_links_backup_returns() {
194 return new external_single_structure(
195 array(
196 'filesize' => new external_value(PARAM_TEXT, 'Backup file size'),
197 'fileurl' => new external_value(PARAM_URL, 'Backup file URL'),
198 'restoreurl' => new external_value(PARAM_URL, 'Backup restore URL'),
199 ), 'Table row data.');
202 * Returns description of method parameters
204 * @return external_function_parameters
205 * @since Moodle 3.7
207 public static function get_async_backup_links_restore_parameters() {
208 return new external_function_parameters(
209 array(
210 'backupid' => new external_value(PARAM_ALPHANUMEXT, 'Backup id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
211 'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
217 * Get the data to be used when generating the table row for an asynchronous restore,
218 * the table row updates via ajax when restore is complete.
220 * @param string $backupid The id of the backup record.
221 * @param int $contextid The context the restore relates to.
222 * @return array $results The array of results.
223 * @since Moodle 3.7
225 public static function get_async_backup_links_restore($backupid, $contextid) {
226 // Release session lock.
227 \core\session\manager::write_close();
229 // Parameter validation.
230 self::validate_parameters(
231 self::get_async_backup_links_restore_parameters(),
232 array(
233 'backupid' => $backupid,
234 'contextid' => $contextid
238 // Context validation.
239 if ($contextid == 0) {
240 $copyrec = \async_helper::get_backup_record($backupid);
241 $context = context_course::instance($copyrec->itemid);
242 } else {
243 $context = context::instance_by_id($contextid);
245 self::validate_context($context);
246 require_capability('moodle/restore:restorecourse', $context);
248 $results = \async_helper::get_restore_url($backupid);
250 return $results;
254 * Returns description of method result value.
256 * @return \core_external\external_description
257 * @since Moodle 3.7
259 public static function get_async_backup_links_restore_returns() {
260 return new external_single_structure(
261 array(
262 'restoreurl' => new external_value(PARAM_URL, 'Restore url'),
263 ), 'Table row data.');
267 * Returns description of method parameters
269 * @return external_function_parameters
270 * @since Moodle 3.9
272 public static function get_copy_progress_parameters() {
273 return new external_function_parameters(
274 array(
275 'copies' => new external_multiple_structure(
276 new external_single_structure(
277 array(
278 'backupid' => new external_value(PARAM_ALPHANUM, 'Backup id'),
279 'restoreid' => new external_value(PARAM_ALPHANUM, 'Restore id'),
280 'operation' => new external_value(PARAM_ALPHANUM, 'Operation type'),
281 ), 'Copy data'
282 ), 'Copy data'
289 * Get the data to be used when generating the table row for a course copy,
290 * the table row updates via ajax when copy is complete.
292 * @param array $copies Array of ids.
293 * @return array $results The array of results.
294 * @since Moodle 3.9
296 public static function get_copy_progress($copies) {
297 // Release session lock.
298 \core\session\manager::write_close();
300 // Parameter validation.
301 self::validate_parameters(
302 self::get_copy_progress_parameters(),
303 array('copies' => $copies)
306 $results = array();
308 foreach ($copies as $copy) {
310 if ($copy['operation'] == \backup::OPERATION_BACKUP) {
311 $copyid = $copy['backupid'];
312 } else {
313 $copyid = $copy['restoreid'];
316 $copyrec = \async_helper::get_backup_record($copyid);
317 $context = context_course::instance($copyrec->itemid);
318 self::validate_context($context);
320 $copycaps = \core_course\management\helper::get_course_copy_capabilities();
321 require_all_capabilities($copycaps, $context);
323 if ($copy['operation'] == \backup::OPERATION_BACKUP) {
324 $result = \backup_controller_dbops::get_progress($copyid);
325 if ($result['status'] == \backup::STATUS_FINISHED_OK) {
326 $copyid = $copy['restoreid'];
330 $results[] = \backup_controller_dbops::get_progress($copyid);
333 return $results;
337 * Returns description of method result value.
339 * @return \core_external\external_description
340 * @since Moodle 3.9
342 public static function get_copy_progress_returns() {
343 return new external_multiple_structure(
344 new external_single_structure(
345 array(
346 'status' => new external_value(PARAM_INT, 'Copy Status'),
347 'progress' => new external_value(PARAM_FLOAT, 'Copy progress'),
348 'backupid' => new external_value(PARAM_ALPHANUM, 'Copy id'),
349 'operation' => new external_value(PARAM_ALPHANUM, 'Operation type'),
350 ), 'Copy completion status'
351 ), 'Copy data'
356 * Returns description of method parameters
358 * @return external_function_parameters
359 * @since Moodle 3.9
361 public static function submit_copy_form_parameters() {
362 return new external_function_parameters(
363 array(
364 'jsonformdata' => new external_value(PARAM_RAW, 'The data from the create copy form, encoded as a json array')
370 * Submit the course group form.
372 * @param string $jsonformdata The data from the form, encoded as a json array.
373 * @return int new group id.
375 public static function submit_copy_form($jsonformdata) {
377 // Release session lock.
378 \core\session\manager::write_close();
380 // We always must pass webservice params through validate_parameters.
381 $params = self::validate_parameters(
382 self::submit_copy_form_parameters(),
383 array('jsonformdata' => $jsonformdata)
386 $formdata = json_decode($params['jsonformdata']);
388 $data = array();
389 parse_str($formdata, $data);
391 $context = context_course::instance($data['courseid']);
392 self::validate_context($context);
393 $copycaps = \core_course\management\helper::get_course_copy_capabilities();
394 require_all_capabilities($copycaps, $context);
396 // Submit the form data.
397 $course = get_course($data['courseid']);
398 $mform = new \core_backup\output\copy_form(
399 null,
400 array('course' => $course, 'returnto' => '', 'returnurl' => ''),
401 'post', '', ['class' => 'ignoredirty'], true, $data);
402 $mdata = $mform->get_data();
404 if ($mdata) {
405 // Create the copy task.
406 $copydata = \copy_helper::process_formdata($mdata);
407 $copyids = \copy_helper::create_copy($copydata);
408 } else {
409 throw new moodle_exception('copyformfail', 'backup');
412 return json_encode($copyids);
416 * Returns description of method result value.
418 * @return \core_external\external_description
419 * @since Moodle 3.9
421 public static function submit_copy_form_returns() {
422 return new external_value(PARAM_RAW, 'JSON response.');