MDL-67898 check: Add check admin setting

This admin setting allows you to display a check anywhere in the admin
tree. It uses a webservice to execute the check, so the impact on the
admin tree performance is as low as possible.

Checks do not necessarily need to be registered in the plugins callback
to be shown here, allowing customisation of what is shown in the
settings versus the reports.
This commit is contained in:
Matthew Hilton
2023-11-21 12:04:42 +10:00
parent bc4df2af71
commit 9b8acd44d1
13 changed files with 721 additions and 1 deletions
+97
View File
@@ -11679,3 +11679,100 @@ class admin_settings_h5plib_handler_select extends admin_setting_configselect {
return true;
}
}
/**
* Displays the result of a check via ajax.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Matthew Hilton <matthewhilton@catalyst-au.net>
* @copyright Catalyst IT, 2023
*/
class admin_setting_check extends admin_setting {
/** @var \core\check\check $check the check to use **/
private $check;
/** @var bool $includedetails if the details of result are included. **/
private $includedetails;
/**
* Creates check setting.
*
* @param string $name name of setting
* @param \core\check\check $check The check linked to this setting.
* @param bool $includedetails if the details of the result are included
*/
public function __construct(string $name, \core\check\check $check, bool $includedetails = false) {
$this->check = $check;
$this->includedetails = $includedetails;
$heading = $check->get_name();
parent::__construct($name, $heading, '', '');
}
/**
* Returns the check linked to this setting.
*
* @return \core\check\check
*/
public function get_check() {
return $this->check;
}
/**
* Returns setting (unused)
*
* @return true
*/
public function get_setting() {
return true;
}
/**
* Writes the setting (unused)
*
* @param mixed $data
*/
public function write_setting($data) {
return '';
}
/**
* Outputs the admin setting HTML to be rendered.
*
* @param mixed $data
* @param string $query
* @return string html
*/
public function output_html($data, $query = '') {
global $PAGE, $OUTPUT;
$domref = uniqid($this->check->get_ref());
// The actual result is obtained via ajax,
// since its likely somewhat slow to obtain.
$context = [
'domselector' => '[data-check-reference="' . $domref . '"]',
'admintreeid' => $this->get_id(),
'settingname' => $this->name,
'includedetails' => $this->includedetails,
];
$PAGE->requires->js_call_amd('core/check/check_result', 'getAndRender', $context);
// Render a generic loading icon while waiting for ajax.
$loadingstr = get_string('checkloading', '', $this->check->get_name());
$loadingicon = $OUTPUT->pix_icon('i/loading', $loadingstr);
// Wrap it in a notification so we reduce style changes when loading is finished.
$output = $OUTPUT->notification($loadingicon . $loadingstr, \core\output\notification::NOTIFY_INFO, false);
// Add the action link.
$output .= $OUTPUT->render($this->check->get_action_link());
// Wrap in a div with a reference. The JS getAndRender will replace this with the response from the webservice.
$statusdiv = \html_writer::div($output, '', ['data-check-reference' => $domref]);
return format_admin_setting($this, $this->visiblename, '', $statusdiv);
}
}