View source
<?php
namespace Drupal\opigno_learning_path\TwigExtension;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\group\Entity\Group;
use Drupal\opigno_learning_path\Controller\LearningPathController;
use Drupal\opigno_learning_path\LearningPathAccess;
class DefaultTwigExtension extends \Twig_Extension {
public function getTokenParsers() {
return [];
}
public function getNodeVisitors() {
return [];
}
public function getFilters() {
return [];
}
public function getTests() {
return [];
}
public function getFunctions() {
return [
new \Twig_SimpleFunction('is_group_member', [
$this,
'is_group_member',
]),
new \Twig_SimpleFunction('get_join_group_link', [
$this,
'get_join_group_link',
]),
new \Twig_SimpleFunction('get_start_link', [
$this,
'get_start_link',
]),
new \Twig_SimpleFunction('get_progress', [
$this,
'get_progress',
]),
new \Twig_SimpleFunction('get_training_content', [
$this,
'get_training_content',
]),
];
}
public function getOperators() {
return [];
}
public function getName() {
return 'opigno_learning_path.twig.extension';
}
public function is_group_member($group = NULL, $account = NULL) {
if (!$group) {
$group = \Drupal::routeMatch()
->getParameter('group');
}
if (empty($group)) {
return FALSE;
}
if (!$account) {
$account = \Drupal::currentUser();
}
return $group
->getMember($account) !== FALSE;
}
public function get_join_group_link($group = NULL, $account = NULL, array $attributes = []) {
$route = \Drupal::routeMatch();
if (!isset($group)) {
$group = $route
->getParameter('group');
}
if (!isset($account)) {
$account = \Drupal::currentUser();
}
$route_name = $route
->getRouteName();
$visibility = $group->field_learning_path_visibility->value;
$access = isset($group) && $group
->access('view', $account) && ($group
->hasPermission('join group', $account) || $visibility == 'public' || $visibility == 'semiprivate');
$is_member = $group
->getMember($account) !== FALSE;
$module_commerce_enabled = \Drupal::moduleHandler()
->moduleExists('opigno_commerce');
if ($module_commerce_enabled && $group
->hasField('field_lp_price') && $group
->get('field_lp_price')->value != 0 && !$is_member) {
return '';
}
if ($route_name == 'entity.group.canonical' && $access) {
$link = NULL;
$validation = LearningPathAccess::requiredValidation($group, $account);
$is_anonymous = $account
->id() === 0;
if ($visibility == 'semiprivate' && $validation) {
$joinLabel = t('Request subscription to the training');
}
else {
$joinLabel = t('Subscribe to training');
}
if ($is_anonymous) {
if ($visibility === 'public') {
$link = [
'title' => t('Start'),
'route' => 'opigno_learning_path.steps.start',
'args' => [
'group' => $group
->id(),
],
];
$attributes['class'][] = 'use-ajax';
}
else {
$url = Url::fromRoute('entity.group.canonical', [
'group' => $group
->id(),
]);
$link = [
'title' => $joinLabel,
'route' => 'user.login',
'args' => [
'destination' => render($url)
->toString(),
],
];
}
}
elseif (!$is_member) {
$link = [
'title' => $joinLabel,
'route' => 'entity.group.join',
'args' => [
'group' => $group
->id(),
],
];
}
if ($route_name == 'entity.group.canonical' && $is_anonymous && $visibility == 'semiprivate') {
$url = Url::fromRoute('entity.group.canonical', [
'group' => $group
->id(),
]);
$link = [
'title' => t('Create an account and subscribe'),
'route' => 'user.login',
'args' => [
'prev_path' => render($url)
->toString(),
],
];
}
if ($link) {
$url = Url::fromRoute($link['route'], $link['args'], [
'attributes' => $attributes,
]);
$l = Link::fromTextAndUrl($link['title'], $url)
->toRenderable();
return render($l);
}
}
return '';
}
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 [];
}
}
$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)) {
$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);
}
public function get_progress() {
$group = \Drupal::routeMatch()
->getParameter('group');
$account = \Drupal::currentUser();
$progress_service = \Drupal::service('opigno_learning_path.progress');
$content = $progress_service
->getProgressAjaxContainer($group
->id(), $account
->id(), '', 'group-page');
return render($content);
}
public function get_training_content() {
$controller = new LearningPathController();
$content = $controller
->trainingContent();
return render($content);
}
}