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_format_value;
24
use core_external\external_value;
25
use core_external\external_warnings;
26
use tool_policy\api;
27
use context_user;
28
use core_user;
29
use core_external\util;
30
 
31
/**
32
 * External function for retrieving user policies acceptances.
33
 *
34
 * @package    tool_policy
35
 * @copyright  2023 Juan Leyva <juan@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 * @since      Moodle 4.4
38
 */
39
class get_user_acceptances extends external_api {
40
 
41
    /**
42
     * Webservice parameters.
43
     *
44
     * @return external_function_parameters
45
     */
46
    public static function execute_parameters(): external_function_parameters {
47
        return new external_function_parameters(
48
            [
49
                'userid' => new external_value(PARAM_INT, 'The user id we want to retrieve the acceptances.',
50
                    VALUE_DEFAULT, 0),
51
            ]
52
        );
53
    }
54
 
55
    /**
56
     * Returns the acceptance status for all the policies the given user can see.
57
     *
58
     * @param int $userid the user id we want to retrieve the acceptances
59
     * @throws \required_capability_exception
60
     * @return array policies and acceptance status
61
     */
62
    public static function execute(int $userid = 0): array {
63
        global $USER;
64
 
65
        $params = self::validate_parameters(self::execute_parameters(),
66
            [
67
                'userid' => $userid,
68
            ]
69
        );
70
 
71
        // Do not check for the site policies in validate_context() to avoid the redirect loop.
72
        if (!defined('NO_SITEPOLICY_CHECK')) {
73
            define('NO_SITEPOLICY_CHECK', true);
74
        }
75
 
76
        $systemcontext = \context_system::instance();
77
        external_api::validate_context($systemcontext);
78
 
79
        if (empty($params['userid']) || $params['userid'] == $USER->id) {
80
            $user = $USER;
81
        } else {
82
            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
83
            core_user::require_active_user($user);
84
            $usercontext = context_user::instance($user->id);
85
            // Check capability to view acceptances. No capability is needed to view your own acceptances.
86
            if (!has_capability('tool/policy:acceptbehalf', $usercontext)) {
87
                require_capability('tool/policy:viewacceptances', $usercontext);
88
            }
89
        }
90
 
91
        $canviewfullnames = has_capability('moodle/site:viewfullnames', $systemcontext);
92
        $userpolicies = api::get_policies_with_acceptances($user->id);
93
 
94
        $policies = [];
95
        foreach ($userpolicies as $userpolicy) {
96
            foreach ($userpolicy->versions as $version) {
97
 
98
                $policy = (array) clone $version;
99
                unset($policy['acceptance']); // This might return NULL and break the WS response.
100
                $policy['versionid'] = $version->id;
101
                $policy['name'] = util::format_string($version->name, $systemcontext);
102
                $policy['revision'] = util::format_string($version->revision, $systemcontext);
103
                [$policy['summary'], $policy['summaryformat']] = util::format_text($version->summary,
104
                    $version->summaryformat, $systemcontext);
105
                [$policy['content'], $policy['contentformat']] = util::format_text($version->content,
106
                    $version->contentformat, $systemcontext);
107
 
108
                if (!empty($version->acceptance)) {
109
                    $policy['acceptance'] = (array) $version->acceptance;
110
                    if ($version->acceptance->usermodified && $version->acceptance->usermodified != $user->id) {
111
                        // Get the full name of who accepted on behalf.
112
                        $usermodified = (object)['id' => $version->acceptance->usermodified];
113
                        username_load_fields_from_object($usermodified, $version->acceptance, 'mod');
114
                        $override = $canviewfullnames || has_capability('moodle/site:viewfullnames', context_user::instance($version->acceptance->usermodified));
115
                        $policy['acceptance']['modfullname'] = fullname($usermodified, $override);
116
                    }
117
                    if (!empty($version->acceptance->note)) {
118
                        [$policy['acceptance']['note']] = util::format_text($version->acceptance->note, FORMAT_MOODLE, $systemcontext);
119
                    }
120
                }
121
                // Return permission for actions for the current policy and user.
122
                $policy['canaccept'] = api::can_accept_policies([$version->id], $user->id);
123
                $policy['candecline'] = api::can_decline_policies([$version->id], $user->id);
124
                $policy['canrevoke'] = api::can_revoke_policies([$version->id], $user->id);
125
 
126
                $policies[] = $policy;
127
            }
128
        }
129
 
130
        $return = [
131
            'policies' => $policies,
132
            'warnings' => [],
133
        ];
134
        return $return;
135
    }
136
 
137
    /**
138
     * Webservice returns.
139
     *
140
     * @return external_single_structure
141
     */
142
    public static function execute_returns(): external_single_structure {
143
        return new external_single_structure([
144
            'policies' => new external_multiple_structure(
145
                new external_single_structure([
146
                    'policyid' => new external_value(PARAM_INT, 'The policy id.'),
147
                    'versionid' => new external_value(PARAM_INT, 'The policy version id.'),
148
                    'agreementstyle' => new external_value(PARAM_INT, 'The policy agreement style. 0: consent page, 1: own page.'),
149
                    'optional' => new external_value(PARAM_INT, 'Whether the policy is optional. 0: compulsory, 1: optional'),
150
                    'revision' => new external_value(PARAM_TEXT, 'The policy revision.'),
151
                    'status' => new external_value(PARAM_INT, 'The policy status. 0: draft, 1: active, 2: archived.'),
152
                    'name' => new external_value(PARAM_TEXT, 'The policy name'),
153
                    'summary' => new external_value(PARAM_RAW, 'The policy summary.', VALUE_OPTIONAL),
154
                    'summaryformat' => new external_format_value('summary'),
155
                    'content' => new external_value(PARAM_RAW, 'The policy content.', VALUE_OPTIONAL),
156
                    'contentformat' => new external_format_value('content'),
157
                    'acceptance' => new external_single_structure([
158
                        'status' => new external_value(PARAM_INT, 'The acceptance status. 0: declined, 1: accepted.'),
159
                        'lang' => new external_value(PARAM_LANG, 'The policy lang.'),
160
                        'timemodified' => new external_value(PARAM_INT, 'The time the acceptance was set.'),
161
                        'usermodified' => new external_value(PARAM_INT, 'The user who accepted.'),
162
                        'note' => new external_value(PARAM_TEXT, 'The policy note/remarks.', VALUE_OPTIONAL),
163
                        'modfullname' => new external_value(PARAM_NOTAGS, 'The fullname who accepted on behalf.', VALUE_OPTIONAL),
164
                    ], 'Acceptance status for the given user.', VALUE_OPTIONAL),
165
                    'canaccept' => new external_value(PARAM_BOOL, 'Whether the policy can be accepted.'),
166
                    'candecline' => new external_value(PARAM_BOOL, 'Whether the policy can be declined.'),
167
                    'canrevoke' => new external_value(PARAM_BOOL, 'Whether the policy can be revoked.'),
168
                ]), 'Policies and acceptance status for the given user.', VALUE_OPTIONAL
169
            ),
170
            'warnings'  => new external_warnings(),
171
        ]);
172
    }
173
}