public function QuizResultViewBuilder::alterBuild in Quiz 8.5
Same name and namespace in other branches
- 8.6 src/View/QuizResultViewBuilder.php \Drupal\quiz\View\QuizResultViewBuilder::alterBuild()
- 6.x 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 18
Class
Namespace
Drupal\quiz\ViewCode
public function alterBuild(array &$build, Drupal\Core\Entity\EntityInterface $entity, Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
/* @var $entity QuizResult */
$render_controller = Drupal::entityTypeManager()
->getViewBuilder('quiz_result_answer');
if (!$entity->is_evaluated && empty($_POST)) {
$msg = t('Parts of this @quiz have not been evaluated yet. The score below is not final.', array(
'@quiz' => _quiz_get_quiz_name(),
));
$this
->messenger()
->addWarning($msg);
}
$score = $entity
->score();
$account = User::load($entity
->get('uid')
->getString());
if ($display
->getComponent('questions')) {
$questions = array();
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);
$questions[$question
->id()] = array(
'#title' => t('Question @num', array(
'@num' => $qra
->get('display_number')
->getString(),
)),
'#type' => 'fieldset',
'feedback' => $feedback,
'#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 = array(
'%num_correct' => $score['numeric_score'],
'%question_count' => $score['possible_score'],
'@username' => $account
->id() == $account
->id() ? t('You') : theme('username', array(
'account' => $account,
)),
'@score' => $score['percentage_score'],
'@yourtotal' => $account
->id() == $account
->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 (!\Drupal\Core\Render\Element::children($build)) {
$build['no_feedback_text']['#markup'] = t('You have finished this @quiz.', array(
'@quiz' => _quiz_get_quiz_name(),
));
}
return $build;
}