You are here

opigno_statistics.module in Opigno statistics 8

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

File

opigno_statistics.module
View source
<?php

/**
 * @file
 * Contains opigno_statistics.module.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\Group;
use Drupal\opigno_learning_path\LearningPathAccess;
use Drupal\user\UserInterface;
use Drupal\Core\Database\Database;

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function opigno_statistics_theme() {
  return [
    'opigno_statistics_circle_indicator' => [
      'variables' => [
        'width' => NULL,
        'height' => NULL,
        'cx' => NULL,
        'cy' => NULL,
        'radius' => NULL,
        'x' => NULL,
        'y' => NULL,
        'val_rad' => NULL,
      ],
    ],
    'opigno_statistics_chart__user_per_day' => [
      'variables' => [
        'max_count' => NULL,
        'data' => NULL,
      ],
    ],
    'opigno_statistics_user_metric' => [
      'variables' => [
        'label' => NULL,
        'value' => NULL,
        'help_text' => NULL,
      ],
    ],
    'opigno_statistics_user_metrics' => [
      'render element' => 'elements',
    ],
    'opigno_statistics_circle_indicator_value' => [
      'variables' => [
        'label' => NULL,
        'value' => NULL,
        'help_text' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function opigno_statistics_group_access(Group $group, $operation, AccountInterface $account) {
  $opigno_types = [
    'learning_path',
    'opigno_course',
    'opigno_class',
  ];
  $group_type = $group->type->entity
    ->id();
  if (!in_array($group_type, $opigno_types)) {
    return AccessResult::neutral();
  }

  // Update user activity.
  $timestamp = strtotime("midnight", \Drupal::time()
    ->getRequestTime());
  $datetime = DrupalDateTime::createFromTimestamp($timestamp);
  $datetime_str = $datetime
    ->format(DrupalDateTime::FORMAT);
  $connection = Database::getConnection();
  $query_a = $connection
    ->select('opigno_statistics_user_login', 'o_s_u_l')
    ->condition('o_s_u_l.date', $datetime_str, '>')
    ->condition('o_s_u_l.uid', $account
    ->id());
  $user_activity = $query_a
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($user_activity == 0) {
    $timestamp = \Drupal::time()
      ->getRequestTime();
    $datetime = DrupalDateTime::createFromTimestamp($timestamp);
    $datetime_str = $datetime
      ->format(DrupalDateTime::FORMAT);

    // Store user login event to the database.
    \Drupal::database()
      ->insert('opigno_statistics_user_login')
      ->fields([
      'uid' => $account
        ->id(),
      'date' => $datetime_str,
    ])
      ->execute();
  }
  if ($operation === 'view statistics') {
    if ($account
      ->hasPermission('view global statistics') || $account
      ->hasPermission('view any group statistics') || $group
      ->hasPermission('view group statistics', $account) || $group
      ->getMember($account)) {
      return AccessResult::allowed();
    }
    else {

      // Check if user has role 'student manager' in any of trainings.
      $is_user_manager = LearningPathAccess::memberHasRole('user_manager', $account, $group
        ->id());
      if ($is_user_manager > 0) {
        return AccessResult::allowed();
      }
      else {
        return AccessResult::forbidden();
      }
    }
  }
  return AccessResult::neutral();
}

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function opigno_statistics_user_access(UserInterface $user, $operation, AccountInterface $account) {
  if ($operation === 'view statistics') {

    // Allow users to view their own profile.
    if ($account
      ->id() === $user
      ->id()) {
      return AccessResult::allowed();
    }
    if ($account
      ->hasPermission('view global statistics') || $account
      ->hasPermission('view any user statistics')) {
      return AccessResult::allowed();
    }
    else {
      return AccessResult::forbidden();
    }
  }
  return AccessResult::neutral();
}

/**
 * Implements hook_user_login().
 */
function opigno_statistics_user_login($account) {

  /** @var \Drupal\Core\Session\AccountInterface $account */
  $uid = $account
    ->id();
  $timestamp = \Drupal::time()
    ->getRequestTime();
  $datetime = DrupalDateTime::createFromTimestamp($timestamp);
  $datetime_str = $datetime
    ->format(DrupalDateTime::FORMAT);

  // Store user login event to the database.
  \Drupal::database()
    ->insert('opigno_statistics_user_login')
    ->fields([
    'uid' => $uid,
    'date' => $datetime_str,
  ])
    ->execute();
}