Take a look at groups_get_course_group() in lib/grouplib.php. It "Returns group active in course, changes the group by default if 'group' page param present".
Like in this patch
Look at the code of the function, now the part
$changegroup = optional_param('group', -1, PARAM_INT);
appears to be done inside the function itself.