1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace tool_dataprivacy\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_multiple_structure;
|
|
|
23 |
use core_external\external_value;
|
|
|
24 |
use core_external\external_warnings;
|
|
|
25 |
use tool_dataprivacy\api;
|
|
|
26 |
use core_user;
|
|
|
27 |
use context_system;
|
|
|
28 |
use moodle_exception;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* External function for getting data requests.
|
|
|
32 |
*
|
|
|
33 |
* @package tool_dataprivacy
|
|
|
34 |
* @copyright 2023 Juan Leyva <juan@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
* @since Moodle 4.4
|
|
|
37 |
*/
|
|
|
38 |
class get_data_requests extends external_api {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Webservice parameters.
|
|
|
42 |
*
|
|
|
43 |
* @return external_function_parameters
|
|
|
44 |
*/
|
|
|
45 |
public static function execute_parameters(): external_function_parameters {
|
|
|
46 |
return new external_function_parameters(
|
|
|
47 |
[
|
|
|
48 |
'userid' => new external_value(PARAM_INT, 'The id of the user to get the data requests for. Empty for all users.',
|
|
|
49 |
VALUE_DEFAULT, 0),
|
|
|
50 |
'statuses' => new external_multiple_structure(
|
|
|
51 |
new external_value(PARAM_INT, 'The status of the data requests to get.'),
|
|
|
52 |
'The statuses of the data requests to get.
|
|
|
53 |
|
|
|
54 |
4 processed, 5 completed, 6 cancelled, 7 rejected.',
|
|
|
55 |
VALUE_DEFAULT,
|
|
|
56 |
[]
|
|
|
57 |
),
|
|
|
58 |
'types' => new external_multiple_structure(
|
|
|
59 |
new external_value(PARAM_INT, 'The type of the data requests to get.'),
|
|
|
60 |
'The types of the data requests to get. 1 for export, 2 for data deletion.',
|
|
|
61 |
VALUE_DEFAULT,
|
|
|
62 |
[]
|
|
|
63 |
),
|
|
|
64 |
'creationmethods' => new external_multiple_structure(
|
|
|
65 |
new external_value(PARAM_INT, 'The creation method of the data requests to get.'),
|
|
|
66 |
'The creation methods of the data requests to get. 0 for manual, 1 for automatic.',
|
|
|
67 |
VALUE_DEFAULT,
|
|
|
68 |
[]
|
|
|
69 |
),
|
|
|
70 |
'sort' => new external_value(PARAM_NOTAGS, 'The field to sort the data requests by.',
|
|
|
71 |
VALUE_DEFAULT, ''),
|
|
|
72 |
'limitfrom' => new external_value(PARAM_INT, 'The number to start getting the data requests from.',
|
|
|
73 |
VALUE_DEFAULT, 0),
|
|
|
74 |
'limitnum' => new external_value(PARAM_INT, 'The number of data requests to get.',
|
|
|
75 |
VALUE_DEFAULT, 0),
|
|
|
76 |
]
|
|
|
77 |
);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Get data requests.
|
|
|
83 |
*
|
|
|
84 |
* @param int $userid The user id.
|
|
|
85 |
* @param array $statuses The status filters.
|
|
|
86 |
* @param array $types The request type filters.
|
|
|
87 |
* @param array $creationmethods The request creation method filters.
|
|
|
88 |
* @param string $sort The order by clause.
|
|
|
89 |
* @param int $limitfrom Amount of records to skip.
|
|
|
90 |
* @param int $limitnum Amount of records to fetch.
|
|
|
91 |
* @throws moodle_exception
|
|
|
92 |
* @return array containing the data requests and warnings.
|
|
|
93 |
*/
|
|
|
94 |
public static function execute($userid = 0, $statuses = [], $types = [], $creationmethods = [],
|
|
|
95 |
$sort = '', $limitfrom = 0, $limitnum = 0): array {
|
|
|
96 |
|
|
|
97 |
global $USER, $PAGE;
|
|
|
98 |
|
|
|
99 |
$params = self::validate_parameters(self::execute_parameters(), [
|
|
|
100 |
'userid' => $userid,
|
|
|
101 |
'statuses' => $statuses,
|
|
|
102 |
'types' => $types,
|
|
|
103 |
'creationmethods' => $creationmethods,
|
|
|
104 |
'sort' => $sort,
|
|
|
105 |
'limitfrom' => $limitfrom,
|
|
|
106 |
'limitnum' => $limitnum,
|
|
|
107 |
]);
|
|
|
108 |
$systemcontext = context_system::instance();
|
|
|
109 |
|
|
|
110 |
if ($params['userid'] == $USER->id) {
|
|
|
111 |
$userid = $USER->id;
|
|
|
112 |
} else {
|
|
|
113 |
// Additional security checks when obtaining data requests for other users.
|
|
|
114 |
if (!has_capability('tool/dataprivacy:managedatarequests', $systemcontext) || !api::is_site_dpo($USER->id)) {
|
|
|
115 |
$dponamestring = implode (', ', api::get_dpo_role_names());
|
|
|
116 |
throw new moodle_exception('privacyofficeronly', 'tool_dataprivacy', '', $dponamestring);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$userid = 0;
|
|
|
120 |
if (!empty($params['userid'])) {
|
|
|
121 |
$user = core_user::get_user($params['userid'], '*', MUST_EXIST);
|
|
|
122 |
core_user::require_active_user($user);
|
|
|
123 |
$userid = $user->id;
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
// Ensure sort parameter is safe to use. Fallback to default value of the parameter itself.
|
|
|
128 |
$sortorderparts = explode(' ', $params['sort'], 2);
|
|
|
129 |
$sortorder = get_safe_orderby([
|
|
|
130 |
'id' => 'id',
|
|
|
131 |
'status' => 'status',
|
|
|
132 |
'timemodified' => 'timemodified',
|
|
|
133 |
'default' => '',
|
|
|
134 |
], $sortorderparts[0], $sortorderparts[1] ?? '', false);
|
|
|
135 |
|
|
|
136 |
$userrequests = api::get_data_requests($userid, $params['statuses'], $params['types'], $params['creationmethods'],
|
|
|
137 |
$sortorder, $params['limitfrom'], $params['limitnum']);
|
|
|
138 |
|
|
|
139 |
$requests = [];
|
|
|
140 |
foreach ($userrequests as $requestpersistent) {
|
|
|
141 |
$exporter = new data_request_exporter($requestpersistent, ['context' => $systemcontext]);
|
|
|
142 |
$renderer = $PAGE->get_renderer('tool_dataprivacy');
|
|
|
143 |
$requests[] = $exporter->export($renderer);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return [
|
|
|
147 |
'requests' => $requests,
|
|
|
148 |
'warnings' => [],
|
|
|
149 |
];
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Webservice returns.
|
|
|
154 |
*
|
|
|
155 |
* @return external_single_structure
|
|
|
156 |
*/
|
|
|
157 |
public static function execute_returns(): external_single_structure {
|
|
|
158 |
return new external_single_structure(
|
|
|
159 |
[
|
|
|
160 |
'requests' => new external_multiple_structure(data_request_exporter::get_read_structure(), 'The data requests.'),
|
|
|
161 |
'warnings' => new external_warnings(),
|
|
|
162 |
]
|
|
|
163 |
);
|
|
|
164 |
}
|
|
|
165 |
}
|