You are here

function opigno_ilt_preprocess_page in Opigno Instructor-led Trainings 8

Same name and namespace in other branches
  1. 3.x opigno_ilt.module \opigno_ilt_preprocess_page()

Implements hook_preprocess_page().

Adds upcoming instructor-led training banner to the training pages.

File

./opigno_ilt.module, line 375
Contains opigno_ilt.module.

Code

function opigno_ilt_preprocess_page(&$variables) {
  $training_routes = [
    'entity.group.canonical',
    'opigno_module.group.answer_form',
    'opigno_module.module_result',
  ];
  $route = \Drupal::routeMatch();
  $route_name = $route
    ->getRouteName();
  if (!in_array($route_name, $training_routes)) {
    return;
  }

  /** @var \Drupal\group\Entity\GroupInterface $group */
  $group = $route
    ->getParameter('group');
  if (!isset($group)) {
    $group_id = OpignoGroupContext::getCurrentGroupId();
    if (!isset($group_id)) {
      return;
    }
    $group = Group::load($group_id);
  }
  $bundle = $group
    ->bundle();
  if ($bundle !== 'learning_path') {
    return;
  }
  $user = \Drupal::currentUser();
  $user_id = $user
    ->id();
  $steps = opigno_learning_path_get_steps($group
    ->id(), $user_id);
  $ilt_steps = array_filter($steps, function ($step) {
    return $step['typology'] === 'ILT';
  });
  $ilt_ids = array_map(function ($step) {
    return $step['id'];
  }, $ilt_steps);

  /** @var \Drupal\opigno_ilt\ILTInterface[] $ilts */
  $ilts = ILT::loadMultiple($ilt_ids);
  foreach ($ilts as $ilt) {
    if (!$ilt
      ->isMember($user_id)) {
      continue;
    }
    $date_str = $ilt
      ->getStartDate();
    $date = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $date_str);
    $diff = $date
      ->getTimestamp() - time();

    // If instructor-led training is in two days.
    if ($diff > 0 && $diff < 60 * 60 * 24 * 2) {
      $title = $ilt
        ->getTitle();
      $link = Link::createFromRoute($title, 'entity.opigno_ilt.canonical', [
        'opigno_ilt' => $ilt
          ->id(),
      ]);
      $variables['page']['top'][] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'ilt-banner',
          ],
        ],
        'content' => [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'ilt-banner-text',
            ],
          ],
          '#value' => t('Upcoming Instructor Led Training %training on %date !', [
            '%training' => $link
              ->toString(),
            '%date' => $date
              ->format('jS F Y - g:i A'),
          ]),
        ],
        '#attached' => [
          'library' => [
            'opigno_ilt/banner',
          ],
        ],
      ];
      break;
    }
  }
}