You are here

public function OpignoModule::checkModuleAvailability in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Entity/OpignoModule.php \Drupal\opigno_module\Entity\OpignoModule::checkModuleAvailability()

Checks module availability.

File

src/Entity/OpignoModule.php, line 362

Class

OpignoModule
Defines the Module entity.

Namespace

Drupal\opigno_module\Entity

Code

public function checkModuleAvailability() {
  $availability = [
    'open' => TRUE,
    'message' => '',
  ];
  $module_availability = 0;
  $group_id = OpignoGroupContext::getCurrentGroupId();
  $lp_module_availability = LPModuleAvailability::loadByProperties([
    'group_id' => $group_id,
    'entity_id' => $this
      ->id(),
  ]);
  if ($lp_module_availability) {
    $lp_module_availability = current($lp_module_availability);
    $module_availability = $lp_module_availability
      ->getAvailability();
  }
  if ($module_availability) {
    $open_date = $lp_module_availability
      ->getOpenDate();
    $close_date = $lp_module_availability
      ->getCloseDate();
    if ($open_date && $close_date) {
      $quiz_open = \Drupal::time()
        ->getRequestTime() >= $open_date;
      $quiz_closed = \Drupal::time()
        ->getRequestTime() >= $close_date;
    }
    if (isset($quiz_open) && isset($quiz_closed)) {
      if (!$quiz_open || $quiz_closed) {
        $message = '';

        // Load Config.
        $config = \Drupal::config('opigno_module.settings');
        if ($quiz_closed) {
          $message = $config
            ->get('availability_closed_message');
        }
        elseif (!$quiz_open) {
          $message = $config
            ->get('availability_unavailable_message');
        }
        if (\Drupal::moduleHandler()
          ->moduleExists('token')) {
          $message = \Drupal::token()
            ->replace($message, [
            'opigno_module' => $this,
          ]);
        }
        $availability = [
          'open' => FALSE,
          'message' => $message,
        ];
      }
    }
  }
  return $availability;
}