Added admin events to the Moodle log - Issue #14

This commit is contained in:
Daniel Thee Roperto
2016-09-05 10:05:59 +10:00
parent 334fd907ad
commit f8034abc0e
9 changed files with 172 additions and 18 deletions

View File

@@ -0,0 +1,48 @@
<?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\event;
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
* @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']);
}
}

View File

@@ -0,0 +1,48 @@
<?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\event;
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
* @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']);
}
}

View File

@@ -0,0 +1,49 @@
<?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\event;
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
* @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');
}
}

View File

@@ -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]);
}
}