function opigno_module_views_query_alter in Opigno module 8
Same name and namespace in other branches
- 3.x opigno_module.module \opigno_module_views_query_alter()
Implements hook_views_query_alter().
File
- ./
opigno_module.module, line 912 - Contains opigno_module.module.
Code
function opigno_module_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
// Filter not evaluated modules.
if ($view
->id() === 'opigno_score_modules') {
$user = \Drupal::currentUser();
$roles = $user
->getRoles();
if ($user
->hasPermission('evaluate modules')) {
return;
}
if (in_array('administrator', $roles) || in_array('user_manager', $roles)) {
// If current user is an admin and global user manager.
return;
}
// Get trainings where the current user is a student manager.
/** @var \Drupal\group\GroupMembershipLoaderInterface $membership_service */
$membership_service = \Drupal::service('group.membership_loader');
$memberships = $membership_service
->loadByUser($user, [
'learning_path-user_manager',
'opigno_class-class_manager',
]);
$groups_ids = [];
$members_ids = [];
foreach ($memberships as $membership) {
$group = $membership
->getGroup();
$members = $group
->getMembers();
foreach ($members as $member) {
$members_ids[] = $member
->getUser()
->id();
}
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();
}
}
$members_ids = array_unique($members_ids);
// Get modules.
$modules_ids = [];
if (!empty($groups_ids)) {
foreach ($groups_ids as $id) {
$group = Group::load($id);
$mids = opigno_learning_path_get_modules_ids_by_group($group);
$modules_ids = array_merge($modules_ids, $mids);
}
}
// Filter listed modules by them.
if (!empty($modules_ids)) {
$query->where[] = [
'conditions' => [
[
'field' => 'user_module_status.module',
'value' => $modules_ids,
'operator' => 'IN',
],
],
'type' => 'AND',
];
$query->where[] = [
'conditions' => [
[
'field' => 'user_module_status.user_id',
'value' => $members_ids,
'operator' => 'IN',
],
],
'type' => 'AND',
];
}
else {
$query->where[] = [
'conditions' => [
[
'field' => 'FALSE',
'value' => [],
'operator' => 'formula',
],
],
'type' => 'AND',
];
}
}
if ($view
->id() === 'opigno_activities_bank_lp_interface') {
$route = \Drupal::routeMatch();
$moduleHandler = \Drupal::service('module_handler');
if (in_array($route
->getRouteName(), [
'opigno_module.activities_bank',
'opigno_module.activities_bank_lpm',
])) {
/* @var \Drupal\opigno_module\Entity\OpignoModule $module */
$module = $route
->getParameter('opigno_module');
$activities = $module
->getModuleActivities();
// Get all activities ids.
$aids = array_keys($activities);
// Filter activities to separate auto skills management and manual skills management.
if ($moduleHandler
->moduleExists('opigno_skills_system')) {
$activities_storage = \Drupal::entityTypeManager()
->getStorage('opigno_activity');
$query_a = $activities_storage
->getQuery();
if ($module
->getSkillsActive()) {
if ($module
->getTargetSkill() && $module
->getTargetSkill() > 0) {
$target_skill = $module
->getTargetSkill();
$term_storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$skills_tree = $term_storage
->loadTree('skills', $target_skill);
$skills_ids = [];
foreach ($skills_tree as $skill) {
$skills_ids[] = $skill->tid;
}
if (!empty($skills_ids)) {
$group = $query_a
->orConditionGroup()
->condition('skills_list', $skills_ids, 'NOT IN')
->notExists('auto_skills');
$ids = $query_a
->condition($group)
->execute();
$aids = array_merge($aids, $ids);
}
}
}
else {
$ids = $query_a
->condition('auto_skills', 1)
->execute();
$aids = array_merge($aids, $ids);
}
}
// Filter activities which are already added to module.
if (!empty($aids)) {
$query->where[] = [
'conditions' => [
[
'field' => 'opigno_activity_field_data.id',
'value' => $aids,
'operator' => 'NOT IN',
],
],
'type' => 'AND',
];
}
$condition = (new Condition('AND'))
->isNotNull('opigno_activity_field_data.name');
$query
->addWhere(1, $condition);
}
}
}