You are here

public function MeetingScoreForm::submitForm in Opigno Moxtra 8

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

Class

MeetingScoreForm
Provides a form for scoring a opigno_moxtra_meeting entity.

Namespace

Drupal\opigno_moxtra\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\opigno_moxtra\MeetingInterface $entity */
  $entity = $this
    ->getRequest()
    ->get('opigno_moxtra_meeting');
  $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_moxtra\MeetingResultInterface[] $results */
    $results = $this->entityTypeManager
      ->getStorage('opigno_moxtra_meeting_result')
      ->loadByProperties([
      'meeting' => $entity
        ->id(),
      'user_id' => $user_id,
    ]);
    $result = current($results);
    if ($result === FALSE) {

      // Create new result.
      $result = MeetingResult::create();
      $result
        ->setMeeting($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_meeting_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);
    }
  }
}