3 * Enrol users in all the courses.
5 * Users with username starting with:
10 * In every single course on the site.
13 define('CLI_SCRIPT', true
);
14 require(__DIR__
. '/config.php');
15 require_once($CFG->libdir
. '/accesslib.php');
16 require_once($CFG->dirroot
. '/enrol/manual/lib.php');
18 function mdk_get_enrol_instance($courseid) {
20 static $coursecache = array();
21 if (!isset($coursecache[$courseid])) {
22 $coursecache[$courseid] = $DB->get_record('enrol', array('courseid' => $courseid, 'enrol' => 'manual'));
23 if (!$coursecache[$courseid]) {
24 mtrace("Could not find manual enrolment method for course {$courseid}.");
27 return $coursecache[$courseid];
30 function mdk_get_role($username) {
31 static $rolecache = array();
32 $letter = substr($username, 0, 1);
35 $archetype = 'student';
38 $archetype = 'editingteacher';
41 $archetype = 'manager';
46 if (!isset($rolecache[$archetype])) {
47 $role = get_archetype_roles($archetype);
48 $rolecache[$archetype] = reset($role);
50 return $rolecache[$archetype];
53 $sql = "SELECT id, username
55 WHERE (username LIKE 's%'
57 OR username LIKE 'm%')
59 AND username NOT LIKE 'tool_generator_%'";
60 $users = $DB->get_recordset_sql($sql, array());
61 $courses = $DB->get_records_select('course', 'id > ?', array(1), '', 'id, startdate');
62 $plugin = new enrol_manual_plugin();
64 foreach ($users as $user) {
65 mtrace('Enrolling ' . $user->username
);
66 $role = mdk_get_role($user->username
);
70 foreach ($courses as $course) {
71 $instance = mdk_get_enrol_instance($course->id
);
75 // Enrol the day before the course startdate, because if we create a course today its default
76 // startdate is tomorrow, and we would never realise why the enrolments do not work.
77 $plugin->enrol_user($instance, $user->id
, $role->id
, $course->startdate
- 86400, 0);