You are here

function CourseObjectAccessConditional::take in Course 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessConditional::take()
  2. 3.x src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessConditional::take()

Overrides CourseObjectAccessPluginBase::take

1 call to CourseObjectAccessConditional::take()
CourseObjectAccessConditional::view in src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php

File

src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php, line 26

Class

CourseObjectAccessConditional
Plugin annotation @CourseObjectAccess( id = "conditional", label = @Translation("Conditional"), )

Namespace

Drupal\course\Plugin\course\CourseObjectAccess

Code

function take($account) {
  $type = $this
    ->getOption('conditional_type');
  $offset = (int) $this
    ->getOption('conditional_time');
  $time = \Drupal::time()
    ->getRequestTime();
  $access = TRUE;
  $title = $this
    ->getCourseObject()
    ->getTitle();
  $msg = t('%title is not yet available.', array(
    '%title' => $title,
  ));
  if ($type) {
    foreach ($this
      ->getCourseObject()
      ->getCourse()
      ->getObjects() as $courseObject) {
      if ($courseObject
        ->getId() == $this
        ->getOption('conditional_object')) {
        switch ($type) {
          case 'completed':
            $completed = $courseObject
              ->getFulfillment($account)
              ->getOption('date_completed');
            if (!$completed) {
              $access = FALSE;
            }
            else {
              $date = $completed + $offset;
              $access = $time >= $date;
              $msg = t('%title will be available on %date', array(
                '%title' => $title,
                '%date' => \Drupal::service('date.formatter')
                  ->format($date, 'short'),
              ));
            }
            break;
          case 'started':
            $started = $courseObject
              ->getFulfillment($account)
              ->getOption('date_started');
            if (!$started) {
              $access = FALSE;
            }
            else {
              $date = $started + $offset;
              $access = $time >= $date;
              $msg = t('%title will be available on %date', array(
                '%title' => $title,
                '%date' => \Drupal::service('date.formatter')
                  ->format($date, 'short'),
              ));
            }
            break;
        }
      }
    }
  }
  if (!$access) {
    $this
      ->getCourseObject()
      ->setAccessMessage('conditional', $msg);
  }
  return $access;
}