You are here

public function DeleteAchievementsForm::submitForm in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Form/DeleteAchievementsForm.php \Drupal\opigno_learning_path\Form\DeleteAchievementsForm::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/DeleteAchievementsForm.php, line 130

Class

DeleteAchievementsForm
Class LearningPathAdminSettingsForm.

Namespace

Drupal\opigno_learning_path\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $element = $form_state
    ->getTriggeringElement();
  if (isset($element['#name'])) {
    if ($element['#name'] === 'submit') {
      $uid = $this
        ->currentUser()
        ->id();
      $gid = $form_state
        ->getValue('group');
      if (isset($gid)) {
        $this->database
          ->delete('opigno_learning_path_step_achievements')
          ->condition('uid', $uid)
          ->condition('gid', $gid)
          ->execute();
        $this->database
          ->delete('opigno_learning_path_achievements')
          ->condition('uid', $uid)
          ->condition('gid', $gid)
          ->execute();
        $group = Group::load($gid);
        if (isset($group)) {

          // Load modules of a LP group.
          $modules = $group
            ->getContentEntities('opigno_module_group');

          // Delete all answers connected to this user and LP.
          $this
            ->deleteAnswers((int) $group
            ->id(), $uid, $modules);
          $module = reset($modules);
          if (!empty($module)) {

            // Create new unfinished user module attempt on the first module
            // of training to disable the training resume.
            $attempt = UserModuleStatus::create([]);
            $attempt
              ->setModule($module);
            $attempt
              ->setFinished(0);
            $attempt
              ->save();
          }
        }
        $form_state
          ->setRedirect('opigno_learning_path.steps.start', [
          'group' => $gid,
        ]);
      }
    }
    elseif ($element['#name'] === 'cancel') {
      $form_state
        ->setRedirectUrl($this
        ->getCancelUrl());
    }
  }
}