MDL-72182 user: cross-DB compatibility for getting user pages/blocks.
[moodle.git] / webservice / classes / token_filter.php
blob16c6d1a42046c054f3b3b66e1201709235aa491e
1 <?php
2 // This file is part of Moodle - https://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.
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/>.
17 /**
18 * Provides the {@see core_webservice\token_filter} class.
20 * @package core_webservice
21 * @copyright 2020 David Mudrák <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_webservice;
27 use moodleform;
29 /**
30 * Form allowing to filter displayed tokens.
32 * @copyright 2020 David Mudrák <david@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class token_filter extends moodleform {
37 /**
38 * Defines the form fields.
40 public function definition() {
41 global $DB;
43 $mform = $this->_form;
44 $presetdata = $this->_customdata;
46 $mform->addElement('header', 'tokenfilter', get_string('tokenfilter', 'webservice'));
48 if (empty($presetdata->token) && empty($presetdata->users) && empty($presetdata->services)) {
49 $mform->setExpanded('tokenfilter', false);
50 } else {
51 $mform->setExpanded('tokenfilter', true);
54 // Token.
55 $mform->addElement('text', 'token', get_string('token', 'core_webservice'), ['size' => 32]);
56 $mform->setType('token', PARAM_ALPHANUM);
58 // User selector.
59 $attributes = [
60 'multiple' => true,
61 'ajax' => 'core_user/form_user_selector',
62 'valuehtmlcallback' => function($userid) {
63 global $DB, $OUTPUT;
65 $context = \context_system::instance();
66 $fields = \core_user\fields::for_name()->with_identity($context, false);
67 $record = \core_user::get_user($userid, 'id' . $fields->get_sql()->selects, MUST_EXIST);
69 $user = (object)[
70 'id' => $record->id,
71 'fullname' => fullname($record, has_capability('moodle/site:viewfullnames', $context)),
72 'extrafields' => [],
75 foreach ($fields->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]) as $extrafield) {
76 $user->extrafields[] = (object)[
77 'name' => $extrafield,
78 'value' => s($record->$extrafield)
82 return $OUTPUT->render_from_template('core_user/form_user_selector_suggestion', $user);
85 $mform->addElement('autocomplete', 'users', get_string('user'), [], $attributes);
87 // Service selector.
88 $options = $DB->get_records_menu('external_services', null, '', 'id, name');
89 $attributes = [
90 'multiple' => true,
92 $mform->addElement('autocomplete', 'services', get_string('service', 'webservice'), $options, $attributes);
94 // Action buttons.
95 $mform->addGroup([
96 $mform->createElement('submit', 'submitbutton', get_string('tokenfiltersubmit', 'core_webservice')),
97 $mform->createElement('submit', 'resetbutton', get_string('tokenfilterreset', 'core_webservice'), [], false),
98 ], 'actionbuttons', '', ' ', false);