Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute events trought adhoc task. #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Moodle plugin CI
on: [push, pull_request]

jobs:
test:
runs-on: 'ubuntu-latest'
strategy:
fail-fast: false
matrix:
include:
- php: '8.0'
moodle-branch: 'MOODLE_401_STABLE'
database: 'pgsql'
- php: '8.0'
moodle-branch: 'MOODLE_401_STABLE'
database: 'mariadb'

services:
postgres:
image: postgres:13
env:
POSTGRES_USER: 'postgres'
POSTGRES_HOST_AUTH_METHOD: 'trust'
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 3
ports:
- 5432:5432

mariadb:
image: mariadb:10
env:
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3

steps:
- name: Check out out repository code
uses: actions/checkout@v3
with:
path: plugin

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ matrix.extensions }}
ini-values: max_input_vars=5000
coverage: none

- name: Initialise moodle-plugin-ci
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV

- name: Install moodle-plugin-ci
run: |
moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
env:
DB: ${{ matrix.database }}
MOODLE_BRANCH: ${{ matrix.moodle-branch }}

- name: PHPUnit tests
if: ${{ always() }}
run: moodle-plugin-ci phpunit --fail-on-warning
2 changes: 1 addition & 1 deletion classes/domain/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function ensure_user_is_member($userid) {
}

// User was not found as a member so will now make member a user.
\groups_add_member($this->as_object(), $userid, 'local_autogroup');
\groups_add_member($this->as_object()->id, $userid, 'local_autogroup');
return true;
}

Expand Down
99 changes: 65 additions & 34 deletions classes/event_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

use \core\event;
use local_autogroup\domain\group;
use local_autogroup\task\process_event;

