social_lms_integrator-1.0.0-beta4/modules/social_lms_integrator_iteration_invite/social_lms_integrator_iteration_invite.module

modules/social_lms_integrator_iteration_invite/social_lms_integrator_iteration_invite.module
<?php

/**
 * @file
 * The Social LMS Integrator iteration invite enroll module.
 */

use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\social_lms_integrator_enrollment\IterationEnrollmentInterface;
use Drupal\user\UserInterface;

/**
 * Sends email when invitation created for not registered user.
 *
 * Implements hook_ENTITY_TYPE_insert().
 */
function social_lms_integrator_iteration_invite_iteration_enrollment_insert(IterationEnrollmentInterface $iterationEnrollment) {




  $messenger = \Drupal::messenger();

  $mail = $iterationEnrollment->get('field_email')->getString();
  if(empty($mail)) {
    $user_entity = $iterationEnrollment->get('field_account')->first()->get('entity')->getTarget()->getEntity();
    $mail = $user_entity->getEmail();
  }

  $langcode = 'en';

  $mailManager = \Drupal::service('plugin.manager.mail');
  $groupHelperService = \Drupal::service('social_group.helper_service');
  $entityTypeManager = \Drupal::service('entity_type.manager');
  $from = $iterationEnrollment->get('user_id')->first()->get('entity')->getTarget()->getValue();
  $node = $iterationEnrollment->get('field_iteration')->first()->get('entity')->getTarget()->getValue();

  if ($node instanceof NodeInterface) {
    $nid = $node->id();
  }

  $gid_from_entity = $groupHelperService->getGroupFromEntity([
    'target_type' => 'node',
    'target_id' => $nid,
  ]);

  if ($gid_from_entity !== NULL) {
    /** @var \Drupal\group\Entity\GroupInterface $group */
    $group = $entityTypeManager
      ->getStorage('group')
      ->load($gid_from_entity);
  }

  $params = [
    'user' => $from,
    'node' => $node,
    'group' => $group,
    'existing_user' => TRUE,
    'iteration_enrollment' => $iterationEnrollment->id(),
  ];

  // Only set this message when the request status is pending.
  if ((int) $iterationEnrollment->get('field_request_or_invite_status')->value === IterationEnrollmentInterface::INVITE_PENDING_REPLY &&
    empty($iterationEnrollment->get('field_account')->getString())) {
    // Send a normal mail when the user has no account yet.
    $params['existing_user'] = FALSE;
    $mailManager->mail('social_lms_integrator_iteration_invite', 'invite_iteration', $mail, $langcode, $params, NULL, TRUE);
  }

  if ((int) $iterationEnrollment->get('field_request_or_invite_status')->value === IterationEnrollmentInterface::INVITE_PENDING_REPLY) {
    $params['existing_user'] = TRUE;
    $mailManager->mail('social_lms_integrator_iteration_invite', 'invite_iteration', $mail, $langcode, $params, NULL, TRUE);
  }  

}

/**
 * Implements hook_mail().
 *
 * This mail is sent when people who do not have an account on the website yet
 * are invited into an iteration. It is sent in the language the inviter was using
 * the website in.
 */
function social_lms_integrator_iteration_invite_mail($key, &$message, $params) {
  if ($key === 'invite_iteration') {
    $token_service = \Drupal::token();
    $language_manager = \Drupal::languageManager();

    $langcode = $message['langcode'];

    $language = $language_manager->getLanguage($langcode);
    $original_language = $language_manager->getConfigOverrideLanguage();
    $language_manager->setConfigOverrideLanguage($language);

    // Load iteration invite configuration.
    $invite_config = \Drupal::service('config.factory')->getEditable('social_lms_integrator_iteration_invite.settings');

    $invitation_subject = $invite_config->get('invite_subject');
    $invitation_body = $invite_config->get('invite_message');

    unset($params['existing_user']);

    $body = $token_service->replace($invitation_body, $params);
    $subject = $token_service->replace($invitation_subject, $params);

    $message['subject'] = $subject;
    $message['body'][] = $body;

    $language_manager->setConfigOverrideLanguage($original_language);

  }
}

/**
 * Prefill email address on registration url.
 *
 * Implements hook_form_FORM_ID_alter().
 */
