You are here

public function QuizResult::getLayout in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::getLayout()
  2. 6.x src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::getLayout()

Get the layout for this quiz result.

The layout contains the questions to be delivered.

Return value

QuizResultAnswer[]

3 calls to QuizResult::getLayout()
QuizResult::copyToQuizResult in src/Entity/QuizResult.php
Copy this result's answers onto another Quiz result, based on the new Quiz result's settings.
QuizResult::finalize in src/Entity/QuizResult.php
Score a quiz result.
QuizResult::score in src/Entity/QuizResult.php
Calculates the score user received on quiz.

File

src/Entity/QuizResult.php, line 76

Class

QuizResult
Defines the Quiz entity class.

Namespace

Drupal\quiz\Entity

Code

public function getLayout() {
  if ($this
    ->isNew()) {

    // New results do not have layouts yet.
    return [];
  }
  $quiz_result_answers = Drupal::entityTypeManager()
    ->getStorage('quiz_result_answer')
    ->loadByProperties([
    'result_id' => $this
      ->id(),
  ]);

  // @todo when we load the layout we have to load the question relationships
  // too because we need to know the parentage
  $quiz_question_relationship = Drupal::entityTypeManager()
    ->getStorage('quiz_question_relationship')
    ->loadByProperties([
    'quiz_vid' => $this
      ->get('vid')
      ->getString(),
  ]);
  $id_qqr = [];
  foreach ($quiz_question_relationship as $rel) {
    $id_qqr[$rel
      ->get('question_id')
      ->getString()] = $rel;
  }
  $layout = [];
  foreach ($quiz_result_answers as $quiz_result_answer) {
    $layout[$quiz_result_answer
      ->get('number')
      ->getString()] = $quiz_result_answer;
    if (isset($id_qqr[$quiz_result_answer
      ->get('question_id')
      ->getString()])) {

      // Question is in a relationship.
      // @todo better way to do this? We need to load the relationship
      // hierarchy onto the result answers.
      $quiz_result_answer->qqr_id = $id_qqr[$quiz_result_answer
        ->get('question_id')
        ->getString()]
        ->get('qqr_id')
        ->getString();
      $quiz_result_answer->qqr_pid = $id_qqr[$quiz_result_answer
        ->get('question_id')
        ->getString()]
        ->get('qqr_pid')
        ->getString();
    }
  }
  ksort($layout, SORT_NUMERIC);
  return $layout;
}