Skip to content

Commit

Permalink
MDL-76797 core_role: Add create role event class
Browse files Browse the repository at this point in the history
  • Loading branch information
JBThong committed Feb 20, 2023
1 parent 8503f2c commit 5db011a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
1 change: 1 addition & 0 deletions lang/en/role.php
Expand Up @@ -242,6 +242,7 @@
$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';
Expand Down
16 changes: 14 additions & 2 deletions lib/accesslib.php
Expand Up @@ -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,
]
]);

$event->add_record_snapshot('role', $role);
$event->trigger();

return $id;
return $role->id;
}

/**
Expand Down
68 changes: 68 additions & 0 deletions lib/classes/event/role_created.php
@@ -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.');
}
}
}
16 changes: 15 additions & 1 deletion lib/tests/accesslib_test.php
Expand Up @@ -375,14 +375,28 @@ public function test_create_role() {

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

/**
Expand Down
1 change: 1 addition & 0 deletions lib/upgrade.txt
Expand Up @@ -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 ===

Expand Down

0 comments on commit 5db011a

Please sign in to comment.