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_policy\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_policy\api;
26
use tool_policy\policy_version;
27
use core_user;
28
 
29
/**
30
 * External function for setting user policies acceptances.
31
 *
32
 * @package    tool_policy
33
 * @copyright  2023 Juan Leyva <juan@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @since      Moodle 4.4
36
 */
37
class set_acceptances_status extends external_api {
38
 
39
    /**
40
     * Webservice parameters.
41
     *
42
     * @return external_function_parameters
43
     */
44
    public static function execute_parameters(): external_function_parameters {
45
        return new external_function_parameters(
46
            [
47
                'policies' => new external_multiple_structure(
48
                    new external_single_structure([
49
                        'versionid' => new external_value(PARAM_INT, 'The policy version id.'),
50
                        'status' => new external_value(PARAM_INT, 'The policy acceptance status. 0: decline, 1: accept.'),
51
                        'note' => new external_value(PARAM_NOTAGS,
52
                            'Any comments added by a user when giving consent on behalf of another user.', VALUE_OPTIONAL, null),
53
                    ]), 'Policies acceptances for the given user.'
54
                ),
55
                'userid' => new external_value(PARAM_INT,
56
                    'The user id we want to set the acceptances. Default is the current user.', VALUE_DEFAULT, 0
57
                ),
58
 
59
            ]
60
        );
61
    }
62
 
63
    /**
64
     * Set the acceptance status (accept or decline only) for the indicated policies for the given user.
65
     *
66
     * @param array $policies the policies to set the acceptance status
67
     * @param int $userid the user id we want to retrieve the acceptances
68
     * @throws \moodle_exception
69
     * @return array policies and acceptance status
70
     */
71
    public static function execute(array $policies, int $userid = 0): array {
72
        global $USER;
73
 
74
        $params = self::validate_parameters(self::execute_parameters(),
75
            [
76
                'policies' => $policies,
77
                'userid' => $userid,
78
            ]
79
        );
80
 
81
        // Do not check for the site policies in validate_context() to avoid the redirect loop.
82
        if (!defined('NO_SITEPOLICY_CHECK')) {
83
            define('NO_SITEPOLICY_CHECK', true);
84
        }
85
 
86
        $systemcontext = \context_system::instance();
87
        external_api::validate_context($systemcontext);
88
 
89
        if (empty($params['userid']) || $params['userid'] == $USER->id) {
90
            $user = $USER;
91
        } else {
92
            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
93
            core_user::require_active_user($user);
94
        }
95
 
96
        // Split acceptances.
97
        $requestedpolicies = $agreepolicies = $declinepolicies = $notes = [];
98
        foreach ($params['policies'] as $policy) {
99
            $requestedpolicies[$policy['versionid']] = $policy['status'];
100
            if ($USER->id != $user->id) {
101
                // Notes are only allowed when setting acceptances on behalf of another user.
102
                $notes[$policy['versionid']] = $policy['note'] ?? null;
103
            }
104
        }
105
 
106
        // Retrieve all  policies and their acceptances.
107
        $allpolicies = api::get_policies_with_acceptances($user->id);
108
        foreach ($allpolicies as $policy) {
109
            foreach ($policy->versions as $version) {
110
                if (isset($requestedpolicies[$version->id])) {
111
                    if ($requestedpolicies[$version->id] === 1) {
112
                        $agreepolicies[] = $version->id;
113
                    } else if ($requestedpolicies[$version->id] === 0) {
114
                        $declinepolicies[] = $version->id;
115
                    }
116
                }
117
            }
118
        }
119
 
120
        // Permissions check.
121
        api::can_accept_policies($agreepolicies, $user->id, true);
122
        api::can_decline_policies($declinepolicies, $user->id, true);
123
 
124
        // Good to go.
125
        foreach ($agreepolicies as $policyversionid) {
126
            api::accept_policies($policyversionid, $user->id, $notes[$policyversionid] ?? null);
127
        }
128
        foreach ($declinepolicies as $policyversionid) {
129
            api::decline_policies($policyversionid, $user->id, $notes[$policyversionid] ?? null);
130
        }
131
 
132
        $return = [
133
            'policyagreed' => (int) $user->policyagreed,  // Final policy agreement status for $user.
134
            'warnings' => [],
135
        ];
136
        return $return;
137
    }
138
 
139
    /**
140
     * Webservice returns.
141
     *
142
     * @return external_single_structure
143
     */
144
    public static function execute_returns(): external_single_structure {
145
        return new external_single_structure([
146
            'policyagreed' => new external_value(PARAM_INT,
147
                'Whether the user has provided acceptance to all current site policies. 1 if yes, 0 if not'),
148
            'warnings'  => new external_warnings(),
149
        ]);
150
    }
151
}