by Darko Miletić.
Query for this :
SELECT COUNT(DISTINCT ra.userid)
FROM
mdl_role_assignments ra,
mdl_context ct,
mdl_config mc,
mdl_user u
WHERE
ra.contextid = ct.id
AND
ra.userid = u.id
AND
u.deleted = 0
AND
mc.name = 'gradebookroles'
AND
ra.roleid IN (mc.value)
AND
ct.contextlevel = 50
AND
ct.instanceid = <my course id>
If you want to use standard Moodle API from PHP code than you have this:
get_users_from_role_on_context but it does not do all of the checks as the above query does.