You are here

public function ILTScoreForm::submitForm in Opigno Instructor-led Trainings 8

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

Class

ILTScoreForm
Provides a form for scoring a opigno_ilt entity.

Namespace

Drupal\opigno_ilt\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $entity = $this->opigno_ilt;
  $scores = $form_state
    ->getValue('submit_scores');
  foreach ($scores as $user_id => $values) {
    $status = $values['attendance'];
    $score = $values['score'];

    // Try load existing result.

    /** @var \Drupal\opigno_ilt\ILTResultInterface[] $results */
    $results = $this->entityTypeManager
      ->getStorage('opigno_ilt_result')
      ->loadByProperties([
      'opigno_ilt' => $entity
        ->id(),
      'user_id' => $user_id,
    ]);
    $result = current($results);
    if ($result === FALSE) {

      // Create new result.
      $result = ILTResult::create();
      $result
        ->setILT($entity);
      $result
        ->setUserId($user_id);
    }

    // Update values.
    $result
      ->setStatus($status);
    $result
      ->setScore($score);
    $result
      ->save();

    // Update user achievements.
    $gid = $entity
      ->getTrainingId();
    if (isset($gid)) {
      $step = opigno_learning_path_get_ilt_step($gid, $user_id, $entity);
      opigno_learning_path_save_step_achievements($gid, $user_id, $step, 0);
      opigno_learning_path_save_achievements($gid, $user_id, TRUE);
    }
  }
}