You are here

public function QuizResultViewBuilder::alterBuild in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 src/View/QuizResultViewBuilder.php \Drupal\quiz\View\QuizResultViewBuilder::alterBuild()
  2. 8.5 src/View/QuizResultViewBuilder.php \Drupal\quiz\View\QuizResultViewBuilder::alterBuild()

Specific per-entity building.

Parameters

array $build: The render array that is being created.

\Drupal\Core\Entity\EntityInterface $entity: The entity to be prepared.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display: The entity view display holding the display options configured for the entity components.

string $view_mode: The view mode that should be used to prepare the entity.

Overrides EntityViewBuilder::alterBuild

File

src/View/QuizResultViewBuilder.php, line 23

Class

QuizResultViewBuilder

Namespace

Drupal\quiz\View

Code

public function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  /* @var $entity QuizResult */
  $render_controller = Drupal::entityTypeManager()
    ->getViewBuilder('quiz_result_answer');
  if ($entity
    ->get('is_invalid')->value && \Drupal::currentUser()
    ->id() == $entity
    ->get('uid')
    ->getString()) {
    \Drupal::messenger()
      ->addWarning(t('Your previous score on this @quiz was equal or better. This result will not be saved.', [
      '@quiz' => QuizUtil::getQuizName(),
    ]));
  }
  if (!$entity->is_evaluated && empty($_POST)) {
    $msg = t('Parts of this @quiz have not been evaluated yet. The score below is not final.', [
      '@quiz' => QuizUtil::getQuizName(),
    ]);
    $this
      ->messenger()
      ->addWarning($msg);
  }
  $score = $entity
    ->score();
  $account = User::load($entity
    ->get('uid')
    ->getString());
  if ($display
    ->getComponent('questions')) {
    $questions = [];
    foreach ($entity
      ->getLayout() as $qra) {

      // Loop through all the questions and get their feedback.
      $question = Drupal::entityTypeManager()
        ->getStorage('quiz_question')
        ->loadRevision($qra
        ->get('question_vid')
        ->getString());
      if (!$question) {

        // Question went missing...
        continue;
      }
      if ($question
        ->hasFeedback() && $entity
        ->hasReview()) {
        $feedback = $render_controller
          ->view($qra);
        $feedback_rendered = \Drupal::service('renderer')
          ->renderRoot($feedback);
        if ($feedback_rendered) {
          $questions[$question
            ->id()] = [
            '#title' => t('Question @num', [
              '@num' => $qra
                ->get('display_number')
                ->getString(),
            ]),
            '#type' => 'fieldset',
            'feedback' => [
              '#markup' => $feedback_rendered,
            ],
            '#weight' => $qra
              ->get('number')
              ->getString(),
          ];
        }
      }
    }
    if ($questions) {
      $build['questions'] = $questions;
    }
  }
  $quiz_feedback['#children'] = '';
  if ($display
    ->getComponent('summary') && $entity
    ->canReview('quiz_feedback')) {
    $summary = $this
      ->getSummaryText($entity);

    // Show quiz feedback.
    if (!empty($summary['passfail'])) {
      $quiz_feedback['#children'] .= '<div id="quiz-summary">' . $summary['passfail'] . '</div>';
    }
    if (!empty($summary['result'])) {
      $quiz_feedback['#children'] .= '<div id="quiz-summary">' . $summary['result'] . '</div>';
    }
  }
  if ($quiz_feedback['#children']) {
    $build['summary']['#children'] = $quiz_feedback['#children'];
  }
  if ($display
    ->getComponent('score') && $entity
    ->canReview('score')) {
    $params = [
      '%num_correct' => $score['numeric_score'],
      '%question_count' => $score['possible_score'],
      '@username' => $account
        ->id() == \Drupal::currentUser()
        ->id() ? t('You') : $account
        ->getDisplayName(),
      '@score' => $score['percentage_score'],
      '@yourtotal' => $account
        ->id() == \Drupal::currentUser()
        ->id() ? t('Your') : t('Total'),
    ];

    // Show score.
    $build['score']['#markup'] = '<div id="quiz_score_possible">' . t('@username got %num_correct of %question_count possible points.', $params) . '</div>' . "\n";
    $build['score']['#markup'] .= '<div id="quiz_score_percent">' . t('@yourtotal score: @score%', $params) . '</div>';
  }
  if (!Element::children($build)) {
    $build['no_feedback_text']['#markup'] = t('You have finished this @quiz.', [
      '@quiz' => QuizUtil::getQuizName(),
    ]);
  }

  // The visibility of feedback may change based on time or other conditions.
  $build['#cache']['max-age'] = 0;
  return $build;
}