In order to implement
MDL-648 (see http://docs.moodle.org/en/Development:How_to_create_a_patch for a spec), we need to get users who:
a) are in group $studentgroup and have capability mod/quiz:emailnotifysubmission in the context of a particular quiz, or
b) are in any groups, and have capabilities mod/quiz:emailnotifysubmission and moodle/site:accessallgroups in the context of a particular quiz.
You could do that with three calls to get_users_by_capability, then combine the results in PHP.
Alternatively, you could change get_users_by_capability to optionally do the "in group $groups, or in any group and has moodle/site:accessallgroups". The attached patch does that.
I have no idea what effect the attached patch will have on performance (when $useviewallgroups is false, when it is true, things will certainly be slower).
The patch applies to Moodle 1.8 or HEAD.