MDL-76797 core_role: Add create role event class

This commit is contained in:
Thong Bui 2023-01-10 14:26:12 +07:00
parent 8503f2cfd8
commit 5db011a600
5 changed files with 99 additions and 3 deletions

View File

@ -242,6 +242,7 @@ $string['eventroleallowswitchupdated'] = 'Allow role switch';
$string['eventroleallowviewupdated'] = 'Allow role view';
$string['eventroleassigned'] = 'Role assigned';
$string['eventrolecapabilitiesupdated'] = 'Role capabilities updated';
$string['eventrolecreated'] = 'Role created';
$string['eventroledeleted'] = 'Role deleted';
$string['eventroleunassigned'] = 'Role unassigned';
$string['eventroleupdated'] = 'Role updated';

View File

@ -1316,9 +1316,21 @@ function create_role($name, $shortname, $description, $archetype = '') {
if (empty($role->sortorder)) {
$role->sortorder = 1;
}
$id = $DB->insert_record('role', $role);
$role->id = $DB->insert_record('role', $role);
$event = \core\event\role_created::create([
'objectid' => $role->id,
'context' => context_system::instance(),
'other' => [
'name' => $role->name,
'shortname' => $role->shortname,
'archetype' => $role->archetype,
]
]);
return $id;
$event->add_record_snapshot('role', $role);
$event->trigger();
return $role->id;
}
/**

View File

@ -0,0 +1,68 @@
<?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 core\event;
/**
* Role create event class.
*
* @property-read array $other {
* Extra information about the event.
*
* - string name: The name of role.
* - string shortname: The shortname of role.
* - string archetype: The archetype.
* }
*
* @package core
* @copyright 2023 The Open University.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class role_created extends base {
protected function init() {
$this->data['objecttable'] = 'role';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
public static function get_name() {
return get_string('eventrolecreated', 'role');
}
public function get_url() {
return new \moodle_url('/admin/roles/define.php', ['action' => 'view', 'roleid' => $this->objectid]);
}
public function get_description() {
return "The user with id '$this->userid' created the role with id '$this->objectid'.";
}
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['name'])) {
throw new \coding_exception('The \'name\' value must be set in other.');
}
if (!isset($this->other['shortname'])) {
throw new \coding_exception('The \'shortname\' value must be set in other.');
}
if (!isset($this->other['archetype'])) {
throw new \coding_exception('The \'archetype\' value must be set in other.');
}
}
}

View File

@ -375,14 +375,28 @@ class accesslib_test extends advanced_testcase {
$this->resetAfterTest();
// Create role and get event.
$sink = $this->redirectEvents();
$id = create_role('New student role', 'student2', 'New student description', 'student');
$role = $DB->get_record('role', array('id'=>$id));
$events = $sink->get_events();
$sink->close();
$event = array_pop($events);
$role = $DB->get_record('role', ['id' => $id]);
$this->assertNotEmpty($role);
$this->assertSame('New student role', $role->name);
$this->assertSame('student2', $role->shortname);
$this->assertSame('New student description', $role->description);
$this->assertSame('student', $role->archetype);
// Test triggered event.
$this->assertInstanceOf('\core\event\role_created', $event);
$this->assertSame('role', $event->target);
$this->assertSame('role', $event->objecttable);
$this->assertSame((int)$role->id, $event->objectid);
$this->assertEquals(context_system::instance(), $event->get_context());
$this->assertSame($role->shortname, $event->other['shortname']);
$this->assertSame($role->archetype, $event->other['archetype']);
}
/**

View File

@ -62,6 +62,7 @@ information provided here is intended especially for developers.
description being incorrectly passed for the $required parameter). A debugging notice will be shown when such cases occur.
* The moodle-core-popuphelp YUI modal has been removed. It has not been actively used in Moodle since 3.3. It should be replaced with appropriate ESM/AMD JavaScript.
* The moodle-core-tooltip YUI modal has been removed. It should be replaced with appropriate ESM/AMD JavaScript.
* A `\core\event\role_created` event is now triggered when roles are created via the `create_role` API.
=== 4.1 ===