diff --git a/classes/form/override_form.php b/classes/form/override_form.php index e9dbf1e..02c8876 100644 --- a/classes/form/override_form.php +++ b/classes/form/override_form.php @@ -24,8 +24,8 @@ namespace tool_heartbeat\form; -use core\check\result; use core\form\persistent; +use tool_heartbeat\object\override; /** * Override form @@ -50,15 +50,7 @@ public function definition() { $mform->setConstant('ref', $this->_customdata['ref']); // Override. - $mform->addElement('select', 'override', get_string('override', 'tool_heartbeat'), [ - result::NA => get_string('statusna'), - result::OK => get_string('statusok'), - result::INFO => get_string('statusinfo'), - result::UNKNOWN => get_string('statusunknown'), - result::WARNING => get_string('statuswarning'), - result::CRITICAL => get_string('statuscritical'), - result::ERROR => get_string('statuserror'), - ]); + $mform->addElement('select', 'override', get_string('override', 'tool_heartbeat'), override::get_status_list()); // Note. $mform->addElement('textarea', 'note', get_string('notes', 'core_notes'), ['rows' => 3]); @@ -67,6 +59,13 @@ public function definition() { // URL. $mform->addElement('text', 'url', get_string('url'), ['size' => 80]); $mform->setType('url', PARAM_URL); + $urlregex = get_config('tool_heartbeat', 'muteurlregex'); + if (!empty($urlregex)) { + $urlregex = stripslashes($urlregex); + $mform->addRule('url', get_string('required'), 'required', null, 'client'); + $mform->addRule('url', get_string('muteurlregex', 'tool_heartbeat', $urlregex), 'regex', $urlregex, 'client'); + $mform->addElement('static', 'url_help', '', get_string('muteurlregex', 'tool_heartbeat', $urlregex)); + } // Override until. $mform->addElement('date_selector', 'expires_at', get_string('expiresat', 'tool_heartbeat')); diff --git a/classes/object/override.php b/classes/object/override.php index 10ce115..44d3dbf 100644 --- a/classes/object/override.php +++ b/classes/object/override.php @@ -25,6 +25,7 @@ namespace tool_heartbeat\object; +use core\check\result; use core\persistent; /** @@ -45,6 +46,7 @@ class override extends Persistent { */ public function __construct(int $id = 0, \stdClass $record = null) { + $this->set_default_status(); $this->set_default_expiry(); parent::__construct($id, $record); } @@ -99,6 +101,18 @@ private function set_default_expiry() { $this->set('expires_at', strtotime($expiredate)); } + /** + * Sets the default override status + * + * @return void + */ + private function set_default_status() { + $defaultstatus = get_config('tool_heartbeat', 'mutedefaultstatus'); + if (!empty($defaultstatus)) { + $this->set('override', $defaultstatus); + } + } + /** * Resolves and unmutes a check. * @@ -204,4 +218,24 @@ public function get_time_until_mute_ends(): string { $remainingtime = format_time($this->get('expires_at') - time()); return "$enddate ($remainingtime)"; } + + /** + * Returns an array of statuses that can be used as an override + * + * @return array of status + */ + public static function get_status_list(): array { + // Moodle 3.9-3.11 sites that haven't backported MDL-71627 will be missing 'statusunknown'. + $unknown = (moodle_major_version() < '4.0') ? get_string('statusunknown', 'tool_heartbeat') : get_string('statusunknown'); + + return [ + result::NA => get_string('statusna'), + result::OK => get_string('statusok'), + result::INFO => get_string('statusinfo'), + result::UNKNOWN => $unknown, + result::WARNING => get_string('statuswarning'), + result::CRITICAL => get_string('statuscritical'), + result::ERROR => get_string('statuserror'), + ]; + } } diff --git a/lang/en/tool_heartbeat.php b/lang/en/tool_heartbeat.php index 9b8b2bd..5ab9179 100644 --- a/lang/en/tool_heartbeat.php +++ b/lang/en/tool_heartbeat.php @@ -97,8 +97,14 @@ $string['editmute'] = 'Edit mute'; $string['overriderestore'] = 'Fields have been pre-filled with information from a previous override.'; $string['noterequired'] = 'Please add some notes'; +$string['muteurlregex'] = 'The URL must match the regex defined in settings: {$a}'; +$string['statusunknown'] = 'Unknown'; $string['settings:mutedefault'] = 'Default mute duration'; $string['settings:mutedefault:desc'] = 'Adjust the default duration of a check mute.'; +$string['settings:mutedefaultstatus'] = 'Default mute status'; +$string['settings:mutedefaultstatus:desc'] = 'Adjust the default status of a check mute.'; +$string['settings:muteurlregex'] = 'Mute URL regex rule'; +$string['settings:muteurlregex:desc'] = 'Adds a regex matching rule for check mute URLs and makes it a required field. The provided regex should include delimiters.'; $string['settings:cachecheckheading'] = 'Cache consistency check'; $string['settings:shouldlogcacheping:heading'] = 'Log cache ping'; diff --git a/settings.php b/settings.php index 4375c40..4567a27 100644 --- a/settings.php +++ b/settings.php @@ -105,6 +105,16 @@ get_string('settings:mutedefault', 'tool_heartbeat'), get_string('settings:mutedefault:desc', 'tool_heartbeat'), 12 * WEEKSECS, WEEKSECS)); + $statuslist = tool_heartbeat\object\override::get_status_list(); + reset($statuslist); + $settings->add(new admin_setting_configselect('tool_heartbeat/mutedefaultstatus', + get_string('settings:mutedefaultstatus', 'tool_heartbeat'), + get_string('settings:mutedefaultstatus:desc', 'tool_heartbeat'), key($statuslist), $statuslist)); + + $settings->add(new admin_setting_configtext('tool_heartbeat/muteurlregex', + get_string('settings:muteurlregex', 'tool_heartbeat'), + get_string('settings:muteurlregex:desc', 'tool_heartbeat'), '', PARAM_TEXT)); + $example = '\logstore_standard\task\cleanup_task, 5, 5, 5'; $settings->add(new admin_setting_configtextarea('tool_heartbeat/tasklatencymonitoring', get_string('tasklatencymonitoring', 'tool_heartbeat'), diff --git a/version.php b/version.php index e9f27d5..1a5ae2c 100644 --- a/version.php +++ b/version.php @@ -24,8 +24,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024073000; -$plugin->release = 2024073000; // Match release exactly to version. +$plugin->version = 2024080200; +$plugin->release = 2024080200; // Match release exactly to version. $plugin->requires = 2020061500; // Support for 3.9 and above, due to the Check API. $plugin->supported = [39, 404]; $plugin->component = 'tool_heartbeat';