function opigno_statistics_group_access in Opigno statistics 8
Same name and namespace in other branches
- 3.x opigno_statistics.module \opigno_statistics_group_access()
Implements hook_ENTITY_TYPE_access().
2 calls to opigno_statistics_group_access()
- OpignoStatisticsAccess::accessGroup in src/
Access/ OpignoStatisticsAccess.php - Checks access for a route with group in params.
- OpignoStatisticsAccess::accessModule in src/
Access/ OpignoStatisticsAccess.php - Checks access for a route with group and module in params.
File
- ./
opigno_statistics.module, line 62 - Contains opigno_statistics.module.
Code
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();
}