You are here

function Course::enroll in Course 8.3

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

File

src/Entity/Course.php, line 396

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