Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_value;
23
use core_external\external_warnings;
24
use tool_dataprivacy\api;
25
use core_user;
26
use moodle_exception;
27
 
28
/**
29
 * External function for creating a data request.
30
 *
31
 * @package    tool_dataprivacy
32
 * @copyright  2023 Juan Leyva <juan@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @since      Moodle 4.4
35
 */
36
class create_data_request extends external_api {
37
 
38
    /**
39
     * Webservice parameters.
40
     *
41
     * @return external_function_parameters
42
     */
43
    public static function execute_parameters(): external_function_parameters {
44
        return new external_function_parameters(
45
            [
46
                'type' => new external_value(PARAM_INT, 'The type of data request to create. 1 for export, 2 for data deletion.'),
47
                'comments' => new external_value(PARAM_RAW, 'Comments for the data request.', VALUE_DEFAULT, ''),
48
                'foruserid' => new external_value(PARAM_INT, 'The id of the user to create the data request for. Empty for current user.',
49
                    VALUE_DEFAULT, 0),
50
            ]
51
        );
52
    }
53
 
54
    /**
55
     * Create a data request.
56
     *
57
     * @param int $type The type of data request to create.
58
     * @param string $comments Comments for the data request.
59
     * @param int $foruserid The id of the user to create the data request for.
60
     * @return array containing the id of the request created and warnings.
61
     * @throws moodle_exception
62
     */
63
    public static function execute(int $type, string $comments = '', int $foruserid = 0): array {
64
        global $USER;
65
 
66
        $params = self::validate_parameters(self::execute_parameters(), [
67
            'type' => $type,
68
            'comments' => $comments,
69
            'foruserid' => $foruserid,
70
        ]);
71
 
72
        $system = \context_system::instance();
73
        external_api::validate_context($system);
74
 
75
        $cancontactdpo = api::can_contact_dpo();
76
        $canmanage = false;
77
 
78
        if (empty($params['foruserid']) || $params['foruserid'] == $USER->id) {
79
            $user = $USER;
80
        } else {
81
            $user = core_user::get_user($params['foruserid'], '*', MUST_EXIST);
82
            core_user::require_active_user($user);
83
 
84
            if (!$canmanage = api::can_manage_data_requests($user->id)) {
85
                api::require_can_create_data_request_for_user($user->id);
86
            }
87
        }
88
 
89
        if (!$canmanage && !$cancontactdpo) {
90
            throw new moodle_exception('contactdpoviaprivacypolicy', 'tool_dataprivacy');
91
        }
92
 
93
        // Validate the data.
94
        $validationerrors = api::validate_create_data_request((object) ['userid' => $user->id, 'type' => $params['type']]);
95
        if (!empty($validationerrors)) {
96
            $error = array_key_first($validationerrors);
97
            throw new moodle_exception($error, 'tool_dataprivacy');
98
        }
99
        // All clear now, create the request.
100
        $datarequest = api::create_data_request($user->id, $params['type'], $params['comments']);
101
 
102
        return [
103
            'datarequestid' => $datarequest->get('id'),
104
            'warnings' => [],
105
        ];
106
    }
107
 
108
    /**
109
     * Webservice returns.
110
     *
111
     * @return external_single_structure
112
     */
113
    public static function execute_returns(): external_single_structure {
114
        return new external_single_structure(
115
            [
116
                'datarequestid' => new external_value(PARAM_INT, 'The id of the created data request.'),
117
                'warnings' => new external_warnings(),
118
            ]
119
        );
120
    }
121
}