You are here

protected function MultichoiceResponse::orderAlternatives in Quiz 8.5

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. 6.x 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 165

Class

MultichoiceResponse
Extension of QuizQuestionResponse.

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

protected function orderAlternatives(array &$alternatives) {
  if (!$this->question->choice_random) {
    return;
  }
  $result = db_query('SELECT choice_order FROM {quiz_multichoice_user_answers}
            WHERE result_answer_id = :raid', array(
    ':raid' => $this->result_answer_id,
  ))
    ->fetchField();
  if (!$result) {
    return;
  }
  $order = explode(',', $result);
  $newAlternatives = array();
  foreach ($order as $value) {
    foreach ($alternatives as $alternative) {
      if ($alternative['id'] == $value) {
        $newAlternatives[] = $alternative;
        break;
      }
    }
  }
  $alternatives = $newAlternatives;
}