1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core_user\external;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use core_external\external_function_parameters;
|
|
|
21 |
use core_external\external_single_structure;
|
|
|
22 |
use core_external\external_value;
|
|
|
23 |
use core_external\external_warnings;
|
|
|
24 |
use context_user;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Updates current user private files.
|
|
|
28 |
*
|
|
|
29 |
* @package core_user
|
|
|
30 |
* @category external
|
|
|
31 |
* @copyright 2024 Juan Leyva
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class update_private_files extends external_api {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Describes the external function parameters.
|
|
|
38 |
*
|
|
|
39 |
* @return external_function_parameters
|
|
|
40 |
*/
|
|
|
41 |
public static function execute_parameters(): external_function_parameters {
|
|
|
42 |
return new external_function_parameters(
|
|
|
43 |
[
|
|
|
44 |
'draftitemid' => new external_value(PARAM_INT, 'The draft item id with the files.'),
|
|
|
45 |
]
|
|
|
46 |
);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Updates current user private files.
|
|
|
51 |
*
|
|
|
52 |
* @param int $draftitemid The draft item id with the files.
|
|
|
53 |
* @throws \moodle_exception;
|
|
|
54 |
* @return array
|
|
|
55 |
*/
|
|
|
56 |
public static function execute(int $draftitemid): array {
|
|
|
57 |
global $USER;
|
|
|
58 |
|
|
|
59 |
$params = self::validate_parameters(self::execute_parameters(), [
|
|
|
60 |
'draftitemid' => $draftitemid,
|
|
|
61 |
]);
|
|
|
62 |
$warnings = [];
|
|
|
63 |
|
|
|
64 |
$usercontext = context_user::instance($USER->id);
|
|
|
65 |
self::validate_context($usercontext);
|
|
|
66 |
|
|
|
67 |
$fs = get_file_storage();
|
|
|
68 |
if (empty($fs->get_area_files($usercontext->id, 'user', 'draft', $params['draftitemid']))) {
|
|
|
69 |
throw new \moodle_exception('Invalid draft item id.');
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// Data structure for the draft item id.
|
|
|
73 |
$data = ['files_filemanager' => $params['draftitemid']];
|
|
|
74 |
// Use existing form for validation.
|
|
|
75 |
$form = new \core_user\form\private_files();
|
|
|
76 |
$form->check_access_for_dynamic_submission();
|
|
|
77 |
$errors = $form->validation($data, []);
|
|
|
78 |
|
|
|
79 |
if (!empty($errors)) {
|
|
|
80 |
$status = false;
|
|
|
81 |
foreach ($errors as $itemname => $message) {
|
|
|
82 |
$warnings[] = [
|
|
|
83 |
'item' => $itemname,
|
|
|
84 |
'itemid' => 0,
|
|
|
85 |
'warningcode' => 'fielderror',
|
|
|
86 |
'message' => s($message),
|
|
|
87 |
];
|
|
|
88 |
}
|
|
|
89 |
} else {
|
|
|
90 |
file_postupdate_standard_filemanager((object) $data, 'files',
|
|
|
91 |
$form->get_options(), $usercontext, 'user', 'private', 0);
|
|
|
92 |
$status = true;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return [
|
|
|
96 |
'status' => $status,
|
|
|
97 |
'warnings' => $warnings,
|
|
|
98 |
];
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Describe the return structure of the external service.
|
|
|
103 |
*
|
|
|
104 |
* @return external_single_structure
|
|
|
105 |
*/
|
|
|
106 |
public static function execute_returns(): external_single_structure {
|
|
|
107 |
return new external_single_structure([
|
|
|
108 |
'status' => new external_value(PARAM_BOOL, 'The update result, true if everything went well.'),
|
|
|
109 |
'warnings' => new external_warnings(),
|
|
|
110 |
]);
|
|
|
111 |
}
|
|
|
112 |
}
|