Skip to content

Commit f03bc53

Browse files
matthewhiltonsarjona
authored andcommitted
[docs] check: add new admin setting docs
New features as part of MDL-67898
1 parent df2a305 commit f03bc53

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

docs/apis/subsystems/check/index.md

+79
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,85 @@ https://github.com/moodle/moodle/blob/master/lib/classes/check/access/riskxss_re
225225

226226
Some checks are by their nature asynchronous. For instance having moodle send an email to itself and then having it processed by the inbound mail handler to make it's properly configured (see [MDL-48800](https://tracker.moodle.org/browse/MDL-48800)). In cases like these please make sure the age or staleness of the check is shown in the summary, and you should also consider turning the result status into a warning if the result is too old. If appropriate make the threshold a configurable admin setting.
227227

228+
## Showing checks in admin settings
229+
230+
Even though checks will appear in the corresponding report pages, it may be useful to have these show up in admin settings close to where they may be actioned. For example, showing a LDAP connection check on the same page as the LDAP connection settings.
231+
232+
Use the `admin_setting_check` class to easily add these.
233+
234+
```php title="mod/myplugin/settings.php"
235+
$settings->add(
236+
new admin_setting_check(
237+
'myplugin/usefulcheck',
238+
new \mod\myplugin\check\usefulcheck(),
239+
),
240+
);
241+
242+
// Or with multiple instances of checks.
243+
$settings->add(
244+
new admin_setting_check(
245+
'myplugin/usefulcheck1',
246+
new \mod\myplugin\check\usefulcheck(1),
247+
),
248+
);
249+
$settings->add(
250+
new admin_setting_check(
251+
'myplugin/usefulcheck2',
252+
new \mod\myplugin\check\usefulcheck(2),
253+
),
254+
);
255+
```
256+
257+
The results of the check are fetched asynchronously using AJAX, so the page loading time is impacted as little as possible.
258+
259+
### Utilising as a plugin configuration check
260+
261+
Internally, the `admin_setting_check` class calls a webservice that will load the admin tree to find the check object it is linked to. Therefore it is possible to include checks here that are not included in your plugin's lib.php callback, or pass flags to your checks to change how they behave.
262+
263+
For example, a check might look like the following:
264+
265+
```php title="mod/myplugin/classes/connectioncheck.php"
266+
class connectioncheck extends check {
267+
268+
private $isprecheck;
269+
270+
public function __construct($isprecheck = false) {
271+
$this->isprecheck = $isprecheck;
272+
}
273+
274+
public function get_result(): result {
275+
$enabled = get_config(...);
276+
277+
if ($enabled || $this->isprecheck) {
278+
return new foobar_result();
279+
}
280+
281+
return na_result();
282+
}
283+
}
284+
```
285+
286+
The lib.php callback will pass `$isprecheck = false`, so it reports N/A if the plugin is disabled in the status reports.
287+
288+
```php title="mod/myplugin/lib.php"
289+
function mod_myplugin_status_checks() {
290+
return [new connection_check(false)];
291+
}
292+
```
293+
294+
But when viewed in the admin settings, `$isprecheck = true` is passed so the check runs regardless of the plugin being enabled.
295+
296+
```php title="mod/myplugin/settings.php"
297+
$settings->add(
298+
new admin_setting_check(
299+
'myplugin/usefulcheck',
300+
new \mod\myplugin\check\connectioncheck(true),
301+
),
302+
);
303+
```
304+
305+
Users can use this to confirm they have plugin settings (such as connection details or API tokens) correct before enabling the plugin.
306+
228307
## See also
229308

230309
- [Performance overview](https://docs.moodle.org/en/Performance_overview) user docs

0 commit comments

Comments
 (0)