forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
externallib.php
413 lines (363 loc) · 14.1 KB
/
externallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?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/>.
/**
* External backup API.
*
* @package core_backup
* @category external
* @copyright 2018 Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Backup external functions.
*
* @package core_backup
* @category external
* @copyright 2018 Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.7
*/
class core_backup_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.7
*/
public static function get_async_backup_progress_parameters() {
return new external_function_parameters(
array(
'backupids' => new external_multiple_structure(
new external_value(PARAM_ALPHANUM, 'Backup id to get progress for', VALUE_REQUIRED, null, NULL_ALLOWED),
'Backup id to get progress for', VALUE_REQUIRED
),
'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
)
);
}
/**
* Get asynchronous backup progress.
*
* @param string $backupids The ids of the backup to get progress for.
* @param int $contextid The context the backup relates to.
* @return array $results The array of results.
* @since Moodle 3.7
*/
public static function get_async_backup_progress($backupids, $contextid) {
// Release session lock.
\core\session\manager::write_close();
// Parameter validation.
self::validate_parameters(
self::get_async_backup_progress_parameters(),
array(
'backupids' => $backupids,
'contextid' => $contextid
)
);
// Context validation.
list($context, $course, $cm) = get_context_info_array($contextid);
self::validate_context($context);
if ($cm) {
require_capability('moodle/backup:backupactivity', $context);
} else {
require_capability('moodle/backup:backupcourse', $context);
}
$results = array();
foreach ($backupids as $backupid) {
$results[] = backup_controller_dbops::get_progress($backupid);
}
return $results;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.7
*/
public static function get_async_backup_progress_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'status' => new external_value(PARAM_INT, 'Backup Status'),
'progress' => new external_value(PARAM_FLOAT, 'Backup progress'),
'backupid' => new external_value(PARAM_ALPHANUM, 'Backup id'),
'operation' => new external_value(PARAM_ALPHANUM, 'operation type'),
), 'Backup completion status'
), 'Backup data'
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.7
*/
public static function get_async_backup_links_backup_parameters() {
return new external_function_parameters(
array(
'filename' => new external_value(PARAM_FILE, 'Backup filename', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
)
);
}
/**
* Get the data to be used when generating the table row for an asynchronous backup,
* the table row updates via ajax when backup is complete.
*
* @param string $filename The file name of the backup file.
* @param int $contextid The context the backup relates to.
* @since Moodle 3.7
*/
public static function get_async_backup_links_backup($filename, $contextid) {
// Release session lock.
\core\session\manager::write_close();
// Parameter validation.
self::validate_parameters(
self::get_async_backup_links_backup_parameters(),
array(
'filename' => $filename,
'contextid' => $contextid
)
);
// Context validation.
list($context, $course, $cm) = get_context_info_array($contextid);
self::validate_context($context);
require_capability('moodle/backup:backupcourse', $context);
if ($cm) {
$filearea = 'activity';
} else {
$filearea = 'course';
}
$results = \async_helper::get_backup_file_info($filename, $filearea, $contextid);
return $results;
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description
* @since Moodle 3.7
*/
public static function get_async_backup_links_backup_returns() {
return new external_single_structure(
array(
'filesize' => new external_value(PARAM_TEXT, 'Backup file size'),
'fileurl' => new external_value(PARAM_URL, 'Backup file URL'),
'restoreurl' => new external_value(PARAM_URL, 'Backup restore URL'),
), 'Table row data.');
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.7
*/
public static function get_async_backup_links_restore_parameters() {
return new external_function_parameters(
array(
'backupid' => new external_value(PARAM_ALPHANUMEXT, 'Backup id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
)
);
}
/**
* Get the data to be used when generating the table row for an asynchronous restore,
* the table row updates via ajax when restore is complete.
*
* @param string $backupid The id of the backup record.
* @param int $contextid The context the restore relates to.
* @return array $results The array of results.
* @since Moodle 3.7
*/
public static function get_async_backup_links_restore($backupid, $contextid) {
// Release session lock.
\core\session\manager::write_close();
// Parameter validation.
self::validate_parameters(
self::get_async_backup_links_restore_parameters(),
array(
'backupid' => $backupid,
'contextid' => $contextid
)
);
// Context validation.
if ($contextid == 0) {
$copyrec = \async_helper::get_backup_record($backupid);
$context = context_course::instance($copyrec->itemid);
} else {
$context = context::instance_by_id($contextid);
}
self::validate_context($context);
require_capability('moodle/restore:restorecourse', $context);
$results = \async_helper::get_restore_url($backupid);
return $results;
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description
* @since Moodle 3.7
*/
public static function get_async_backup_links_restore_returns() {
return new external_single_structure(
array(
'restoreurl' => new external_value(PARAM_URL, 'Restore url'),
), 'Table row data.');
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.9
*/
public static function get_copy_progress_parameters() {
return new external_function_parameters(
array(
'copies' => new external_multiple_structure(
new external_single_structure(
array(
'backupid' => new external_value(PARAM_ALPHANUM, 'Backup id'),
'restoreid' => new external_value(PARAM_ALPHANUM, 'Restore id'),
'operation' => new external_value(PARAM_ALPHANUM, 'Operation type'),
), 'Copy data'
), 'Copy data'
),
)
);
}
/**
* Get the data to be used when generating the table row for a course copy,
* the table row updates via ajax when copy is complete.
*
* @param array $copies Array of ids.
* @return array $results The array of results.
* @since Moodle 3.9
*/
public static function get_copy_progress($copies) {
// Release session lock.
\core\session\manager::write_close();
// Parameter validation.
self::validate_parameters(
self::get_copy_progress_parameters(),
array('copies' => $copies)
);
$results = array();
foreach ($copies as $copy) {
if ($copy['operation'] == \backup::OPERATION_BACKUP) {
$copyid = $copy['backupid'];
} else {
$copyid = $copy['restoreid'];
}
$copyrec = \async_helper::get_backup_record($copyid);
$context = context_course::instance($copyrec->itemid);
self::validate_context($context);
$copycaps = \core_course\management\helper::get_course_copy_capabilities();
require_all_capabilities($copycaps, $context);
if ($copy['operation'] == \backup::OPERATION_BACKUP) {
$result = \backup_controller_dbops::get_progress($copyid);
if ($result['status'] == \backup::STATUS_FINISHED_OK) {
$copyid = $copy['restoreid'];
}
}
$results[] = \backup_controller_dbops::get_progress($copyid);
}
return $results;
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description
* @since Moodle 3.9
*/
public static function get_copy_progress_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'status' => new external_value(PARAM_INT, 'Copy Status'),
'progress' => new external_value(PARAM_FLOAT, 'Copy progress'),
'backupid' => new external_value(PARAM_ALPHANUM, 'Copy id'),
'operation' => new external_value(PARAM_ALPHANUM, 'Operation type'),
), 'Copy completion status'
), 'Copy data'
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.9
*/
public static function submit_copy_form_parameters() {
return new external_function_parameters(
array(
'jsonformdata' => new external_value(PARAM_RAW, 'The data from the create copy form, encoded as a json array')
)
);
}
/**
* Submit the course group form.
*
* @param string $jsonformdata The data from the form, encoded as a json array.
* @return int new group id.
*/
public static function submit_copy_form($jsonformdata) {
// Release session lock.
\core\session\manager::write_close();
// We always must pass webservice params through validate_parameters.
$params = self::validate_parameters(
self::submit_copy_form_parameters(),
array('jsonformdata' => $jsonformdata)
);
$formdata = json_decode($params['jsonformdata']);
$data = array();
parse_str($formdata, $data);
$context = context_course::instance($data['courseid']);
self::validate_context($context);
$copycaps = \core_course\management\helper::get_course_copy_capabilities();
require_all_capabilities($copycaps, $context);
// Submit the form data.
$course = get_course($data['courseid']);
$mform = new \core_backup\output\copy_form(
null,
array('course' => $course, 'returnto' => '', 'returnurl' => ''),
'post', '', ['class' => 'ignoredirty'], true, $data);
$mdata = $mform->get_data();
if ($mdata) {
// Create the copy task.
$copydata = \copy_helper::process_formdata($mdata);
$copyids = \copy_helper::create_copy($copydata);
} else {
throw new moodle_exception('copyformfail', 'backup');
}
return json_encode($copyids);
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description
* @since Moodle 3.9
*/
public static function submit_copy_form_returns() {
return new external_value(PARAM_RAW, 'JSON response.');
}
}