You are here

opigno_class.module in Opigno class 8

Same filename and directory in other branches
  1. 3.x opigno_class.module

File

opigno_class.module
View source
<?php

/**
 * @file
 * Contains opigno_class.module.
 */
use Drupal\block\Entity\Block;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Entity\Group;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;

/**
 * Implements hook_help().
 */
function opigno_class_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the opigno_class module.
    case 'help.page.opigno_class':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Adds the classes to your Opigno instance. Works well with Opigno Learning Path.') . '</p>';
      return $output;
    default:
      return '';
  }
}

/**
 * Implements hook_preprocess_page().
 */
function opigno_class_preprocess_page(&$variables) {
  $route = \Drupal::routeMatch();
  $route_name = $route
    ->getRouteName();

  /** @var \Drupal\group\Entity\GroupInterface $group */
  $group = $route
    ->getParameter('group');
  if ($group && !is_object($group)) {
    $group = Group::load($group);
  }
  if ($group === NULL || $group
    ->bundle() !== 'opigno_class') {
    return;
  }
  if ($route_name === 'entity.group.canonical') {
    $user = \Drupal::currentUser();
    if ($user
      ->hasPermission('manage group members in any group') || $group
      ->hasPermission('administer members', $user)) {

      // If user can manage group members, add link to the members overview.
      $url = Url::fromRoute('opigno_learning_path.membership.overview', [
        'group' => $group
          ->id(),
      ]);
      $link = Link::fromTextAndUrl(t('Manage members'), $url)
        ->toRenderable();
      array_unshift($variables['page']['content'], $link);
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function opigno_class_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (in_array($form_id, [
    'group_opigno_class_add_form',
    'group_opigno_class_edit_form',
  ])) {
    $form['actions']['submit']['#submit'][] = '_opigno_class_group_membership_add_form_submit';
  }
}

/**
 * Submit callback for opigno_class_form_alter().
 */
function _opigno_class_group_membership_add_form_submit($form, FormStateInterface $form_state) {
  $route = \Drupal::routeMatch();
  $group = $route
    ->getParameter('group');
  $gid = NULL;
  if (empty($group)) {
    $route_options = $form_state
      ->getRedirect()
      ->getRouteParameters();
    $gid = !empty($route_options['group']) ? $route_options['group'] : NULL;
  }
  else {
    $gid = $group
      ->id();
  }
  if (!empty($gid)) {
    $form_state
      ->setRedirect('opigno_learning_path.membership.overview', [
      'group' => $gid,
    ]);
  }
}

/**
 * Implements hook_views_query_alter().
 */
function opigno_class_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  $user = \Drupal::currentUser();

  // Show classes where user has role 'opigno_class-class_manager'.
  if ($view
    ->id() === 'opigno_classes' && !$user
    ->hasPermission('manage group members in any group')) {
    $uid = $user
      ->id();
    $connection = Database::getConnection();
    $query_c = $connection
      ->select('group_content_field_data', 'g_c_f_d')
      ->fields('g_c_f_d', [
      'gid',
    ]);
    $query_c
      ->leftJoin('group_content__group_roles', 'g_c_g_r', 'g_c_f_d.id = g_c_g_r.entity_id');
    $query_c
      ->condition('g_c_g_r.group_roles_target_id', 'opigno_class-class_manager');
    $query_c
      ->condition('g_c_f_d.entity_id', $uid);
    $query_c
      ->condition('g_c_f_d.type', 'opigno_class-group_membership');
    $classes = $query_c
      ->execute()
      ->fetchAllAssoc('gid');
    $class_ids = [];
    foreach ($classes as $class) {
      $class_ids[] = $class->gid;
    }
    if (!empty($class_ids)) {
      $group_and = $query
        ->setWhereGroup('AND');
      $query
        ->addWhere($group_and, 'groups_field_data.id', $class_ids, 'IN');
    }
    else {
      $group_and = $query
        ->setWhereGroup('AND');
      $query
        ->addWhere($group_and, 'groups_field_data.id', -1);
    }
  }
}

/**
 * Implements hook_block_access().
 */
function opigno_class_block_access(Block $block, $operation, AccountInterface $account) {
  if ($block
    ->id() == 'opignoadmin') {
    $uid = $account
      ->id();
    $roles = $account
      ->getRoles();
    $access = FALSE;
    foreach ($roles as $role) {
      if (in_array($role, [
        'administrator',
        'content_manager',
        'user_manager',
      ]) || $uid == 1) {
        $access = TRUE;
      }
    }
    if (!$access) {

      // Get trainings where the current user is a 'student manager' or user has global role 'class manager'.

      /** @var \Drupal\group\GroupMembershipLoaderInterface $membership_service */
      $membership_service = \Drupal::service('group.membership_loader');
      $memberships = $membership_service
        ->loadByUser($account, [
        'learning_path-user_manager',
        'opigno_class-class_manager',
      ]);
      $groups_ids = [];
      foreach ($memberships as $membership) {
        $group = $membership
          ->getGroup();
        if ($group
          ->bundle() == 'opigno_class') {
          $db_connection = \Drupal::service('database');
          $query_class = $db_connection
            ->select('group_content_field_data', 'g_c_f_d')
            ->fields('g_c_f_d', [
            'gid',
          ])
            ->condition('entity_id', $group
            ->id())
            ->condition('type', 'group_content_type_27efa0097d858')
            ->execute()
            ->fetchAll();
          foreach ($query_class as $result_ids) {
            $groups_ids[] = $result_ids->gid;
          }
        }
        else {
          $groups_ids[] = $group
            ->id();
        }
      }
      if (!empty($groups_ids)) {
        $access = TRUE;
      }
    }
    if (!$access) {
      return AccessResult::forbidden();
    }
  }
}