mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-16 21:41:31 +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;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Update outages (create, update, delete).
|
||||
* Create new outage.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
@@ -24,35 +24,28 @@
|
||||
*/
|
||||
|
||||
use \auth_outage\outage;
|
||||
use \auth_outage\outageutils;
|
||||
use \auth_outage\outagedb;
|
||||
use \auth_outage\outageform;
|
||||
|
||||
require_once('../../config.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
|
||||
// Check parameters.
|
||||
require_sesskey();
|
||||
$action = required_param('action', PARAM_ALPHA);
|
||||
switch ($action) {
|
||||
case 'add':
|
||||
$title = 'Add new Outage';
|
||||
break;
|
||||
default:
|
||||
print_error('auth_outage_invalidaction1');
|
||||
outageutils::pagesetup();
|
||||
|
||||
$mform = new outageform();
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($listurl);
|
||||
} else if ($fromform = $mform->get_data()) {
|
||||
$fromform = outageutils::parseformdata($fromform);
|
||||
$outage = new outage($fromform);
|
||||
$id = outagedb::get()->save($outage);
|
||||
redirect('/auth/outage/list.php#auth_outage_id=' . $id);
|
||||
}
|
||||
|
||||
admin_externalpage_setup('auth_outage_manage');
|
||||
$PAGE->set_title($title);
|
||||
$PAGE->set_heading($title);
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/update.php'));
|
||||
$renderer = $PAGE->get_renderer('auth_outage');
|
||||
|
||||
echo $OUTPUT->header();
|
||||
|
||||
switch ($action) {
|
||||
case 'add':
|
||||
$outage = new outage();
|
||||
break;
|
||||
default:
|
||||
print_error('auth_outage_invalidaction2');
|
||||
}
|
||||
$mform->display();
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
@@ -9,7 +9,7 @@
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
|
||||
<FIELD NAME="starttime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage starts."/>
|
||||
<FIELD NAME="stoptime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage ends."/>
|
||||
<FIELD NAME="warningminutes" TYPE="int" LENGTH="7" NOTNULL="true" SEQUENCE="false" COMMENT="How many minutes before the outage to display a warning to all users."/>
|
||||
<FIELD NAME="warningduration" TYPE="int" LENGTH="7" NOTNULL="true" SEQUENCE="false" COMMENT="How many seconds before the outage to display a warning to all users."/>
|
||||
<FIELD NAME="title" TYPE="char" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="Title for the outage (short description)."/>
|
||||
<FIELD NAME="description" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="More information about the outage."/>
|
||||
<FIELD NAME="createdby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who created this entry."/>
|
||||
|
||||
14
list.php
14
list.php
@@ -24,25 +24,17 @@
|
||||
*/
|
||||
|
||||
use \auth_outage\outage;
|
||||
use \auth_outage\outageutils;
|
||||
use \auth_outage\outagedb;
|
||||
|
||||
require_once('../../config.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
// TODO Check parameters.
|
||||
|
||||
// Read https://docs.moodle.org/dev/Page_API#.24PAGE_The_Moodle_page_global for Page API info.
|
||||
admin_externalpage_setup('auth_outage_manage'); // Does require_login and set_context inside.
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/list.php'));
|
||||
$PAGE->set_title('Outage List');
|
||||
$PAGE->set_heading('List of registered outages.');
|
||||
|
||||
$renderer = $PAGE->get_renderer('auth_outage');
|
||||
|
||||
// TODO Add paging or limiting past entries displayed.
|
||||
$renderer = outageutils::pagesetup();
|
||||
|
||||
echo $OUTPUT->header();
|
||||
|
||||
// TODO Add paging or limiting past entries displayed.
|
||||
echo $renderer->renderoutagelist(outagedb::get()->getall());
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
|
||||
25
renderer.php
25
renderer.php
@@ -15,6 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use \auth_outage\outage;
|
||||
use \auth_outage\outageform;
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
@@ -41,7 +42,7 @@ class auth_outage_renderer extends plugin_renderer_base
|
||||
}
|
||||
|
||||
// Add 'add' button.
|
||||
$url = new moodle_url('/auth/outage/update.php', ['action' => 'add', 'sesskey' => sesskey()]);
|
||||
$url = new moodle_url('/auth/outage/create.php');
|
||||
$img = html_writer::empty_tag('img',
|
||||
['src' => $OUTPUT->pix_url('t/add'), 'alt' => get_string('add'), 'class' => 'iconsmall']);
|
||||
$html .= html_writer::empty_tag('br')
|
||||
@@ -54,19 +55,19 @@ class auth_outage_renderer extends plugin_renderer_base
|
||||
private function renderoutagelistentry(outage $outage) {
|
||||
global $OUTPUT;
|
||||
|
||||
$created = core_user::get_user($outage->get_createdby(), 'firstname,lastname', MUST_EXIST);
|
||||
$created = core_user::get_user($outage->createdby, 'firstname,lastname', MUST_EXIST);
|
||||
$created = html_writer::link(
|
||||
new moodle_url('/user/profile.php', ['id' => $outage->get_createdby()]),
|
||||
new moodle_url('/user/profile.php', ['id' => $outage->createdby]),
|
||||
trim($created->firstname . ' ' . $created->lastname)
|
||||
);
|
||||
|
||||
$modified = core_user::get_user($outage->get_modifiedby(), 'firstname,lastname', MUST_EXIST);
|
||||
$modified = core_user::get_user($outage->modifiedby, 'firstname,lastname', MUST_EXIST);
|
||||
$modified = html_writer::link(
|
||||
new moodle_url('/user/profile.php', ['id' => $outage->get_modifiedby()]),
|
||||
new moodle_url('/user/profile.php', ['id' => $outage->modifiedby]),
|
||||
trim($modified->firstname . ' ' . $modified->lastname)
|
||||
);
|
||||
|
||||
$url = new moodle_url('/auth/outage/update.php', ['id' => $outage->get_id(), 'sesskey' => sesskey()]);
|
||||
$url = new moodle_url('/auth/outage/update.php', ['id' => $outage->id, 'sesskey' => sesskey()]);
|
||||
|
||||
$url->param('action', 'edit');
|
||||
$img = html_writer::empty_tag('img',
|
||||
@@ -80,23 +81,23 @@ class auth_outage_renderer extends plugin_renderer_base
|
||||
|
||||
return html_writer::div(
|
||||
html_writer::span(
|
||||
html_writer::tag('b', $outage->get_title(), ['data-id' => $outage->get_id()])
|
||||
html_writer::tag('b', $outage->title, ['data-id' => $outage->id])
|
||||
. html_writer::empty_tag('br')
|
||||
. html_writer::tag('i', $outage->get_description())
|
||||
. html_writer::tag('i', $outage->description)
|
||||
. html_writer::empty_tag('br')
|
||||
. html_writer::tag('b', 'Warning: ')
|
||||
. userdate($outage->get_starttime() - ($outage->get_warningminutes() * 60))
|
||||
. userdate($outage->starttime - ($outage->warningduration * 60))
|
||||
. html_writer::empty_tag('br')
|
||||
. html_writer::tag('b', 'Starts: ')
|
||||
. userdate($outage->get_starttime(), '%d %h %Y %l:%M%P')
|
||||
. userdate($outage->starttime, '%d %h %Y %l:%M%P')
|
||||
. html_writer::empty_tag('br')
|
||||
. html_writer::tag('b', 'Stops: ')
|
||||
. userdate($outage->get_stoptime(), '%d %h %Y %l:%M%P')
|
||||
. userdate($outage->stoptime, '%d %h %Y %l:%M%P')
|
||||
. html_writer::empty_tag('br')
|
||||
. html_writer::tag('small',
|
||||
'Created by ' . $created
|
||||
. ', modified by ' . $modified . ' on '
|
||||
. userdate($outage->get_lastmodified(), '%d %h %Y %l:%M%P')
|
||||
. userdate($outage->lastmodified, '%d %h %Y %l:%M%P')
|
||||
)
|
||||
. html_writer::empty_tag('br')
|
||||
. $linkedit . $linkdelete
|
||||
|
||||
@@ -29,7 +29,7 @@ if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
}
|
||||
|
||||
$plugin->version = 2016083101; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->version = 2016083102; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->release = $plugin->version; // Same as version
|
||||
$plugin->requires = 2014051200; // Requires Moodle 2.7 or later.
|
||||
$plugin->component = "auth_outage";
|
||||
|
||||
Reference in New Issue
Block a user