aboutsummaryrefslogtreecommitdiff
path: root/externallib.php
blob: 010b1737ce81752a182775207d3a41987c74a540 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file defines the plugin's external functions.
 *
 * @copyright 2018 'Valentin Popov' <info@valentineus.link>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @package   local_webhooks
 */

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/local/webhooks/lib.php');
require_once($CFG->libdir . '/externallib.php');

/**
 * External functions.
 *
 * @copyright 2018 'Valentin Popov' <info@valentineus.link>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @package   local_webhooks
 */
class local_webhooks_external extends external_api {
    /**
     * Returns description of method parameters.
     *
     * @return \external_function_parameters
     *
     * @since Moodle 2.2
     * @since Moodle 2.9 Options available
     */
    public static function get_service_parameters() {
        return new external_function_parameters(
            array(
                'serviceid' => new external_value(PARAM_INT, 'Service ID.'),
            )
        );
    }

    /**
     * Get information about the service.
     *
     * @param $serviceid
     *
     * @return array
     *
     * @since Moodle 2.2
     * @since Moodle 2.9 Options available
     *
     * @throws \dml_exception
     * @throws \invalid_parameter_exception
     * @throws \moodle_exception
     * @throws \restricted_context_exception
     */
    public static function get_service($serviceid) {
        $parameters = self::validate_parameters(self::get_service_parameters(), array('serviceid' => $serviceid));

        $context = context_system::instance();
        self::validate_context($context);

        return (array) local_webhooks_api::get_service($parameters['serviceid']);
    }

    /**
     * Returns description of method result value.
     *
     * @return \external_single_structure
     *
     * @since Moodle 2.2
     * @since Moodle 2.9 Options available
     */
    public static function get_service_returns() {
        return new external_single_structure(
            array(
                'id'     => new external_value(PARAM_INT, 'Service ID.'),
                'header' => new external_value(PARAM_RAW, 'Type of outgoing header.'),
                'name'   => new external_value(PARAM_RAW, 'Name of the service.'),
                'point'  => new external_value(PARAM_URL, 'Point of delivery of notifications.'),
                'status' => new external_value(PARAM_BOOL, 'Current status of the service.'),
                'token'  => new external_value(PARAM_RAW, 'Token for verification of requests.'),
                'events' => new external_multiple_structure(
                    new external_value(PARAM_RAW, 'Event name.'), 'List of events.'
                ),
            )
        );
    }
}