You are here

function opigno_moxtra_preprocess_page in Opigno Moxtra 8

Same name and namespace in other branches
  1. 3.x opigno_moxtra.module \opigno_moxtra_preprocess_page()

Implements hook_preprocess_page().

Adds upcoming live meeting banner to the training pages.

File

./opigno_moxtra.module, line 786
Contains opigno_moxtra.module.

Code

function opigno_moxtra_preprocess_page(&$variables) {
  $training_routes = [
    'entity.group.canonical',
    'opigno_module.group.answer_form',
    'opigno_module.module_result',
  ];
  $connector = \Drupal::service('opigno_moxtra.connector');
  if (!$connector
    ->checkSettings()) {
    return;
  }
  $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();
  if ($user
    ->isAnonymous()) {
    return;
  }
  $moxtra_api = _opigno_moxtra_get_moxtra_api();
  $steps = opigno_learning_path_get_steps($group
    ->id(), $user_id);
  $live_meeting_steps = array_filter($steps, function ($step) {
    return $step['typology'] === 'Meeting';
  });
  $live_meeting_ids = array_map(function ($step) {
    return $step['id'];
  }, $live_meeting_steps);

  /** @var \Drupal\opigno_moxtra\MeetingInterface[] $live_meetings */
  $live_meetings = Meeting::loadMultiple($live_meeting_ids);
  foreach ($live_meetings as $live_meeting) {
    $owner_id = $live_meeting
      ->getOwnerId();
    $session_key = $live_meeting
      ->getSessionKey();
    if (!empty($session_key)) {
      $info = $moxtra_api
        ->getMeetingInfo($owner_id, $session_key);
      $status = !empty($info['data']) ? $info['data']['status'] : FALSE;
      if ($status === 'SESSION_ENDED') {
        continue;
      }
    }
    $members_ids = $live_meeting
      ->getMembersIds();
    if (empty($members_ids)) {
      $training = $live_meeting
        ->getTraining();
      if (isset($training)) {
        $members_ids = array_map(function ($member) {

          /** @var \Drupal\group\GroupMembership $member */
          return $member
            ->getUser()
            ->id();
        }, $training
          ->getMembers());
      }
    }
    if (!in_array($user_id, $members_ids)) {
      continue;
    }
    $date_str = $live_meeting
      ->getStartDate();
    $date = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $date_str);
    $diff = $date
      ->getTimestamp() - time();

    // If live meeting is in two days.
    if ($diff > 0 && $diff < 60 * 60 * 24 * 2) {
      $title = $live_meeting
        ->getTitle();
      $link = Link::createFromRoute($title, 'opigno_moxtra.meeting', [
        'opigno_moxtra_meeting' => $live_meeting
          ->id(),
      ]);
      $variables['page']['top'][] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'moxtra-meeting-banner',
          ],
        ],
        'content' => [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'moxtra-meeting-banner-text',
            ],
          ],
          '#value' => t('Click here to join the live meeting %live_meeting!', [
            '%live_meeting' => $link
              ->toString(),
          ]),
        ],
        '#attached' => [
          'library' => [
            'opigno_moxtra/meeting',
          ],
        ],
      ];
      break;
    }
  }
}