diff --git a/lang/en/role.php b/lang/en/role.php index d96e43813a7..4b366744316 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -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'; diff --git a/lib/accesslib.php b/lib/accesslib.php index 8bc42803a37..e2cfce8ebb7 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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; } /** diff --git a/lib/classes/event/role_created.php b/lib/classes/event/role_created.php new file mode 100644 index 00000000000..c17458a22c3 --- /dev/null +++ b/lib/classes/event/role_created.php @@ -0,0 +1,68 @@ +. + +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.'); + } + } +} diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index 899252e95fa..8e72c3b542d 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -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']); } /** diff --git a/lib/upgrade.txt b/lib/upgrade.txt index e4a5232abc6..d4331ad693e 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -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 ===