1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Check API result functions
|
|
|
18 |
*
|
|
|
19 |
* @module core/check
|
|
|
20 |
* @author Matthew Hilton <matthewhilton@catalyst-au.net>
|
|
|
21 |
* @copyright Catalyst IT, 2023
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
import {getCheckResult} from './repository';
|
|
|
26 |
import {getString} from 'core/str';
|
|
|
27 |
import * as Templates from 'core/templates';
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Get the result of a check and replace a given DOM element with the result.
|
|
|
31 |
*
|
|
|
32 |
* @method getAndRender
|
|
|
33 |
* @param {String} domSelector A CSS selector for a dom element to replace the the HTML for.
|
|
|
34 |
* @param {String} adminTreeId Id of the admin_setting that called this webservice. Used to retrieve the check registered to it.
|
|
|
35 |
* @param {String} settingName Name of setting (used to find the parent node in the admin tree)
|
|
|
36 |
* @param {Boolean} includeDetails If true, details will be included in the check.
|
|
|
37 |
* By default only the status and the summary is returned.
|
|
|
38 |
*/
|
|
|
39 |
export async function getAndRender(domSelector, adminTreeId, settingName, includeDetails) {
|
|
|
40 |
const element = document.querySelector(domSelector);
|
|
|
41 |
|
|
|
42 |
if (!element) {
|
|
|
43 |
window.console.error('Check selector not found');
|
|
|
44 |
return;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
try {
|
|
|
48 |
const result = await getCheckResult(adminTreeId, settingName, includeDetails);
|
|
|
49 |
const decoded = new DOMParser().parseFromString(result.html, "text/html").documentElement.textContent;
|
|
|
50 |
element.innerHTML = decoded;
|
|
|
51 |
} catch (e) {
|
|
|
52 |
window.console.error(e);
|
|
|
53 |
|
|
|
54 |
// Render error as a red notification.
|
|
|
55 |
element.innerHTML = await Templates.render('core/notification', {
|
|
|
56 |
iserror: true,
|
|
|
57 |
closebutton: false,
|
|
|
58 |
announce: 0,
|
|
|
59 |
extraclasses: '',
|
|
|
60 |
message: await getString('checkerror', 'core', adminTreeId)
|
|
|
61 |
});
|
|
|
62 |
}
|
|
|
63 |
}
|