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_multiple_structure;
|
|
|
22 |
use core_external\external_single_structure;
|
|
|
23 |
use core_external\external_value;
|
|
|
24 |
use core_external\external_warnings;
|
|
|
25 |
use context_user;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Prepares the draft area for user private files.
|
|
|
29 |
*
|
|
|
30 |
* @package core_user
|
|
|
31 |
* @category external
|
|
|
32 |
* @copyright 2024 Juan Leyva
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class prepare_private_files_for_edition extends external_api {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Describes the external function parameters.
|
|
|
39 |
*
|
|
|
40 |
* @return external_function_parameters
|
|
|
41 |
*/
|
|
|
42 |
public static function execute_parameters(): external_function_parameters {
|
|
|
43 |
return new external_function_parameters([]);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Prepare a draft area for private files.
|
|
|
48 |
*
|
|
|
49 |
* @throws \moodle_exception;
|
|
|
50 |
* @return array
|
|
|
51 |
*/
|
|
|
52 |
public static function execute(): array {
|
|
|
53 |
global $USER;
|
|
|
54 |
|
|
|
55 |
$usercontext = context_user::instance($USER->id);
|
|
|
56 |
self::validate_context($usercontext);
|
|
|
57 |
|
|
|
58 |
$form = new \core_user\form\private_files();
|
|
|
59 |
// Permission checks.
|
|
|
60 |
$form->check_access_for_dynamic_submission();
|
|
|
61 |
|
|
|
62 |
$areaoptions = $form->get_options();
|
|
|
63 |
$draftitemid = 0;
|
|
|
64 |
file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0, $areaoptions);
|
|
|
65 |
|
|
|
66 |
// Just get a structure compatible with external API.
|
|
|
67 |
array_walk($areaoptions, function(&$item, $key) {
|
|
|
68 |
$item = ['name' => $key, 'value' => $item];
|
|
|
69 |
});
|
|
|
70 |
|
|
|
71 |
return [
|
|
|
72 |
'draftitemid' => $draftitemid,
|
|
|
73 |
'areaoptions' => $areaoptions,
|
|
|
74 |
'warnings' => [],
|
|
|
75 |
];
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Describe the return structure of the external service.
|
|
|
80 |
*
|
|
|
81 |
* @return external_single_structure
|
|
|
82 |
*/
|
|
|
83 |
public static function execute_returns(): external_single_structure {
|
|
|
84 |
return new external_single_structure(
|
|
|
85 |
[
|
|
|
86 |
'draftitemid' => new external_value(PARAM_INT, 'Draft item id for the file area.'),
|
|
|
87 |
'areaoptions' => new external_multiple_structure(
|
|
|
88 |
new external_single_structure(
|
|
|
89 |
[
|
|
|
90 |
'name' => new external_value(PARAM_RAW, 'Name of option.'),
|
|
|
91 |
'value' => new external_value(PARAM_RAW, 'Value of option.'),
|
|
|
92 |
]
|
|
|
93 |
), 'Draft file area options.'
|
|
|
94 |
),
|
|
|
95 |
'warnings' => new external_warnings(),
|
|
|
96 |
]
|
|
|
97 |
);
|
|
|
98 |
}
|
|
|
99 |
}
|