Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core_ai\external;
18
 
19
use core_ai\manager;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_value;
23
 
24
/**
25
 * External API to get a users AI policy acceptance.
26
 *
27
 * @package    core_ai
28
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class get_policy_status extends external_api {
32
    /**
33
     * Get policy parameters.
34
     *
35
     * @since  Moodle 4.5
36
     * @return external_function_parameters
37
     */
38
    public static function execute_parameters(): external_function_parameters {
39
        return new external_function_parameters([
40
            'userid' => new external_value(
41
                PARAM_INT,
42
                'The user ID',
43
                VALUE_REQUIRED,
44
            ),
45
        ]);
46
    }
47
 
48
    /**
49
     * Set a users AI policy acceptance.
50
     *
51
     * @since  Moodle 4.5
52
     * @param int $userid The user ID.
53
     * @return array The generated content.
54
     */
55
    public static function execute(
56
        int $userid,
57
    ): array {
58
        global $USER;
59
 
60
        // Parameter validation.
61
        [
62
            'userid' => $userid,
63
        ] = self::validate_parameters(self::execute_parameters(), [
64
            'userid' => $userid,
65
        ]);
66
 
67
        // Context validation and permission check.
68
        // Get the context from the passed in ID.
69
        $context = \core\context\user::instance($userid);
70
 
71
        self::validate_context($context);
72
 
73
        if ($userid === $USER->id) {
74
            require_capability('moodle/ai:fetchpolicy', $context);
75
        } else {
76
            require_capability('moodle/ai:fetchanyuserpolicystatus', $context);
77
        }
78
 
79
        return [
80
            'status' => manager::get_user_policy_status($userid),
81
        ];
82
    }
83
 
84
    /**
85
     * Generate content return value.
86
     *
87
     * @since  Moodle 4.5
88
     * @return external_function_parameters
89
     */
90
    public static function execute_returns(): external_function_parameters {
91
        return new external_function_parameters([
92
            'status' => new external_value(
93
                PARAM_BOOL,
94
                'True if the policy was accepted, false otherwise.',
95
                VALUE_REQUIRED,
96
            ),
97
        ]);
98
    }
99
}