Moodle self enrolment for one school's email domain only

Moodle's self enrolment is open to every account or locked behind a key. Our free enrol_domain plugin lets people enrol only when their email is at your school's domain.

Drawing of an envelope going through a slot in a door, the door painted blue.

Short answer: core Moodle’s self enrolment lets every logged-in account join a course, or only those who know an enrolment key. Keys leak: one gets posted in a group chat and the course is open to everyone. Our free plugin, enrol_domain, lets people enrol themselves only when the email on their Moodle account is at a domain you list, such as school.edu.

This is the fourth plugin in our series: one plugin for each Moodle plugin type, each one teaching DevLMS Studio, our AI plugin builder, how that type works.

How it works

  • In the course: Participants > Enrolment methods > Add method > Email domain self enrolment, then list the domains, one per line.
  • Subdomains count: school.edu also lets in ana@students.school.edu. Look-alikes do not: school.edu.evil.com and notschool.edu are refused.
  • People at an allowed domain get an “Enrol me” button. Everyone else sees which domains are allowed, so nobody wonders why the button is missing.
  • You choose the role it gives (student by default). Teachers and managers can set it up.
  • The rule is checked again when the button is pressed, not only when the page is drawn.

The check trusts the email address on the Moodle account, so it fits sites where addresses are confirmed: Moodle’s email-based sign-up confirms new addresses, and “Email change confirmation” (on by default) stops anyone switching to an address they do not own. Core also has a cohort-only option in its own self enrolment; use that instead when your school already syncs cohorts. It supports Moodle 4.5 LTS to 5.3 and is GPL. The code is on GitHub. An admin enables it first under Site administration > Plugins > Enrolments > Manage enrol plugins.

How a Moodle enrolment plugin works

From Moodle 5.3’s enrol_plugin base class and the core self enrolment plugin:

  1. lib.php holds one class, enrol_NAME_plugin. Each course’s settings for the method live in spare columns of Moodle’s own enrol table (customtext1, roleid and so on), so the plugin needs no table of its own.
  2. use_standard_editing_ui() returns true and Moodle builds the settings page from your edit_instance_form(). Without edit_instance_validation(), Moodle prints a developer warning.
  3. Capabilities are named after the plugin. If the method lets teachers unenrol people, core looks for enrol/NAME:unenrol. Forget it and the unenrol icon never shows.
  4. enrol_page_hook() draws what a visitor sees on the course’s enrolment page. Moodle 5.0 added a newer page component with modal forms, but it does not exist in 4.5, so a plugin that supports both draws a plain button.

We tested it with PHPUnit (domain matching, subdomains, look-alikes, enrolling, refusing, disabled methods) and a Behat run where one user at the domain enrols and another is told why they cannot.

What Studio learned

The enrolment guide is now in DevLMS Studio’s builder: the class name core expects, the enrol table columns, the capability names, and the 4.5 versus 5.0 enrolment page.

Need your own way into courses, like enrolment after a payment or approval by a manager? Describe it in DevLMS Studio and watch it built and tested in a real Moodle.

Need a plugin like this for your own Moodle?

Describe it in DevLMS Studio and watch it built and tested in a real Moodle, or ask our team for a fixed price.

Open DevLMS Studio