You are here

public function ModuleResultForm::submitForm in Opigno module 8

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

Class

ModuleResultForm
Class ModuleResultForm.

Namespace

Drupal\opigno_module\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $build_info = $form_state
    ->getBuildInfo();
  $module = $build_info['args'][0];
  $user_status = $build_info['args'][1];
  $answer_storage = \Drupal::entityTypeManager()
    ->getStorage('opigno_answer');
  $form_values = $form_state
    ->getValues();
  foreach ($form_values['answers'] as $answer_id => $value) {
    if (isset($value['score'])) {
      $answer = $answer_storage
        ->load($answer_id);
      $answer
        ->setScore($value['score']);
      $answer
        ->setEvaluated(1);
      $answer
        ->save();

      // Send email to the user.
      $this
        ->sendEmailToManager($module, $user_status, $answer);
    }
  }
  $score = $user_status
    ->calculateScore();
  $max_score = $user_status
    ->calculateMaxScore();
  if ($max_score > 0) {
    $percents = round($score / $max_score * 100);
  }
  else {
    $percents = 100;
  }
  $user_status
    ->setScore((int) $percents);
  $user_status
    ->setMaxScore($max_score);
  $user_status
    ->setEvaluated(1);
  $user_status
    ->save();
  if (function_exists('opigno_learning_path_get_all_steps')) {
    $module_id = $module
      ->id();
    $uid = $user_status
      ->get('user_id')
      ->getValue();
    $uid = $uid[0]['target_id'];
    $gid = $user_status
      ->get('learning_path')
      ->getValue();
    $gid = $gid[0]['target_id'];
    $step = [];
    $steps = opigno_learning_path_get_all_steps($gid, $uid);
    if ($steps) {
      foreach ($steps as $item) {
        if ($item["id"] == $module_id) {
          $step = $item;
          break;
        }
      }
    }
    if ($step) {

      // Save module step achievements.
      opigno_learning_path_save_step_achievements($gid, $uid, $step, isset($step["parent"]) ? $step["parent"] : 0);
    }

    // Save training achievements.
    opigno_learning_path_save_achievements($gid, $uid);
  }
  $form_state
    ->setRedirect('view.opigno_score_modules.opigno_not_evaluated');
}