function social_lms_integrator_iteration_invite_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($_GET["invitee_mail"])) {
    $invitee_mail = $_GET["invitee_mail"];
    $invitee_mail = base64_decode(str_replace(['-', '_'], ['+', '/'], $invitee_mail));
    if (\Drupal::service('email.validator')->isValid($invitee_mail)) {
      $form["account"]["mail"]["#default_value"] = $invitee_mail;
      $form["account"]["mail"]["#disabled"] = TRUE;
    }
  }
}

/**
 * Implements hook_views_data().
 */
function social_lms_integrator_iteration_invite_views_data() {
  $data['views']['social_lms_integrator_iteration_invite_recipient'] = [
    'group' => t('Iteration enrollment'),
    'title' => t('Recipient'),
    'help' => t('The recipient of an iteration invite.'),
    'field' => [
      'id' => 'social_lms_integrator_iteration_invite_recipient',
    ],
  ];
  return $data;
}

/**
 * Implements hook_preprocess_HOOK().
 */
function social_lms_integrator_iteration_invite_preprocess_views_view_table(&$variables) {
  if ($variables['view']->id() === 'iteration_manage_enrollment_invites' ||
    $variables['view']->id() === 'user_iteration_invites') {
    // Add this to ensure our overviews get rendered nicely
    // even if VBO is not enabled (yet) we use the same look and feel.
    $variables['attributes']['class'][] = 'vbo-table';
  }
}

/**
 * Implements template_preprocess_views_view().
 */
