Wow, this seems convoluted. I kind of wish we could just query the database, joining the badges and groups tables, but that's probably beyond the scope of this.