You are here

public static function LearningPathAccess::mergeUserStatus in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/LearningPathAccess.php \Drupal\opigno_learning_path\LearningPathAccess::mergeUserStatus()

Merges Learning Path group user status.

Parameters

\Drupal\Core\Entity\EntityInterface $membership: Membership object.

Throws

\Exception

3 calls to LearningPathAccess::mergeUserStatus()
LearningPathAccess::setLearningPathCourseMember in src/LearningPathAccess.php
Sets Learning Path content course member.
opigno_learning_path_entity_insert in ./opigno_learning_path.module
Implements hook_entity_insert().
opigno_learning_path_entity_update in ./opigno_learning_path.module
Implements hook_entity_update().

File

src/LearningPathAccess.php, line 283

Class

LearningPathAccess
Class LearningPathAccess.

Namespace

Drupal\opigno_learning_path

Code

public static function mergeUserStatus(EntityInterface $membership) {
  $message = \Drupal::request()
    ->get('user_message');
  $message = !empty($message) ? Html::escape($message) : '';

  /** @var \Drupal\group\Entity\GroupContentInterface $membership */
  $group = $membership
    ->getGroup();
  $uid = $membership
    ->getEntity()
    ->id();
  $gid = $group
    ->id();
  $visibility = $group->field_learning_path_visibility->value;

  // Check if we need to wait validation.
  $validation = LearningPathAccess::requiredValidation($group);
  $is_new = $membership
    ->isNew();
  $user_join = $membership
    ->getEntity()
    ->id() == $membership
    ->getOwnerId();
  if ($is_new && $user_join) {
    $status = in_array($visibility, [
      'semiprivate',
      'private',
    ]) && $validation ? 2 : 1;
  }
  elseif (!empty($_SESSION['opigno_learning_path_group_membership_edit']['user_status'])) {
    $status = $_SESSION['opigno_learning_path_group_membership_edit']['user_status'];
    unset($_SESSION['opigno_learning_path_group_membership_edit']);
  }
  else {
    $status = 1;
  }
  $query = \Drupal::database()
    ->merge('opigno_learning_path_group_user_status')
    ->key('mid', $membership
    ->id())
    ->insertFields([
    'mid' => $membership
      ->id(),
    'uid' => $uid,
    'gid' => $gid,
    'status' => $status,
    'message' => $message,
  ])
    ->updateFields([
    'uid' => $uid,
    'gid' => $gid,
    'status' => $status,
    'message' => $message,
  ]);
  $result = $query
    ->execute();
  if ($result) {
    if ($group
      ->bundle() === 'learning_path') {
      $token = \Drupal::moduleHandler()
        ->moduleExists('token') ? TRUE : FALSE;
      LearningPathAccess::notifyUsersByMail($group, $uid, $status, $token);
      if ($membership
        ->isNew() && $membership
        ->getEntity()
        ->id() == $membership
        ->getOwnerId()) {
        $messenger = \Drupal::messenger();
        if ($status == 2) {
          $messenger
            ->addMessage(t('Thanks ! The subscription to that training requires the validation of an administrator or a teacher.
              You will receive an email as soon as your subscription has been validated.'));
        }
      }
    }
  }
}