mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Several changes to easily integrate with moodle db api. Create outage page working.
This commit is contained in:
@@ -30,133 +30,62 @@ class outage
|
||||
/**
|
||||
* @var int Outage ID (auto generated by the DB).
|
||||
*/
|
||||
private $id;
|
||||
public $id = null;
|
||||
|
||||
/**
|
||||
* @var int Start Time timestamp.
|
||||
*/
|
||||
private $starttime;
|
||||
public $starttime = null;
|
||||
|
||||
/**
|
||||
* @var int Stop Time timestamp.
|
||||
*/
|
||||
private $stoptime;
|
||||
public $stoptime = null;
|
||||
|
||||
/**
|
||||
* @var int Amount of minutes before outage starts to show the warning message.
|
||||
*/
|
||||
private $warningminutes;
|
||||
public $warningduration = null;
|
||||
|
||||
/**
|
||||
* @var string Short description of the outage (no HTML).
|
||||
*/
|
||||
private $title;
|
||||
public $title = null;
|
||||
|
||||
/**
|
||||
* @var string Description of the outage (some HTML allowed).
|
||||
*/
|
||||
private $description;
|
||||
public $description = null;
|
||||
|
||||
/**
|
||||
* @var int Moodle User Id that created this outage.
|
||||
*/
|
||||
private $createdby;
|
||||
public $createdby = null;
|
||||
|
||||
/**
|
||||
* @var int Moodle User Id that last modified this outage.
|
||||
*/
|
||||
private $modifiedby;
|
||||
public $modifiedby = null;
|
||||
|
||||
/**
|
||||
* @var int Timestamp of when this outage was last modified.
|
||||
*/
|
||||
private $lastmodified;
|
||||
public $lastmodified = null;
|
||||
|
||||
/**
|
||||
* outage constructor.
|
||||
* @param $id
|
||||
* @param $starttime
|
||||
* @param $stoptime
|
||||
* @param $warningminutes
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @param $createdby
|
||||
* @param $modifiedby
|
||||
* @param $lastmodified
|
||||
* @param mixed $data An object, an array or null.
|
||||
*/
|
||||
public function __construct($id, $starttime, $stoptime, $warningminutes,
|
||||
$title, $description, $createdby, $modifiedby, $lastmodified) {
|
||||
$this->id = $id;
|
||||
$this->starttime = $starttime;
|
||||
$this->stoptime = $stoptime;
|
||||
$this->warningminutes = $warningminutes;
|
||||
$this->title = $title;
|
||||
$this->description = $description;
|
||||
$this->createdby = $createdby;
|
||||
$this->modifiedby = $modifiedby;
|
||||
$this->lastmodified = $lastmodified;
|
||||
}
|
||||
public function __construct($data = null) {
|
||||
if (is_null($data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->id;
|
||||
}
|
||||
if (is_object($data) || is_array($data)) {
|
||||
outageutils::data2object($data, $this);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_starttime() {
|
||||
return $this->starttime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_stoptime() {
|
||||
return $this->stoptime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_warningminutes() {
|
||||
return $this->warningminutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_title() {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_createdby() {
|
||||
return $this->createdby;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_modifiedby() {
|
||||
return $this->modifiedby;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_lastmodified() {
|
||||
return $this->lastmodified;
|
||||
throw new \InvalidArgumentException('$data must be null (default), an array or an object.');
|
||||
}
|
||||
}
|
||||
@@ -44,11 +44,6 @@ final class outagedb
|
||||
return self::$singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Referente to Moodle global $DB.
|
||||
*/
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* Private clone method to prevent cloning singleton.
|
||||
*
|
||||
@@ -69,40 +64,38 @@ final class outagedb
|
||||
* Private constructor (singleton), use outagedb::get() instead.
|
||||
*/
|
||||
private function __construct() {
|
||||
global $DB;
|
||||
$this->db = $DB;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an stdClass object to an outage.
|
||||
*
|
||||
* @param \stdClass $obj Object from DB.
|
||||
* @return outage
|
||||
*/
|
||||
private function object2outage(\stdClass $obj) {
|
||||
return new outage(
|
||||
$obj->id,
|
||||
$obj->starttime,
|
||||
$obj->stoptime,
|
||||
$obj->warningminutes,
|
||||
$obj->title,
|
||||
$obj->description,
|
||||
$obj->createdby,
|
||||
$obj->modifiedby,
|
||||
$obj->lastmod
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all outage entries.
|
||||
*/
|
||||
public function getall() {
|
||||
global $DB;
|
||||
|
||||
$outages = [];
|
||||
$rs = $this->db->get_recordset('auth_outage', null, 'starttime,stoptime,title');
|
||||
|
||||
$rs = $DB->get_recordset('auth_outage', null, 'starttime,stoptime,title');
|
||||
foreach ($rs as $r) {
|
||||
$outages[] = $this->object2outage($r);
|
||||
$outages[] = new outage($r);
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
return $outages;
|
||||
}
|
||||
|
||||
public function save(outage $outage) {
|
||||
global $DB, $USER;
|
||||
|
||||
// If new outage, set its creator.
|
||||
if ($outage->id === null) {
|
||||
$outage->createdby = $USER->id;
|
||||
}
|
||||
|
||||
// Update control fields.
|
||||
$outage->modifiedby = $USER->id;
|
||||
$outage->lastmodified = time();
|
||||
|
||||
// Save it and return the id.
|
||||
return $DB->insert_record('auth_outage', $outage, true);
|
||||
}
|
||||
}
|
||||
70
classes/outageform.php
Normal file
70
classes/outageform.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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;
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
}
|
||||
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
|
||||
/**
|
||||
* Outage form.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outageform extends \moodleform {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see moodleform::definition()
|
||||
*/
|
||||
public function definition() {
|
||||
$mform = $this->_form;
|
||||
$data = $this->_customdata;
|
||||
|
||||
$mform->addElement('date_time_selector', 'starttime', 'Start Time');
|
||||
|
||||
$mform->addElement('date_time_selector', 'stoptime', 'Stop Time');
|
||||
|
||||
$mform->addElement('duration', 'warningduration', 'Warning Duration');
|
||||
|
||||
$mform->addElement('text', 'title', 'Title');
|
||||
$mform->setType('title', PARAM_TEXT);
|
||||
|
||||
$mform->addElement('editor', 'description', 'Description');
|
||||
|
||||
$this->add_action_buttons();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function validation($data, $files) {
|
||||
$errors = parent::validation($data, $files);
|
||||
$mform = $this->_form;
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
}
|
||||
89
classes/outageutils.php
Normal file
89
classes/outageutils.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?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;
|
||||
|
||||
use Horde\Socket\Client\Exception;
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
}
|
||||
|
||||
/**
|
||||
* Outage related functions.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outageutils
|
||||
{
|
||||
/**
|
||||
* Initializes admin pages for outage.
|
||||
*
|
||||
* @return \renderer_base
|
||||
*/
|
||||
public static function pagesetup() {
|
||||
global $PAGE;
|
||||
admin_externalpage_setup('auth_outage_manage');
|
||||
$PAGE->set_url(new \moodle_url('/auth/outage/list.php'));
|
||||
return $PAGE->get_renderer('auth_outage');
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads data from an object or array into another object.
|
||||
*
|
||||
* @param $data mixed An object or array.
|
||||
* @param $obj object Destination object to write the properties.
|
||||
* @param $strict bool All data fields must be used in the destination object or an exception will be thrown.
|
||||
*/
|
||||
public static function data2object($data, $obj, $strict = false) {
|
||||
if (is_object($data)) {
|
||||
$data = get_object_vars($data);
|
||||
}
|
||||
if (!is_array($data)) {
|
||||
throw new \InvalidArgumentException('$data must be an array or an object.');
|
||||
}
|
||||
if (!is_object($obj)) {
|
||||
throw new \InvalidArgumentException('$obj must be an object.');
|
||||
}
|
||||
if (!is_bool($strict)) {
|
||||
throw new \InvalidArgumentException('$strict must be a bool.');
|
||||
}
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
if (!property_exists($obj, $k)) {
|
||||
if ($strict) {
|
||||
throw new \InvalidArgumentException('$obj does not have a property called ' . $k);
|
||||
}
|
||||
} else {
|
||||
if (method_exists($obj, $k)) {
|
||||
throw new \InvalidArgumentException('$obj has a method called ' . $k);
|
||||
}
|
||||
$obj->$k = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function parseformdata($data) {
|
||||
if ($data->description['format'] != '1') {
|
||||
throw new \InvalidArgumentException('Not implemented for format ' . $data->description['format']);
|
||||
}
|
||||
$data->description = $data->description['text'];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user