Was working on porting
MDL-23848 to head and came across a couple of bugs within get_enrolled_sql.
In this case it appears the SQL for making use of $groupid is broken, it is joining on group_members.id with $groupid, which of course only ever returns one record.
I assume it is meant to be joining on group_members.groupid and as this is a blocker I will commit the fix, but could you please review and make any required changes, I'd hate to have broken other areas with this commit.
Also worth noting I just created
MDL-24245 which you may want to look at at the same time