Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace tool_brickfield\local\htmlchecker\common;
  18  
  19  /**
  20   * Base test class for tests which checks that the given input tag has an associated lable tag.
  21   *
  22   * @package    tool_brickfield
  23   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class input_has_label extends brickfield_accessibility_test {
  27  
  28      // To override, just override the tag and type variables, and use $no_type = true if it is a special form tag like textarea.
  29  
  30      /** @var string The tag name that this test applies to */
  31      public $tag = 'input';
  32  
  33      /** @var string The type of input tag this is */
  34      public $type = 'text';
  35  
  36      /** @var bool Wehether or not we should check the type attribute of the input tags */
  37      public $notype = false;
  38  
  39      /**
  40       * Iterate through all the elemetns using the $tag tagname and the $type attribute (if appropriate)
  41       * and then check it against a list of all LABEL tags.
  42       */
  43      public function check() {
  44          foreach ($this->get_all_elements('label') as $label) {
  45              if ($label->hasAttribute('for')) {
  46                  $labels[$label->getAttribute('for')] = $label;
  47              } else {
  48                  foreach ($label->childNodes as $child) {
  49                      if (property_exists($child, 'tagName') &&
  50                          ($child->tagName == $this->tag) &&
  51                          (($child->getAttribute('type') == $this->type) || $this->notype)) {
  52                          $inputinlabel[$child->getAttribute('name')] = $child;
  53                      }
  54                  }
  55              }
  56          }
  57          foreach ($this->get_all_elements($this->tag) as $input) {
  58              if ($input->getAttribute('type') == $this->type || $this->notype) {
  59                  if (!$input->hasAttribute('title')) {
  60                      if (!isset($inputinlabel[$input->getAttribute('name')])) {
  61                          if (!isset($labels[$input->getAttribute('id')]) ||
  62                              (trim($labels[$input->getAttribute('id')]->nodeValue) == '')) {
  63                              $this->add_report($input);
  64                          }
  65                      }
  66                  }
  67              }
  68          }
  69      }
  70  }