public function DefaultTwigExtension::get_start_link in Opigno Learning path 3.x
Same name and namespace in other branches
- 8 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 233
Class
- DefaultTwigExtension
- Class DefaultTwigExtension.
Namespace
Drupal\opigno_learning_path\TwigExtensionCode
public function get_start_link($group = NULL, array $attributes = [], $one_button = FALSE) {
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() : '';
$top_text = $group
->get('field_lp_price')->value . ' ' . $default_currency;
$top_text = [
'#type' => 'inline_template',
'#template' => '<div class="top-text price">{{top_text}}</div>',
'#context' => [
'top_text' => $top_text ?? '',
],
];
$text = t('Buy');
$attributes['class'][] = 'btn-bg';
$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') : t('Enroll');
$attributes['class'][] = 'btn-bg';
$attributes['data-toggle'][] = 'modal';
$attributes['data-target'][] = '#join-group-form-overlay';
}
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) {
$route = 'entity.group.canonical';
if ($required_trainings) {
// Display only the icon for certain cases (for ex., on the catalog).
if ($one_button) {
$top_text = [
'#markup' => Markup::create('<i class="fi fi-rr-lock"></i>'),
];
}
else {
$links = [];
foreach ($required_trainings as $gid) {
$training = Group::load($gid);
$url = Url::fromRoute($route, [
'group' => $training
->id(),
]);
$link = Link::fromTextAndUrl($training
->label(), $url)
->toRenderable();
array_push($links, $link);
}
$top_text = $links;
$top_text = [
'#type' => 'inline_template',
'#template' => '<div class="top-text complete"><i class="fi fi-rr-lock"></i><div>{{"Complete"|t}}<br>{{top_text}}<br>{{"before"|t}}</div></div>',
'#context' => [
'top_text' => render($top_text) ?? '',
],
];
}
}
else {
// Display only the icon for certain cases (for ex., on the catalog).
if ($one_button) {
$top_text = [
'#markup' => Markup::create('<i class="fi fi-rr-menu-dots"></i>'),
];
}
else {
$top_text = [
'#type' => 'inline_template',
'#template' => '<div class="top-text approval"><i class="fi fi-rr-menu-dots"></i><div>{{top_text}}</div></div>',
'#context' => [
'top_text' => t('Approval Pending'),
],
];
}
}
$text = t('Start');
$attributes['class'][] = 'disabled';
$attributes['class'][] = 'approval-pending-link';
}
else {
$uid = $account
->id();
$expired = LPStatus::isCertificateExpired($group, $uid);
$is_passed = opigno_learning_path_is_passed($group, $uid, $expired);
$status_class = $is_passed ? 'passed' : 'pending';
switch ($status_class) {
case 'passed':
case 'failed':
$text = t('Restart');
if (!$one_button) {
$top_link_text = t('See result');
}
break;
default:
// Old implementation.
if (opigno_learning_path_started($group, $account)) {
if (!$one_button) {
$top_link_text = t('See progress');
}
$text = t('Continue training');
}
else {
$text = t('Start');
}
}
$url = Url::fromRoute('opigno_learning_path.training', [
'group' => $group
->id(),
]);
$top_text = isset($top_link_text) ? [
'#type' => 'link',
'#url' => $url,
'#title' => $top_link_text,
'#access' => $url
->access($this
->currentUser()),
'#attributes' => [
'class' => [
'btn',
'btn-rounded',
'continue-link',
],
],
] : [];
$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 [
$top_text ?? [],
$l,
];
}