1<?php
2
3require_once($CFG->libdir.'/formslib.php');
4
5
6class webservice_test_client_form extends moodleform {
7    public function definition() {
8        global $CFG;
9
10        $mform = $this->_form;
11        list($functions, $protocols) = $this->_customdata;
12
13        $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
14
15        $authmethod = array('simple' => 'simple', 'token' => 'token');
16        $mform->addElement('select', 'authmethod', get_string('authmethod', 'webservice'), $authmethod);
17        $mform->setType('simple', PARAM_ALPHA);
18
19        $mform->addElement('select', 'protocol', get_string('protocol', 'webservice'), $protocols);
20        $mform->setType('protocol', PARAM_ALPHA);
21
22        $mform->addElement('select', 'function', get_string('function', 'webservice'), $functions);
23        $mform->setType('function', PARAM_PLUGIN);
24
25        $this->add_action_buttons(false, get_string('select'));
26    }
27}
28
29// === Test client forms ===
30
31/**
32 * Base class for implementations of WS test client forms.
33 *
34 * @package   core_webservice
35 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @copyright 2017 Marina Glancy
37 */
38abstract class webservice_test_client_base_form extends moodleform {
39
40    /**
41     * Definition of the parameters used by this WS function
42     */
43    protected abstract function test_client_definition();
44
45    /**
46     * The form definition.
47     */
48    public function definition() {
49        $mform = $this->_form;
50
51        $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
52
53        // Note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters.
54        $data = $this->_customdata;
55        if ($data['authmethod'] == 'simple') {
56            $mform->addElement('text', 'wsusername', 'wsusername');
57            $mform->setType('wsusername', core_user::get_property_type('username'));
58            $mform->addElement('text', 'wspassword', 'wspassword');
59            $mform->setType('wspassword', core_user::get_property_type('password'));
60        } else if ($data['authmethod'] == 'token') {
61            $mform->addElement('text', 'token', 'token');
62            $mform->setType('token', PARAM_RAW_TRIMMED);
63        }
64
65        $mform->addElement('hidden', 'authmethod', $data['authmethod']);
66        $mform->setType('authmethod', PARAM_ALPHA);
67
68        $mform->addElement('hidden', 'function');
69        $mform->setType('function', PARAM_PLUGIN);
70
71        $mform->addElement('hidden', 'protocol');
72        $mform->setType('protocol', PARAM_ALPHA);
73
74        $this->test_client_definition();
75
76        $this->add_action_buttons(true, get_string('execute', 'webservice'));
77    }
78
79    /**
80     * Get the parameters that the user submitted using the form.
81     * @return array|null
82     */
83    public function get_params() {
84        if (!$data = $this->get_data()) {
85            return null;
86        }
87        return array_diff_key((array)$data, ['submitbutton' => 1, 'protocol' => 1, 'function' => 1,
88            'wsusername' => 1, 'wspassword' => 1, 'token' => 1, 'authmethod' => 1]);
89    }
90}
91
92/**
93 * Form class for create_categories() web service function test.
94 *
95 * @package   core_webservice
96 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
97 * @copyright 2012 Fabio Souto
98 */
99class core_course_create_categories_testclient_form extends webservice_test_client_base_form {
100    /**
101     * The form definition.
102     */
103    protected function test_client_definition() {
104        $mform = $this->_form;
105        $mform->addElement('text', 'name[0]', 'name[0]');
106        $mform->addElement('text', 'parent[0]', 'parent[0]');
107        $mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
108        $mform->addElement('text', 'description[0]', 'description[0]');
109        $mform->addElement('text', 'name[1]', 'name[1]');
110        $mform->addElement('text', 'parent[1]', 'parent[1]');
111        $mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
112        $mform->addElement('text', 'description[1]', 'description[1]');
113        $mform->setType('name', PARAM_TEXT);
114        $mform->setType('parent', PARAM_INT);
115        $mform->setType('idnumber', PARAM_RAW);
116        $mform->setType('description', PARAM_RAW);
117    }
118
119    /**
120     * Get the parameters that the user submitted using the form.
121     * @return array|null
122     */
123    public function get_params() {
124        if (!$data = $this->get_data()) {
125            return null;
126        }
127
128        $params = array();
129        $params['categories'] = array();
130        for ($i=0; $i<10; $i++) {
131            if (empty($data->name[$i])) {
132                continue;
133            }
134            $params['categories'][] = array('name'=>$data->name[$i], 'parent'=>$data->parent[$i],
135                                            'idnumber'=>$data->idnumber[$i], 'description'=>$data->description[$i]);
136        }
137        return $params;
138    }
139}
140
141/**
142 * Form class for delete_categories() web service function test.
143 *
144 * @package   core_webservice
145 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
146 * @copyright 2012 Fabio Souto
147 */
148class core_course_delete_categories_testclient_form extends webservice_test_client_base_form {
149    /**
150     * The form definition.
151     */
152    protected function test_client_definition() {
153        $mform = $this->_form;
154        $mform->addElement('text', 'id[0]', 'id[0]');
155        $mform->addElement('text', 'newparent[0]', 'newparent[0]');
156        $mform->addElement('text', 'recursive[0]', 'recursive[0]');
157        $mform->addElement('text', 'id[1]', 'id[1]');
158        $mform->addElement('text', 'newparent[1]', 'newparent[1]');
159        $mform->addElement('text', 'recursive[1]', 'recursive[1]');
160        $mform->setType('id', PARAM_INT);
161        $mform->setType('newparent', PARAM_INT);
162        $mform->setType('recursive', PARAM_BOOL);
163    }
164
165    /**
166     * Get the parameters that the user submitted using the form.
167     * @return array|null
168     */
169    public function get_params() {
170        if (!$data = $this->get_data()) {
171            return null;
172        }
173        $params = array();
174        $params['categories'] = array();
175        for ($i=0; $i<10; $i++) {
176            if (empty($data->id[$i])) {
177                continue;
178            }
179            $attrs = array();
180            $attrs['id'] = $data->id[$i];
181            if (!empty($data->newparent[$i])) {
182                $attrs['newparent'] = $data->newparent[$i];
183            }
184            if (!empty($data->recursive[$i])) {
185                $attrs['recursive'] = $data->recursive[$i];
186            }
187            $params['categories'][] = $attrs;
188        }
189        return $params;
190    }
191}
192
193/**
194 * Form class for create_categories() web service function test.
195 *
196 * @package   core_webservice
197 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
198 * @copyright 2012 Fabio Souto
199 */
200class core_course_update_categories_testclient_form extends webservice_test_client_base_form {
201    /**
202     * The form definition.
203     */
204    protected function test_client_definition() {
205        $mform = $this->_form;
206        $mform->addElement('text', 'id[0]', 'id[0]');
207        $mform->addElement('text', 'name[0]', 'name[0]');
208        $mform->addElement('text', 'parent[0]', 'parent[0]');
209        $mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
210        $mform->addElement('text', 'description[0]', 'description[0]');
211        $mform->addElement('text', 'id[1]', 'id[1]');
212        $mform->addElement('text', 'name[1]', 'name[1]');
213        $mform->addElement('text', 'parent[1]', 'parent[1]');
214        $mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
215        $mform->addElement('text', 'description[1]', 'description[1]');
216        $mform->setType('id', PARAM_INT);
217        $mform->setType('name', PARAM_TEXT);
218        $mform->setType('parent', PARAM_INT);
219        $mform->setType('idnumber', PARAM_RAW);
220        $mform->setType('description', PARAM_RAW);
221    }
222
223    /**
224     * Get the parameters that the user submitted using the form.
225     * @return array|null
226     */
227    public function get_params() {
228        if (!$data = $this->get_data()) {
229            return null;
230        }
231        $params = array();
232        $params['categories'] = array();
233        for ($i=0; $i<10; $i++) {
234
235            if (empty($data->id[$i])) {
236                continue;
237            }
238            $attrs = array();
239            $attrs['id'] = $data->id[$i];
240            if (!empty($data->name[$i])) {
241                $attrs['name'] = $data->name[$i];
242            }
243            if (!empty($data->parent[$i])) {
244                $attrs['parent'] = $data->parent[$i];
245            }
246            if (!empty($data->idnumber[$i])) {
247                $attrs['idnumber'] = $data->idnumber[$i];
248            }
249            if (!empty($data->description[$i])) {
250                $attrs['description'] = $data->description[$i];
251            }
252            $params['categories'][] = $attrs;
253        }
254        return $params;
255    }
256}
257
258/**
259 * Test class for WS function core_fetch_notifications
260 *
261 * @package   core_webservice
262 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
263 * @copyright 2017 Marina Glancy
264 */
265class core_fetch_notifications_testclient_form extends webservice_test_client_base_form {
266    /**
267     * The form definition.
268     */
269    protected function test_client_definition() {
270        $mform = $this->_form;
271        $mform->addElement('text', 'contextid', 'contextid');
272        $mform->setType('contextid', PARAM_INT);
273        $mform->setDefault('contextid', context_system::instance()->id);
274    }
275}
276
277/**
278 * Test class for WS function get_site_info
279 *
280 * @package   core_webservice
281 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
282 * @copyright 2017 Marina Glancy
283 */
284class core_webservice_get_site_info_testclient_form extends webservice_test_client_base_form {
285    /**
286     * The form definition.
287     */
288    protected function test_client_definition() {
289    }
290}
291
292/**
293 * Test class for WS function core_get_string
294 *
295 * @package   core_webservice
296 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
297 * @copyright 2017 Marina Glancy
298 */
299class core_get_string_testclient_form extends webservice_test_client_base_form {
300    /**
301     * The form definition.
302     */
303    protected function test_client_definition() {
304        $mform = $this->_form;
305        $mform->addElement('text', 'stringid', 'stringid');
306        $mform->setType('stringid', PARAM_STRINGID);
307        $mform->addElement('text', 'component', 'component');
308        $mform->setType('component', PARAM_COMPONENT);
309        $mform->addElement('text', 'lang', 'lang');
310        $mform->setType('lang', PARAM_LANG);
311        $mform->addElement('text', 'stringparams_name[1]', 'Parameter 1 name');
312        $mform->setType('stringparams_name[1]', PARAM_ALPHANUMEXT);
313        $mform->addElement('text', 'stringparams_value[1]', 'Parameter 1 value');
314        $mform->setType('stringparams_value[1]', PARAM_RAW);
315        $mform->addElement('text', 'stringparams_name[2]', 'Parameter 2 name');
316        $mform->setType('stringparams_name[2]', PARAM_ALPHANUMEXT);
317        $mform->addElement('text', 'stringparams_value[2]', 'Parameter 2 value');
318        $mform->setType('stringparams_value[2]', PARAM_RAW);
319        $mform->addElement('text', 'stringparams_name[3]', 'Parameter 3 name');
320        $mform->setType('stringparams_name[3]', PARAM_ALPHANUMEXT);
321        $mform->addElement('text', 'stringparams_value[3]', 'Parameter 3 value');
322        $mform->setType('stringparams_value[3]', PARAM_RAW);
323        $mform->addElement('static', 'paramnote', '', 'If a parameter is not an object, only specify "Parameter 1 value"');
324    }
325
326    /**
327     * Get the parameters that the user submitted using the form.
328     * @return array|null
329     */
330    public function get_params() {
331        $params = parent::get_params();
332        if ($params === null) {
333            return null;
334        }
335
336        $params['stringparams'] = [];
337        for ($idx = 1; $idx <= 3; $idx++) {
338            $name = isset($params['stringparams_name'][$idx]) ? strval($params['stringparams_name'][$idx]) : '';
339            $value = isset($params['stringparams_value'][$idx]) ? strval($params['stringparams_value'][$idx]) : '';
340            if ($name !== '' || $value !== '') {
341                if ($name === '') {
342                    $params['stringparams'][] = ['value' => $value];
343                } else {
344                    $params['stringparams'][] = ['name' => $name, 'value' => $value];
345                }
346            }
347        }
348        unset($params['stringparams_name']);
349        unset($params['stringparams_value']);
350        return $params;
351    }
352}
353