social_lms_integrator-1.0.0-beta4/modules/social_lms_integrator_iteration_managers/social_lms_integrator_iteration_managers.module
modules/social_lms_integrator_iteration_managers/social_lms_integrator_iteration_managers.module
<?php
/**
* @file
* Contains social_lms_integrator_iteration_managers.module.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\block\Entity\Block;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\social_lms_integrator_enrollment\IterationEnrollmentInterface;
use Drupal\social_lms_integrator_iteration_managers\SocialLMSIntegratorIterationManagersAccessHelper;
use Drupal\user\Entity\User;
use Drupal\views\ViewExecutable;
use Drupal\social_lms_integrator_enrollment\Entity\IterationEnrollment;
use Drupal\views_bulk_operations\ViewsBulkOperationsBatch;
use Drupal\group\Entity\GroupInterface;
function social_lms_integrator_iteration_managers_theme($existing, $type, $theme, $path) {
return [
'bootstrap_dropdown' => [
'template' => 'bootstrap-dropdown',
'base hook' => 'bootstrap_dropdown',
],
];
}
/**
* Implements hook_views_data_alter().
*/
function social_lms_integrator_iteration_managers_views_data_alter(array &$data) {
// Create our own views VBO field for enrollments.
$data['views']['social_views_bulk_operations_bulk_form_iteration_enrollments'] = [
'title' => t('Social Views bulk operations for Iteration Enrollments'),
'help' => t("Process enrollments returned by the view with Views Bulk Operations actions."),
'field' => [
'id' => 'social_views_bulk_operations_bulk_form_iteration_enrollments',
],
];
// Create our own views VBO field for enrollment requests.
$data['views']['social_views_bulk_operations_bulk_form_iteration_enrollment_requests'] = [
'title' => t('Social Views bulk operations for Iteration Enrollment Requests'),
'help' => t("Process enrollments returned by the view with Views Bulk Operations actions."),
'field' => [
'id' => 'social_views_bulk_operations_bulk_form_iteration_enrollment_requests',
],
];
// Create our own views VBO field for enrollment invites.
$data['views']['social_views_bulk_operations_bulk_form_iteration_enrollment_invites'] = [
'title' => t('Social Views bulk operations for Iteration Enrollment Invites'),
'help' => t("Process enrollments returned by the view with Views Bulk Operations actions."),
'field' => [
'id' => 'social_views_bulk_operations_bulk_form_iteration_enrollment_invites',
],
];
// Create our own views VBO field for application.
$data['views']['social_views_bulk_operations_bulk_form_iteration_application'] = [
'title' => t('Social Views bulk operations for Iteration Applications'),
'help' => t("Process applications returned by the view with Views Bulk Operations actions."),
'field' => [
'id' => 'social_views_bulk_operations_bulk_form_iteration_application',
],
];
// Create our own views VBO field for application.
$data['views']['social_views_bulk_operations_bulk_form_iteration_nomination'] = [
'title' => t('Social Views bulk operations for Iteration Nominations'),
'help' => t("Process nominations returned by the view with Views Bulk Operations actions."),
'field' => [
'id' => 'social_views_bulk_operations_bulk_form_iteration_nomination',
],
];
}
/**
* Implements hook_entity_access().
*/
function social_lms_integrator_iteration_managers_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Iteration Manager & Organizers can view/delete/edit enrollments for iterations
// they are organizing.
if ($entity instanceof IterationEnrollmentInterface) {
if (social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
return AccessResult::allowedIf($entity instanceof IterationEnrollmentInterface);
}
}
}
/**
* Implements hook_form_alter().
*/
function social_lms_integrator_iteration_managers_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Make sure we pass along the ccorrect view id, display id and node
// parameter to our custom views bulk forms and redirects.
if (isset($form['views_bulk_operations_bulk_form']) && isset($form['output'][0]['#view'])) {
$view = &$form['output'][0]['#view'];
if ($view instanceof ViewExecutable) {
$view_id = $view->id();
$display_id = $view->current_display;
}
}
elseif (strpos($form_id, 'views_form_iteration_manage_enrollments_page_manage_enrollments') !== FALSE && isset($form['output'][0]['#view'])) {
$view = &$form['output'][0]['#view'];
if ($view instanceof ViewExecutable) {
$view_id = $view->id();
$display_id = $view->current_display;
}
}
elseif (isset($form['social_views_bulk_operations_bulk_form_iteration_enrollments']) && isset($form['output'][0]['#view'])) {
$view = &$form['output'][0]['#view'];
if ($view instanceof ViewExecutable) {
$view_id = $view->id();
$display_id = $view->current_display;
}
}
elseif ($form_id === 'views_bulk_operations_configure_action') {
$data = $form_state->get('views_bulk_operations');
$view_id = $data['view_id'];
$display_id = $data['display_id'];
}
if (isset($view_id) && $view_id === 'iteration_manage_enrollments' && $display_id === 'page_manage_enrollments') {
$form_state->set('node', \Drupal::routeMatch()->getRawParameter('node'));
if ($form_id === 'views_bulk_operations_configure_action') {
$callbacks = &$form['actions']['submit']['#submit'];
}
else {
$callbacks = &$form['#submit'];
}
$callbacks[] = '_social_lms_integrator_iteration_managers_views_bulk_operations_bulk_form_submit';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function social_lms_integrator_iteration_managers_form_node_iteration_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// On iteration edit node form we check if users can alter author.
$node = \Drupal::routeMatch()->getParameter('node');
// Not on newly created nodes so we check if there is a route match for a node
// object.
if ($node) {
// Get the current user.
$user = \Drupal::currentUser();
// Remove authoring information for everybody on node iteration edit form.
$form['author']['#access'] = FALSE;
// Check for permission. Otherwise you can't change the author.
// Unless you are the author / have the right permissions.
if ($user->hasPermission('administer nodes') || $user->id() == $node->getOwnerId()) {
$form['author']['#access'] = TRUE;
}
}
}
/**
* Implements hook_node_access_records().
*/
function social_lms_integrator_iteration_managers_node_access_records(NodeInterface $node) {
$grants = [];
// Only for iterations.
if (($node->getType() === 'iteration') && $iteration_managers_ids = array_column($node->get('field_iteration_managers')
->getValue(), 'target_id')) {
// Iteration organizers should be granted access.
// Load the iteration managers accounts.
$users = User::loadMultiple($iteration_managers_ids);
foreach ($users as $iteration_manager) {
// Iteration organizers must have access
// to view the record in the first place.
if ($node->access('view', $iteration_manager)) {
// Add grant.
$grants[] = [
'realm' => 'social_iteration_managers:' . $node->id(),
'gid' => $iteration_manager->id(),
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 0,
];
}
}
}
return $grants;
}
/**
* Implements hook_node_grants().
*/
function social_lms_integrator_iteration_managers_node_grants(AccountInterface $account, $op) {
$grants = [];
// @todo Fetch all nodes this user has access to
// and add a grant for each of those.
$query = \Drupal::database()->select('node__field_iteration_managers', 'em');
$query->fields('em', ['entity_id']);
$query->condition('em.field_iteration_managers_target_id', $account->id());
// Add grants.
foreach ($query->execute()->fetchAllAssoc('entity_id') as $nid) {
$grants['social_iteration_managers:' . $nid->entity_id][] = $account->id();
}
// Tell Drupal about users grants.
return $grants;
}
/**
* Implements hook_module_implements_alter().
*/
function social_lms_integrator_iteration_managers_module_implements_alter(&$implementations, $hook) {
if ($hook == 'node_access') {
// Remove the gnode implementation, we have a fallback in our hook.
if (isset($implementations['gnode']) && function_exists('gnode_node_access')) {
unset($implementations['gnode']);
}
}
}
/**
* Implements hook_node_access().
*
* Remember: if any module returns forbidden and denies access to certain node
* and operation it will not allow the user to do the operation on the node.
*
* We need this implementation because we also want to give edit access to iteration
* manager regardless these scenarios thought of in gnode_node_access:
* - is a member in the group and:
* - has edit own or edit any permission in the group
*
* The gnode module specifically returns allowed if any of the above scenarios
* are met, but forbidden in all the other scenarios. Our code ensures that if
* we are in operation update and if gnode already returns forbidden we are able
* to return an allowed if user is an iteration manager.
*/
function social_lms_integrator_iteration_managers_node_access(NodeInterface $node, $op, AccountInterface $account) {
// Only continue if the gnode module is enabled.
if (function_exists('gnode_node_access')) {
$gnode_access = gnode_node_access($node, $op, $account);
if ($op === 'update') {
if ($gnode_access instanceof AccessResultForbidden) {
$social_iteration_managers_access = SocialLMSIntegratorIterationManagersAccessHelper::getEntityAccessResult($node, $op, $account);
// Only return the result of SocialLMSIntegratorIterationManagersAccessHelper
// if it is allowed.
if ($social_iteration_managers_access instanceof AccessResultAllowed) {
return $social_iteration_managers_access;
}
}
return $gnode_access;
}
return $gnode_access;
}
return SocialLMSIntegratorIterationManagersAccessHelper::getEntityAccessResult($node, $op, $account);
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function social_lms_integrator_iteration_managers_menu_local_tasks_alter(&$data, $route_name) {
$can_show_managers_link = FALSE;
$routes_to_check = [
//'view.iteration_enrollments.view_enrollments',
'entity.node.canonical',
'view.iteration_manage_enrollments.page_manage_enrollments',
'view.iteration_manage_enrollment_requests.page_manage_enrollment_requests',
'view.iteration_manage_enrollment_invites.page_manage_enrollment_invites',
'view.iteration_application.page_application',
'view.iteration_nomination.page_nomination'
];
if (in_array($route_name, $routes_to_check)) {
$node = \Drupal::service('current_route_match')->getParameter('node');
if (!is_null($node) && (!$node instanceof Node)) {
$node = Node::load($node);
}
if (($node instanceof Node) && $node->getType() === 'iteration') {
if (social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
$can_show_managers_link = TRUE;
}
}
if ($node instanceof NodeInterface && $node->bundle() === 'iteration') {
// Get the group from the node
$group = _social_group_get_current_group($node);
if ($group instanceof GroupInterface) {
$account = \Drupal::currentUser();
$bundle = $group->bundle();
if ($member = $group->getMember($account)) {
if ($member->hasPermission('edit group')) {
$can_show_managers_link = TRUE;
}
}
}
}
}
// PLace this here, since hiding it should happen
// always and not only on the mentioned routes.
if (!$can_show_managers_link) {
unset($data['tabs'][0]['views_view:view.iteration_manage_enrollments.page_manage_enrollments']);
unset($data['tabs'][0]['views_view:view.iteration_manage_enrollment_requests.page_manage_enrollment_requests']);
unset($data['tabs'][0]['views_view:view.iteration_manage_enrollment_invites.page_manage_enrollment_invites']);
unset($data['tabs'][0]['views_view:view.iteration_application.page_application']);
unset($data['tabs'][0]['views_view:view.iteration_nomination.page_nomination']);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function social_lms_integrator_iteration_managers_form_node_iteration_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Set author of iteration as iteration organiser automatically.
$config = \Drupal::configFactory()
->getEditable('social_lms_integrator_iteration_managers.settings');
if ($config->get('author_as_manager')) {
if ($form_state->getTriggeringElement() === NULL) {
$account = \Drupal::currentUser();
$user = \Drupal::entityTypeManager()
->getStorage('user')
->load($account->id());
$last_key = $form['field_iteration_managers']['widget']['#max_delta'];
$form['field_iteration_managers']['widget'][$last_key]['target_id']['#default_value'] = $user;
}
}
// Update the field iteration managers widget form.
if (isset($form['field_iteration_managers']['widget']['add_more'])) {
$form['field_iteration_managers']['widget']['add_more']['#value'] = t('Add another organizer');
}
}
/**
* Implements hook_batch_alter().
*/
function social_lms_integrator_iteration_managers_batch_alter(&$batch) {
if (!isset($batch['source_url'])) {
return;
}
$actions = [
'social_lms_integrator_nomination_vbo_export_nominations_action',
'social_lms_integrator_application_vbo_export_applications_action',
'social_lms_integrator_iteration_enrollments_req_export_enrollments_action',
'social_lms_integrator_iteration_enrollments_invite_export_enrollments_action',
'social_lms_integrator_iteration_managers_delete_iteration_enrollment_action',
'social_lms_integrator_iteration_managers_approve_iteration_enrollment_action',
'social_lms_integrator_iteration_managers_decline_iteration_enrollment_action',
'iteration_welcome_message_send_email'
];
/** @var \Drupal\Core\Url $url */
$url = &$batch['source_url'];
if ($url->getRouteName() === 'social_lms_integrator_iteration_managers.vbo.confirm' ||
$url->getRouteName() === 'social_lms_integrator_iteration_managers.request.vbo.confirm' ||
$url->getRouteName() === 'social_lms_integrator_iteration_managers.invite.vbo.confirm' ||
$url->getRouteName() === 'social_lms_integrator_iteration_managers.application.vbo.confirm' ||
$url->getRouteName() === 'social_lms_integrator_iteration_managers.nomination.vbo.confirm' ||
$url->getRouteName() === 'views_bulk_operations.confirm' ||
$url->getRouteName() === 'views_bulk_operations.execute_batch') {
// Get the action ID.
$action_id = _social_lms_integrator_iteration_managers_get_action_id($batch);
$batch['sets'][0]['results']['action'] = $action_id;
if (in_array($action_id, $actions, TRUE)) {
$batch['sets'][0]['finished'] = '_social_lms_integrator_iteration_managers_action_batch_finish';
}
}
}
/**
* Action batch finished callback.
*
* @param bool $success
* Was the process successfull?
* @param array $results
* Batch process results array.
* @param array $operations
* Performed operations array.
*/
function _social_lms_integrator_iteration_managers_action_batch_finish($success, array $results, array $operations) {
// When we do a bulk action on all the items in a view, across multiple pages,
// the saveList function needs to be called. So after pre-populating the list
// the actual action is performed on the entities.
if (!empty($results['view_id']) && !empty($results['display_id'])) {
ViewsBulkOperationsBatch::saveList(TRUE, $results, $operations);
return;
}
$operations = array_count_values($results['operations']);
$results_count = 0;
foreach ($operations as $count) {
$results_count += $count;
}
$hook = 'social_lms_integrator_iteration_managers_action_' . $results['action'] . '_finish';
foreach (\Drupal::moduleHandler()->getImplementations($hook) as $module) {
$function = $module . '_' . $hook;
$messages = $function($success);
if (is_array($messages)) {
$fields = 0;
foreach ($messages as $type => $message) {
if (($type === 'singular' || $type === 'plural') && !empty($message) && is_string($message)) {
$fields++;
}
}
if ($fields === 2) {
// @todo source strings will never be translatable when we use
// variables. This should be refactored.
$message = \Drupal::translation()->formatPlural($results_count, $messages['singular'], $messages['plural']);
$type = $success ? MessengerInterface::TYPE_STATUS : MessengerInterface::TYPE_WARNING;
\Drupal::messenger()->addMessage($message, $type);
}
}
}
}
/**
* Function to get the action id of a batch.
*
* @param array $batch
* The batch array.
*
* @return string
* Returns the batch action id.
*/
function _social_lms_integrator_iteration_managers_get_action_id(array &$batch) {
/** @var \Drupal\Core\Form\FormStateInterface $form_state */
$form_state = &$batch['form_state'];
$action_id = '';
if ($form_state instanceof FormStateInterface) {
$data = $form_state->get('views_bulk_operations');
$action_id = $data['action_id'];
}
else {
foreach ($batch['sets'][0]['operations'] as $operations) {
if (empty($operations) || !is_array($operations)) {
break;
}
foreach ($operations as $operation) {
if (empty($operation) || !is_array($operation)) {
break;
}
foreach ($operation as $items) {
if (empty($items) || !is_array($items)) {
break;
}
if (!empty($items['action_id'])) {
$action_id = $items['action_id'];
break;
}
}
}
}
}
return $action_id;
}
/**
* Implements hook_social_event_action_ACTION_ID_finish().
*/
function social_lms_integrator_iteration_managers_social_lms_integrator_iteration_managers_action_social_lms_integrator_iteration_managers_delete_iteration_enrollment_action_finish($success) {
if ($success) {
return [
'singular' => '1 selected enrollee has been removed from the iteration successfully',
'plural' => '@count selected enrollees have been removed from the iteration successfully',
];
}
return [
'singular' => '1 selected enrollee has not been removed from the iteration successfully',
'plural' => '@count selected enrollees have not been removed from the iteration successfully',
];
}
/**
* Add node ID to the route of action confirmation step.
*/
function _social_lms_integrator_iteration_managers_views_bulk_operations_bulk_form_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Url $url */
$url = $form_state->getRedirect();
$node = '';
if ($form_state->get('node')) {
$node = $form_state->get('node');
}
if (empty($node)) {
$route = \Drupal::routeMatch()->getParameter('node');
$node = $route->id();
}
$route_parameters = [
'node' => $node,
];
if (!empty($node)) {
if ($url->getRouteName() === 'views_bulk_operations.execute_configurable') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.vbo.execute_configurable', $route_parameters);
}
if ($url->getRouteName() === 'social_lms_integrator_iteration_managers.vbo.confirm') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.vbo.confirm', $route_parameters);
}
}
$form_state->setRedirectUrl($url);
}
/**
* Add node ID to the route of action confirmation step.
*/
function _social_lms_integrator_iteration_managers_request_views_bulk_operations_bulk_form_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Url $url */
$url = $form_state->getRedirect();
$node = '';
if ($form_state->get('node')) {
$node = $form_state->get('node');
}
if (empty($node)) {
$route = \Drupal::routeMatch()->getParameter('node');
$node = $route->id();
}
$route_parameters = [
'node' => $node,
];
if (!empty($node)) {
if ($url->getRouteName() === 'views_bulk_operations.execute_configurable') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.request.vbo.execute_configurable', $route_parameters);
}
if ($url->getRouteName() === 'social_lms_integrator_iteration_managers.request.vbo.confirm') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.request.vbo.confirm', $route_parameters);
}
}
$form_state->setRedirectUrl($url);
}
/**
* Add node ID to the route of action confirmation step.
*/
function _social_lms_integrator_iteration_managers_invite_views_bulk_operations_bulk_form_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Url $url */
$url = $form_state->getRedirect();
$node = '';
if ($form_state->get('node')) {
$node = $form_state->get('node');
}
if (empty($node)) {
$route = \Drupal::routeMatch()->getParameter('node');
$node = $route->id();
}
$route_parameters = [
'node' => $node,
];
if (!empty($node)) {
if ($url->getRouteName() === 'views_bulk_operations.execute_configurable') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.invite.vbo.execute_configurable', $route_parameters);
}
if ($url->getRouteName() === 'social_lms_integrator_iteration_managers.invite.vbo.confirm') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.invite.vbo.confirm', $route_parameters);
}
}
$form_state->setRedirectUrl($url);
}
function _social_lms_integrator_iteration_managers_application_views_bulk_operations_bulk_form_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Url $url */
$url = $form_state->getRedirect();
$node = '';
if ($form_state->get('node')) {
$node = $form_state->get('node');
}
if (empty($node)) {
$route = \Drupal::routeMatch()->getParameter('node');
$node = $route->id();
}
$route_parameters = [
'node' => $node,
];
if (!empty($node)) {
if ($url->getRouteName() === 'views_bulk_operations.execute_configurable') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.application.vbo.execute_configurable', $route_parameters);
}
if ($url->getRouteName() === 'social_lms_integrator_iteration_managers.application.vbo.confirm') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.application.vbo.confirm', $route_parameters);
}
}
$form_state->setRedirectUrl($url);
}
function _social_lms_integrator_iteration_managers_nomination_views_bulk_operations_bulk_form_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Url $url */
$url = $form_state->getRedirect();
$node = '';
if ($form_state->get('node')) {
$node = $form_state->get('node');
}
if (empty($node)) {
$route = \Drupal::routeMatch()->getParameter('node');
$node = $route->id();
}
$route_parameters = [
'node' => $node,
];
if (!empty($node)) {
if ($url->getRouteName() === 'views_bulk_operations.execute_configurable') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.nomination.vbo.execute_configurable', $route_parameters);
}
if ($url->getRouteName() === 'social_lms_integrator_iteration_managers.nomination.vbo.confirm') {
$url = Url::fromRoute('social_lms_integrator_iteration_managers.nomination.vbo.confirm', $route_parameters);
}
}
$form_state->setRedirectUrl($url);
}
/**
* Implements hook_preprocess_HOOK().
*/
function social_lms_integrator_iteration_managers_preprocess_views_view(&$variables) {
if (!\Drupal::moduleHandler()->moduleExists('social_lms_integrator_iteration_invite')) {
/** @var \Drupal\views\ViewExecutable $view */
$view = &$variables['view'];
// Remove header & VBO actions from the Enrollment Management tab if the
// user is not a manager or organiser.
if ($view->id() === 'iteration_manage_enrollments') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
unset($variables['rows']['social_views_bulk_operations_bulk_form_iteration_enrollments']);
unset($variables['rows']['header']);
unset($variables['rows']['actions']);
}
$block = \Drupal::entityTypeManager()->getStorage('block')
->load('socialblue_local_actions');
$variables['header']['actions'] = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
}
if ($view->id() === 'iteration_manage_enrollment_requests') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
unset($variables['rows']['social_views_bulk_operations_bulk_form_iteration_enrollment_requests']);
unset($variables['rows']['header']);
unset($variables['rows']['actions']);
}
$block = \Drupal::entityTypeManager()->getStorage('block')
->load('socialblue_local_actions');
$variables['header']['actions'] = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
}
if ($view->id() === 'iteration_manage_enrollment_invites') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
unset($variables['rows']['social_views_bulk_operations_bulk_form_iteration_enrollment_invites']);
unset($variables['rows']['header']);
unset($variables['rows']['actions']);
}
$block = \Drupal::entityTypeManager()->getStorage('block')
->load('socialblue_local_actions');
$variables['header']['actions'] = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
}
if ($view->id() === 'iteration_application') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
unset($variables['rows']['social_views_bulk_operations_bulk_form_iteration_application']);
unset($variables['rows']['header']);
unset($variables['rows']['actions']);
}
$block = \Drupal::entityTypeManager()->getStorage('block')
->load('socialblue_local_actions');
$variables['header']['actions'] = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
}
if ($view->id() === 'iteration_nomination') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
unset($variables['rows']['social_views_bulk_operations_bulk_form_iteration_nomination']);
unset($variables['rows']['header']);
unset($variables['rows']['actions']);
}
$block = \Drupal::entityTypeManager()->getStorage('block')
->load('socialblue_local_actions');
$variables['header']['actions'] = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
}
}
}
/**
* Implements hook_views_pre_view().
*/
function social_lms_integrator_iteration_managers_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
// Remove fields from the Enrollment Management tab if the user is not a
// manager or organiser.
if ($view->id() === 'iteration_manage_enrollments') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
$fields = $view->display_handler->getOption('fields');
$fields['operations']['exclude'] = TRUE;
$fields['social_views_bulk_operations_bulk_form_iteration_enrollments']['exclude'] = TRUE;
$view->display_handler->overrideOption('fields', $fields);
}
}
if ($view->id() === 'iteration_manage_enrollment_requests') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
$fields = $view->display_handler->getOption('fields');
$fields['operations']['exclude'] = TRUE;
$fields['social_views_bulk_operations_bulk_form_iteration_enrollment_requests']['exclude'] = TRUE;
$view->display_handler->overrideOption('fields', $fields);
}
}
if ($view->id() === 'iteration_manage_enrollment_invites') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
$fields = $view->display_handler->getOption('fields');
$fields['operations']['exclude'] = TRUE;
$fields['social_views_bulk_operations_bulk_form_iteration_enrollment_invites']['exclude'] = TRUE;
$view->display_handler->overrideOption('fields', $fields);
}
}
if ($view->id() === 'iteration_application') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
$fields = $view->display_handler->getOption('fields');
//$fields['operations_1']['exclude'] = TRUE;
$fields['social_views_bulk_operations_bulk_form_iteration_application']['exclude'] = TRUE;
$view->display_handler->overrideOption('fields', $fields);
}
}
if ($view->id() === 'iteration_nomination') {
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
$fields = $view->display_handler->getOption('fields');
//$fields['operations_1']['exclude'] = TRUE;
$fields['social_views_bulk_operations_bulk_form_iteration_nomination']['exclude'] = TRUE;
$view->display_handler->overrideOption('fields', $fields);
}
}
}
function social_lms_integrator_iteration_managers_preprocess_bootstrap_dropdown(&$variables) {
// Changes go here.
$operations = !!mb_strpos($variables['theme_hook_original'], 'operations');
$route = \Drupal::routeMatch()->getRouteName();
if ($operations && ($route === 'view.iteration_manage_enrollments.page_manage_enrollments')) {
$variables['default_button'] = FALSE;
$variables['toggle_label'] = t('Actions');
}
}
/**
* Provide a method to alter data when get activity related entity.
*
* @param array $related_object
* Activity related entity object.
* @param array $data
* Activity related entity data.
*
* @ingroup activity_creator_api
*/
function social_lms_integrator_iteration_managers_activity_creator_related_entity_object_alter(array $related_object, array &$data) {
// We return Iteration as related object for all Iteration Enrollments.
if (isset($related_object['target_type']) && $related_object['target_type'] === 'iteration_enrollment') {
$entity_storage = \Drupal::entityTypeManager()
->getStorage($related_object['target_type']);
$entity = $entity_storage->load($related_object['target_id']);
if ($entity instanceof IterationEnrollmentInterface) {
/** @var \Drupal\social_lms_integrator_enrollment\Entity\IterationEnrollment $entity */
$iteration_id = $entity->getFieldValue('field_iteration', 'target_id');
if (!empty($iteration_id)) {
$related_object['target_type'] = 'node';
$related_object['target_id'] = $iteration_id;
}
}
}
}
/**
* Implements hook_activity_recipient_iteration_organizer_alter().
*/
function social_lms_integrator_iteration_managers_activity_recipient_iteration_organizer_alter(array &$recipients, Node $iteration, $data) {
$receiver = '';
$organizers = $iteration->get('field_iteration_managers')->getValue();
if ($data['target_type'] === 'iteration_enrollment' && !empty($data['target_id'])) {
$enrollment = IterationEnrollment::load($data['target_id']);
$receiver = $enrollment->getAccount();
}
// If there are more organizers we want them to receive a notification too
// so we add them to the array of recipients.
if (!empty($organizers)) {
foreach ($organizers as $organizer) {
// We don't want Organizers to receive activity_on_iterations_im_organizing.
// It will already receive it as part of a different context.
if (!empty($receiver) && $organizer['target_id'] === $receiver) {
continue;
}
// Make sure we don't add the people twice.
if (!in_array($organizer['target_id'], array_column($recipients, 'target_id'))) {
$recipients[] = [
'target_type' => 'user',
'target_id' => $organizer['target_id'],
];
}
}
}
}
