Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

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