*
* @param int $userid
* @param int $courseid
- * @param int $roleid optional role id, use only with manual plugin
+ * @param int|string $roleidorshortname optional role id or role shortname, use only with manual plugin
* @param string $enrol name of enrol plugin,
* there must be exactly one instance in course,
* it must support enrol_user() method.
* @param int $status (optional) default to ENROL_USER_ACTIVE for new enrolments
* @return bool success
*/
- public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual', $timestart = 0, $timeend = 0, $status = null) {
+ public function enrol_user($userid, $courseid, $roleidorshortname = null, $enrol = 'manual',
+ $timestart = 0, $timeend = 0, $status = null) {
global $DB;
+ // If role is specified by shortname, convert it into an id.
+ if (!is_numeric($roleidorshortname) && is_string($roleidorshortname)) {
+ $roleid = $DB->get_field('role', 'id', array('shortname' => $roleidorshortname), MUST_EXIST);
+ } else {
+ $roleid = $roleidorshortname;
+ }
+
if (!$plugin = enrol_get_plugin($enrol)) {
return false;
}
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
+ $user4 = $this->getDataGenerator()->create_user();
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user1->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
+ $result = $this->getDataGenerator()->enrol_user($user4->id, $course2->id, 'teacher', 'manual');
+ $this->assertTrue($result);
+ $this->assertTrue($DB->record_exists('user_enrolments',
+ array('enrolid' => $maninstance2->id, 'userid' => $user4->id)));
+ $this->assertTrue($DB->record_exists('role_assignments',
+ array('contextid' => $context2->id, 'userid' => $user4->id, 'roleid' => $teacherrole->id)));
+
$result = $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 0, 'manual');
$this->assertTrue($result);
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id)));