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');
31 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
34 * Backup external functions.
36 * @package core_backup
38 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_backup_external
extends external_api
{
45 * Returns description of method parameters
47 * @return external_function_parameters
50 public static function get_async_backup_progress_parameters() {
51 return new external_function_parameters(
53 'backupids' => new external_multiple_structure(
54 new external_value(PARAM_ALPHANUM
, 'Backup id to get progress for', VALUE_REQUIRED
, null, NULL_ALLOWED
),
55 'Backup id to get progress for', VALUE_REQUIRED
57 'contextid' => new external_value(PARAM_INT
, 'Context id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
63 * Get asynchronous backup progress.
65 * @param string $backupids The ids of the backup to get progress for.
66 * @param int $contextid The context the backup relates to.
67 * @return array $results The array of results.
70 public static function get_async_backup_progress($backupids, $contextid) {
71 // Release session lock.
72 \core\session\manager
::write_close();
74 // Parameter validation.
75 self
::validate_parameters(
76 self
::get_async_backup_progress_parameters(),
78 'backupids' => $backupids,
79 'contextid' => $contextid
83 // Context validation.
84 list($context, $course, $cm) = get_context_info_array($contextid);
85 self
::validate_context($context);
88 require_capability('moodle/backup:backupactivity', $context);
90 require_capability('moodle/backup:backupcourse', $context);
94 foreach ($backupids as $backupid) {
95 $results[] = backup_controller_dbops
::get_progress($backupid);
102 * Returns description of method result value
104 * @return external_description
107 public static function get_async_backup_progress_returns() {
108 return new external_multiple_structure(
109 new external_single_structure(
111 'status' => new external_value(PARAM_INT
, 'Backup Status'),
112 'progress' => new external_value(PARAM_FLOAT
, 'Backup progress'),
113 'backupid' => new external_value(PARAM_ALPHANUM
, 'Backup id'),
114 'operation' => new external_value(PARAM_ALPHANUM
, 'operation type'),
115 ), 'Backup completion status'
121 * Returns description of method parameters
123 * @return external_function_parameters
126 public static function get_async_backup_links_backup_parameters() {
127 return new external_function_parameters(
129 'filename' => new external_value(PARAM_FILE
, 'Backup filename', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
130 'contextid' => new external_value(PARAM_INT
, 'Context id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
136 * Get the data to be used when generating the table row for an asynchronous backup,
137 * the table row updates via ajax when backup is complete.
139 * @param string $filename The file name of the backup file.
140 * @param int $contextid The context the backup relates to.
143 public static function get_async_backup_links_backup($filename, $contextid) {
144 // Release session lock.
145 \core\session\manager
::write_close();
147 // Parameter validation.
148 self
::validate_parameters(
149 self
::get_async_backup_links_backup_parameters(),
151 'filename' => $filename,
152 'contextid' => $contextid
156 // Context validation.
157 list($context, $course, $cm) = get_context_info_array($contextid);
158 self
::validate_context($context);
159 require_capability('moodle/backup:backupcourse', $context);
162 $filearea = 'activity';
164 $filearea = 'course';
167 $results = \async_helper
::get_backup_file_info($filename, $filearea, $contextid);
173 * Returns description of method result value.
175 * @return external_description
178 public static function get_async_backup_links_backup_returns() {
179 return new external_single_structure(
181 'filesize' => new external_value(PARAM_TEXT
, 'Backup file size'),
182 'fileurl' => new external_value(PARAM_URL
, 'Backup file URL'),
183 'restoreurl' => new external_value(PARAM_URL
, 'Backup restore URL'),
184 ), 'Table row data.');
187 * Returns description of method parameters
189 * @return external_function_parameters
192 public static function get_async_backup_links_restore_parameters() {
193 return new external_function_parameters(
195 'backupid' => new external_value(PARAM_ALPHANUMEXT
, 'Backup id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
196 'contextid' => new external_value(PARAM_INT
, 'Context id', VALUE_REQUIRED
, null, NULL_NOT_ALLOWED
),
202 * Get the data to be used when generating the table row for an asynchronous restore,
203 * the table row updates via ajax when restore is complete.
205 * @param string $backupid The id of the backup record.
206 * @param int $contextid The context the restore relates to.
207 * @return array $results The array of results.
210 public static function get_async_backup_links_restore($backupid, $contextid) {
211 // Release session lock.
212 \core\session\manager
::write_close();
214 // Parameter validation.
215 self
::validate_parameters(
216 self
::get_async_backup_links_restore_parameters(),
218 'backupid' => $backupid,
219 'contextid' => $contextid
223 // Context validation.
224 if ($contextid == 0) {
225 $copyrec = \async_helper
::get_backup_record($backupid);
226 $context = context_course
::instance($copyrec->itemid
);
228 $context = context
::instance_by_id($contextid);
230 self
::validate_context($context);
231 require_capability('moodle/restore:restorecourse', $context);
233 $results = \async_helper
::get_restore_url($backupid);
239 * Returns description of method result value.
241 * @return external_description
244 public static function get_async_backup_links_restore_returns() {
245 return new external_single_structure(
247 'restoreurl' => new external_value(PARAM_URL
, 'Restore url'),
248 ), 'Table row data.');
252 * Returns description of method parameters
254 * @return external_function_parameters
257 public static function get_copy_progress_parameters() {
258 return new external_function_parameters(
260 'copies' => new external_multiple_structure(
261 new external_single_structure(
263 'backupid' => new external_value(PARAM_ALPHANUM
, 'Backup id'),
264 'restoreid' => new external_value(PARAM_ALPHANUM
, 'Restore id'),
265 'operation' => new external_value(PARAM_ALPHANUM
, 'Operation type'),
274 * Get the data to be used when generating the table row for a course copy,
275 * the table row updates via ajax when copy is complete.
277 * @param array $copies Array of ids.
278 * @return array $results The array of results.
281 public static function get_copy_progress($copies) {
282 // Release session lock.
283 \core\session\manager
::write_close();
285 // Parameter validation.
286 self
::validate_parameters(
287 self
::get_copy_progress_parameters(),
288 array('copies' => $copies)
293 foreach ($copies as $copy) {
295 if ($copy['operation'] == \backup
::OPERATION_BACKUP
) {
296 $copyid = $copy['backupid'];
298 $copyid = $copy['restoreid'];
301 $copyrec = \async_helper
::get_backup_record($copyid);
302 $context = context_course
::instance($copyrec->itemid
);
303 self
::validate_context($context);
305 $copycaps = \core_course\management\helper
::get_course_copy_capabilities();
306 require_all_capabilities($copycaps, $context);
308 if ($copy['operation'] == \backup
::OPERATION_BACKUP
) {
309 $result = \backup_controller_dbops
::get_progress($copyid);
310 if ($result['status'] == \backup
::STATUS_FINISHED_OK
) {
311 $copyid = $copy['restoreid'];
315 $results[] = \backup_controller_dbops
::get_progress($copyid);
322 * Returns description of method result value.
324 * @return external_description
327 public static function get_copy_progress_returns() {
328 return new external_multiple_structure(
329 new external_single_structure(
331 'status' => new external_value(PARAM_INT
, 'Copy Status'),
332 'progress' => new external_value(PARAM_FLOAT
, 'Copy progress'),
333 'backupid' => new external_value(PARAM_ALPHANUM
, 'Copy id'),
334 'operation' => new external_value(PARAM_ALPHANUM
, 'Operation type'),
335 ), 'Copy completion status'
341 * Returns description of method parameters
343 * @return external_function_parameters
346 public static function submit_copy_form_parameters() {
347 return new external_function_parameters(
349 'jsonformdata' => new external_value(PARAM_RAW
, 'The data from the create copy form, encoded as a json array')
355 * Submit the course group form.
357 * @param string $jsonformdata The data from the form, encoded as a json array.
358 * @return int new group id.
360 public static function submit_copy_form($jsonformdata) {
362 // Release session lock.
363 \core\session\manager
::write_close();
365 // We always must pass webservice params through validate_parameters.
366 $params = self
::validate_parameters(
367 self
::submit_copy_form_parameters(),
368 array('jsonformdata' => $jsonformdata)
371 $formdata = json_decode($params['jsonformdata']);
374 parse_str($formdata, $data);
376 $context = context_course
::instance($data['courseid']);
377 self
::validate_context($context);
378 $copycaps = \core_course\management\helper
::get_course_copy_capabilities();
379 require_all_capabilities($copycaps, $context);
381 // Submit the form data.
382 $course = get_course($data['courseid']);
383 $mform = new \core_backup\output\
copy_form(
385 array('course' => $course, 'returnto' => '', 'returnurl' => ''),
386 'post', '', ['class' => 'ignoredirty'], true, $data);
387 $mdata = $mform->get_data();
390 // Create the copy task.
391 $backupcopy = new \core_backup\copy\
copy($mdata);
392 $copyids = $backupcopy->create_copy();
394 throw new moodle_exception('copyformfail', 'backup');
397 return json_encode($copyids);
401 * Returns description of method result value.
403 * @return external_description
406 public static function submit_copy_form_returns() {
407 return new external_value(PARAM_RAW
, 'JSON response.');