Multi-tenancy is the ability to have multiple virtual Moodle sites running on one copy of Moodle.

Example use case: A commercial training company create Moodle courses and wants to sell training services to multiple customers (eg companies). They do not want to run a whole Moodle for every customer because they want easy management and reporting, yet they also don’t want users from one customer seeing users from another.

Each customer can manage their own users, as well as having a custom FrontPage and a custom theme, enabling them to have what seems like a whole Moodle site each.

List of changes

Database changes

  • tenantid required in tables: user, course, course_category, sessions, log, context, course_request, event, cohort
  • most probably will be required also in: blog, tag, scale and ws token table

Tenant setting overrides

Context changes and access control

  • hardcoded restrictions in has_capability() and similar methods — tenant can not have any capabilities outside of their part of the context tree.
  • hardcoded restrictions in require_login()
  • at he same time we could create a new ‘context’ class
  • one tenant user may access only one tenant site, global user may access only global site


At the same time we should probably rework the PAGE setup:

$PAGE->set_url(‘/mod/page/view.php’, array(‘id’=>$id);

list($page, $cm, $context, $course) = $PAGE->init_module_by_id(‘mod_page’, $id);





Admin settings and UI

Experimental setting: Multi-tenant — no, yes, separate ($CFG->enablemultitenant) 0,1,2


The only problem here is the uniqueness of usernames and emails. One of the simpler solutions would be to enforce username uniqueness with a regex patters in the tenant table — you would be allowed to create new user only if the username matches the regex (for example ‘sk1_.*’ for “school 1”).

The email does not have to be unique because we do not use it as an identifier, the only trouble is password resetting. I think I have found a solution: if you find multiple accounts with the same email address we can add extra password reset step email — ask user to choose which account to reset. This should be simple and 100% reliable


Tenant migration to different site


  • disable blogs completely for tenants — easy
  • add tenantid to blocks — tricky to separate these, it could probably work with different tenant domains only


Article reference from — Moodle

Dave Chirag represents Vibidsoft, a Web and Mobile Application development company providing enterprise IT solutions comprising the full range of services