public static function get_course_user_groups($courseid = 0, $userid = 0, $groupingid = 0) {
        global $USER;

        // Warnings array, it can be empty at the end but is mandatory.
        $warnings = array();

        $params = array(
            'courseid' => $courseid,
            'userid' => $userid,
            'groupingid' => $groupingid
        );
        $params = self::validate_parameters(self::get_course_user_groups_parameters(), $params);

        $courseid = $params['courseid'];
        $userid = $params['userid'];
        $groupingid = $params['groupingid'];

        // Validate user.
        if (empty($userid)) {
            $userid = $USER->id;
        } else {
            $user = core_user::get_user($userid, '*', MUST_EXIST);
            core_user::require_active_user($user);
        }

        // Get courses.
        if (empty($courseid)) {
            $courses = enrol_get_users_courses($userid, true);
            $checkenrolments = false;   // No need to check enrolments here since they are my courses.
        } else {
            $courses = array($courseid => get_course($courseid));
            $checkenrolments = true;
        }

        // Security checks.
        list($courses, $warnings) = external_util::validate_courses(array_keys($courses), $courses, true);

        $usergroups = array();
        foreach ($courses as $course) {
             // Check if we have permissions for retrieve the information.
            if ($userid != $USER->id && !has_capability('moodle/course:managegroups', $course->context)) {
                $warnings[] = array(
                    'item' => 'course',
                    'itemid' => $course->id,
                    'warningcode' => 'cannotmanagegroups',
                    'message' => "User $USER->id cannot manage groups in course $course->id",
                );
                continue;
            }

            // Check if the user being check is enrolled in the given course.
            if ($checkenrolments && !is_enrolled($course->context, $userid)) {
                // We return a warning because the function does not fail for not enrolled users.
                $warnings[] = array(
                    'item' => 'course',
                    'itemid' => $course->id,
                    'warningcode' => 'notenrolled',
                    'message' => "User $userid is not enrolled in course $course->id",
                );
            }

            $groups = groups_get_all_groups($course->id, $userid, $groupingid,
                'g.id, g.name, g.description, g.descriptionformat, g.idnumber');

            foreach ($groups as $group) {
                list($group->description, $group->descriptionformat) =
                    external_format_text($group->description, $group->descriptionformat,
                            $course->context->id, 'group', 'description', $group->id);
                $group->courseid = $course->id;
                $usergroups[] = $group;
            }
        }

        $results = array(
            'groups' => $usergroups,
            'warnings' => $warnings
        );
        return $results;
    }