You are here

public function QuizQuestionsForm::submitForm in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 src/Form/QuizQuestionsForm.php \Drupal\quiz\Form\QuizQuestionsForm::submitForm()
  2. 6.x src/Form/QuizQuestionsForm.php \Drupal\quiz\Form\QuizQuestionsForm::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/QuizQuestionsForm.php, line 206

Class

QuizQuestionsForm
Form to manage questions in a quiz.

Namespace

Drupal\quiz\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $question_list = $form_state
    ->getValue('question_list');
  foreach ($question_list as $qqr_id => $row) {
    $qqr = \Drupal\quiz\Entity\QuizQuestionRelationship::load($qqr_id);
    foreach ($row as $name => $value) {
      if ($name == 'qqr_pid' && empty($value)) {
        $value = NULL;
      }
      $qqr
        ->set($name, $value);
    }
    $qqr
      ->save();
  }
  \Drupal::messenger()
    ->addMessage(t('Questions updated successfully.'));
  return;

  // @todo below logic in D8, maybe we get rid of "remove questions"
  //
  // Load the quiz node.
  $quiz = $form_state['build_info']['args'][0];

  // Update the refresh latest quizzes table so that we know what the users
  // latest quizzes are.
  if (\Drupal::config('quiz.settings')
    ->get('auto_revisioning', 1)) {
    $is_new_revision = $quiz
      ->hasAttempts();
  }
  else {
    $is_new_revision = !empty($form_state['values']['new_revision']);
  }
  $num_random = isset($form_state['values']['num_random_questions']) ? $form_state['values']['num_random_questions'] : 0;
  $quiz->max_score_for_random = isset($form_state['values']['max_score_for_random']) ? $form_state['values']['max_score_for_random'] : 1;

  // Store what questions belong to the quiz.
  $questions = _quiz_update_items($quiz, $weight_map, $max_scores, $auto_update_max_scores, $is_new_revision, $refreshes, $stayers, $qnr_ids_map, $qqr_pids_map, $compulsories);

  // If using random questions and no term ID is specified, make sure we have
  // enough.
  $assigned_random = 0;
  foreach ($questions as $question) {
    if ($question->question_status == QUIZ_QUESTION_RANDOM) {
      ++$assigned_random;
    }
  }

  // Adjust number of random questions downward to match number of selected
  // questions.
  if ($num_random > $assigned_random) {
    $num_random = $assigned_random;
    \Drupal::messenger()
      ->addWarning(t('The number of random questions for this @quiz have been lowered to %anum to match the number of questions you assigned.', array(
      '@quiz' => _quiz_get_quiz_name(),
      '%anum' => $assigned_random,
    )));
  }
  if ($quiz->type == 'quiz') {

    // Update the quiz node properties.
    db_update('quiz_node_properties')
      ->fields(array(
      'number_of_random_questions' => $num_random ? $num_random : 0,
      'max_score_for_random' => $quiz->max_score_for_random,
    ))
      ->condition('vid', $quiz->vid)
      ->condition('nid', $quiz->nid)
      ->execute();

    // Get sum of max_score.
    $query = db_select('quiz_node_relationship', 'qnr');
    $query
      ->addExpression('SUM(max_score)', 'sum');
    $query
      ->condition('parent_vid', $quiz->vid);
    $query
      ->condition('question_status', QUIZ_QUESTION_ALWAYS);
    $score = $query
      ->execute()
      ->fetchAssoc();
    db_update('quiz_node_properties')
      ->expression('max_score', 'max_score_for_random * number_of_random_questions + :sum', array(
      ':sum' => (int) $score['sum'],
    ))
      ->condition('vid', $quiz->vid)
      ->execute();
  }
}