Someone just messaged me asking how to set up multiple Moodles from a single code base, rather than reply on PM I thought I would post it here.
The idea is to run more than one Moodle instance from a single codebase. We do this so that different faculties can be configured differently. It also means individual Moodle instances are smaller, backups are segmented so its easier to restore a single faculty should dissaster strike.
There are some caveats to doing this:
1) If your organisational structure changes you cannot simply rename / move categories as courses exist in different moodles.
2) Users accessing more than one system essentially end up with separate accounts on each of them.
That is not generally an issue for us as both staff and students tend to work only in their faculty instance.
We also have SSO implemented so moving between systems is seamless and if you hit a "new" moodle account creation is automatic.
To implement this you need toparse the URL for (in our case) the faculty name / shortcode.
We used to do this with subdomains like:
facultyone.ourdomain.ac.uk
facultytwo.ourdomain.ac.uk
But later switched to:
ourdomain.ac.uk/ (a central Moodle that sits above the others for generic courses / staff training etc)
ourdomain.ac.uk/fac1 (faculty moodle)
ourdomain.ac.uk/fac2 (faculty moodle)
Clearly you need to make sure you do not use existing Moodle urls like:
ourdomain.ac.uk/course
ourdomain.ac.uk/my (my moodle page)
It will be the second method I explain below.
The only file you need to edit is config.php
First we set default values as normal in config.php, these are set with our top level instance which sits on the root domain:
$CFG->wwwroot = 'ourdomain.ac.uk';
$CFG->dataroot = '/moodledata';
$CFG->dbname = 'default';
Then we grab the requested URL and split it out based on "/"
$url_array=explode("/",$_SERVER['REQUEST_URI']);
Then we grab the second segment (zero being the first):
$faculty_code = $url_array[1];
(If you were using sub domains you would explode on "." and extract the sub domain rather than a URL segment)
so where the url is ourdomain.com/fac1/
$faculty_code would be "fac1"
Next we define an array of allowed shortcodes to ensure we only handle the correct codes. We do this in $CFG as we re-use these values in themes, custom navigation and jump URLs for MNET:
$CFG->facs = array('fac1', 'fac2', 'fac3');
Then we test against this array and if we find a match set parameters accordingly:
foreach ($CFG->facs as $fac){
if($faculty_code == $fac)
{
$CFG->wwwroot = $CFG->wwwroot . "/".$faculty_code;
$CFG->dataroot = '/moodledatafor' . $faculty_code;
$CFG->dbname = 'databasefor' . $faculty_code;
}
}
(You could test with "in_array()" before entering the above)
So if someone hits ourdomain.ac.uk/my they hit my moodle on our central instance using the default values.
If they hit one of the /fac1/ short codes we load a different database and moodledata directory.
If you are using subdomains things are a little different as you dont have to worry so much about people hitting erroneous urls as you can only access a subdomain which has been added in the webserver, though it would still be a good idea to limit them in an array if you are able to.
Once upon a time we used to have only one database and set the prefix i.e. change mdl_ to fac1_ etc, that is not a good idea as Moodle's database is pretty substantial, you really need to use separate DB's.
I hope that's useful some of you