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\reporters;
18
 
19
use tool_brickfield\local\htmlchecker\brickfield_accessibility_reporter;
20
 
21
/**
22
 * A static reporter.
23
 *
24
 * Generates a list of errors which do not pass and their severity.
25
 *
26
 * This is just a demonstration of what you can do with a reporter.
27
 *
28
 * @package    tool_brickfield
29
 * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class report_static extends brickfield_accessibility_reporter {
33
    /**
34
     * Generates a static list of errors within a div.
35
     * @return array A fully-formatted report
36
     */
37
    public function get_report(): array {
38
        $output = [];
39
        foreach ($this->guideline->get_report() as $testname => $test) {
40
            $severity    = $this->guideline->get_severity($testname);
41
            $translation = $this->guideline->get_translation($testname);
42
 
43
            if (isset($translation['title'])) {
44
                $title = $translation['title'];
45
            } else {
46
                $title = null;
47
            }
48
            if (isset($translation['description'])) {
49
                $description = $translation['description'];
50
            } else {
51
                $description = null;
52
            }
53
 
54
            switch ($severity) {
55
                case \tool_brickfield\local\htmlchecker\brickfield_accessibility::BA_TEST_SEVERE:
56
                    $severitylevel  = 'Error';
57
                    $severitynumber = 1;
58
                    break;
59
                case \tool_brickfield\local\htmlchecker\brickfield_accessibility::BA_TEST_MODERATE:
60
                    $severitylevel  = 'Warning';
61
                    $severitynumber = 2;
62
                    break;
63
                case \tool_brickfield\local\htmlchecker\brickfield_accessibility::BA_TEST_SUGGESTION:
64
                    $severitylevel  = 'Suggestion';
65
                    $severitynumber = 3;
66
                    break;
67
            }
68
 
69
            if (is_array($test)) {
70
                $testcount = 0;
71
                foreach ($test as $problem) {
72
                    $testresult = [];
73
                    if (is_object($problem)) {
74
                        $testresult['text_type'] = $problem->message;
75
                        if ($testname === "cssTextHasContrast" || $testname === "cssTextStyleEmphasize") {
76
                            $stylevalue = $problem->message;
77
                            $hexcolors  = [];
78
                            $stylematches = [];
79
                            $weightmatches = [];
80
 
81
                            preg_match_all("/(#[0-9a-f]{6}|#[0-9a-f]{3})/", $stylevalue, $hexcolors);
82
                            preg_match("/font-style:\s([a-z]*);/", $stylevalue, $stylematches);
83
                            preg_match("/font-weight:\s([a-z]*);/", $stylevalue, $weightmatches);
84
                            $hexcolors = array_unique($hexcolors[0]);
85
 
86
                            $testresult['colors'] = $hexcolors;
87
                            $testresult['back_color'] = $hexcolors[0];
88
                            $testresult['fore_color'] = $hexcolors[1];
89
                            $testresult['font_style'] = $stylematches[1];
90
                            $testresult['font_weight'] = $weightmatches[1];
91
                            if ($testresult['font_weight'] === "bolder") {
92
                                $testresult['font_weight'] = "bold";
93
                            }
94
                            $testresult['text_type'] = preg_replace('/(?=:).+/', '', $problem->message);
95
 
96
                        }
97
 
98
                        $testresult['type'] = $testname;
99
                        $testresult['lineNo'] = $problem->line;
100
 
101
                        if (isset($testresult['element'])) {
102
                            $testresult['element'] = $problem->element->tagName;
103
                        }
104
 
105
                        // Edit description for certain cases.
106
                        switch($testname) {
107
                            case 'videosEmbeddedOrLinkedNeedCaptions':
108
                                if ($problem->manual == true || $testcount > 0) {
109
                                    if ($problem->manual == true) {
110
                                        $testcount++;
111
                                    }
112
                                    $testresult['description']  = $description."<p>⚠️ ".$testcount.
113
                                        ' items require manual verification because unable to detect captions.' .
114
                                        ' This is most likely due to the video being unlisted, private, or deleted.</p>';
115
                                } else {
116
                                    $testresult['description']  = $description;
117
                                }
118
                                break;
119
 
120
                            default:
121
                                $testresult['description']  = $description;
122
                                break;
123
                        }
124
 
125
                        $testresult['severity'] = $severitylevel;
126
                        $testresult['severity_num'] = $severitynumber;
127
                        $testresult['title'] = $title;
128
                        $testresult['path'] = count($this->path) > 1 ? $this->path[1] : "None";
129
                        $testresult['html'] = $problem->get_html();
130
                        $testresult['state'] = $problem->state;
131
                        $testresult['manual'] = $problem->manual;
132
                    }
133
 
134
                    $output[] = $testresult;
135
                }
136
            }
137
        }
138
        return $output;
139
    }
140
}