function social_lms_integrator_iteration_invite_preprocess_views_view(&$variables) {
  if ($variables['view']->id() === 'iteration_manage_enrollment_invites') {
    $node_id = \Drupal::routeMatch()->getParameter('node');
    // Implement custom button to go back to the iteration.
    $variables['more'] = [
      '#title' => t('Back to iteration'),
      '#type' => 'link',
      '#url' => Url::fromRoute('entity.node.canonical', ['node' => (int) $node_id]),
      '#attributes' => [
        'class' => [
          'btn',
          'btn-default',
          'btn-raised',
          'waves-effect',
        ],
      ],
    ];
  }
  // We have to override the local actions block.
  // and render our own block instance in the view for placement.
  // hook_theme_registry_alter will ensure our hooks is invoked later.
  // That is also why hook_menu_local_actions_alter won't work.
  if ($variables['view']->id() === 'iteration_manage_enrollments') {
    /** @var \Drupal\social_lms_integrator_iteration_invite\SocialLMSIntegratorIterationInviteAccessHelper $access */
    $access = \Drupal::service('social_lms_integrator_iteration_invite.access_helper');
    $access = $access->iterationFeatureAccess();

    if (!$access instanceof AccessResultForbidden) {
      // Add the roster-link block to the build-array.
      /** @var \Drupal\social_lms_integrator_iteration_invite\Plugin\Block\SocialLMSIntegratorIterationInviteLocalActionsBlock $block */
      $block = \Drupal::service('plugin.manager.block')
        ->createInstance('social_lms_integrator_iteration_invite_block');

      if (NULL !== $block) {
        $block->setContextValue('node', Node::load(\Drupal::routeMatch()->getParameter('node')));
        $block_content = $block->build();

        if (!empty($block_content)) {
          $variables['header']['actions'] = $block_content;
        }
      }
    }
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function social_lms_integrator_iteration_invite_theme_registry_alter(&$theme_registry) {
  // Unfortunately the preprocess functions aren't ordered by module weight.
  // Changing module weight doesn't work, also with dependency set to
  // social_group this should be dealt with but isnt.
  // So we enforce our preprocess after social_group.
  if (!empty($theme_registry['views_view']['preprocess functions'])) {
    $current_key = array_search('social_lms_integrator_iteration_invite_preprocess_views_view', $theme_registry['views_view']['preprocess functions'], FALSE);
    unset($theme_registry['views_view']['preprocess functions'][$current_key]);
    // Give it a new key all the way at the end.
    $theme_registry['views_view']['preprocess functions'][] = 'social_lms_integrator_iteration_invite_preprocess_views_view';
  }
}

/**
 * Implements hook_entity_operation_alter().
 */
function social_lms_integrator_iteration_invite_entity_operation_alter(array &$operations, EntityInterface $entity) {
  // Get the node, so we can pass it as a parameter.
  $node = \Drupal::routeMatch()->getParameter('node');
  // Get the route name.
  $route_name = \Drupal::routeMatch()->getRouteName();
  // Get the current user.
  $user_account = \Drupal::currentUser()->getAccount();

  // Check if the entity type is one of iteration_enrollment and that we're on the
  // correct view. Otherwise it would update all actions across the platform.
  if ($entity->getEntityTypeId() === 'iteration_enrollment') {
    // Build operations for the iteration invites overview for the owner/manager.
    if (social_lms_integrator_enrollment_iteration_manager_or_organizer() && $route_name === 'view.iteration_manage_enrollment_invites.page_manage_enrollment_invites') {
      // Empty the current operations.
      $operations = [];
      // Add the "Cancel invite" option.
      $operations['cancel']['title'] = t('Cancel invite');
      $operations['cancel']['url'] = Url::fromRoute('social_lms_integrator_iteration_invite.cancel_enrollment_invite', [
        'node' => $node,
        'iteration_enrollment' => $entity->id(),
      ]);
      // If the user has declined or if the invite is invalid or expired,
      // provide a delete button so that the iteration_enrollment can be
      // deleted from this iteration.
      $delete_statusses = [
        IterationEnrollmentInterface::REQUEST_OR_INVITE_DECLINED,
        IterationEnrollmentInterface::INVITE_INVALID_OR_EXPIRED,
      ];
      if (in_array((int) $entity->field_request_or_invite_status->value, $delete_statusses)) {
        $operations = [];
        // Add the "Delete invite" option.
        $operations['delete']['title'] = t('Remove');
        $operations['delete']['url'] = Url::fromRoute('social_lms_integrator_iteration_invite.cancel_enrollment_invite', [
          'node' => $node,
          'iteration_enrollment' => $entity->id(),
        ]);
      }
      if ((int) $entity->field_request_or_invite_status->value === IterationEnrollmentInterface::INVITE_ACCEPTED_AND_JOINED) {
        $operations = [];
      }
    }
    // Build operations for the users overview for event invites.
    if ($route_name === 'view.user_iteration_invites.page_user_iteration_invites') {
      // Empty the current operations.
      $operations = [];
      // Add the "Accept invite" option.
      $operations['accept']['title'] = t('Accept invite');
      $operations['accept']['url'] = Url::fromRoute('social_lms_integrator_iteration_invite.update_enrollment_invite', [
        'user' => $user_account->id(),
        'iteration_enrollment' => $entity->id(),
        'accept_decline' => '1',
      ]);
      // Add the "Decline invite" option.
      $operations['decline']['title'] = t('Decline invite');
      $operations['decline']['url'] = Url::fromRoute('social_lms_integrator_iteration_invite.update_enrollment_invite', [
        'user' => $user_account->id(),
        'iteration_enrollment' => $entity->id(),
        'accept_decline' => '0',
      ]);
    }
  }
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
/*
function social_lms_integrator_iteration_invite_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface $cacheability) {
  // Add tasks on these route for invite Groups/Events.
  if ($route_name === 'view.user_iteration_invites.page_user_iteration_invites') {
    $tabs_to_remove = [
      'social_user.groups',
      'social_user.stream',
      'social_user.topics',
      'social_user.iterations',
      'social_profile.information',
      'profile.user_page:profile',
      'entity.user.canonical',
      'entity.user.edit_form',
    ];
    foreach ($tabs_to_remove as $task_name) {
      if (!empty($data['tabs'][0][$task_name])) {
        unset($data['tabs'][0][$task_name]);
      }
    }
  }

  if ($route_name !== 'view.user_iteration_invites.page_user_iteration_invites'
    && $route_name !== 'view.social_group_user_invitations.page_1') {
    $tabs_to_remove = [
      'social_event_invite.user_events',
    ];
    foreach ($tabs_to_remove as $task_name) {
      if (!empty($data['tabs'][0][$task_name])) {
        unset($data['tabs'][0][$task_name]);
      }
    }
  }
}
*/

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc