You are here

final public function CourseObject::takeObject in Course 8.3

Same name and namespace in other branches
  1. 8.2 src/Entity/CourseObject.php \Drupal\course\Entity\CourseObject::takeObject()
  2. 3.x src/Entity/CourseObject.php \Drupal\course\Entity\CourseObject::takeObject()

Take a course object.

  • Set the session of this course object being taken. This allows for non-node objects to be tracked.
  • Delegate the course object take functionality

Return value

mixed HTML content or a redirect.

File

src/Entity/CourseObject.php, line 473

Class

CourseObject
Parent abstract base class of all course objects.

Namespace

Drupal\course\Entity

Code

public final function takeObject() {
  $account = Drupal::currentUser();
  $_SESSION['course']['active'] = $this
    ->getCourse()
    ->id();
  $_SESSION['course'][$this
    ->getCourse()
    ->id()]['taking']['active'] = $this
    ->getId();

  // Run access checks.
  if ($this
    ->access('take')) {

    // Grant access to external course object.
    $this
      ->getFulfillment($account)
      ->grant();

    // Record start date.
    $this
      ->getFulfillment($account)
      ->save();
  }
  else {

    // User can't access this object, revoke access.
    $this
      ->getFulfillment($account)
      ->revoke();
    return FALSE;
  }

  // If we're not displaying any content but we want to fire take() anyway, to
  // let the course object know we sent the user.
  $out = $this
    ->take();
  $url = $this
    ->getTakeUrl();
  switch ($this
    ->getTakeType()) {
    case 'iframe':
      return course_iframe($url);
    case 'popup':
      return "will popup {$url}";
    case 'content':
      return $out;
    case 'redirect':
    default:

      // This URL should have already been url()'d (it might be external).
      return new RedirectResponse($url
        ->toString());
  }
}