Skip to content

Commit

Permalink
Add restore override functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Feb 26, 2024
1 parent 488741f commit 29bca55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
56 changes: 44 additions & 12 deletions classes/object/override.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class override extends Persistent {
*/

public function __construct(int $id = 0, \stdClass $record = null) {
$expiredate = date('Y-m-d', time() + (int) get_config('tool_heartbeat', 'mutedefault'));
$this->set('expires_at', strtotime($expiredate));
$this->set_default_expiry();
parent::__construct($id, $record);
}

Expand Down Expand Up @@ -85,6 +84,16 @@ protected static function define_properties(): array {
];
}

/**
* Sets the expiry time to the default value
*
* @return void
*/
private function set_default_expiry() {
$expiredate = date('Y-m-d', time() + (int) get_config('tool_heartbeat', 'mutedefault'));
$this->set('expires_at', strtotime($expiredate));
}

/**
* Removes a mute from a check.
*
Expand All @@ -104,16 +113,10 @@ public function unmute() {
public static function get_active_override($ref): ?override {
$overrides = self::get_active_overrides();
$id = $overrides[$ref]->id ?? null;
return !empty($id) ? new override($id) : null;
}

/**
* Returns a list of references for all active overrides.
*
* @return array
*/
public static function get_active_override_refs(): array {
return array_keys(self::get_active_overrides());
if (!empty($id)) {
return new override($id);
}
return null;
}

/**
Expand All @@ -134,6 +137,35 @@ protected static function get_active_overrides(): array {
return $overrides;
}

/**
* Returns an instance of a previous override with reset values.
*
* @param string $ref
* @return override|null
*/
public static function get_restored_override(string $ref): ?override {
// Make sure there's no active override.
$active = self::get_active_override($ref);
if (!empty($active)) {
return null;
}

// Get previous override that has expired or been resolved within the last year.
$conditions = "ref = ? AND (expires_at < ? OR resolved_at != 0) AND expires_at > ?";
$params = [$ref, time(), strtotime('-1 year')];
$previous = self::get_records_select($conditions, $params, 'expires_at DESC, resolved_at DESC', 'ref, *', 0, 1);
if (empty($previous)) {
return null;
}

// Copy record and clear previous time and id data so a new record is created.
$override = $previous[$ref];
$override->set('id', 0);
$override->set('resolved_at', 0);
$override->set_default_expiry();
return $override;
}

/**
* Returns the time until mute ends
*
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
$string['override'] = 'Display status';
$string['addmute'] = 'Add mute';
$string['editmute'] = 'Edit mute';
$string['overriderestore'] = 'Fields have been pre-filled with information from a previous override.';
$string['overriderequired'] = 'Please select an status to use while muted';
$string['noterequired'] = 'Please add some notes';
$string['muteuntilrequired'] = 'Please select a date';
Expand Down
15 changes: 13 additions & 2 deletions override.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
redirect(new moodle_url('/admin/tool/heartbeat/status.php'));
}

// Try restoring override.
$restored = false;
if (empty($override)) {
$override = override::get_restored_override($ref);
$restored = !empty($override);
}

// Create the form instance. We need to use the current URL and the custom data.
$customdata = [
'persistent' => $override,
Expand All @@ -65,7 +72,7 @@
$form = new override_form($PAGE->url->out(false), $customdata);

// Get the data. This ensures that the form was validated.
if (($data = $form->get_data())) {
if ($data = $form->get_data()) {

try {
if (empty($data->id)) {
Expand All @@ -86,10 +93,14 @@
redirect(new moodle_url('/admin/tool/heartbeat/status.php'));
}

if ($restored) {
\core\notification::INFO(get_string('overriderestore', 'tool_heartbeat'));
}

// Display the mandatory header and footer.
// And display the form, and its validation errors if there are any.
echo $OUTPUT->header();
$headingstring = empty($override) ? 'addmute' : 'editmute';
$headingstring = (empty($override) || $restored) ? 'addmute' : 'editmute';
echo $OUTPUT->heading(get_string($headingstring, 'tool_heartbeat'));
$form->display();
echo $OUTPUT->footer();

0 comments on commit 29bca55

Please sign in to comment.