Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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  namespace tool_brickfield\local\htmlchecker;
  18  
  19  /**
  20   * The base class for a guideline
  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 brickfield_accessibility_guideline {
  27      /** @var object The current document's DOMDocument */
  28      public $dom;
  29  
  30      /** @var object The current brickfield CSS object */
  31      public $css;
  32  
  33      /** @var array The path to the current document */
  34      public $path;
  35  
  36      /** @var array An array of report objects */
  37      public $report;
  38  
  39      /** @var array An array of translations for all this guideline's tests */
  40      public $translations;
  41  
  42      /** @var bool Whether we are running in CMS mode */
  43      public $cmsmode = false;
  44  
  45      /** @var array An array of all the severity levels for every test */
  46      public $severity = [];
  47  
  48      /**
  49       * The class constructor.
  50       * @param object $dom The current DOMDocument object
  51       * @param object $css The current brickfieldCSS object
  52       * @param array $path The current path
  53       * @param null $arg
  54       * @param string $domain
  55       * @param bool $cmsmode
  56       */
  57      public function __construct(&$dom, &$css, array &$path,
  58                                  $arg = null, string $domain = 'en', bool $cmsmode = false) {
  59          $this->dom = &$dom;
  60          $this->css = &$css;
  61          $this->path = &$path;
  62          $this->cmsmode = $cmsmode;
  63          $this->load_translations($domain);
  64          $this->run($arg, $domain);
  65      }
  66  
  67      /**
  68       * Returns an array of all the tests associated with the current guideline
  69       * @return array
  70       */
  71      public function get_tests(): array {
  72          return $this->tests;
  73      }
  74  
  75      /**
  76       * Loads translations from a file. This can be overriden, just as long as the
  77       * local variable 'translations' is an associative array with test function names
  78       * as the key
  79       * @param string $domain
  80       */
  81      public function load_translations(string $domain) {
  82          $csv = fopen(dirname(__FILE__) .'/guidelines/translations/'. $domain .'.txt', 'r');
  83  
  84          if ($csv) {
  85              while ($translation = fgetcsv($csv)) {
  86                  if (count($translation) == 4) {
  87                      $this->translations[$translation[0]] = [
  88                          'title'       => $translation[1],
  89                          'description' => $translation[2],
  90                      ];
  91                  }
  92              }
  93          }
  94      }
  95  
  96      /**
  97       * Returns the translation for a test name.
  98       * @param string $testname The function name of the test
  99       * @return mixed
 100       */
 101      public function get_translation(string $testname) {
 102          return (isset($this->translations[$testname]))
 103              ? $this->translations[$testname]
 104              : $testname;
 105      }
 106  
 107      /**
 108       * Iterates through each test string, makes a new test object, and runs it against the current DOM
 109       * @param null $arg
 110       * @param string $language
 111       */
 112      public function run($arg = null, string $language = 'en') {
 113          foreach ($this->tests as $testname => $options) {
 114              if (is_numeric($testname) && !is_array($options)) {
 115                  $testname = $options;
 116              }
 117              $name = $testname;
 118              $testname = 'tool_brickfield\local\htmlchecker\common\\checks\\'.$testname;
 119              if (class_exists($testname) && $this->dom) {
 120                  $testname = new $testname($this->dom, $this->css, $this->path, $language, $arg);
 121                  if (!$this->cmsmode || ($testname->cms && $this->cmsmode)) {
 122                      $this->report[$name] = $testname->get_report();
 123                  }
 124                  $this->severity[$name] = $testname->defaultseverity;
 125                  unset($testname);
 126              } else {
 127                  $this->report[$name] = false;
 128              }
 129          }
 130      }
 131  
 132      /**
 133       * Returns all the Report variable
 134       * @return mixed Look to your report to see what it returns
 135       */
 136      public function get_report() {
 137          return $this->report;
 138      }
 139  
 140      /**
 141       * Returns the severity level of a given test
 142       * @param string $testname The name of the test
 143       * @return int The severity level
 144       */
 145      public function get_severity(string $testname): int {
 146          if (isset($this->tests[$testname]['severity'])) {
 147              return $this->tests[$testname]['severity'];
 148          }
 149  
 150          if (isset($this->severity[$testname])) {
 151              return $this->severity[$testname];
 152          }
 153  
 154          return brickfield_accessibility::BA_TEST_MODERATE;
 155      }
 156  }