Skip to content

Commit

Permalink
DEF-1677: Changin event handler behaviour with adhoc tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ojnadjarm committed Aug 23, 2024
1 parent 02558f4 commit dd9bcf6
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 99 deletions.
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
22 changes: 0 additions & 22 deletions MLC-CHANGELOG.md

This file was deleted.

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);
}
}
19 changes: 3 additions & 16 deletions classes/sort_module/user_info_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,10 @@ public function eligible_groups_for_user(stdClass $user) {

$field = $this->field;
$data = $DB->get_record('user_info_data', ['fieldid' => $field, 'userid' => $user->id]);
if ($data && !empty($data->data)){
// Check if the field has comma separated values.
if ($fieldvalues = explode(',', $data->data)) {
$fields = [];
foreach ($fieldvalues as $fieldvalue) {
if ($fieldvalue = trim($fieldvalue)) {
$fields[] = $fieldvalue;
}
}
return $fields;
} else {
return [$data->data];
}
}
else {
return [];
if ($data && !empty($data->data)) {
return [$data->data];
}
return [];
}

/**
Expand Down
Loading

0 comments on commit dd9bcf6

Please sign in to comment.