You are here

public function DefaultTwigExtension::get_start_link in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/TwigExtension/DefaultTwigExtension.php \Drupal\opigno_learning_path\TwigExtension\DefaultTwigExtension::get_start_link()

Returns group start link.

Parameters

mixed $group: Group.

array $attributes: Attributes.

Return value

array|mixed|null Group start link or empty.

File

src/TwigExtension/DefaultTwigExtension.php, line 221

Class

DefaultTwigExtension
Class DefaultTwigExtension.

Namespace

Drupal\opigno_learning_path\TwigExtension

Code

public function get_start_link($group = NULL, array $attributes = []) {
  if (!$group) {
    $group = \Drupal::routeMatch()
      ->getParameter('group');
  }
  if (filter_var($group, FILTER_VALIDATE_INT) !== FALSE) {
    $group = Group::load($group);
  }
  if (empty($group) || !is_object($group) || is_object($group) && $group
    ->bundle() !== 'learning_path') {
    return [];
  }
  $args = [];
  $current_route = \Drupal::routeMatch()
    ->getRouteName();
  $visibility = $group->field_learning_path_visibility->value;
  $account = \Drupal::currentUser();
  $is_anonymous = $account
    ->id() === 0;
  if ($is_anonymous && $visibility != 'public') {
    if ($visibility != 'semiprivate' && (!$group
      ->hasField('field_lp_price') || $group
      ->get('field_lp_price')->value == 0)) {
      return [];
    }
  }

  // Check if we need to wait validation.
  $validation = LearningPathAccess::requiredValidation($group, $account);
  $member_pending = !LearningPathAccess::statusGroupValidation($group, $account);
  $module_commerce_enabled = \Drupal::moduleHandler()
    ->moduleExists('opigno_commerce');
  $required_trainings = LearningPathAccess::hasUncompletedRequiredTrainings($group, $account);
  if ($module_commerce_enabled && $group
    ->hasField('field_lp_price') && $group
    ->get('field_lp_price')->value != 0 && !$group
    ->getMember($account)) {

    // Get currency code.
    $cs = \Drupal::service('commerce_store.current_store');
    $store_default = $cs
      ->getStore();
    $default_currency = $store_default ? $store_default
      ->getDefaultCurrencyCode() : '';
    $text = t('Add to cart') . ' / ' . $group
      ->get('field_lp_price')->value . ' ' . $default_currency;
    $route = 'opigno_commerce.subscribe_with_payment';
  }
  elseif ($visibility === 'public' && $is_anonymous) {
    $text = t('Start');
    $route = 'opigno_learning_path.steps.start';
    $attributes['class'][] = 'use-ajax';
    $attributes['class'][] = 'start-link';
  }
  elseif (!$group
    ->getMember($account) || $is_anonymous) {
    if ($group
      ->hasPermission('join group', $account)) {
      if ($current_route == 'entity.group.canonical') {
        $text = $validation ? t('Request subscription to the training') : t('Subscribe to training');
      }
      else {
        $text = t('Learn more');
      }
      $route = $current_route == 'entity.group.canonical' ? 'entity.group.join' : 'entity.group.canonical';
      if ($current_route == 'entity.group.canonical') {
        $attributes['class'][] = 'join-link';
      }
    }
    elseif ($visibility === 'semiprivate' && $is_anonymous) {
      if ($current_route == 'entity.group.canonical') {
        $text = t('Create an account and subscribe');
        $route = 'user.login';
        $args += [
          'prev_path' => Url::fromRoute('entity.group.canonical', [
            'group' => $group
              ->id(),
          ])
            ->toString(),
        ];
      }
      else {
        $text = t('Learn more');
        $route = 'entity.group.canonical';
      }
    }
    else {
      return '';
    }
  }
  elseif ($member_pending || $required_trainings) {
    $text = $required_trainings ? t('Prerequisites Pending') : t('Approval Pending');
    $route = 'entity.group.canonical';
    $attributes['class'][] = 'approval-pending-link';
  }
  else {
    $text = opigno_learning_path_started($group, $account) ? t('Continue training') : t('Start');
    $route = 'opigno_learning_path.steps.type_start';
    $attributes['class'][] = 'use-ajax';
    if (opigno_learning_path_started($group, $account)) {
      $attributes['class'][] = 'continue-link';
    }
    else {
      $attributes['class'][] = 'start-link';
    }
  }
  $type = $current_route === 'view.opigno_training_catalog.training_catalogue' ? 'catalog' : 'group';
  $args += [
    'group' => $group
      ->id(),
    'type' => $type,
  ];
  $url = Url::fromRoute($route, $args, [
    'attributes' => $attributes,
  ]);
  $l = Link::fromTextAndUrl($text, $url)
    ->toRenderable();
  return render($l);
}