/**
* Class event_handler
Expand All @@ -43,11 +44,23 @@
* @package local_autogroup
*/
class event_handler {

/**
* @param event\user_enrolment_created $event
* @var array
*/
const FUNCTION_MAPPING = [
'group_deleted' => 'group_change',
'group_updated' => 'group_change',
'role_assigned' => 'role_change',
'role_unassigned' => 'role_change',
'role_deleted' => 'role_deleted',
];

/**
* @param object $event
* @return mixed
*/
public static function user_enrolment_created(event\user_enrolment_created $event) {
public static function user_enrolment_created(object $event) {
$pluginconfig = get_config('local_autogroup');
if (!$pluginconfig->listenforrolechanges) {
return false;
Expand All @@ -63,12 +76,12 @@ public static function user_enrolment_created(event\user_enrolment_created $even
}

/**
* @param event\group_member_added $event
* @param object $event
* @return bool
* @throws \Exception
* @throws \dml_exception
*/
public static function group_member_added(event\group_member_added $event) {
public static function group_member_added(object $event) {
if (self::triggered_by_autogroup($event)) {
return false;
}
Expand Down Expand Up @@ -99,12 +112,12 @@ public static function group_member_added(event\group_member_added $event) {
}

/**
* @param event\group_member_removed $event
* @param object $event
* @return bool
* @throws \Exception
* @throws \dml_exception
*/
public static function group_member_removed(event\group_member_removed $event) {
public static function group_member_removed(object $event) {
if (self::triggered_by_autogroup($event)) {
return false;
}
Expand Down Expand Up @@ -135,10 +148,10 @@ public static function group_member_removed(event\group_member_removed $event) {
}

/**
* @param event\user_updated $event
* @param object $event
* @return mixed
*/
public static function user_updated(event\user_updated $event) {
public static function user_updated(object $event) {
$pluginconfig = get_config('local_autogroup');
if (!$pluginconfig->listenforuserprofilechanges) {
return false;
Expand All @@ -153,12 +166,12 @@ public static function user_updated(event\user_updated $event) {
}

/**
* @param event\base $event
* @param object $event
* @return bool
* @throws \Exception
* @throws \dml_exception
*/
public static function group_created(event\base $event) {
public static function group_created(object $event) {
if (self::triggered_by_autogroup($event)) {
return false;
}
Expand All @@ -177,12 +190,12 @@ public static function group_created(event\base $event) {
}

/**
* @param event\base $event
* @param object $event
* @return bool
* @throws \Exception
* @throws \dml_exception
*/
public static function group_change(event\base $event) {
public static function group_change(object $event) {
if (self::triggered_by_autogroup($event)) {
return false;
}
Expand Down Expand Up @@ -212,10 +225,10 @@ public static function group_change(event\base $event) {
}

/**
* @param event\base $event
* @param object $event
* @return mixed
*/
public static function role_change(event\base $event) {
public static function role_change(object $event) {
$pluginconfig = get_config('local_autogroup');
if (!$pluginconfig->listenforrolechanges) {
return false;
Expand All @@ -230,10 +243,10 @@ public static function role_change(event\base $event) {
}

/**
* @param event\role_deleted $event
* @param object $event
* @return mixed
*/
public static function role_deleted(event\role_deleted $event) {
public static function role_deleted(object $event) {
global $DB;

$DB->delete_records('local_autogroup_roles', ['roleid' => $event->objectid]);
Expand All @@ -243,10 +256,10 @@ public static function role_deleted(event\role_deleted $event) {
}

/**
* @param event\course_created $event
* @param object $event
* @return mixed
*/
public static function course_created(event\course_created $event) {
public static function course_created(object $event) {
$config = get_config('local_autogroup');
if (!$config->addtonewcourses) {
return false;
Expand All @@ -260,10 +273,10 @@ public static function course_created(event\course_created $event) {
}

/**
* @param event\course_restored $event
* @param object $event
* @return mixed
*/
public static function course_restored(event\course_restored $event) {
public static function course_restored(object $event) {
$config = get_config('local_autogroup');
if (!$config->addtorestoredcourses) {
return false;
Expand All @@ -277,10 +290,10 @@ public static function course_restored(event\course_restored $event) {
}

/**
* @param \totara_core\event\position_updated $event
* @param object $event
* @return bool
*/
public static function position_updated(\totara_core\event\position_updated $event) {
public static function position_updated(object $event) {
$pluginconfig = get_config('local_autogroup');
if (!$pluginconfig->listenforuserpositionchanges) {
return false;
Expand All @@ -298,21 +311,39 @@ public static function position_updated(\totara_core\event\position_updated $eve
* Checks the data of an event to see whether it was initiated
* by the local_autogroup component
*
* @param event\base $event
* @param object $data
* @return bool
*/
private static function triggered_by_autogroup(\core\event\base $event) {
$data = $event->get_data();
if (
isset($data['other']) &&
is_array($data['other']) &&
isset($data['other']['component']) &&
is_string($data['other']['component']) &&
strstr($data['other']['component'], 'autogroup')
) {
return true;
private static function triggered_by_autogroup(object $data) {
return !empty($data->other->component) && strstr($data->other->component, 'autogroup');
}

/**
* Create ad hoc task for event.
*
* @param event\base $event
* @return void
*/
public static function create_adhoc_task(\core\event\base $event) {

$task = new process_event();
$task->set_custom_data($event->get_data());
$queryadhoc = get_config('local_autogroup', 'adhoceventhandler');
if (!empty($queryadhoc)) {
\core\task\manager::queue_adhoc_task($task);
} else {
$task->execute();
}
}

return false;
/**
* @param object $event
* @return mixed
*/
public static function process_event(object $event) {
$explodename = explode('\\', $event->eventname);
$eventname = end($explodename);
$functionname = self::FUNCTION_MAPPING[$eventname] ?? $eventname;
self::$functionname($event);
}
}
48 changes: 48 additions & 0 deletions classes/task/process_event.php
Original file line number Diff line number Diff line change
@@ -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/>.

/**
* Task that processes an event.
*
* @package local_autogroup
* @author Oscar Nadjar <[email protected]>
* @copyright Moodle US
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_autogroup\task;
use local_autogroup\event_handler;

defined('MOODLE_INTERNAL') || die();

class process_event extends \core\task\adhoc_task {

/**
* Get task name
*/
public function get_name() {
return get_string('process_event', 'local_autogroup');
}

/**
* Execute task
*/
public function execute() {
$event = (object) $this->get_custom_data();

event_handler::process_event($event);
}
}
2 changes: 1 addition & 1 deletion classes/usecase/verify_course_group_membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function invoke() {
return false;
}

set_time_limit(500);
!PHPUNIT_TEST ? set_time_limit(500) : null;
return $this->course->verify_all_group_membership($this->db);
}
}
Loading