mirrored from git://git.moodle.org/moodle.git
/
accessibility.php
576 lines (502 loc) · 20.5 KB
/
accessibility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_brickfield;
use context_system;
use moodle_exception;
use moodle_url;
use stdClass;
use tool_brickfield\local\tool\filter;
/**
* Provides the Brickfield Accessibility toolkit API.
*
* @package tool_brickfield
* @copyright 2020 onward Brickfield Education Labs Ltd, https://www.brickfield.ie
* @author Mike Churchward (mike@brickfieldlabs.ie)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class accessibility {
/** @var string The component sub path */
private static $pluginpath = 'tool/brickfield';
/** @var string Supported format of topics */
const TOOL_BRICKFIELD_FORMAT_TOPIC = 'topics';
/** @var string Supported format of weeks */
const TOOL_BRICKFIELD_FORMAT_WEEKLY = 'weeks';
/**
* Return the state of the site enable condition.
* @return bool
*/
public static function is_accessibility_enabled(): bool {
global $CFG;
if (isset($CFG->enableaccessibilitytools)) {
return $CFG->enableaccessibilitytools;
}
// Enabled by default.
return true;
}
/**
* Throw an error if the toolkit is not enabled.
* @return bool
* @throws moodle_exception
*/
public static function require_accessibility_enabled(): bool {
if (!static::is_accessibility_enabled()) {
throw new moodle_exception('accessibilitydisabled', manager::PLUGINNAME);
}
return true;
}
/**
* Get a URL for a page within the plugin.
*
* This takes into account the value of the admin config value.
*
* @param string $url The URL within the plugin
* @return moodle_url
*/
public static function get_plugin_url(string $url = ''): moodle_url {
$url = ($url == '') ? 'index.php' : $url;
$pluginpath = self::$pluginpath;
return new moodle_url("/admin/{$pluginpath}/{$url}");
}
/**
* Get a file path for a file within the plugin.
*
* This takes into account the value of the admin config value.
*
* @param string $path The path within the plugin
* @return string
*/
public static function get_file_path(string $path): string {
global $CFG;
return implode(DIRECTORY_SEPARATOR, [$CFG->dirroot, $CFG->admin, self::$pluginpath, $path, ]);
}
/**
* Get the canonicalised name of a capability.
*
* @param string $capability
* @return string
*/
public static function get_capability_name(string $capability): string {
return self::$pluginpath . ':' . $capability;
}
/**
* Get the relevant title.
* @param filter $filter
* @param int $countdata
* @return string
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
*/
public static function get_title(filter $filter, int $countdata): string {
global $DB;
$tmp = new \stdClass();
$tmp->count = $countdata;
$langstr = 'title' . $filter->tab . 'partial';
if ($filter->courseid != 0) {
$thiscourse = get_fast_modinfo($filter->courseid)->get_course();
$tmp->name = $thiscourse->fullname;
} else {
$langstr = 'title' . $filter->tab . 'all';
}
return get_string($langstr, manager::PLUGINNAME, $tmp);
}
/**
* Function to be run periodically according to the scheduled task.
* Return true if a process was completed. False if no process executed.
* Finds all unprocessed courses for bulk batch processing and completes them.
* @param int $batch
* @return bool
* @throws \ReflectionException
* @throws \coding_exception
* @throws \ddl_exception
* @throws \ddl_table_missing_exception
* @throws \dml_exception
*/
public static function bulk_process_courses_cron(int $batch = 0): bool {
global $PAGE;
// Run a registration check.
if (!(new registration())->validate()) {
return false;
}
if (analysis::is_enabled()) {
$PAGE->set_context(context_system::instance());
mtrace("Starting cron for bulk_process_courses");
// Do regular processing. True if full deployment type isn't selected as well.
static::bulk_processing($batch);
mtrace("Ending cron for bulk_process_courses");
return true;
} else {
mtrace('Content analysis is currently disabled in settings.');
return false;
}
}
/**
* Bulk processing.
* @param int $batch
* @return bool
*/
protected static function bulk_processing(int $batch = 0): bool {
manager::check_course_updates();
mtrace("check_course_updates completed at " . time());
$recordsprocessed = manager::check_scheduled_areas($batch);
mtrace("check_scheduled_areas completed at " . time());
manager::check_scheduled_deletions();
mtrace("check_scheduled_deletions completed at " . time());
manager::delete_historical_data();
mtrace("delete_historical_data completed at " . time());
return $recordsprocessed;
}
/**
* Function to be run periodically according to the scheduled task.
* Finds all unprocessed courses for cache processing and completes them.
*/
public static function bulk_process_caches_cron() {
global $DB;
// Run a registration check.
if (!(new registration())->validate()) {
return;
}
if (analysis::is_enabled()) {
mtrace("Starting cron for bulk_process_caches");
// Monitor ongoing caching requests.
$fields = 'DISTINCT courseid';
$reruns = $DB->get_records(manager::DB_PROCESS, ['item' => 'cache'], '', $fields);
foreach ($reruns as $rerun) {
mtrace("Running rerun caching for Courseid " . $rerun->courseid);
manager::store_result_summary($rerun->courseid);
mtrace("rerun cache completed at " . time());
$DB->delete_records(manager::DB_PROCESS, ['courseid' => $rerun->courseid, 'item' => 'cache']);
}
mtrace("Ending cron for bulk_process_caches at " . time());
} else {
mtrace('Content analysis is currently disabled in settings.');
}
}
/**
* This function runs the checks on the html item
*
* @param string $html The html string to be analysed; might be NULL.
* @param int $contentid The content area ID
* @param int $processingtime
* @param int $resultstime
*/
public static function run_check(string $html, int $contentid, int &$processingtime, int &$resultstime) {
global $DB;
// Change the limit if 10,000 is not appropriate.
$bulkrecordlimit = manager::BULKRECORDLIMIT;
$bulkrecordcount = 0;
$checkids = static::checkids();
$checknameids = array_flip($checkids);
$testname = 'brickfield';
$stime = time();
// Swapping in new library.
$htmlchecker = new local\htmlchecker\brickfield_accessibility($html, $testname, 'string');
$htmlchecker->run_check();
$tests = $htmlchecker->guideline->get_tests();
$report = $htmlchecker->get_report();
$processingtime += (time() - $stime);
$records = [];
foreach ($tests as $test) {
$records[$test]['count'] = 0;
$records[$test]['errors'] = [];
}
foreach ($report['report'] as $a) {
if (!isset($a['type'])) {
continue;
}
$type = $a['type'];
$records[$type]['errors'][] = $a;
if (!isset($records[$type]['count'])) {
$records[$type]['count'] = 0;
}
$records[$type]['count']++;
}
$stime = time();
$returnchecks = [];
$errors = [];
// Build up records for inserting.
foreach ($records as $key => $rec) {
$recordres = new stdClass();
// Handling if checkid is unknown.
$checkid = (isset($checknameids[$key])) ? $checknameids[$key] : 0;
$recordres->contentid = $contentid;
$recordres->checkid = $checkid;
$recordres->errorcount = $rec['count'];
// Build error inserts if needed.
if ($rec['count'] > 0) {
foreach ($rec['errors'] as $tmp) {
$error = new stdClass();
$error->resultid = 0;
$error->linenumber = $tmp['lineNo'];
$error->htmlcode = $tmp['html'];
$error->errordescription = $tmp['title'];
// Add contentid and checkid so that we can query for the results record id later.
$error->contentid = $contentid;
$error->checkid = $checkid;
$errors[] = $error;
}
}
$returnchecks[] = $recordres;
$bulkrecordcount++;
// If we've hit the bulk limit, write the results records and reset.
if ($bulkrecordcount > $bulkrecordlimit) {
$DB->insert_records(manager::DB_RESULTS, $returnchecks);
$bulkrecordcount = 0;
$returnchecks = [];
// Get the results id value for each error record and write the errors.
foreach ($errors as $key2 => $error) {
$errors[$key2]->resultid = $DB->get_field(manager::DB_RESULTS, 'id',
['contentid' => $error->contentid, 'checkid' => $error->checkid]);
unset($errors[$key2]->contentid);
unset($errors[$key2]->checkid);
}
$DB->insert_records(manager::DB_ERRORS, $errors);
$errors = [];
}
}
// Write any leftover records.
if ($bulkrecordcount > 0) {
$DB->insert_records(manager::DB_RESULTS, $returnchecks);
// Get the results id value for each error record and write the errors.
foreach ($errors as $key => $error) {
$errors[$key]->resultid = $DB->get_field(manager::DB_RESULTS, 'id',
['contentid' => $error->contentid, 'checkid' => $error->checkid]);
unset($errors[$key]->contentid);
unset($errors[$key]->checkid);
}
$DB->insert_records(manager::DB_ERRORS, $errors);
}
$resultstime += (time() - $stime);
}
/**
* This function runs one specified check on the html item
*
* @param string $html The html string to be analysed; might be NULL.
* @param int $contentid The content area ID
* @param int $errid The error ID
* @param string $check The check name to run
* @param int $processingtime
* @param int $resultstime
* @throws \coding_exception
* @throws \dml_exception
*/
public static function run_one_check(
string $html = null,
int $contentid,
int $errid,
string $check,
int &$processingtime,
int &$resultstime
) {
global $DB;
$stime = time();
$checkdata = $DB->get_record(manager::DB_CHECKS, ['shortname' => $check], 'id,shortname,severity');
$testname = 'brickfield';
// Swapping in new library.
$htmlchecker = new local\htmlchecker\brickfield_accessibility($html, $testname, 'string');
$htmlchecker->run_check();
$report = $htmlchecker->get_test($check);
$processingtime += (time() - $stime);
$record = [];
$record['count'] = 0;
$record['errors'] = [];
foreach ($report as $a) {
$a->html = $a->get_html();
$record['errors'][] = $a;
$record['count']++;
}
// Build up record for inserting.
$recordres = new stdClass();
// Handling if checkid is unknown.
$checkid = (isset($checkdata->id)) ? $checkdata->id : 0;
$recordres->contentid = $contentid;
$recordres->checkid = $checkid;
$recordres->errorcount = $record['count'];
if ($exists = $DB->get_record(manager::DB_RESULTS, ['contentid' => $contentid, 'checkid' => $checkid])) {
$resultid = $exists->id;
$DB->set_field(manager::DB_RESULTS, 'errorcount', $record['count'], ['id' => $resultid]);
// Remove old error records for specific resultid, if existing.
$DB->delete_records(manager::DB_ERRORS, ['id' => $errid]);
} else {
$resultid = $DB->insert_record(manager::DB_RESULTS, $recordres);
}
$errors = [];
// Build error inserts if needed.
if ($record['count'] > 0) {
// Reporting all found errors for this check, so need to ignore existing other error records.
foreach ($record['errors'] as $tmp) {
// Confirm if error is reported separately.
if ($DB->record_exists_select(manager::DB_ERRORS,
'resultid = ? AND ' . $DB->sql_compare_text('htmlcode', 255) . ' = ' . $DB->sql_compare_text('?', 255),
[$resultid, html_entity_decode($tmp->html)])) {
continue;
}
$error = new stdClass();
$error->resultid = $resultid;
$error->linenumber = $tmp->line;
$error->htmlcode = html_entity_decode($tmp->html);
$errors[] = $error;
}
$DB->insert_records(manager::DB_ERRORS, $errors);
}
$resultstime += (time() - $stime);
}
/**
* Returns all of the id's and shortnames of all of the checks.
* @param int $status
* @return array
* @throws \dml_exception
*/
public static function checkids(int $status = 1): array {
global $DB;
$checks = $DB->get_records_menu(manager::DB_CHECKS, ['status' => $status], 'id ASC', 'id,shortname');
return $checks;
}
/**
* Returns an array of translations from htmlchecker of all of the checks, and their descriptions.
* @return array
* @throws \dml_exception
*/
public static function get_translations(): array {
global $DB;
$htmlchecker = new local\htmlchecker\brickfield_accessibility('test', 'brickfield', 'string');
$htmlchecker->run_check();
ksort($htmlchecker->guideline->translations);
// Need to limit to active checks.
$activechecks = $DB->get_fieldset_select(manager::DB_CHECKS, 'shortname', 'status = :status', ['status' => 1]);
$translations = [];
foreach ($htmlchecker->guideline->translations as $key => $trans) {
if (in_array($key, $activechecks)) {
$translations[$key] = $trans;
}
}
return $translations;
}
/**
* Returns an array of all of the course id's for a given category.
* @param int $categoryid
* @return array|null
* @throws \dml_exception
*/
public static function get_category_courseids(int $categoryid): ?array {
global $DB;
if (!$DB->record_exists('course_categories', ['id' => $categoryid])) {
return null;
}
$sql = "SELECT {course}.id
FROM {course}, {course_categories}
WHERE {course}.category = {course_categories}.id
AND (
" . $DB->sql_like('path', ':categoryid1') . "
OR " . $DB->sql_like('path', ':categoryid2') . "
)";
$params = ['categoryid1' => "%/$categoryid/%", 'categoryid2' => "%/$categoryid"];
$courseids = $DB->get_fieldset_sql($sql, $params);
return $courseids;
}
/**
* Get summary data for this site.
* @param int $id
* @return \stdClass
* @throws \dml_exception
*/
public static function get_summary_data(int $id): \stdClass {
global $CFG, $DB;
$components = $DB->get_records(manager::DB_AREAS);
$summarydata = new \stdClass();
$summarydata->siteurl = (substr($CFG->wwwroot, -1) !== '/') ? $CFG->wwwroot . '/' : $CFG->wwwroot;
$summarydata->moodlerelease = (preg_match('/^(\d+\.\d.*?)[. ]/', $CFG->release, $matches)) ? $matches[1] : $CFG->release;
$summarydata->numcourses = $DB->count_records('course') - 1;
$summarydata->numusers = $DB->count_records('user', array('deleted' => 0));
$summarydata->numfiles = $DB->count_records('files');
$summarydata->numfactivities = $DB->count_records('course_modules');
$summarydata->mobileservice = (int)$CFG->enablemobilewebservice === 1 ? true : false;
$summarydata->usersmobileregistered = $DB->count_records('user_devices');
$summarydata->contenttyperesults = static::get_contenttyperesults($id, $components);
$summarydata->contenttypeerrors = static::get_contenttypeerrors();
$summarydata->percheckerrors = static::get_percheckerrors();
return $summarydata;
}
/**
* Get content type results.
* @param int $id
* @param array $components
* @return \stdClass
*/
private static function get_contenttyperesults(int $id, array $components): \stdClass {
$contenttyperesults = new \stdClass();
$contenttyperesults->id = $id;
$datacomponents = array();
foreach ($components as $component) {
$datacomponents[$component->component][] = $component;
}
$contenttyperesults->contenttype = new \stdClass();
foreach ($datacomponents as $key => $component) {
$contenttyperesults->contenttype->$key = count($component);
}
$contenttyperesults->summarydatastorage = static::get_summary_data_storage();
$contenttyperesults->datachecked = time(); // Correct??
return $contenttyperesults;
}
/**
* Get per check errors.
* @return stdClass
* @throws dml_exception
*/
private static function get_percheckerrors(): stdClass {
global $DB;
$sql = 'SELECT ' . $DB->sql_concat_join("'_'", ['courseid', 'checkid']) . ' as tmpid,
ca.courseid, ca.status, ca.checkid, ch.shortname, ca.checkcount, ca.errorcount
FROM {' . manager::DB_CACHECHECK . '} ca
INNER JOIN {' . manager::DB_CHECKS . '} ch on ch.id = ca.checkid
ORDER BY courseid, checkid ASC';
$combo = $DB->get_records_sql($sql);
return (object) [
'percheckerrors' => $combo,
];
}
/**
* Get content type errors.
* @return stdClass
* @throws dml_exception
*/
private static function get_contenttypeerrors(): stdClass {
global $DB;
$fields = 'courseid, status, activities, activitiespassed, activitiesfailed,
errorschecktype1, errorschecktype2, errorschecktype3, errorschecktype4,
errorschecktype5, errorschecktype6, errorschecktype7,
failedchecktype1, failedchecktype2, failedchecktype3, failedchecktype4,
failedchecktype5, failedchecktype6, failedchecktype7,
percentchecktype1, percentchecktype2, percentchecktype3, percentchecktype4,
percentchecktype5, percentchecktype6, percentchecktype7';
$combo = $DB->get_records(manager::DB_SUMMARY, null, 'courseid ASC', $fields);
return (object) [
'typeerrors' => $combo,
];
}
/**
* Get summary data storage.
* @return array
* @throws dml_exception
*/
private static function get_summary_data_storage(): array {
global $DB;
$fields = $DB->sql_concat_join("''", ['component', 'courseid']) . ' as tmpid,
courseid, component, errorcount, totalactivities, failedactivities, passedactivities';
$combo = $DB->get_records(manager::DB_CACHEACTS, null, 'courseid, component ASC', $fields);
return $combo;
}
}