forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternallib.php
248 lines (220 loc) · 8.52 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
<?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
*/
defined('MOODLE_INTERNAL') || die;
require_once("$CFG->libdir/externallib.php");
require_once($CFG->dirroot . '/backup/util/includes/backup_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) {
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
// 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 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 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.
$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 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.');
}
}