2803fe38ad57cc9981ead7ca362abe7c5e5d7622
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'));
24 return $coursecache[$courseid];
27 function mdk_get_role($username) {
28 static $rolecache = array();
29 $letter = substr($username, 0, 1);
32 $archetype = 'student';
35 $archetype = 'editingteacher';
38 $archetype = 'manager';
43 if (!isset($rolecache[$archetype])) {
44 $role = get_archetype_roles($archetype);
45 $rolecache[$archetype] = reset($role);
47 return $rolecache[$archetype];
50 $sql = "SELECT id, username
52 WHERE (username LIKE 's%'
54 OR username LIKE 'm%')
56 AND username NOT LIKE 'tool_generator_%'";
57 $users = $DB->get_recordset_sql($sql, array());
58 $courses = $DB->get_records_select('course', 'id > ?', array(1), '', 'id, startdate');
59 $plugin = new enrol_manual_plugin();
61 foreach ($users as $user) {
62 mtrace('Enrolling ' . $user->username
);
63 $role = mdk_get_role($user->username
);
67 foreach ($courses as $course) {
68 $instance = mdk_get_enrol_instance($course->id
);
69 // Enrol the day before the course startdate, because if we create a course today its default
70 // startdate is tomorrow, and we would never realise why the enrolments do not work.
71 $plugin->enrol_user($instance, $user->id
, $role->id
, $course->startdate
- 86400, 0);