social_lms_integrator-1.0.0-beta4/modules/social_lms_integrator_enrollment/social_lms_integrator_enrollment.module
modules/social_lms_integrator_enrollment/social_lms_integrator_enrollment.module
<?php
/**
* @file
* Contains social_lms_integrator_enrollment.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\social_lms_integrator_enrollment\IterationEnrollmentInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\group\GroupMembershipLoaderInterface;
use Drupal\group\Entity\GroupInterface;
function social_lms_integrator_enrollment_get_supervisor_users($supervisor) {
$users = FALSE;
$database = \Drupal::database();
$query = $database->select('profile', 'p');
$query->fields('p', ['uid']);
$query->join('profile__field_supervisor', 'ps', 'p.profile_id = ps.entity_id');
$query->condition('ps.field_supervisor_target_id', $supervisor, '=');
$result = $query->execute();
if ($result) {
foreach($result as $value) {
$users[$value->uid] = $value->uid;
}
}
return $users;
}
function social_lms_integrator_enrollment_get_users_supervisor($user) {
$supervisor = FALSE;
$database = \Drupal::database();
$query = $database->select('profile', 'p');
$query->fields('ps', ['field_supervisor_target_id']);
$query->join('profile__field_supervisor', 'ps', 'p.profile_id = ps.entity_id');
$query->condition('p.uid', $user, '=');
$result = $query->execute();
if ($result) {
foreach($result as $value) {
$supervisor = $value->field_supervisor_target_id;
}
}
return $supervisor;
}
/**
* Check if the user is allowed to manage Enrollments.
*
* @param \Drupal\node\NodeInterface|null $node
* The node the current user could be organizer of.
* @param bool $skip_trusted_roles
* Should we skip CM/SM with the manage everything enrollments.
*
* @return bool
* If the user is actually a manager or organizer.
*/
function social_lms_integrator_enrollment_iteration_manager_or_organizer(NodeInterface $node = NULL, $skip_trusted_roles = FALSE) {
$social_iteration_manager_or_organizer = &drupal_static(__FUNCTION__);
if (!isset($social_iteration_manager_or_organizer)) {
$account = \Drupal::currentUser();
// Allow if user has the manage everything permission.
// We can skip this to make sure we truly only check organizer & managers
// used for context in notifications.
if ($skip_trusted_roles === FALSE && $account->hasPermission('manage everything iteration enrollments')) {
$result = TRUE;
}
if (!$node && !isset($result)) {
// Get the current iteration node.
$node = social_lms_integrator_iteration_get_current_iteration();
// If there's no node, we might be looking at an iteration enrollment.
if (!$node) {
// If we are altering / deleting an Iteration Enrollment check if user
// is manager or organizer from the referenced node.
$iteration_enrollment = \Drupal::routeMatch()
->getParameter('iteration_enrollment');
if ($iteration_enrollment instanceof IterationEnrollmentInterface) {
$node = $iteration_enrollment->field_iteration->entity;
}
}
}
// If we now have a node we can check if there are iteration managers.
if ($node instanceof NodeInterface && !isset($result) && $node->bundle() === 'iteration') {
// The iteration owner has access.
if ($node->getOwnerId() === $account->id()) {
$result = TRUE;
}
// Check if the user is an iteration manager/organizer.
if (!isset($result) && $node->hasField('field_iteration_managers') && !$node->field_iteration_managers->isEmpty()) {
foreach ($node->field_iteration_managers->getValue() as $value) {
if ($value && $value['target_id'] === $account->id()) {
$result = TRUE;
break;
}
}
}
// Check if the user has the edit group permission.
$group = _social_group_get_current_group($node);
if ($group instanceof GroupInterface) {
$account = \Drupal::currentUser();
if ($member = $group->getMember($account)) {
if ($member->hasPermission('edit group')) {
$result = TRUE;
}
}
}
}
// No result means the user does not have access.
if (!isset($result)) {
$result = FALSE;
}
$social_iteration_manager_or_organizer = $result;
}
return $social_iteration_manager_or_organizer;
}
function social_lms_integrator_enrollment_iteration_enrollment_insert(EntityInterface $entity) {
// Check only enrolled records
if ($entity->field_enrollment_status->value == 1) {
// Get the nid of the iteration
$nid = $entity->field_iteration->entity->id();
// Get the owner
$uid = $entity->field_account->target_id;
// Get Account
$account = \Drupal::entityTypeManager()
->getStorage('user')
->load($uid);
$groupHelperService = \Drupal::service('social_group.helper_service');
$entityTypeManager = \Drupal::service('entity_type.manager');
// We need to get the group via groupHelperService
$gid_from_entity = $groupHelperService->getGroupFromEntity([
'target_type' => 'node',
'target_id' => $nid,
]);
//\Drupal::logger('social_lms_integrator_enrollment')->notice($gid_from_entity);
if ($gid_from_entity !== NULL) {
/** @var \Drupal\group\Entity\GroupInterface $group */
$group = $entityTypeManager
->getStorage('group')
->load($gid_from_entity);
}
if ($group instanceof GroupInterface) {
$is_member = $group->getMember($account) instanceof GroupMembershipLoaderInterface;
// Only add member if not already a member
if (!$is_member) {
$group->addMember($account);
}
}
}
}
function social_lms_integrator_enrollment_iteration_enrollment_update(EntityInterface $entity) {
// Check only enrolled records
if ($entity->field_enrollment_status->value === '1' &&
$entity->field_request_or_invite_status->value === IterationEnrollmentInterface::REQUEST_APPROVED) {
// Get the nid of the iteration
$nid = $entity->field_iteration->entity->id();
// Get the owner
$uid = $entity->field_account->target_id;
$enrollment_request = \Drupal::service('social_lms_integrator_mail.mail_enrollment_request_message_deliverer');
// Add user to group
$enrollment_request->addEnrollee($nid,$uid);
// Send message
$enrollment_request->sendApproveMessage($nid,$uid);
}
if ($entity->field_request_or_invite_status->value === IterationEnrollmentInterface::REQUEST_OR_INVITE_DECLINED) {
// Get the nid of the iteration
$nid = $entity->field_iteration->entity->id();
// Get the owner
$uid = $entity->field_account->target_id;
$enrollment_request = \Drupal::service('social_lms_integrator_mail.mail_enrollment_request_message_deliverer');
// Send message
$enrollment_request->sendDeclineMessage($nid,$uid);
}
// Check only enrolled records
if ($entity->field_enrollment_status->value === '1' &&
$entity->field_request_or_invite_status->value === IterationEnrollmentInterface::INVITE_ACCEPTED_AND_JOINED) {
// Get the nid of the iteration
$nid = $entity->field_iteration->entity->id();
// Get the owner
$uid = $entity->field_account->target_id;
$enrollment_request = \Drupal::service('social_lms_integrator_mail.mail_enrollment_request_message_deliverer');
// Add user to group
$enrollment_request->addEnrollee($nid,$uid);
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
* Delete all iteration enrollment records on user delete
*/
function social_lms_integrator_enrollment_user_delete(EntityInterface $entity) {
$storage = \Drupal::entityTypeManager()->getStorage('iteration_enrollment');
$entities = $storage->loadByProperties(['user_id' => $entity->id()]);
$storage->delete($entities);
}
/**
* Implements hook_entity_operation_alter().
*/
function social_lms_integrator_enrollment_entity_operation_alter(array &$operations, EntityInterface $entity) {
// Check access first.
if (!social_lms_integrator_enrollment_iteration_manager_or_organizer()) {
return;
}
// Get the node, so we can pass it as a parameter.
$node = \Drupal::routeMatch()->getParameter('node');
// 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' && \Drupal::routeMatch()->getRouteName() === 'view.iteration_manage_enrollment_requests.page_manage_enrollment_requests') {
// Empty the current operations.
$operations = [];
// Add the "Approve" option.
$operations['approve']['title'] = t('Approve');
$operations['approve']['url'] = Url::fromRoute('social_lms_integrator_enrollment.update_enrollment_request', [
'node' => $node,
'iteration_enrollment' => $entity->id(),
'approve' => 1,
]);
// Add the "Decline" option.
$operations['decline']['title'] = t('Decline');
$operations['decline']['url'] = Url::fromRoute('social_lms_integrator_enrollment.enrollment_request_decline_form', [
'node' => $node,
'iteration_enrollment' => $entity->id(),
]);
}
}
