From: Frederic Massart Date: Wed, 18 Dec 2013 07:28:09 +0000 (+0100) Subject: Added script to enrol users X-Git-Tag: v1.0~29 X-Git-Url: https://git.cameron1729.xyz/?a=commitdiff_plain;h=444f5d427f7bd5eeba1637cf47b3cc4b25fe06d3;p=mdk.git Added script to enrol users --- diff --git a/scripts/enrol.php b/scripts/enrol.php new file mode 100644 index 0000000..4c24912 --- /dev/null +++ b/scripts/enrol.php @@ -0,0 +1,73 @@ +libdir . '/accesslib.php'); +require_once($CFG->dirroot . '/enrol/manual/lib.php'); + +function mdk_get_enrol_instance($courseid) { + global $DB; + static $coursecache = array(); + if (!isset($coursecache[$courseid])) { + $coursecache[$courseid] = $DB->get_record('enrol', array('courseid' => $courseid, 'enrol' => 'manual')); + } + return $coursecache[$courseid]; +} + +function mdk_get_role($username) { + static $rolecache = array(); + $letter = substr($username, 0, 1); + switch ($letter) { + case 's': + $archetype = 'student'; + break; + case 't': + $archetype = 'editingteacher'; + break; + case 'm': + $archetype = 'manager'; + break; + default: + return false; + } + if (!isset($rolecache[$archetype])) { + $role = get_archetype_roles($archetype); + $rolecache[$archetype] = reset($role); + } + return $rolecache[$archetype]; +} + +$sql = "SELECT id, username + FROM {user} + WHERE username LIKE 's%' + OR username LIKE 't%' + OR username LIKE 'm%'"; +$users = $DB->get_recordset_sql($sql, array()); +$courses = $DB->get_records_select('course', 'id > ?', array(1), '', 'id, startdate'); +$plugin = new enrol_manual_plugin(); + +foreach ($users as $user) { + mtrace('Enrolling ' . $user->username); + $role = mdk_get_role($user->username); + if (!$role) { + continue; + } + foreach ($courses as $course) { + $instance = mdk_get_enrol_instance($course->id); + // Enrol the day before the course startdate, because if we create a course today its default + // startdate is tomorrow, and we would never realise why the enrolments do not work. + $plugin->enrol_user($instance, $user->id, $role->id, $course->startdate - 86400, 0); + } +} + +$users->close();