You are here

protected function MultichoiceResponse::orderAlternatives in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse::orderAlternatives()
  2. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse::orderAlternatives()

Order the alternatives according to the choice order stored in the database.

Parameters

array $alternatives: The alternatives to be ordered.

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php, line 147

Class

MultichoiceResponse
Extension of QuizQuestionResponse.

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

protected function orderAlternatives(array &$alternatives) {
  if (!$this->question->choice_random) {
    return;
  }

  // TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
  // You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
  $result = \Drupal::database()
    ->query('SELECT choice_order FROM {quiz_multichoice_user_answers}
            WHERE result_answer_id = :raid', [
    ':raid' => $this->result_answer_id,
  ])
    ->fetchField();
  if (!$result) {
    return;
  }
  $order = explode(',', $result);
  $newAlternatives = [];
  foreach ($order as $value) {
    foreach ($alternatives as $alternative) {
      if ($alternative['id'] == $value) {
        $newAlternatives[] = $alternative;
        break;
      }
    }
  }
  $alternatives = $newAlternatives;
}