Cohort members to group

Maintained by Elearnity Team
Cohort to group sync for users already enrolled via different method.
Price option: Free

Supports Moodle 2.4-5.0 See all versions
Latest release: 6 years ago
Installations: 931
Downloads (last 90 days): 249

Frankenstyle name: enrol_groupsync
Enrolment

Comments

Product ratings and reviews are coming to Moodle Marketplace. Until then, existing comments remain available to read, but new posts have been disabled.

Hi Ulrik,
I've tested the plugin on Moodle 4.4.10 and have not been able to reproduce the error. The stack trace indicates that the failure occurs within Moodle core’s grade deletion process, specifically in the sequence enrol_cohort → grade_user_unenrol() → grade_grade->delete() and is unrelated to the enrol_groupsync observer. The groupsync handler only removes the user from synced groups after the cohort removal event triggers, without affecting the grade record deletion path. Note that enrol_cohort plugin also has its own observer for the cohort_member_removed event, which is the one referenced in frame #4.
Let me know if I can assist with further debugging or tracing.
We have a customer who are using this plugin and they have some issues.
When we call the webservice core_cohort_delete_cohort_members the service calls an event:
[
'eventname' => '\core\event\cohort_member_removed',
'callback' => 'enrol_groupsync_observer::cohort_member_removed',
],
The enrol_groupsync listens to that event and runs the following code:
public static function cohort_member_removed(\core\event\cohort_member_removed $event) {
global $DB, $CFG;
require_once($CFG->dirroot.'/group/lib.php');
if (!enrol_is_enabled('groupsync')) {
return true;
}
$sql = "SELECT DISTINCT e.id, e.customint2
FROM {enrol} e
JOIN {groups} g ON (g.id = e.customint2 AND g.courseid = e.courseid)
JOIN {groups_members} gm ON (gm.userid = :userid AND gm.groupid = g.id AND gm.component = 'enrol_groupsync'
AND gm.itemid = e.id)
WHERE e.customint1 = :cohortid AND e.enrol = 'groupsync'
ORDER BY e.id ASC";
$instances = $DB->get_records_sql($sql, ['cohortid' => $event->objectid, 'userid' => $event->relateduserid]);
foreach ($instances as $instance) {
groups_remove_member($instance->customint2, $event->relateduserid);
}
return true;
}
Which fails:
#0 /var/www/html/moodle/lib/dml/moodle_transaction.php(105): moodle_database->rollback_delegated_transaction(Object(moodle_transaction), Object(moodle_exception))
#1 /var/www/html/moodle/lib/grade/grade_grade.php(1160): moodle_transaction->rollback(Object(moodle_exception))
#2 /var/www/html/moodle/lib/gradelib.php(1577): grade_grade->delete('...')
#3 /var/www/html/moodle/lib/enrollib.php(2338): grade_user_unenrol('...', '...')
#4 /var/www/html/moodle/enrol/cohort/locallib.php(110): enrol_plugin->unenrol_user(Object(stdClass), '...')
#5 [internal function]: enrol_cohort_handler::member_removed(Object(core\event\cohort_member_removed))
#6 /var/www/html/moodle/lib/classes/event/manager.php(155): call_user_func('...', Object(core\event\cohort_member_removed))
#7 /var/www/html/moodle/lib/classes/event/manager.php(75): core\event\manager::process_buffers()
#8 /var/www/html/moodle/lib/classes/event/base.php(795): core\event\manager::dispatch(Object(core\event\cohort_member_removed))
#9 /var/www/html/moodle/cohort/lib.php(230): core\event\base->trigger()
#10 /var/www/html/moodle/cohort/externallib.php(789): cohort_remove_member('...', '...')
#11 /var/www/html/moodle/webservice/lib.php(1504): core_cohort_external::delete_cohort_members(Array)
#12 /var/www/html/moodle/webservice/lib.php(1350): webservice_base_server->execute()
#13 /var/www/html/moodle/webservice/rest/server.php(44): webservice_base_server->run()
As you see in line two it has something to do with deleting grades. It only happens to some users in some cohorts and if you unenrol them through Moodle, it doesn't fail and afterwards you are again able to call the webservice with no errors.
We are using Moodle 4.4.10 (to be upgraded 6 Jan. 2026 to Moodle 5.0.3)
Is this something you could have a look at.
Kind regards,
Ulrik McArdle, Moxis
Premium partner
Hello, I am having issues and not sure if it is the plugin or cohort sync. I remove user from cohort_members and group_members after cron executes it adds the user back to group_members. Is this a bug or must I add some settings? I use cohort_remove_members moodle function

According to https://moodle.org/plugins/enrol_groupsync/stats there are some 4.2 sites having the plugins installed. It still needs to be tested on new PHP versions. Any help and feedback will be welcome.

Hi There, this is a great plugin in - we use it a lot. I was wondering if this plugin will be compatible with PHP 8 and Moodle 4.2? We really need it - nothing like it for grouping lots of people quickly and easily.
Hi David, just to clarify- this can be used with Cohort Sync?

Hi Learning Works. Sorry, I have no idea what you are referring to? That field is part of the self enrolment form AFAIK. This plugin does not touch any enrolment at all.

Hi. what happened to the 'Max enrolled users' field in the cohort sync form?
@Robert Yes I'm basically duplicating rows within the enrol table, as David says this is generally discouraged as altering the tables can mess everything up and render your site unusable. Unfortunately there's no way to do it easily because the id for groups within each course is unique, even though they might have the same name. Duplicating the rows still require the "customint2" value to be changed to match the corresponding id within the groups table. I have resorted to writing my own scripts to do this laborious process instead.

Hi Robert. There is no UI for that. I suppose that Chye made some low-level operations directly in the enrol table, which is something I'd generallyt discourage from doing so, unless you know what you're doing.

Hi David, is it possible to transfer enrolment-definitions made for one course to another? Chye's speaking about 'duplicating database entries' - but could you please explain about how to do this? Thanks a lot smile

That's weird that the CLI script did not work for you. Have you tried to run it in the verbose mode?

Thanks for the quick reply David. Is there a way to manually trigger the groupsync? To duplicate it across dozens of courses, I've basically duplicated the database entries with corresponding data. This works fine (I've done and double-checked it), I just needed to trigger the grouping but running "moodle/enrol/groupsync/cli/sync.php" doesn't seem to always trigger the grouping. Turning one of the enrolment off and back on again does trigger it for that specific course, however, but just wondered if there's a way without having to do it for every course.

Hi Chye Siaw. The plugin makes use of observing events that trigger when the user is enrolled to the course or added to a cohort.

Hi David, when is the plugin triggered? We have some students in cohorts but weren't put into the groups in some courses.