mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-16 21:41:31 +02:00
Issue #21 - Added flag to outages, if set it will automatically start the maintenance mode once the outage starts.
This commit is contained in:
@@ -18,6 +18,7 @@ namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagedb;
|
||||
use coding_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@@ -44,6 +45,7 @@ class create extends clibase {
|
||||
return [
|
||||
'help' => false,
|
||||
'clone' => null,
|
||||
'autostart' => null,
|
||||
'warn' => null,
|
||||
'start' => null,
|
||||
'duration' => null,
|
||||
@@ -60,6 +62,7 @@ class create extends clibase {
|
||||
*/
|
||||
public function generate_shortcuts() {
|
||||
return [
|
||||
'a' => 'autostart',
|
||||
'b' => 'block',
|
||||
'c' => 'clone',
|
||||
'd' => 'duration',
|
||||
@@ -74,8 +77,26 @@ class create extends clibase {
|
||||
/**
|
||||
* Sets the default values for options.
|
||||
* @param mixed[] $defaults Defaults.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function set_defaults(array $defaults) {
|
||||
$missing = $this->generate_options();
|
||||
|
||||
// Check if any extra parameter was given.
|
||||
foreach (array_keys($defaults) as $key) {
|
||||
if (!array_key_exists($key, $missing)) {
|
||||
throw new coding_exception('$default['.$key.'] is not valid.');
|
||||
}
|
||||
unset($missing[$key]);
|
||||
}
|
||||
|
||||
// Check if any required parameter is missing.
|
||||
foreach (array_keys($missing) as $k => $v) {
|
||||
if (is_null($v)) {
|
||||
throw new coding_exception('$default[] missing: '.$k);
|
||||
}
|
||||
}
|
||||
|
||||
$this->defaults = $defaults;
|
||||
}
|
||||
|
||||
@@ -110,12 +131,13 @@ class create extends clibase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges provided options with defaults, checking and converting types as needed.
|
||||
* Merges provided options with defaults.
|
||||
* @return mixed[] Parameters to use.
|
||||
* @throws cli_exception
|
||||
*/
|
||||
private function merge_options() {
|
||||
$options = $this->options;
|
||||
|
||||
// Merge with defaults.
|
||||
if (!is_null($this->defaults)) {
|
||||
foreach ($options as $k => $v) {
|
||||
@@ -140,6 +162,7 @@ class create extends clibase {
|
||||
// Create the outage.
|
||||
$start = $this->time + $options['start'];
|
||||
$outage = new outage([
|
||||
'autostart' => $options['autostart'],
|
||||
'warntime' => $start - $options['warn'],
|
||||
'starttime' => $start,
|
||||
'stoptime' => $start + $options['duration'],
|
||||
@@ -166,6 +189,7 @@ class create extends clibase {
|
||||
|
||||
$outage = outagedb::get_by_id((int)$id);
|
||||
$this->set_defaults([
|
||||
'autostart' => $outage->autostart,
|
||||
'warn' => $outage->get_warning_duration(),
|
||||
'duration' => $outage->get_duration_planned(),
|
||||
'title' => $outage->title,
|
||||
@@ -202,6 +226,29 @@ class create extends clibase {
|
||||
}
|
||||
}
|
||||
|
||||
// Check parameters that must be a specified bool.
|
||||
foreach (['autostart'] as $param) {
|
||||
if (is_string($options[$param])) {
|
||||
switch (strtoupper($options[$param])) {
|
||||
case '0':
|
||||
case 'FALSE':
|
||||
case 'NO':
|
||||
case 'N':
|
||||
$options[$param] = false;
|
||||
break;
|
||||
case '1':
|
||||
case 'TRUE':
|
||||
case 'YES':
|
||||
case 'Y':
|
||||
$options[$param] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!is_bool($options[$param])) {
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,9 @@ class infopage {
|
||||
* Saves a static info page for the given outage.
|
||||
* @param outage $outage Outage to generate the info page.
|
||||
* @param string $file File to save the static info page.
|
||||
* @throws coding_exception
|
||||
* @throws file_exception
|
||||
* @throws invalid_state_exception
|
||||
*/
|
||||
public static function save_static_page(outage $outage, $file) {
|
||||
if (!is_string($file)) {
|
||||
|
||||
@@ -56,22 +56,27 @@ class outage {
|
||||
const STAGE_STOPPED = 'stopped';
|
||||
|
||||
/**
|
||||
* @var int Outage ID (auto generated by the DB).
|
||||
* @var int|null Outage ID (auto generated by the DB).
|
||||
*/
|
||||
public $id = null;
|
||||
|
||||
/**
|
||||
* @var int Start Time timestamp.
|
||||
* @var bool|null Maintenance mode auto start flag.
|
||||
*/
|
||||
public $autostart = null;
|
||||
|
||||
/**
|
||||
* @var int|null Start Time timestamp.
|
||||
*/
|
||||
public $starttime = null;
|
||||
|
||||
/**
|
||||
* @var int Stop Time timestamp.
|
||||
* @var int|null Stop Time timestamp.
|
||||
*/
|
||||
public $stoptime = null;
|
||||
|
||||
/**
|
||||
* @var int Warning start timestamp.
|
||||
* @var int|null Warning start timestamp.
|
||||
*/
|
||||
public $warntime = null;
|
||||
|
||||
@@ -81,27 +86,27 @@ class outage {
|
||||
public $finished = null;
|
||||
|
||||
/**
|
||||
* @var string Short description of the outage (no HTML).
|
||||
* @var string|null Short description of the outage (no HTML).
|
||||
*/
|
||||
public $title = null;
|
||||
|
||||
/**
|
||||
* @var string Description of the outage (some HTML allowed).
|
||||
* @var string|null Description of the outage (some HTML allowed).
|
||||
*/
|
||||
public $description = null;
|
||||
|
||||
/**
|
||||
* @var int Moodle User Id that created this outage.
|
||||
* @var int|null Moodle User Id that created this outage.
|
||||
*/
|
||||
public $createdby = null;
|
||||
|
||||
/**
|
||||
* @var int Moodle User Id that last modified this outage.
|
||||
* @var int|null Moodle User Id that last modified this outage.
|
||||
*/
|
||||
public $modifiedby = null;
|
||||
|
||||
/**
|
||||
* @var int Timestamp of when this outage was last modified.
|
||||
* @var int|null Timestamp of when this outage was last modified.
|
||||
*/
|
||||
public $lastmodified = null;
|
||||
|
||||
@@ -133,6 +138,9 @@ class outage {
|
||||
foreach ($fs as $f) {
|
||||
$this->$f = ($this->$f === null) ? null : (int)$this->$f;
|
||||
}
|
||||
|
||||
// Adjust bool fields.
|
||||
$this->autostart = ($this->autostart === null) ? null : (bool)$this->autostart;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,6 @@ namespace auth_outage\local;
|
||||
use auth_outage\event\outage_created;
|
||||
use auth_outage\event\outage_deleted;
|
||||
use auth_outage\event\outage_updated;
|
||||
use auth_outage\local\controllers\infopage;
|
||||
use calendar_event;
|
||||
use coding_exception;
|
||||
|
||||
@@ -118,8 +117,8 @@ class outagedb {
|
||||
self::calendar_update($outage);
|
||||
}
|
||||
|
||||
// Trigger static page update.
|
||||
infopage::update_static_page();
|
||||
// Trigger outages modified events.
|
||||
outagelib::outages_modified();
|
||||
|
||||
// All done, return the id.
|
||||
return $outage->id;
|
||||
@@ -148,8 +147,8 @@ class outagedb {
|
||||
$DB->delete_records('auth_outage', ['id' => $id]);
|
||||
self::calendar_delete($id);
|
||||
|
||||
// Trigger static page update.
|
||||
infopage::update_static_page();
|
||||
// Trigger events.
|
||||
outagelib::outages_modified();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -297,10 +296,40 @@ class outagedb {
|
||||
throw new coding_exception('$time must be null or a positive int.', $time);
|
||||
}
|
||||
|
||||
$select = ':datetime <= starttime'; // End condition.
|
||||
$data = $DB->get_records_select(
|
||||
'auth_outage',
|
||||
$select,
|
||||
':datetime <= starttime',
|
||||
['datetime' => $time],
|
||||
'starttime ASC',
|
||||
'*',
|
||||
0,
|
||||
1
|
||||
);
|
||||
|
||||
// Not using $DB->get_record_select instead because there is no 'limit' parameter.
|
||||
// Allowing multiple records still raises an internal error.
|
||||
return (count($data) == 0) ? null : new outage(array_shift($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next outage which has not started yet and has the autostart flag set to true.
|
||||
* @param null $time Timestamp reference for current time.
|
||||
* @return outage|null The outage or null if not found.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function get_next_autostarting($time = null) {
|
||||
global $DB;
|
||||
|
||||
if ($time === null) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new coding_exception('$time must be null or a positive int.', $time);
|
||||
}
|
||||
|
||||
$data = $DB->get_records_select(
|
||||
'auth_outage',
|
||||
'(:datetime <= starttime) AND (autostart = 1)',
|
||||
['datetime' => $time],
|
||||
'starttime ASC',
|
||||
'*',
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
namespace auth_outage\local;
|
||||
|
||||
use auth_outage\local\controllers\infopage;
|
||||
use auth_outage_renderer;
|
||||
use Exception;
|
||||
use moodle_url;
|
||||
@@ -112,11 +113,32 @@ class outagelib {
|
||||
global $CFG;
|
||||
|
||||
return [
|
||||
'default_autostart' => false,
|
||||
'default_duration' => 60,
|
||||
'warning_duration' => 60,
|
||||
'warning_title' => get_string('defaultwarningtitlevalue', 'auth_outage'),
|
||||
'warning_description' => get_string('defaultwarningdescriptionvalue', 'auth_outage'),
|
||||
'default_warning_duration' => 60,
|
||||
'default_warning_title' => get_string('defaultwarningtitlevalue', 'auth_outage'),
|
||||
'default_warning_description' => get_string('defaultwarningdescriptionvalue', 'auth_outage'),
|
||||
'css' => file_get_contents($CFG->dirroot.'/auth/outage/views/warningbar.css'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when outages are modified (created, updated or deleted).
|
||||
*/
|
||||
public static function outages_modified() {
|
||||
infopage::update_static_page();
|
||||
self::update_maintenance_later();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls Moodle API - set_maintenance_later() to set when the next outage starts.
|
||||
*/
|
||||
private static function update_maintenance_later() {
|
||||
$next = outagedb::get_next_autostarting();
|
||||
if (is_null($next)) {
|
||||
unset_config('maintenance_later');
|
||||
} else {
|
||||
set_config('maintenance_later', $next->starttime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
39
classes/task/update_static_page.php
Normal file
39
classes/task/update_static_page.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace auth_outage\task;
|
||||
|
||||
use auth_outage\local\controllers\infopage;
|
||||
use core\task\scheduled_task;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Tasks information.
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class update_static_page extends scheduled_task {
|
||||
public function get_name() {
|
||||
return get_string('taskupdatestaticpage', 'auth_outage');
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
infopage::update_static_page();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user