Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 403]

   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  /**
  18   * Radio input form element.
  19   *
  20   * @package    core_form
  21   * @category   test
  22   * @copyright  2013 David MonllaĆ³
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  
  28  require_once (__DIR__  . '/behat_form_checkbox.php');
  29  
  30  /**
  31   * Radio input form field.
  32   *
  33   * Extends behat_form_checkbox as the set_value() behaviour
  34   * is the same and it behaves closer to a checkbox than to
  35   * a text field.
  36   *
  37   * This form field type can be added to forms as any other
  38   * moodle form element, but it does not make sense without
  39   * a group of radio inputs, so is hard to find it alone and
  40   * detect it by behat_field_manager::get_form_field(), where is useful
  41   * is when the default behat_form_field class is being used, it
  42   * finds a input[type=radio] and it delegates set_value() and
  43   * get_value() to behat_form_radio.
  44   *
  45   * @package    core_form
  46   * @category   test
  47   * @copyright  2013 David MonllaĆ³
  48   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  49   */
  50  class behat_form_radio extends behat_form_checkbox {
  51  
  52      /**
  53       * Returns the radio input value attribute.
  54       *
  55       * Here we can not extend behat_form_checkbox because
  56       * isChecked() does internally a (bool)getValue() and
  57       * it is not good for radio buttons.
  58       *
  59       * @return string The value attribute
  60       */
  61      public function get_value() {
  62          return $this->field->isSelected();
  63      }
  64  
  65      /**
  66       * Sets the value of a radio
  67       *
  68       * Partially overwriting behat_form_checkbox
  69       * implementation as when JS is disabled we
  70       * can not check() and we should use setValue()
  71       *
  72       * @param string $value
  73       * @return void
  74       */
  75      public function set_value($value) {
  76  
  77          if ($this->running_javascript()) {
  78              // Check on radio button.
  79              $this->field->click();
  80  
  81              // Trigger the onchange event as triggered when 'selecting' the radio.
  82              if (!empty($value) && !$this->field->isSelected()) {
  83                  $this->trigger_on_change();
  84              }
  85          } else {
  86              // Goutte does not accept a check nor a click in an input[type=radio].
  87              $this->field->setValue($this->field->getAttribute('value'));
  88          }
  89      }
  90  
  91  }