MDL-66032 and, looking in context... when it's checked that ALL the badges have been required (BADGE_CRITERIA_AGGREGATION_ALL)... there is something looking really wrong, because the $where variable is overwritten on each looping execution.
I've not tested it, but it seems obvious that:
1) it leads to incomplete SQL command (only last badge is checked effectively).
2) it's not covered with tests.
So this issue is about to achieve 1) and 2) above. Ciao