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
defined('MOODLE_INTERNAL') || die();
18
 
19
global $CFG;
20
require_once(__DIR__ . '/../../lib.php');
21
 
22
/**
23
 * Data generator for core_webservice plugin.
24
 *
25
 * @package    core_webservice
26
 * @category   test
27
 * @copyright  2021 Andrew Nicols <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class core_webservice_generator extends component_generator_base {
31
    /**
32
     * Create a new webservice service.
33
     *
34
     * @param   array $data
35
     * @return  stdClass
36
     */
37
    public function create_service(array $data): \stdClass {
38
        $webservicemanager = new webservice();
39
 
40
        $requiredfields = [
41
            'name',
42
            'shortname',
43
        ];
44
 
45
        foreach ($requiredfields as $fieldname) {
46
            if (!array_key_exists($fieldname, $data)) {
47
                throw new \coding_exception("Field '{$fieldname}' missing when creating new service");
48
            }
49
        }
50
 
51
        $optionalfields = [
52
            'enabled' => false,
53
            'requiredcapability' => '',
54
            'restrictedusers' => 0,
55
            'component' => '',
56
            'timemodified' => time(),
57
        ];
58
 
59
        foreach ($optionalfields as $fieldname => $value) {
60
            if (!array_key_exists($fieldname, $data)) {
61
                $data[$fieldname] = $value;
62
            }
63
        }
64
 
65
        $serviceid = $webservicemanager->add_external_service((object) $data);
66
 
67
        return $webservicemanager->get_external_service_by_id($serviceid);
68
    }
69
 
70
    /**
71
     * Associate a webservice function with service.
72
     *
73
     * @param   array $data
74
     */
75
    public function create_service_functions(array $data): void {
76
        $webservicemanager = new webservice();
77
 
78
        $requiredfields = [
79
            'service',
80
            'functions',
81
        ];
82
 
83
        foreach ($requiredfields as $fieldname) {
84
            if (!array_key_exists($fieldname, $data)) {
85
                throw new \coding_exception("Field '{$fieldname}' missing when creating new service");
86
            }
87
        }
88
 
89
        $service = $webservicemanager->get_external_service_by_shortname($data['service']);
90
 
91
        $functions = explode(',', $data['functions']);
92
        foreach ($functions as $functionname) {
93
            $functionname = trim($functionname);
94
            $webservicemanager->add_external_function_to_service($functionname, $service->id);
95
        }
96
    }
97
 
98
    /**
99
     * Create a new webservice token.
100
     *
101
     * @param   array $data
102
     */
103
    public function create_token(array $data): void {
104
        $webservicemanager = new webservice();
105
 
106
        $requiredfields = [
107
            'userid',
108
            'service',
109
        ];
110
 
111
        foreach ($requiredfields as $fieldname) {
112
            if (!array_key_exists($fieldname, $data)) {
113
                throw new \coding_exception("Field '{$fieldname}' missing when creating new service");
114
            }
115
        }
116
 
117
        $optionalfields = [
118
            'context' => context_system::instance(),
119
            'validuntil' => 0,
120
            'iprestriction' => '',
121
            'name' => '',
122
        ];
123
 
124
        foreach ($optionalfields as $fieldname => $value) {
125
            if (!array_key_exists($fieldname, $data)) {
126
                $data[$fieldname] = $value;
127
            }
128
        }
129
 
130
        $service = $webservicemanager->get_external_service_by_shortname($data['service']);
131
 
132
        \core_external\util::generate_token(
133
            EXTERNAL_TOKEN_PERMANENT,
134
            $service,
135
            $data['userid'],
136
            $data['context'],
137
            $data['validuntil'],
138
            $data['iprestriction'],
139
            $data['name']
140
        );
141
    }
142
}