function Course::enroll in Course 3.x
Same name and namespace in other branches
- 8.3 src/Entity/Course.php \Drupal\course\Entity\Course::enroll()
- 8.2 src/Entity/Course.php \Drupal\course\Entity\Course::enroll()
Enroll a user in this course.
Parameters
AccountInterface $account: The user to enroll.
array $values: Any other entity values as they would be passed to CourseEnrollment::create().
Return value
CourseEnrollment A saved CourseEnrollment.
File
- src/
Entity/ Course.php, line 456
Class
- Course
- Defines the Course entity class.
Namespace
Drupal\course\EntityCode
function enroll(AccountInterface $account, $values = []) {
if (!($enrollment = $this
->getEnrollment($account))) {
// User is not enrolled yet.
$enrollment = CourseEnrollment::create([
'cid' => $this
->id(),
'uid' => $account
->id(),
] + $values);
$enrollment
->save();
}
return $enrollment;
}