From f8034abc0e07c5a1f622a48228468c4c4494bb5d Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Mon, 5 Sep 2016 10:05:59 +1000 Subject: [PATCH] Added admin events to the Moodle log - Issue #14 --- .travis.yml | 2 -- change.php | 2 +- classes/event/outage_created.php | 48 +++++++++++++++++++++++++++++++ classes/event/outage_deleted.php | 48 +++++++++++++++++++++++++++++++ classes/event/outage_updated.php | 49 ++++++++++++++++++++++++++++++++ classes/outagedb.php | 35 +++++++++++++++-------- create.php | 2 +- lang/en/auth_outage.php | 2 +- version.php | 2 +- 9 files changed, 172 insertions(+), 18 deletions(-) create mode 100644 classes/event/outage_created.php create mode 100644 classes/event/outage_deleted.php create mode 100644 classes/event/outage_updated.php diff --git a/.travis.yml b/.travis.yml index 4fbe65c..6bc1707 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,14 +17,12 @@ php: - 5.6 env: - - DB=pgsql MOODLE_BRANCH=MOODLE_26_STABLE - DB=pgsql MOODLE_BRANCH=MOODLE_27_STABLE - DB=pgsql MOODLE_BRANCH=MOODLE_28_STABLE - DB=pgsql MOODLE_BRANCH=MOODLE_29_STABLE - DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE - DB=pgsql MOODLE_BRANCH=MOODLE_31_STABLE - DB=pgsql MOODLE_BRANCH=master - - DB=mysqli MOODLE_BRANCH=MOODLE_26_STABLE - DB=mysqli MOODLE_BRANCH=MOODLE_27_STABLE - DB=mysqli MOODLE_BRANCH=MOODLE_28_STABLE - DB=mysqli MOODLE_BRANCH=MOODLE_29_STABLE diff --git a/change.php b/change.php index e16fa41..675b05d 100644 --- a/change.php +++ b/change.php @@ -41,7 +41,7 @@ if ($mform->is_cancelled()) { $fromform = outagelib::parseformdata($fromform); $outage = new outage($fromform); $id = outagedb::save($outage); - redirect('/auth/outage/list.php#auth_outage_id=' . $id); + redirect('/auth/outage/list.php#auth_outage_id_' . $id); } $id = required_param('id', PARAM_INT); diff --git a/classes/event/outage_created.php b/classes/event/outage_created.php new file mode 100644 index 0000000..28ef9d1 --- /dev/null +++ b/classes/event/outage_created.php @@ -0,0 +1,48 @@ +. + +namespace auth_outage\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The auth_outage outage created class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class outage_created extends \core\event\base { + /** + * Init method. + */ + protected function init() { + $this->data['objecttable'] = 'auth_outage'; + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->context = \context_system::instance(); + } + + 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']}'."; + } + + public function get_url() { + return new \moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']); + } +} diff --git a/classes/event/outage_deleted.php b/classes/event/outage_deleted.php new file mode 100644 index 0000000..f73a516 --- /dev/null +++ b/classes/event/outage_deleted.php @@ -0,0 +1,48 @@ +. + +namespace auth_outage\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The auth_outage outage deleted class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class outage_deleted extends \core\event\base { + /** + * Init method. + */ + protected function init() { + $this->data['objecttable'] = 'auth_outage'; + $this->data['crud'] = 'd'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->context = \context_system::instance(); + } + + public function get_description() { + return "The user with the id '{$this->userid}' deleted the outage titled '{$this->other['title']}' " + . "with id '{$this->other['id']}'."; + } + + public function get_url() { + return new \moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']); + } +} diff --git a/classes/event/outage_updated.php b/classes/event/outage_updated.php new file mode 100644 index 0000000..59a846e --- /dev/null +++ b/classes/event/outage_updated.php @@ -0,0 +1,49 @@ +. + +namespace auth_outage\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The auth_outage outage updated class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class outage_updated extends \core\event\base { + /** + * Init method. + */ + protected function init() { + $this->data['objecttable'] = 'auth_outage'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->context = \context_system::instance(); + } + + + public function get_description() { + return "The user with the id '{$this->userid}' updated the outage title '{$this->other['title']}' " + . "with id '{$this->other['id']}'."; + } + + public function get_url() { + return new \moodle_url('/auth/outage/list.php'); + } +} diff --git a/classes/outagedb.php b/classes/outagedb.php index ff0da94..2fcaf75 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -27,8 +27,7 @@ namespace auth_outage; use auth_outage\models\outage; -final class outagedb -{ +final class outagedb { /** * Private constructor, use static methods instead. */ @@ -86,23 +85,29 @@ final class outagedb // Do not change the original object. $outage = clone $outage; - // If new outage, set its creator. - if ($outage->id === null) { - $outage->createdby = $USER->id; - } - // Update control fields. $outage->modifiedby = $USER->id; $outage->lastmodified = time(); - // If new, create it and return the id. if ($outage->id === null) { - return $DB->insert_record('auth_outage', $outage, true); + // If new outage, set its creator. + $outage->createdby = $USER->id; + // Then create it, log it and adjust its id. + $outage->id = $DB->insert_record('auth_outage', $outage, true); + \auth_outage\event\outage_created::create( + ['objectid' => $outage->id, 'other' => (array)$outage] + )->trigger(); + } else { + // Not new, clean up the class (remove creator field) then update. + unset($outage->createdby); + $DB->update_record('auth_outage', $outage); + // Log it. + \auth_outage\event\outage_updated::create( + ['objectid' => $outage->id, 'other' => (array)$outage] + )->trigger(); } - // Clean up the class (remove creator field), then update it and return its id. - unset($outage->createdby); - $DB->update_record('auth_outage', $outage); + // All done, return the id. return $outage->id; } @@ -122,6 +127,12 @@ final class outagedb throw new \InvalidArgumentException('$id must be positive.'); } + // Log it. + $previous = $DB->get_record('auth_outage', ['id' => $id], '*', MUST_EXIST); + $event = \auth_outage\event\outage_deleted::create(['objectid' => $id, 'other' => (array)$previous]); + $event->add_record_snapshot('auth_outage', $previous); + $event->trigger(); + $DB->delete_records('auth_outage', ['id' => $id]); } } \ No newline at end of file diff --git a/create.php b/create.php index f658da1..c48af19 100644 --- a/create.php +++ b/create.php @@ -40,7 +40,7 @@ if ($mform->is_cancelled()) { $fromform = outagelib::parseformdata($fromform); $outage = new outage($fromform); $id = outagedb::save($outage); - redirect('/auth/outage/list.php#auth_outage_id=' . $id); + redirect('/auth/outage/list.php#auth_outage_id_' . $id); } echo $OUTPUT->header(); diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 83918af..f232f1b 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -45,4 +45,4 @@ $string['removeoutage'] = 'Remove Outage'; $string['removeoutagewarning'] = 'You are about to permanently remove the outage below. This cannot be undone.'; $string['outageslist'] = 'Outages List'; $string['createoutage'] = 'Create Outage'; -$string['modify'] = 'Modify'; \ No newline at end of file +$string['modify'] = 'Modify'; diff --git a/version.php b/version.php index 4a42a07..0f174e7 100644 --- a/version.php +++ b/version.php @@ -31,6 +31,6 @@ if (!defined('MOODLE_INTERNAL')) { $plugin->version = 2016090100; // The current plugin version (Date: YYYYMMDDXX). $plugin->release = $plugin->version; // Same as version -$plugin->requires = 2013111811; // Requires Moodle 2.6 or later. +$plugin->requires = 2014051200; // Requires Moodle 2.7 or later. $plugin->component = "auth_outage"; $plugin->maturity = MATURITY_ALPHA; // Not suitable for PRODUCTION environments yet!