What the hell has changed in schools in the last 23 years since I was at one..
We had a system, one pupil released to the toilet at a time, if someone else wanted to go they had to wait til the previous one was back.
Everyone reminded regularly to go at break and lunchtime.
If you do that, you have no gatherings in loos during lessons as only one can go at once.
If you need more control - each kid has a toilet pass that is seen and signed and the time out and back in noted, so each teacher can SEE when, how often and how long that kid is spending in the toilet during lessons, then its easy to spot the piss takers.
Our school briefly trialed locking loos in lesson times - but the issue was often loos would then not be unlocked at break (what with teachers being too busy to go unlock them) and then MORE kids needing the loo in lesson times.
The time between lessons was not sufficient in most cases, to go to the toilet and do what you needed, particularly if your lesson was not near one of the available toilets - in my school it was barely enough time to get from one end of the building to the other, so really it was ONLY morning break and lunchtime you could go.
Surely, if you have within a school, a SERIOUS problem of kids misusing the toilets and avoiding lessons, ... so serious that it is genuinely worth forcing kids to piss themselves or bleed through clothing... then you have a FAR bigger problem and the school is really failing quite dramatically.