MDL-78551 core_group: Add hooks api for group and membership

This commit is contained in:
Safat 2024-01-04 12:46:55 +11:00
parent 757be30c39
commit ae8cae6d9a
6 changed files with 241 additions and 0 deletions

View File

@ -0,0 +1,41 @@
<?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_group\hook;
use stdClass;
/**
* Hook after group creation.
*
* @package core_group
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allows plugins or features to perform actions after a group is created.')]
#[\core\attribute\tags('group')]
class after_group_created {
/**
* Constructor for the hook.
*
* @param stdClass $groupinstance The group instance.
*/
public function __construct(
public readonly stdClass $groupinstance,
) {
}
}

View File

@ -0,0 +1,41 @@
<?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_group\hook;
use stdClass;
/**
* Hook after group deletion.
*
* @package core_group
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allows plugins or features to perform actions after a group is deleted.')]
#[\core\attribute\tags('group')]
class after_group_deleted {
/**
* Constructor for the hook.
*
* @param stdClass $groupinstance The group instance.
*/
public function __construct(
public readonly stdClass $groupinstance,
) {
}
}

View File

@ -0,0 +1,43 @@
<?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_group\hook;
use stdClass;
/**
* Hook after a member added to the group.
*
* @package core_group
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allows plugins or features to perform actions after members added to the group.')]
#[\core\attribute\tags('group', 'user')]
class after_group_membership_added {
/**
* Constructor for the hook.
*
* @param stdClass $groupinstance The group instance.
* @param array $userids The user ids.
*/
public function __construct(
public readonly stdClass $groupinstance,
public readonly array $userids,
) {
}
}

View File

@ -0,0 +1,43 @@
<?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_group\hook;
use stdClass;
/**
* Hook after a member removed from the group.
*
* @package core_group
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allows plugins or features to perform actions after members removed from the group.')]
#[\core\attribute\tags('group', 'user')]
class after_group_membership_removed {
/**
* Constructor for the hook.
*
* @param stdClass $groupinstance The group instance.
* @param array $userids The user ids.
*/
public function __construct(
public readonly stdClass $groupinstance,
public readonly array $userids,
) {
}
}

View File

@ -0,0 +1,41 @@
<?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_group\hook;
use stdClass;
/**
* Hook after group updates.
*
* @package core_group
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allows plugins or features to perform actions after a group is updated.')]
#[\core\attribute\tags('group')]
class after_group_updated {
/**
* Constructor for the hook.
*
* @param stdClass $groupinstance The group instance.
*/
public function __construct(
public readonly stdClass $groupinstance,
) {
}
}

View File

@ -126,6 +126,13 @@ function groups_add_member($grouporid, $userorid, $component=null, $itemid=0) {
$event->add_record_snapshot('groups', $group);
$event->trigger();
// Dispatch the hook for a user added to the group.
$hook = new \core_group\hook\after_group_membership_added(
groupinstance: $group,
userids: [$userid],
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
return true;
}
@ -232,6 +239,13 @@ function groups_remove_member($grouporid, $userorid) {
$event->add_record_snapshot('groups', $group);
$event->trigger();
// Dispatch the hook for a user removed from the group.
$hook = new \core_group\hook\after_group_membership_removed(
groupinstance: $group,
userids: [$userid],
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
return true;
}
@ -323,6 +337,12 @@ function groups_create_group($data, $editform = false, $editoroptions = false) {
$event->add_record_snapshot('groups', $group);
$event->trigger();
// Dispatch the hook for post group creation actions.
$hook = new \core_group\hook\after_group_created(
groupinstance: $group,
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
return $group->id;
}
@ -511,6 +531,12 @@ function groups_update_group($data, $editform = false, $editoroptions = false) {
$event->add_record_snapshot('groups', $group);
$event->trigger();
// Dispatch the hook for post group update actions.
$hook = new \core_group\hook\after_group_updated(
groupinstance: $group,
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
return true;
}
@ -616,6 +642,12 @@ function groups_delete_group($grouporid) {
$event->add_record_snapshot('groups', $group);
$event->trigger();
// Dispatch the hook for post group delete actions.
$hook = new \core_group\hook\after_group_deleted(
groupinstance: $group,
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
return true;
}