From 3714b3d104c47f2a082b7f7a7085d0e82dfda227 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 25 May 2018 11:40:26 +0800 Subject: [PATCH] MDL-55609 testing: Add a create_and_enrol helper --- lib/testing/generator/data_generator.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 5dc5e4ca83d..b52fa58da65 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -1163,4 +1163,24 @@ EOD; return $event->properties(); } + + /** + * Create a new user, and enrol them in the specified course as the supplied role. + * + * @param \stdClass $course The course to enrol in + * @param string $role The role to give within the course + * @param \stdClass $userparams User parameters + * @return \stdClass The created user + */ + public function create_and_enrol($course, $role = 'student', $userparams = null, $enrol = 'manual', + $timestart = 0, $timeend = 0, $status = null) { + global $DB; + + $user = $this->create_user($userparams); + $roleid = $DB->get_field('role', 'id', ['shortname' => $role ]); + + $this->enrol_user($user->id, $course->id, $roleid, $enrol, $timestart, $timeend, $status); + + return $user; + } }