You are here

function Course::enroll in Course 3.x

Same name and namespace in other branches
  1. 8.3 src/Entity/Course.php \Drupal\course\Entity\Course::enroll()
  2. 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\Entity

Code

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;
}