Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Special base class which provides helper methods for tables.
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_table_test extends brickfield_accessibility_test {
27
    /**
28
     * Takes the element object of a main table and returns the number of rows and columns in it.
29
     * @param \stdClass $table
30
     * @return array An array with the 'rows' value showing the number of rows, and column showing the number of columns
31
     */
32
    public function get_table(\stdClass $table): array {
33
        $rows = 0;
34
        $columns = 0;
35
        $firstrow = true;
36
        if ($table->tagName != 'table') {
37
            return false;
38
        }
39
        foreach ($table->childNodes as $child) {
40
            if (property_exists($child, 'tagName') && $child->tagName == 'tr') {
41
                $rows++;
42
                if ($firstrow) {
43
                    foreach ($child->childNodes as $columnchild) {
44
                        if ($columnchild->tagName == 'th' || $columnchild->tagName == 'td') {
45
                            $columns++;
46
                        }
47
                    }
48
                    $firstrow = false;
49
                }
50
            }
51
        }
52
 
53
        return ['rows' => $rows, 'columns' => $columns];
54
    }
55
 
56
    /**
57
     * Finds whether or not the table is a data table. Checks that the
58
     * table has a logical order and uses 'th' or 'thead' tags to illustrate
59
     * the page author thought it was a data table.
60
     * @param object $table The DOMElement object of the table tag
61
     * @return bool TRUE if the element is a data table, otherwise false
62
     */
63
    public function is_data($table): bool {
64
        if ($table->tagName != 'table') {
65
            return false;
66
        }
67
 
68
        foreach ($table->childNodes as $child) {
69
            if (property_exists($child, 'tagName') && $child->tagName == 'tr') {
70
                foreach ($child->childNodes as $rowchild) {
71
                    if (property_exists($rowchild, 'tagName') && $rowchild->tagName == 'th') {
72
                        return true;
73
                    }
74
                }
75
            }
76
            if (property_exists($child, 'tagName') && $child->tagName == 'thead') {
77
                return true;
78
            }
79
        }
80
        return false;
81
    }
82
}