Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 403]

   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.
  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  /**
  18   * Provides the {@see core_webservice\token_filter} class.
  19   *
  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
  23   */
  24  
  25  namespace core_webservice;
  26  
  27  use moodleform;
  28  
  29  /**
  30   * Form allowing to filter displayed tokens.
  31   *
  32   * @copyright 2020 David Mudrák <david@moodle.com>
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class token_filter extends moodleform {
  36  
  37      /**
  38       * Defines the form fields.
  39       */
  40      public function definition() {
  41          global $DB;
  42  
  43          $mform = $this->_form;
  44          $presetdata = $this->_customdata;
  45  
  46          $mform->addElement('header', 'tokenfilter', get_string('tokenfilter', 'webservice'));
  47  
  48          if (empty($presetdata->token) && empty($presetdata->users) && empty($presetdata->services)) {
  49              $mform->setExpanded('tokenfilter', false);
  50          } else {
  51              $mform->setExpanded('tokenfilter', true);
  52          }
  53  
  54          // Token.
  55          $mform->addElement('text', 'token', get_string('token', 'core_webservice'), ['size' => 32]);
  56          $mform->setType('token', PARAM_ALPHANUM);
  57  
  58          // User selector.
  59          $attributes = [
  60              'multiple' => true,
  61              'ajax' => 'core_user/form_user_selector',
  62              'valuehtmlcallback' => function($userid) {
  63                  global $DB, $OUTPUT;
  64  
  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);
  68  
  69                  $user = (object)[
  70                      'id' => $record->id,
  71                      'fullname' => fullname($record, has_capability('moodle/site:viewfullnames', $context)),
  72                      'extrafields' => [],
  73                  ];
  74  
  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)
  79                      ];
  80                  }
  81  
  82                  return $OUTPUT->render_from_template('core_user/form_user_selector_suggestion', $user);
  83              },
  84          ];
  85          $mform->addElement('autocomplete', 'users', get_string('user'), [], $attributes);
  86  
  87          // Service selector.
  88          $options = $DB->get_records_menu('external_services', null, '', 'id, name');
  89          $attributes = [
  90              'multiple' => true,
  91          ];
  92          $mform->addElement('autocomplete', 'services', get_string('service', 'webservice'), $options, $attributes);
  93  
  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);
  99      }
 100  }