You are here

public static function MembersPlugin::updateMembersAjax in Opigno Learning path 8

File

src/Plugin/LearningPathMembers/MembersPlugin.php, line 157

Class

MembersPlugin
Class MembersPlugin.

Namespace

Drupal\opigno_learning_path\Plugin\LearningPathMembers

Code

public static function updateMembersAjax(array $form, FormStateInterface $form_state) {
  $storage = $form_state
    ->getStorage();
  $members_wrapper =& $form['members']['field_calendar_event_members'];
  $members =& $members_wrapper['widget'];
  $current_user = \Drupal::currentUser();
  $show_all = $current_user
    ->hasPermission('add any members to calendar event') ? TRUE : FALSE;

  // Get the current values that are selected.
  $current_selected_users = $form_state
    ->getValue('field_calendar_event_members');

  // Keep the users that were previously selected as selected.
  foreach ($current_selected_users as $user) {
    $members['#default_value'][$user['target_id']] = $user['target_id'];
  }

  // Remove from the list of option users the ones that are not selected.
  foreach ($members['#options'] as $uid => $name) {
    if (!in_array($uid, $members['#default_value'])) {
      unset($members['#options'][$uid]);
    }
  }

  // Add to the users of the new group to the options.
  $class_id = $form_state
    ->getValue('class');
  $learning_path_id = $form_state
    ->getValue('training');
  if (!is_numeric($class_id)) {
    $class_id = 0;
  }
  if (!is_numeric($learning_path_id)) {
    $learning_path_id = 0;
  }
  $class_users = opigno_messaging_get_user_for_group($class_id, $show_all);
  $learning_path_users = opigno_messaging_get_user_for_group($learning_path_id, $show_all);
  if (!empty($class_id) && !empty($learning_path_id)) {

    // Chosen both class or training.
    $users = array_uintersect($class_users, $learning_path_users, function ($user1, $user2) {

      /** @var \Drupal\user\UserInterface $user1 */

      /** @var \Drupal\user\UserInterface $user2 */
      return $user2
        ->id() - $user1
        ->id();
    });
  }
  elseif (!empty($class_id) || !empty($learning_path_id)) {

    // Chosen only class or training.
    $users = array_merge($class_id ? $class_users : [], $learning_path_id ? $learning_path_users : []);
  }
  else {

    // No class or training were chosen.
    $users = opigno_messaging_get_all_recipients($show_all);
  }
  $allowed_uids = $storage["allowed_uids"];

  // Filter members options.
  foreach ($users as $user) {
    $uid = $user
      ->id();
    if (!empty($storage['show_all'])) {

      // Add all the members.

      /** @var \Drupal\user\UserInterface $user */
      $members['#options'][$user
        ->id()] = $user
        ->getDisplayName();
    }
    elseif ($allowed_uids && in_array($uid, $allowed_uids)) {

      // Add only members from current user groups.

      /** @var \Drupal\user\UserInterface $user */
      $members['#options'][$user
        ->id()] = $user
        ->getDisplayName();
    }
  }
  $user = \Drupal::currentUser();
  $uid = $user
    ->id();
  if (!empty($members["#options"]) && array_key_exists($uid, $members["#options"])) {
    unset($members["#options"][$uid]);
  }
  uasort($members['#options'], 'strcasecmp');
  return $members_wrapper;
}