mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-16 21:41:31 +02:00
Issue #42 - Refactoring code according to Moodle Coding Style.
This commit is contained in:
@@ -19,15 +19,35 @@ namespace auth_outage\event;
|
||||
use core\event\base;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The auth_outage outage created class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outage_created extends base {
|
||||
/**
|
||||
* Returns non-localised event description with id's for admin use only.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' created a new outage title '{$this->other['title']}' ".
|
||||
" with id '{$this->other['id']}'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns relevant URL, override in subclasses.
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/auth/outage/list.php#auth_outage_id_'.$this->other['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
@@ -37,22 +57,4 @@ class outage_created extends base {
|
||||
$this->data['edulevel'] = self::LEVEL_OTHER;
|
||||
$this->context = \context_system::instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns non-localised event description with id's for admin use only.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' created a new outage title '{$this->other['title']}' "
|
||||
. " with id '{$this->other['id']}'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns relevant URL, override in subclasses.
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,37 @@
|
||||
namespace auth_outage\event;
|
||||
|
||||
use core\event\base;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The auth_outage outage deleted class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outage_deleted extends base {
|
||||
/**
|
||||
* Returns non-localised event description with id's for admin use only.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' deleted the outage titled '{$this->other['title']}' ".
|
||||
"with id '{$this->other['id']}'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns relevant URL, override in subclasses.
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/auth/outage/list.php#auth_outage_id_'.$this->other['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
@@ -36,22 +57,4 @@ class outage_deleted extends base {
|
||||
$this->data['edulevel'] = self::LEVEL_OTHER;
|
||||
$this->context = \context_system::instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns non-localised event description with id's for admin use only.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' deleted the outage titled '{$this->other['title']}' "
|
||||
. "with id '{$this->other['id']}'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns relevant URL, override in subclasses.
|
||||
* @return \moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,37 @@
|
||||
namespace auth_outage\event;
|
||||
|
||||
use core\event\base;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The auth_outage outage updated class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outage_updated extends base {
|
||||
/**
|
||||
* Returns non-localised event description with id's for admin use only.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' updated the outage title '{$this->other['title']}' ".
|
||||
"with id '{$this->other['id']}'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns relevant URL, override in subclasses.
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/auth/outage/list.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
@@ -36,22 +57,4 @@ class outage_updated extends base {
|
||||
$this->data['edulevel'] = self::LEVEL_OTHER;
|
||||
$this->context = \context_system::instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns non-localised event description with id's for admin use only.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' updated the outage title '{$this->other['title']}' "
|
||||
. "with id '{$this->other['id']}'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns relevant URL, override in subclasses.
|
||||
* @return \moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url('/auth/outage/list.php');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,20 +14,20 @@
|
||||
// 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\forms\outage;
|
||||
namespace auth_outage\form\outage;
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
}
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Outage delete confirmation form.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class delete extends \moodleform {
|
||||
@@ -14,22 +14,21 @@
|
||||
// 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\forms\outage;
|
||||
namespace auth_outage\form\outage;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\local\outage;
|
||||
use coding_exception;
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
}
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
/**
|
||||
* Outage form.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class edit extends \moodleform {
|
||||
@@ -58,7 +57,7 @@ class edit extends \moodleform {
|
||||
'text',
|
||||
'title',
|
||||
get_string('title', 'auth_outage'),
|
||||
'maxlength="' . self::TITLE_MAX_CHARS . '" size="60"'
|
||||
'maxlength="'.self::TITLE_MAX_CHARS.'" size="60"'
|
||||
);
|
||||
$mform->setType('title', PARAM_TEXT);
|
||||
$mform->addHelpButton('title', 'title', 'auth_outage');
|
||||
@@ -74,9 +73,9 @@ class edit extends \moodleform {
|
||||
/**
|
||||
* Validate the parts of the request form for this module
|
||||
*
|
||||
* @param array $data An array of form data
|
||||
* @param array $files An array of form files
|
||||
* @return array of error messages
|
||||
* @param mixed[] $data An array of form data
|
||||
* @param string[] $files An array of form files
|
||||
* @return string[] of error messages
|
||||
*/
|
||||
public function validation($data, $files) {
|
||||
$errors = parent::validation($data, $files);
|
||||
@@ -110,7 +109,7 @@ class edit extends \moodleform {
|
||||
return null;
|
||||
}
|
||||
if ($data->description['format'] != '1') {
|
||||
debugging('Not implemented for format ' . $data->description['format'], DEBUG_DEVELOPER);
|
||||
debugging('Not implemented for format '.$data->description['format'], DEBUG_DEVELOPER);
|
||||
return null;
|
||||
}
|
||||
// Return an outage.
|
||||
@@ -120,16 +119,17 @@ class edit extends \moodleform {
|
||||
'stoptime' => $data->starttime + $data->outageduration,
|
||||
'warntime' => $data->starttime - $data->warningduration,
|
||||
'title' => $data->title,
|
||||
'description' => $data->description['text']
|
||||
'description' => $data->description['text'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load in existing outage as form defaults.
|
||||
*
|
||||
* @param outage $outage outage object with default values
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function set_data($outage) {
|
||||
// Cannot change method signature, check type.
|
||||
if ($outage instanceof outage) {
|
||||
$this->_form->setDefaults([
|
||||
'id' => $outage->id,
|
||||
@@ -137,10 +137,10 @@ class edit extends \moodleform {
|
||||
'outageduration' => $outage->get_duration_planned(),
|
||||
'warningduration' => $outage->get_warning_duration(),
|
||||
'title' => $outage->title,
|
||||
'description' => ['text' => $outage->description, 'format' => '1']
|
||||
'description' => ['text' => $outage->description, 'format' => '1'],
|
||||
]);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('$outage must be an outage object.');
|
||||
throw new coding_exception('$outage must be an outage object.', $outage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,22 +14,20 @@
|
||||
// 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\forms\outage;
|
||||
namespace auth_outage\form\outage;
|
||||
|
||||
use moodleform;
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
}
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
/**
|
||||
* Outage finish confirmation form.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class finish extends moodleform {
|
||||
@@ -14,19 +14,21 @@
|
||||
// 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\cli;
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use Exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Exception executing CLI.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cliexception extends Exception {
|
||||
class cli_exception extends Exception {
|
||||
/**
|
||||
* cliexception constructor.
|
||||
* @param string $message An explanation of the exception.
|
||||
@@ -34,6 +36,6 @@ class cliexception extends Exception {
|
||||
* @param Exception|null $previous Another exception as reference.
|
||||
*/
|
||||
public function __construct($message, $code = 1, Exception $previous = null) {
|
||||
parent::__construct('*ERROR* ' . $message, $code, $previous = null);
|
||||
parent::__construct('*ERROR* '.$message, $code, $previous = null);
|
||||
}
|
||||
}
|
||||
@@ -14,22 +14,24 @@
|
||||
// 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\cli;
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use coding_exception;
|
||||
use core\session\manager;
|
||||
use InvalidArgumentException;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Outage CLI base class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class clibase {
|
||||
/**
|
||||
* @var array Options passed as parameters to the CLI.
|
||||
* @var mixed[] Options passed as parameters to the CLI.
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
@@ -40,28 +42,28 @@ abstract class clibase {
|
||||
|
||||
/**
|
||||
* clibase constructor.
|
||||
* @param array|null $options The parameters to use or null to read from the command line.
|
||||
* @throws cliexception
|
||||
* @param mixed[]|null $options The parameters to use or null to read from the command line.
|
||||
* @throws cli_exception
|
||||
*/
|
||||
public function __construct(array $options = null) {
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/clilib.php');
|
||||
require_once($CFG->libdir.'/clilib.php');
|
||||
|
||||
$this->becomeadmin();
|
||||
$this->become_admin_user();
|
||||
|
||||
if (is_null($options)) {
|
||||
// Using Moodle CLI API to read the parameters.
|
||||
list($options, $unrecognized) = cli_get_params($this->generateoptions(), $this->generateshortcuts());
|
||||
list($options, $unrecognized) = cli_get_params($this->generate_options(), $this->generate_shortcuts());
|
||||
if ($unrecognized) {
|
||||
$unrecognized = implode("\n ", $unrecognized);
|
||||
throw new cliexception(get_string('cliunknowoption', 'admin', $unrecognized));
|
||||
throw new cli_exception(get_string('cliunknowoption', 'admin', $unrecognized));
|
||||
}
|
||||
} else {
|
||||
// If not using Moodle CLI API to read parameters, ensure all keys exist.
|
||||
$default = $this->generateoptions();
|
||||
$default = $this->generate_options();
|
||||
foreach ($options as $k => $v) {
|
||||
if (!array_key_exists($k, $default)) {
|
||||
throw new cliexception(get_string('cliunknowoption', 'admin', $k));
|
||||
throw new cli_exception(get_string('cliunknowoption', 'admin', $k));
|
||||
}
|
||||
$default[$k] = $v;
|
||||
}
|
||||
@@ -75,25 +77,26 @@ abstract class clibase {
|
||||
/**
|
||||
* Sets the reference time for creating outages.
|
||||
* @param int $time Timestamp for the reference time.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function set_referencetime($time) {
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new InvalidArgumentException('$time must be a positive int.');
|
||||
throw new coding_exception('$time must be a positive int.', $time);
|
||||
}
|
||||
$this->time = $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates all options (parameters) available for the CLI command.
|
||||
* @return array Options.
|
||||
* @return mixed[] Options.
|
||||
*/
|
||||
public abstract function generateoptions();
|
||||
public abstract function generate_options();
|
||||
|
||||
/**
|
||||
* Generate all short forms for the available options.
|
||||
* @return array Short form options.
|
||||
* @return string[] Short form options.
|
||||
*/
|
||||
public abstract function generateshortcuts();
|
||||
public abstract function generate_shortcuts();
|
||||
|
||||
/**
|
||||
* Executes the CLI script.
|
||||
@@ -103,9 +106,9 @@ abstract class clibase {
|
||||
/**
|
||||
* Change session to admin user.
|
||||
*/
|
||||
protected function becomeadmin() {
|
||||
protected function become_admin_user() {
|
||||
global $DB;
|
||||
$user = $DB->get_record('user', array('id' => 2));
|
||||
$user = $DB->get_record('user', ['id' => 2]);
|
||||
unset($user->description);
|
||||
unset($user->access);
|
||||
unset($user->preference);
|
||||
@@ -117,15 +120,15 @@ abstract class clibase {
|
||||
* Outputs a help message.
|
||||
* @param string $cliname Name of CLI used in the language file.
|
||||
*/
|
||||
protected function showhelp($cliname) {
|
||||
$options = $this->generateoptions();
|
||||
$shorts = array_flip($this->generateshortcuts());
|
||||
protected function show_help($cliname) {
|
||||
$options = $this->generate_options();
|
||||
$shorts = array_flip($this->generate_shortcuts());
|
||||
|
||||
printf("%s\n\n", get_string('cli' . $cliname . 'help', 'auth_outage'));
|
||||
printf("%s\n\n", get_string('cli'.$cliname.'help', 'auth_outage'));
|
||||
foreach (array_keys($options) as $long) {
|
||||
$text = get_string('cli' . $cliname . 'param' . $long, 'auth_outage');
|
||||
$short = isset($shorts[$long]) ? ('-' . $shorts[$long] . ',') : '';
|
||||
$long = '--' . $long;
|
||||
$text = get_string('cli'.$cliname.'param'.$long, 'auth_outage');
|
||||
$short = isset($shorts[$long]) ? ('-'.$shorts[$long].',') : '';
|
||||
$long = '--'.$long;
|
||||
printf(" %-4s %-20s %s\n", $short, $long, $text);
|
||||
}
|
||||
}
|
||||
@@ -14,32 +14,34 @@
|
||||
// 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\cli;
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagedb;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Outage CLI to create outage.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class create extends clibase {
|
||||
/**
|
||||
* @var array Defaults to use if given option is null.
|
||||
* @var mixed[] Defaults to use if given option is null.
|
||||
*/
|
||||
private $defaults;
|
||||
|
||||
/**
|
||||
* Generates all options (parameters) available for the CLI command.
|
||||
* @return array Options.
|
||||
* @return mixed[] Options.
|
||||
*/
|
||||
public function generateoptions() {
|
||||
public function generate_options() {
|
||||
// Do not provide some defaults, if cloning an outage we need to know which parameters were provided.
|
||||
$options = [
|
||||
return [
|
||||
'help' => false,
|
||||
'clone' => null,
|
||||
'warn' => null,
|
||||
@@ -50,14 +52,13 @@ class create extends clibase {
|
||||
'onlyid' => false,
|
||||
'block' => false,
|
||||
];
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate all short forms for the available options.
|
||||
* @return array Short form options.
|
||||
* @return string[] Short form options.
|
||||
*/
|
||||
public function generateshortcuts() {
|
||||
public function generate_shortcuts() {
|
||||
return [
|
||||
'b' => 'block',
|
||||
'c' => 'clone',
|
||||
@@ -72,7 +73,7 @@ class create extends clibase {
|
||||
|
||||
/**
|
||||
* Sets the default values for options.
|
||||
* @param array $defaults Defaults.
|
||||
* @param mixed[] $defaults Defaults.
|
||||
*/
|
||||
public function set_defaults(array $defaults) {
|
||||
$this->defaults = $defaults;
|
||||
@@ -84,23 +85,23 @@ class create extends clibase {
|
||||
public function execute() {
|
||||
// Help always overrides any other parameter.
|
||||
if ($this->options['help']) {
|
||||
$this->showhelp('create');
|
||||
$this->show_help('create');
|
||||
return;
|
||||
}
|
||||
|
||||
// If not help mode, 'start' is required and cannot use default.
|
||||
if (is_null($this->options['start'])) {
|
||||
throw new cliexception(get_string('clierrormissingparamaters', 'auth_outage'));
|
||||
throw new cli_exception(get_string('clierrormissingparamaters', 'auth_outage'));
|
||||
}
|
||||
|
||||
// If cloning, set defaults to outage being cloned.
|
||||
if (!is_null($this->options['clone'])) {
|
||||
$this->clonedefaults();
|
||||
$this->clone_defaults();
|
||||
}
|
||||
|
||||
// Merge provided parameters with defaults then create outage.
|
||||
$options = $this->mergeoptions();
|
||||
$id = $this->createoutage($options);
|
||||
$options = $this->merge_options();
|
||||
$id = $this->create_outage($options);
|
||||
|
||||
if ($options['block']) {
|
||||
$block = new waitforit(['outageid' => $id]);
|
||||
@@ -110,10 +111,10 @@ class create extends clibase {
|
||||
|
||||
/**
|
||||
* Merges provided options with defaults, checking and converting types as needed.
|
||||
* @return array Parameters to use.
|
||||
* @throws cliexception
|
||||
* @return mixed[] Parameters to use.
|
||||
* @throws cli_exception
|
||||
*/
|
||||
private function mergeoptions() {
|
||||
private function merge_options() {
|
||||
$options = $this->options;
|
||||
// Merge with defaults.
|
||||
if (!is_null($this->defaults)) {
|
||||
@@ -124,17 +125,17 @@ class create extends clibase {
|
||||
}
|
||||
}
|
||||
|
||||
return $this->mergeoptions_checkparameters($options);
|
||||
return $this->merge_options_check_parameters($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an outages based on the provided options.
|
||||
* @param array $options Options used to create the outage.
|
||||
* @param mixed[] $options Options used to create the outage.
|
||||
* @return int Id of the new outage.
|
||||
*/
|
||||
private function createoutage(array $options) {
|
||||
private function create_outage(array $options) {
|
||||
// We need to become an admin to avoid permission problems.
|
||||
$this->becomeadmin();
|
||||
$this->become_admin_user();
|
||||
|
||||
// Create the outage.
|
||||
$start = $this->time + $options['start'];
|
||||
@@ -157,10 +158,10 @@ class create extends clibase {
|
||||
return $id;
|
||||
}
|
||||
|
||||
private function clonedefaults() {
|
||||
private function clone_defaults() {
|
||||
$id = $this->options['clone'];
|
||||
if (!is_number($id) || ($id <= 0)) {
|
||||
throw new cliexception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'clone']));
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'clone']));
|
||||
}
|
||||
|
||||
$outage = outagedb::get_by_id((int)$id);
|
||||
@@ -174,30 +175,30 @@ class create extends clibase {
|
||||
|
||||
/**
|
||||
* Check parameters converting their type as needed.
|
||||
* @param array $options Input options.
|
||||
* @return array Output options.
|
||||
* @throws cliexception
|
||||
* @param mixed $options Input options.
|
||||
* @return mixed Output options.
|
||||
* @throws cli_exception
|
||||
*/
|
||||
private function mergeoptions_checkparameters(array $options) {
|
||||
private function merge_options_check_parameters(array $options) {
|
||||
// Check parameters that must be a non-negative int while converting their type to int.
|
||||
foreach (['start', 'warn', 'duration'] as $param) {
|
||||
if (!is_number($options[$param])) {
|
||||
throw new cliexception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
}
|
||||
$options[$param] = (int)$options[$param];
|
||||
if ($options[$param] < 0) {
|
||||
throw new cliexception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
}
|
||||
}
|
||||
|
||||
// Check parameters that must be a non empty string.
|
||||
foreach (['title', 'description'] as $param) {
|
||||
if (!is_string($options[$param])) {
|
||||
throw new cliexception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
}
|
||||
$options[$param] = trim($options[$param]);
|
||||
if (strlen($options[$param]) == 0) {
|
||||
throw new cliexception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,25 +14,27 @@
|
||||
// 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\cli;
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagedb;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Outage CLI to finish an outage.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class finish extends clibase {
|
||||
/**
|
||||
* Generates all options (parameters) available for the CLI command.
|
||||
* @return array Options.
|
||||
* @return mixed[] Options.
|
||||
*/
|
||||
public function generateoptions() {
|
||||
public function generate_options() {
|
||||
// Do not provide some defaults, if cloning an outage we need to know which parameters were provided.
|
||||
$options = [
|
||||
'help' => false,
|
||||
@@ -44,9 +46,9 @@ class finish extends clibase {
|
||||
|
||||
/**
|
||||
* Generate all short forms for the available options.
|
||||
* @return array Short form options.
|
||||
* @return string[] Short form options.
|
||||
*/
|
||||
public function generateshortcuts() {
|
||||
public function generate_shortcuts() {
|
||||
return [
|
||||
'h' => 'help',
|
||||
'id' => 'outageid',
|
||||
@@ -60,7 +62,7 @@ class finish extends clibase {
|
||||
public function execute() {
|
||||
// Help always overrides any other parameter.
|
||||
if ($this->options['help']) {
|
||||
$this->showhelp('finish');
|
||||
$this->show_help('finish');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -68,12 +70,12 @@ class finish extends clibase {
|
||||
$byid = !is_null($this->options['outageid']);
|
||||
$byactive = $this->options['active'];
|
||||
if ($byid == $byactive) {
|
||||
throw new cliexception(get_string('cliwaitforiterroridxoractive', 'auth_outage'));
|
||||
throw new cli_exception(get_string('cliwaitforiterroridxoractive', 'auth_outage'));
|
||||
}
|
||||
|
||||
$outage = $this->get_outage();
|
||||
if (!$outage->is_ongoing()) {
|
||||
throw new cliexception(get_string('clifinishnotongoing', 'auth_outage'));
|
||||
throw new cli_exception(get_string('clifinishnotongoing', 'auth_outage'));
|
||||
}
|
||||
|
||||
outagedb::finish($outage->id, $this->time);
|
||||
@@ -82,7 +84,7 @@ class finish extends clibase {
|
||||
/**
|
||||
* Gets the outage to finish.
|
||||
* @return outage|null The outage to wait for.
|
||||
* @throws cliexception
|
||||
* @throws cli_exception
|
||||
*/
|
||||
private function get_outage() {
|
||||
if ($this->options['active']) {
|
||||
@@ -90,13 +92,13 @@ class finish extends clibase {
|
||||
} else {
|
||||
$id = $this->options['outageid'];
|
||||
if (!is_number($id) || ($id <= 0)) {
|
||||
throw new cliexception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid']));
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid']));
|
||||
}
|
||||
$outage = outagedb::get_by_id((int)$id);
|
||||
}
|
||||
|
||||
if (is_null($outage)) {
|
||||
throw new cliexception(get_string('clierroroutagenotfound', 'auth_outage'));
|
||||
throw new cli_exception(get_string('clierroroutagenotfound', 'auth_outage'));
|
||||
}
|
||||
|
||||
return $outage;
|
||||
@@ -14,17 +14,19 @@
|
||||
// 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\cli;
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagedb;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Outage CLI to wait for an outage to start.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class waitforit extends clibase {
|
||||
@@ -40,9 +42,9 @@ class waitforit extends clibase {
|
||||
|
||||
/**
|
||||
* Generates all options (parameters) available for the CLI command.
|
||||
* @return array Options.
|
||||
* @return mixed[] Options.
|
||||
*/
|
||||
public function generateoptions() {
|
||||
public function generate_options() {
|
||||
// Do not provide some defaults, if cloning an outage we need to know which parameters were provided.
|
||||
$options = [
|
||||
'help' => false,
|
||||
@@ -56,9 +58,9 @@ class waitforit extends clibase {
|
||||
|
||||
/**
|
||||
* Generate all short forms for the available options.
|
||||
* @return array Short form options.
|
||||
* @return string[] Short form options.
|
||||
*/
|
||||
public function generateshortcuts() {
|
||||
public function generate_shortcuts() {
|
||||
return [
|
||||
'h' => 'help',
|
||||
'id' => 'outageid',
|
||||
@@ -82,7 +84,7 @@ class waitforit extends clibase {
|
||||
public function execute() {
|
||||
// Help always overrides any other parameter.
|
||||
if ($this->options['help']) {
|
||||
$this->showhelp('waitforit');
|
||||
$this->show_help('waitforit');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -90,20 +92,20 @@ class waitforit extends clibase {
|
||||
$byid = !is_null($this->options['outageid']);
|
||||
$byactive = $this->options['active'];
|
||||
if ($byid == $byactive) {
|
||||
throw new cliexception(get_string('cliwaitforiterroridxoractive', 'auth_outage'));
|
||||
throw new cli_exception(get_string('cliwaitforiterroridxoractive', 'auth_outage'));
|
||||
}
|
||||
|
||||
$this->verbose('Verbose mode activated.');
|
||||
|
||||
$outage = $this->get_outage();
|
||||
|
||||
while ($sleep = $this->waitforoutagestart($outage)) {
|
||||
while ($sleep = $this->wait_for_outage_to_start($outage)) {
|
||||
if (is_null($this->sleepcallback)) {
|
||||
$this->verbose('Sleeping for ' . $sleep . ' second(s).');
|
||||
$this->verbose('Sleeping for '.$sleep.' second(s).');
|
||||
sleep($sleep);
|
||||
$this->time = time();
|
||||
} else {
|
||||
$this->verbose('Calling callback to sleep ' . $sleep . ' second(s).');
|
||||
$this->verbose('Calling callback to sleep '.$sleep.' second(s).');
|
||||
$callback = $this->sleepcallback;
|
||||
$this->time = $callback($sleep);
|
||||
}
|
||||
@@ -126,7 +128,7 @@ class waitforit extends clibase {
|
||||
/**
|
||||
* Gets the outage to wait for.
|
||||
* @return outage|null The outage to wait for.
|
||||
* @throws cliexception
|
||||
* @throws cli_exception
|
||||
*/
|
||||
private function get_outage() {
|
||||
if ($this->options['active']) {
|
||||
@@ -135,29 +137,35 @@ class waitforit extends clibase {
|
||||
} else {
|
||||
$id = $this->options['outageid'];
|
||||
if (!is_number($id) || ($id <= 0)) {
|
||||
throw new cliexception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid']));
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid']));
|
||||
}
|
||||
$this->verbose('Querying database for outage #' . $id . '...');
|
||||
$this->verbose('Querying database for outage #'.$id.'...');
|
||||
$outage = outagedb::get_by_id((int)$id);
|
||||
}
|
||||
|
||||
if (is_null($outage)) {
|
||||
throw new cliexception(get_string('clierroroutagenotfound', 'auth_outage'));
|
||||
throw new cli_exception(get_string('clierroroutagenotfound', 'auth_outage'));
|
||||
}
|
||||
|
||||
$this->verbose('Found outage #' . $outage->id . ': ' . $outage->get_title());
|
||||
$this->verbose('Found outage #'.$outage->id.': '.$outage->get_title());
|
||||
return $outage;
|
||||
}
|
||||
|
||||
private function waitforoutagestart(outage $outage) {
|
||||
/**
|
||||
* Calculate how many seconds to wait for the outage to start.
|
||||
* @param outage $outage Outage to consider.
|
||||
* @return int Seconds until it stars.
|
||||
* @throws cli_exception
|
||||
*/
|
||||
private function wait_for_outage_to_start(outage $outage) {
|
||||
$this->verbose('Checking outage status...');
|
||||
// Outage should not change while waiting to start.
|
||||
if (outagedb::get_by_id($outage->id) != $outage) {
|
||||
throw new cliexception(get_string('clierroroutagechanged', 'auth_outage'));
|
||||
throw new cli_exception(get_string('clierroroutagechanged', 'auth_outage'));
|
||||
}
|
||||
// Outage cannot have already ended.
|
||||
if ($outage->has_ended($this->time)) {
|
||||
throw new cliexception(get_string('clierroroutageended', 'auth_outage'));
|
||||
throw new cli_exception(get_string('clierroroutageended', 'auth_outage'));
|
||||
}
|
||||
// If outage has started, do not wait.
|
||||
if ($outage->is_ongoing($this->time)) {
|
||||
@@ -14,13 +14,15 @@
|
||||
// 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;
|
||||
namespace auth_outage\local\controllers;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagedb;
|
||||
use auth_outage\local\outagelib;
|
||||
use coding_exception;
|
||||
use context_system;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use file_exception;
|
||||
use invalid_state_exception;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
@@ -33,7 +35,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class infopage_controller {
|
||||
class infopage {
|
||||
/**
|
||||
* @var outage|null The outage to display or null if none found.
|
||||
*/
|
||||
@@ -46,7 +48,7 @@ class infopage_controller {
|
||||
|
||||
/**
|
||||
* infopage_controller constructor.
|
||||
* @param array|null $params Parameters to use or null to get from Moodle API (request).
|
||||
* @param mixed[]|null $params Parameters to use or null to get from Moodle API (request).
|
||||
*/
|
||||
public function __construct(array $params = null) {
|
||||
if (is_null($params)) {
|
||||
@@ -74,7 +76,7 @@ class infopage_controller {
|
||||
*/
|
||||
public static function find_outageid_from_infopage($html) {
|
||||
if (!is_string($html)) {
|
||||
throw new InvalidArgumentException('$html must be a string.');
|
||||
throw new coding_exception('$html must be a string.', $html);
|
||||
}
|
||||
|
||||
$output = [];
|
||||
@@ -88,24 +90,23 @@ class infopage_controller {
|
||||
* 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 Exception
|
||||
*/
|
||||
public static function save_static_page(outage $outage, $file) {
|
||||
if (!is_string($file)) {
|
||||
throw new InvalidArgumentException('$file is not a string.');
|
||||
throw new coding_exception('$file is not a string.', $file);
|
||||
}
|
||||
|
||||
$info = new infopage_controller(['outage' => $outage, 'static' => true]);
|
||||
$info = new infopage(['outage' => $outage, 'static' => true]);
|
||||
$html = $info->get_output();
|
||||
|
||||
// Sanity check before writing/overwriting old file.
|
||||
if (!is_string($html) || ($html == '') || (html_to_text($html) == '')) {
|
||||
throw new Exception('Sanity check failed. Invalid contents on $html.');
|
||||
throw new invalid_state_exception('Sanity check failed. Invalid contents on $html.');
|
||||
}
|
||||
|
||||
$dir = dirname($file);
|
||||
if (!file_exists($dir) || !is_dir($dir)) {
|
||||
throw new Exception('Directory must exists: ' . $dir);
|
||||
throw new file_exception('Directory must exists: '.$dir);
|
||||
}
|
||||
file_put_contents($file, $html);
|
||||
}
|
||||
@@ -113,14 +114,13 @@ class infopage_controller {
|
||||
/**
|
||||
* Updates the static info page by (re)creating or deleting it as needed.
|
||||
* @param null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function update_static_page($file = null) {
|
||||
if (is_null($file)) {
|
||||
$file = self::get_defaulttemplatefile();
|
||||
}
|
||||
if (!is_string($file)) {
|
||||
throw new InvalidArgumentException('$file is not a string.');
|
||||
throw new coding_exception('$file is not a string.', $file);
|
||||
}
|
||||
|
||||
$outage = outagedb::get_next_starting();
|
||||
@@ -129,7 +129,7 @@ class infopage_controller {
|
||||
if (is_file($file) && is_writable($file)) {
|
||||
unlink($file);
|
||||
} else {
|
||||
throw new Exception('Cannot remove: ' . $file);
|
||||
throw new file_exception('Cannot remove: '.$file);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -142,7 +142,7 @@ class infopage_controller {
|
||||
*/
|
||||
public static function get_defaulttemplatefile() {
|
||||
global $CFG;
|
||||
return $CFG->dataroot . '/climaintenance.template.html';
|
||||
return $CFG->dataroot.'/climaintenance.template.html';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,6 +152,7 @@ class infopage_controller {
|
||||
public function get_output() {
|
||||
ob_start();
|
||||
try {
|
||||
// TODO what if redirection occurs here?
|
||||
$this->output();
|
||||
return ob_get_contents();
|
||||
} finally {
|
||||
@@ -184,7 +185,7 @@ class infopage_controller {
|
||||
|
||||
$PAGE->set_context(context_system::instance());
|
||||
if ($this->static) {
|
||||
require($CFG->dirroot . '/auth/outage/views/info/static.php');
|
||||
require($CFG->dirroot.'/auth/outage/views/info/static.php');
|
||||
} else {
|
||||
$PAGE->set_title($this->outage->get_title());
|
||||
$PAGE->set_heading($this->outage->get_title());
|
||||
@@ -194,22 +195,22 @@ class infopage_controller {
|
||||
outagelib::inject();
|
||||
|
||||
echo $OUTPUT->header();
|
||||
require($CFG->dirroot . '/auth/outage/views/info/content.php');
|
||||
require($CFG->dirroot.'/auth/outage/views/info/content.php');
|
||||
echo $OUTPUT->footer();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the fields according to the given parameters.
|
||||
* @param array $params
|
||||
* @param mixed[] $params
|
||||
*/
|
||||
private function set_parameters(array $params) {
|
||||
if (!is_null($params['outage']) && !($params['outage'] instanceof outage)) {
|
||||
throw new InvalidArgumentException('Provided outage is not a valid outage object.');
|
||||
throw new coding_exception('Provided outage is not a valid outage object.', $params['outage']);
|
||||
}
|
||||
|
||||
if (!is_null($params['id']) && !is_null($params['outage']) && ($params['id'] !== $params['outage']->id)) {
|
||||
throw new InvalidArgumentException('Provided id and outage->id do not match.');
|
||||
throw new coding_exception('Provided id and outage->id do not match.', $params);
|
||||
}
|
||||
|
||||
if (is_null($params['id']) && is_null($params['outage'])) {
|
||||
@@ -14,16 +14,19 @@
|
||||
// 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\models;
|
||||
namespace auth_outage\local;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use coding_exception;
|
||||
use stdClass;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Outage class with all information about one specific outage.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outage {
|
||||
@@ -104,8 +107,8 @@ class outage {
|
||||
|
||||
/**
|
||||
* outage constructor.
|
||||
* @param object|array|null The data for the outage.
|
||||
* @throws InvalidArgumentException
|
||||
* @param stdClass|mixed[]|null The data for the outage.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function __construct($data = null) {
|
||||
if (is_null($data)) {
|
||||
@@ -115,7 +118,7 @@ class outage {
|
||||
$data = (array)$data;
|
||||
}
|
||||
if (!is_array($data)) {
|
||||
throw new InvalidArgumentException('$data is not an object, an array or null.');
|
||||
throw new coding_exception('$data is not an object, an array or null.', $data);
|
||||
}
|
||||
|
||||
// Load data from array.
|
||||
@@ -136,13 +139,14 @@ class outage {
|
||||
* Gets at which stage is this outage.
|
||||
* @param int|null $time Null to check the current stage or a timestamp to check for another time.
|
||||
* @return int Stage, compare with STAGE_* constants.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function get_stage($time = null) {
|
||||
if ($time === null) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new InvalidArgumentException('$time must be an positive int.');
|
||||
throw new coding_exception('$time must be an positive int.', $time);
|
||||
}
|
||||
|
||||
if (!is_null($this->finished) && ($time >= $this->finished)) {
|
||||
@@ -218,27 +222,6 @@ class outage {
|
||||
return $this->finished - $this->starttime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the input string with all placeholders replaced.
|
||||
* @param $str string Input string.
|
||||
* @return string Output string.
|
||||
*/
|
||||
private function replace_placeholders($str) {
|
||||
return str_replace(
|
||||
[
|
||||
'{{start}}',
|
||||
'{{stop}}',
|
||||
'{{duration}}',
|
||||
],
|
||||
[
|
||||
userdate($this->starttime, get_string('datetimeformat', 'auth_outage')),
|
||||
userdate($this->stoptime, get_string('datetimeformat', 'auth_outage')),
|
||||
format_time($this->get_duration_planned()),
|
||||
],
|
||||
$str
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the planned duration of the outage (start to planned stop, warning not included).
|
||||
* @return int Duration in seconds.
|
||||
@@ -262,4 +245,25 @@ class outage {
|
||||
public function get_warning_duration() {
|
||||
return $this->starttime - $this->warntime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the input string with all placeholders replaced.
|
||||
* @param $str string Input string.
|
||||
* @return string Output string.
|
||||
*/
|
||||
private function replace_placeholders($str) {
|
||||
return str_replace(
|
||||
[
|
||||
'{{start}}',
|
||||
'{{stop}}',
|
||||
'{{duration}}',
|
||||
],
|
||||
[
|
||||
userdate($this->starttime, get_string('datetimeformat', 'auth_outage')),
|
||||
userdate($this->stoptime, get_string('datetimeformat', 'auth_outage')),
|
||||
format_time($this->get_duration_planned()),
|
||||
],
|
||||
$str
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -14,18 +14,18 @@
|
||||
// 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;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->dirroot . '/calendar/lib.php');
|
||||
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\models\outage;
|
||||
use auth_outage\local\controllers\infopage;
|
||||
use calendar_event;
|
||||
use InvalidArgumentException;
|
||||
use coding_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->dirroot.'/calendar/lib.php');
|
||||
|
||||
/**
|
||||
* The DB Context to manipulate Outages.
|
||||
@@ -33,7 +33,7 @@ use InvalidArgumentException;
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outagedb {
|
||||
@@ -63,15 +63,13 @@ class outagedb {
|
||||
/**
|
||||
* @param $id int Outage id to get.
|
||||
* @return outage|null Returns the outage or null if not found.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function get_by_id($id) {
|
||||
global $DB;
|
||||
|
||||
if (!is_int($id)) {
|
||||
throw new InvalidArgumentException('$id must be an int.');
|
||||
}
|
||||
if ($id <= 0) {
|
||||
throw new InvalidArgumentException('$id must be positive.');
|
||||
if (!is_int($id) || ($id <= 0)) {
|
||||
throw new coding_exception('$id must be an positive int.', $id);
|
||||
}
|
||||
|
||||
$outage = $DB->get_record('auth_outage', ['id' => $id]);
|
||||
@@ -121,7 +119,7 @@ class outagedb {
|
||||
}
|
||||
|
||||
// Trigger static page update.
|
||||
infopage_controller::update_static_page();
|
||||
infopage::update_static_page();
|
||||
|
||||
// All done, return the id.
|
||||
return $outage->id;
|
||||
@@ -131,16 +129,13 @@ class outagedb {
|
||||
* Deletes an outage from the database.
|
||||
*
|
||||
* @param int $id Outage ID to delete
|
||||
* @throws InvalidArgumentException If ID is not valid.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function delete($id) {
|
||||
global $DB;
|
||||
|
||||
if (!is_int($id)) {
|
||||
throw new InvalidArgumentException('$id must be an int.');
|
||||
}
|
||||
if ($id <= 0) {
|
||||
throw new InvalidArgumentException('$id must be positive.');
|
||||
if (!is_int($id) || ($id <= 0)) {
|
||||
throw new coding_exception('$id must be an int.', $id);
|
||||
}
|
||||
|
||||
// Log it.
|
||||
@@ -154,7 +149,7 @@ class outagedb {
|
||||
self::calendar_delete($id);
|
||||
|
||||
// Trigger static page update.
|
||||
infopage_controller::update_static_page();
|
||||
infopage::update_static_page();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,6 +159,7 @@ class outagedb {
|
||||
* - Outages that stop later are more important.
|
||||
* @param int|null $time Timestamp considered to check for outages, null for current date/time.
|
||||
* @return outage|null The outage or null if no active outages were found.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function get_active($time = null) {
|
||||
global $DB;
|
||||
@@ -171,8 +167,8 @@ class outagedb {
|
||||
if ($time === null) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time)) {
|
||||
throw new InvalidArgumentException('$time must be null or an int.');
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new coding_exception('$time must be null or a positive int.', $time);
|
||||
}
|
||||
|
||||
$select = ':datetime2 <= stoptime AND (finished IS NULL OR :datetime3 <= finished)'; // End condition.
|
||||
@@ -195,7 +191,8 @@ class outagedb {
|
||||
/**
|
||||
* Gets all outages that have not ended yet.
|
||||
* @param int|null $time Timestamp considered to check for outages, null for current date/time.
|
||||
* @return array An array of outages or an empty array if no unded outages were found.
|
||||
* @return outage[] An array of outages or an empty array if no unded outages were found.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function get_all_unended($time = null) {
|
||||
global $DB;
|
||||
@@ -203,8 +200,8 @@ class outagedb {
|
||||
if ($time === null) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time)) {
|
||||
throw new InvalidArgumentException('$time must be null or an int.');
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new coding_exception('$time must be null or a positive int.');
|
||||
}
|
||||
|
||||
$outages = [];
|
||||
@@ -226,7 +223,8 @@ class outagedb {
|
||||
/**
|
||||
* Gets all ended outages.
|
||||
* @param int|null $time Timestamp considered to check for outages, null for current date/time.
|
||||
* @return array An array of outages or an empty array if no ended outages found.
|
||||
* @return outage[] An array of outages or an empty array if no ended outages found.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function get_all_ended($time = null) {
|
||||
global $DB;
|
||||
@@ -234,8 +232,8 @@ class outagedb {
|
||||
if ($time === null) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time)) {
|
||||
throw new InvalidArgumentException('$time must be null or an int.');
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new coding_exception('$time must be null or a positive int.', $time);
|
||||
}
|
||||
|
||||
$outages = [];
|
||||
@@ -258,23 +256,24 @@ class outagedb {
|
||||
* Marks an outage as finished.
|
||||
* @param int $id Outage id.
|
||||
* @param int|null $time Timestamp to consider as finished date or null for current time.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function finish($id, $time = null) {
|
||||
if (is_null($time)) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time) && ($time <= 0)) {
|
||||
throw new InvalidArgumentException('$time must be an int or null.');
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new coding_exception('$time must be null or a positive int.', $time);
|
||||
}
|
||||
|
||||
$outage = self::get_by_id($id);
|
||||
if (is_null($outage)) {
|
||||
debugging('Cannot finish outage #' . $id . ': outage not found.');
|
||||
debugging('Cannot finish outage #'.$id.': outage not found.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$outage->is_ongoing($time)) {
|
||||
debugging('Cannot finish outage #' . $id . ': outage not ongoing.');
|
||||
debugging('Cannot finish outage #'.$id.': outage not ongoing.');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -286,6 +285,7 @@ class outagedb {
|
||||
* Gets the next outage which has not started yet.
|
||||
* @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_starting($time = null) {
|
||||
global $DB;
|
||||
@@ -294,7 +294,7 @@ class outagedb {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new InvalidArgumentException('$time must be null or an positive int.');
|
||||
throw new coding_exception('$time must be null or a positive int.', $time);
|
||||
}
|
||||
|
||||
$select = ':datetime <= starttime'; // End condition.
|
||||
@@ -329,7 +329,7 @@ class outagedb {
|
||||
$event = self::calendar_load($outage->id);
|
||||
|
||||
if (is_null($event)) {
|
||||
debugging('Cannot update calendar entry for outage #' . $outage->id . ', event not found. Creating it...');
|
||||
debugging('Cannot update calendar entry for outage #'.$outage->id.', event not found. Creating it...');
|
||||
self::calendar_create($outage);
|
||||
} else {
|
||||
$event->update(self::calendar_data($outage));
|
||||
@@ -345,7 +345,7 @@ class outagedb {
|
||||
|
||||
// If not found (was not created before) ignore it.
|
||||
if (is_null($event)) {
|
||||
debugging('Cannot delete calendar entry for outage #' . $outageid . ', event not found. Ignoring it...');
|
||||
debugging('Cannot delete calendar entry for outage #'.$outageid.', event not found. Ignoring it...');
|
||||
} else {
|
||||
$event->delete();
|
||||
}
|
||||
@@ -354,7 +354,7 @@ class outagedb {
|
||||
/**
|
||||
* Generates an array with the calendar event data based on an outage object.
|
||||
* @param outage $outage Outage to use as reference for the calendar event.
|
||||
* @return array Calendar event data.
|
||||
* @return mixed[] Calendar event data.
|
||||
*/
|
||||
private static function calendar_data(outage $outage) {
|
||||
return [
|
||||
@@ -14,11 +14,12 @@
|
||||
// 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;
|
||||
namespace auth_outage\local;
|
||||
|
||||
use auth_outage_renderer;
|
||||
use Exception;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@@ -27,7 +28,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outagelib {
|
||||
@@ -37,7 +38,7 @@ class outagelib {
|
||||
* Initializes admin pages for outage.
|
||||
* @return auth_outage_renderer The outage renderer for the page.
|
||||
*/
|
||||
public static function pagesetup() {
|
||||
public static function page_setup() {
|
||||
global $PAGE;
|
||||
admin_externalpage_setup('auth_outage_manage');
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
|
||||
@@ -86,10 +87,10 @@ class outagelib {
|
||||
}
|
||||
|
||||
// There is a previewing or active outage.
|
||||
$CFG->additionalhtmltopofbody = self::get_renderer()->renderoutagebar($active, $time)
|
||||
. $CFG->additionalhtmltopofbody;
|
||||
$CFG->additionalhtmltopofbody = self::get_renderer()->renderoutagebar($active, $time).
|
||||
$CFG->additionalhtmltopofbody;
|
||||
} catch (Exception $e) {
|
||||
debugging('Exception occured while injecting our code: ' . $e->getMessage());
|
||||
debugging('Exception occured while injecting our code: '.$e->getMessage());
|
||||
debugging($e->getTraceAsString(), DEBUG_DEVELOPER);
|
||||
}
|
||||
}
|
||||
@@ -97,7 +98,7 @@ class outagelib {
|
||||
/**
|
||||
* Creates a configuration object ensuring all parameters are set,
|
||||
* loading defaults even if the plugin is not configured.
|
||||
* @return object Configuration object with all parameters set.
|
||||
* @return stdClass Configuration object with all parameters set.
|
||||
*/
|
||||
public static function get_config() {
|
||||
return (object)array_merge(self::get_config_defaults(), (array)get_config('auth_outage'));
|
||||
@@ -105,7 +106,7 @@ class outagelib {
|
||||
|
||||
/**
|
||||
* Creates the default configurations. If the plugin is not configured we should use those defaults.
|
||||
* @return array Default configuration.
|
||||
* @return mixed[] Default configuration.
|
||||
*/
|
||||
public static function get_config_defaults() {
|
||||
global $CFG;
|
||||
@@ -115,7 +116,7 @@ class outagelib {
|
||||
'warning_duration' => 60,
|
||||
'warning_title' => get_string('defaultwarningtitlevalue', 'auth_outage'),
|
||||
'warning_description' => get_string('defaultwarningdescriptionvalue', 'auth_outage'),
|
||||
'css' => file_get_contents($CFG->dirroot . '/auth/outage/views/warningbar.css'),
|
||||
'css' => file_get_contents($CFG->dirroot.'/auth/outage/views/warningbar.css'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -14,24 +14,26 @@
|
||||
// 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\tables\manage;
|
||||
namespace auth_outage\local\output\manage;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\local\outage;
|
||||
use flexible_table;
|
||||
use html_writer;
|
||||
use moodle_url;
|
||||
|
||||
require_once($CFG->libdir . '/tablelib.php');
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir.'/tablelib.php');
|
||||
|
||||
/**
|
||||
* Manage outages table base.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <danielroperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class managebase extends flexible_table {
|
||||
class base_table extends flexible_table {
|
||||
private static $autoid = 0;
|
||||
|
||||
/**
|
||||
@@ -42,7 +44,7 @@ class managebase extends flexible_table {
|
||||
global $PAGE;
|
||||
|
||||
$id = (is_null($id) ? self::$autoid++ : $id);
|
||||
parent::__construct('auth_outage_manage_' . $id);
|
||||
parent::__construct('auth_outage_manage_'.$id);
|
||||
|
||||
$this->define_baseurl($PAGE->url);
|
||||
$this->set_attribute('class', 'generaltable admintable');
|
||||
@@ -80,7 +82,7 @@ class managebase extends flexible_table {
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/edit'),
|
||||
'alt' => get_string('edit'),
|
||||
'class' => 'iconsmall'
|
||||
'class' => 'iconsmall',
|
||||
]),
|
||||
['title' => get_string('edit')]
|
||||
);
|
||||
@@ -105,7 +107,7 @@ class managebase extends flexible_table {
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/check'),
|
||||
'alt' => get_string('finish', 'auth_outage'),
|
||||
'class' => 'iconsmall'
|
||||
'class' => 'iconsmall',
|
||||
]),
|
||||
['title' => get_string('finish', 'auth_outage')]
|
||||
);
|
||||
@@ -118,12 +120,12 @@ class managebase extends flexible_table {
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/delete'),
|
||||
'alt' => get_string('delete'),
|
||||
'class' => 'iconsmall'
|
||||
'class' => 'iconsmall',
|
||||
]),
|
||||
['title' => get_string('delete')]
|
||||
);
|
||||
}
|
||||
|
||||
return '<nobr>' . $buttons . '</nobr>';
|
||||
return '<nobr>'.$buttons.'</nobr>';
|
||||
}
|
||||
}
|
||||
@@ -14,23 +14,23 @@
|
||||
// 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\tables\manage;
|
||||
namespace auth_outage\local\output\manage;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use html_writer;
|
||||
use moodle_url;
|
||||
use auth_outage\local\outage;
|
||||
|
||||
require_once($CFG->libdir . '/tablelib.php');
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir.'/tablelib.php');
|
||||
|
||||
/**
|
||||
* Manage outages table.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <danielroperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class history extends managebase {
|
||||
class history_table extends base_table {
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
@@ -58,22 +58,15 @@ class history extends managebase {
|
||||
*/
|
||||
public function set_data(array $outages) {
|
||||
foreach ($outages as $outage) {
|
||||
$title = html_writer::link(
|
||||
new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]),
|
||||
$outage->get_title(),
|
||||
['title' => get_string('edit')]
|
||||
);
|
||||
|
||||
$finished = $outage->get_duration_actual();
|
||||
$finished = is_null($finished) ? '-' : format_time($finished);
|
||||
|
||||
$this->add_data([
|
||||
format_time($outage->get_warning_duration()),
|
||||
userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')),
|
||||
format_time($outage->get_duration_planned()),
|
||||
$finished,
|
||||
$title,
|
||||
$this->set_data_buttons($outage, false),
|
||||
$outage->get_title(),
|
||||
$this->set_data_buttons($outage, true),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -14,21 +14,25 @@
|
||||
// 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\tables\manage;
|
||||
namespace auth_outage\local\output\manage;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\local\outage;
|
||||
use html_writer;
|
||||
use moodle_url;
|
||||
|
||||
require_once($CFG->libdir . '/tablelib.php');
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir.'/tablelib.php');
|
||||
|
||||
/**
|
||||
* Manage outages table.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <danielroperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class planned extends managebase {
|
||||
class planned_table extends base_table {
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
@@ -55,12 +59,18 @@ class planned extends managebase {
|
||||
*/
|
||||
public function set_data(array $outages) {
|
||||
foreach ($outages as $outage) {
|
||||
$title = html_writer::link(
|
||||
new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]),
|
||||
$outage->get_title(),
|
||||
['title' => get_string('edit')]
|
||||
);
|
||||
|
||||
$this->add_data([
|
||||
format_time($outage->get_warning_duration()),
|
||||
userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')),
|
||||
format_time($outage->get_duration_planned()),
|
||||
$outage->get_title(),
|
||||
$this->set_data_buttons($outage, true),
|
||||
$title,
|
||||
$this->set_data_buttons($outage, false),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user