You are here

public function RecipientsPlugin::getMembersForm in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Plugin/LearningPathMembers/RecipientsPlugin.php \Drupal\opigno_learning_path\Plugin\LearningPathMembers\RecipientsPlugin::getMembersForm()

Get members form.

Parameters

array $form: Form array.

mixed $form_state: Form state.

mixed $current_user: User object.

Return value

mixed From.

Overrides LearningPathMembersPluginBase::getMembersForm

File

src/Plugin/LearningPathMembers/RecipientsPlugin.php, line 21

Class

RecipientsPlugin
Class RecipientsPlugin.

Namespace

Drupal\opigno_learning_path\Plugin\LearningPathMembers

Code

public function getMembersForm(array &$form, FormStateInterface $form_state, User $current_user) {

  // Get the groups, this allows to filter the available users.
  $classes = opigno_messaging_get_groups('opigno_class');
  $learning_paths = opigno_messaging_get_groups('learning_path');
  $form['filters'] = [
    '#type' => 'container',
    '#weight' => -2,
  ];
  $form['filters']['title'] = [
    '#type' => 'label',
    '#title' => t('Participants'),
  ];
  $form['filters']['class'] = [
    '#type' => 'select',
    '#options' => [
      'All' => t('Filter by class'),
    ],
    '#default_value' => t('All'),
    '#ajax' => [
      'callback' => [
        $this,
        'updateMembersAjax',
      ],
      'event' => 'change',
      'wrapper' => 'users-to-send',
      'progress' => [
        'type' => 'throbber',
        'message' => t('Verifying entry...'),
      ],
    ],
  ];
  $form['filters']['learning_path'] = [
    '#type' => 'select',
    '#wrapper_attributes' => [
      'class' => [
        'col-6',
      ],
    ],
    '#options' => [
      'All' => t('Filter by training'),
    ],
    '#default_value' => t('All'),
    '#ajax' => [
      'callback' => [
        $this,
        'updateMembersAjax',
      ],
      'event' => 'change',
      'wrapper' => 'users-to-send',
      'progress' => [
        'type' => 'throbber',
        'message' => t('Verifying entry...'),
      ],
    ],
  ];
  $options = [];
  foreach ($classes as $class) {
    $id = $class['entity_id'];
    $options[$id] = $class['title'];
  }
  uasort($options, 'strcasecmp');
  $form['filters']['class']['#options'] += $options;
  $options = [];
  foreach ($learning_paths as $learning_path) {
    $id = $learning_path['entity_id'];
    $options[$id] = $learning_path['title'];
  }
  uasort($options, 'strcasecmp');
  $form['filters']['learning_path']['#options'] += $options;

  // Get the users for the specific group.
  $current_user = \Drupal::currentUser();
  $show_all = $current_user
    ->hasPermission('message anyone regardless of groups');
  $users = opigno_messaging_get_all_recipients($show_all);
  $options = [];
  foreach ($users as $user) {
    $options[$user
      ->id()] = $user
      ->getDisplayName();
  }

  // Remove the current users from the list of users
  // that once can send a message to.
  if (isset($options[$current_user
    ->id()])) {
    unset($options[$current_user
      ->id()]);
  }

  // Sort the users by name.
  uasort($options, 'strcasecmp');
  $form['users_to_send'] = [
    '#title' => t('Select the users you want to send a message to'),
    '#type' => 'multiselect',
    '#options' => $options,
    '#weight' => -1,
    '#prefix' => '<div id="users-to-send">',
    '#suffix' => '</div>',
    // Fixes multiselect issue 2852654.
    '#process' => [
      [
        'Drupal\\multiselect\\Element\\MultiSelect',
        'processSelect',
      ],
    ],
  ];
}