From 1d7a042e65c11f969d23c451df63bc3b1b0c0760 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Wed, 31 Aug 2016 15:03:36 +1000 Subject: [PATCH] Several changes to easily integrate with moodle db api. Create outage page working. --- classes/outage.php | 109 +++++++-------------------------------- classes/outagedb.php | 51 ++++++++---------- classes/outageform.php | 70 +++++++++++++++++++++++++ classes/outageutils.php | 89 ++++++++++++++++++++++++++++++++ update.php => create.php | 39 ++++++-------- db/install.xml | 2 +- list.php | 14 ++--- renderer.php | 25 ++++----- version.php | 2 +- 9 files changed, 234 insertions(+), 167 deletions(-) create mode 100644 classes/outageform.php create mode 100644 classes/outageutils.php rename update.php => create.php (62%) diff --git a/classes/outage.php b/classes/outage.php index 168a400..1520632 100644 --- a/classes/outage.php +++ b/classes/outage.php @@ -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.'); } } \ No newline at end of file diff --git a/classes/outagedb.php b/classes/outagedb.php index 27d85b7..2efe26d 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -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); + } } \ No newline at end of file diff --git a/classes/outageform.php b/classes/outageform.php new file mode 100644 index 0000000..ac87c44 --- /dev/null +++ b/classes/outageform.php @@ -0,0 +1,70 @@ +. + +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 + * @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; + } + +} \ No newline at end of file diff --git a/classes/outageutils.php b/classes/outageutils.php new file mode 100644 index 0000000..f5c6847 --- /dev/null +++ b/classes/outageutils.php @@ -0,0 +1,89 @@ +. + +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 + * @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; + } +} \ No newline at end of file diff --git a/update.php b/create.php similarity index 62% rename from update.php rename to create.php index 72d6ca0..94c81f3 100644 --- a/update.php +++ b/create.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Update outages (create, update, delete). + * Create new outage. * * @package auth_outage * @author Daniel Thee Roperto @@ -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(); diff --git a/db/install.xml b/db/install.xml index d5a046d..4f2297e 100644 --- a/db/install.xml +++ b/db/install.xml @@ -9,7 +9,7 @@ - + diff --git a/list.php b/list.php index 733693d..d61fec3 100644 --- a/list.php +++ b/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(); diff --git a/renderer.php b/renderer.php index 99c4258..08c0324 100644 --- a/renderer.php +++ b/renderer.php @@ -15,6 +15,7 @@ // along with Moodle. If not, see . 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 diff --git a/version.php b/version.php index a922cb7..b53c672 100644 --- a/version.php +++ b/version.php @@ -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";