You are here

function opigno_moxtra_preprocess_page in Opigno Moxtra 3.x

Same name and namespace in other branches
  1. 8 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 800
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();
      $url = Url::fromRoute('opigno_moxtra.meeting', [
        'opigno_moxtra_meeting' => $live_meeting
          ->id(),
      ]);
      $start_date = $live_meeting
        ->getStartDate();
      $end_date = $live_meeting
        ->getEndDate();
      $variables['page']['top'][] = [
        '#theme' => 'meeting_start_soon',
        '#link' => $url,
        '#title' => $title,
        '#start_date' => $start_date,
        '#end_date' => $end_date,
      ];
      break;
    }
  }
}