I'm thinking a firewall rule on the AP that the teacher is connected could block everybody's internet access, but, what about the teacher itself or other APs nearby?
You can use chatGPT, Gemini, Copilot, to ask for the code and create something like a webapp to achieve what you want, i faced something similar (another use case) and worked..
The challenge here is to determine who and how you block...
If students have one SSID and teacher another, you could ask for a code to change the availability of the students SSID. The teacher would have a screen to select wich classroom their are (and as such, the AP there) and for how long they want the student SSID disabled.
But this does not achieve the wifi connectivity you want to keep. Any requirements for such connectivity (local, to the classroom it self? airplay?)
Hope that helps with a direction... I don't think there's an app ready for that..