You are here

public function LearningPathCreateClassForm::submitForm in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Form/LearningPathCreateClassForm.php \Drupal\opigno_learning_path\Form\LearningPathCreateClassForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/LearningPathCreateClassForm.php, line 122

Class

LearningPathCreateClassForm
Members create form.

Namespace

Drupal\opigno_learning_path\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $name = $form_state
    ->getValue('name');
  $users = $form_state
    ->getValue('new_class_users');

  // Parse uids.
  $uids = array_map(function ($user) {
    list($type, $id) = explode('_', $user);
    return $id;
  }, $users);

  // Load users.
  $users = User::loadMultiple($uids);

  // Create new class.

  /** @var \Drupal\group\Entity\Group $class */
  $class = Group::create([
    'type' => 'opigno_class',
    'label' => $name,
  ]);
  $class
    ->save();

  // Assign the class to the learning path.
  $group = $this
    ->getRequest()
    ->get('group');
  $group
    ->addContent($class, 'subgroup:opigno_class');

  // Assign users to the class.
  foreach ($users as $user) {
    if (!isset($user)) {
      continue;
    }
    $class
      ->addMember($user);
  }

  // Assign users to the learning path.
  foreach ($users as $user) {
    if (!isset($user)) {
      continue;
    }
    $group
      ->addMember($user);
  }
  $this
    ->messenger()
    ->addMessage(t('New class created'));
}