View source
<?php
namespace Drupal\quiz\View;
use Drupal;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizResult;
use Drupal\user\Entity\User;
use function _quiz_get_quiz_name;
use function check_markup;
class QuizResultViewBuilder extends EntityViewBuilder {
use \Drupal\Core\Messenger\MessengerTrait;
public function alterBuild(array &$build, Drupal\Core\Entity\EntityInterface $entity, Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$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) {
$question = Drupal::entityTypeManager()
->getStorage('quiz_question')
->loadRevision($qra
->get('question_vid')
->getString());
if (!$question) {
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);
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'),
);
$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;
}
function getSummaryText(QuizResult $quiz_result) {
$config = Drupal::config('quiz.settings');
$quiz = Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$token = Drupal::token();
$account = $quiz_result
->get('uid')
->referencedEntities()[0];
$token_types = array(
'global' => NULL,
'node' => $quiz,
'user' => $account,
'quiz_result' => $quiz_result,
);
$summary = array();
if ($paragraph = $this
->getRangeFeedback($quiz, $quiz_result
->get('score')
->getString())) {
$token = Drupal::token();
$paragraph_text = $paragraph
->get('quiz_feedback')
->get(0)
->getValue();
$summary['result'] = check_markup($token
->replace($paragraph_text['value'], $token_types), $paragraph_text['format']);
}
$pass_text = $quiz
->get('summary_pass')
->getValue()[0];
$default_text = $quiz
->get('summary_default')
->getValue()[0];
if ($config
->get('use_passfail', 1) && $quiz
->get('pass_rate')
->getString() > 0) {
if ($quiz_result
->get('score')
->getString() >= $quiz
->get('pass_rate')
->getString()) {
$summary['passfail'] = check_markup($token
->replace($pass_text['value'], $token_types), $pass_text['format']);
}
else {
$summary['passfail'] = check_markup($token
->replace($default_text['value'], $token_types), $default_text['format']);
}
}
else {
$summary['passfail'] = check_markup($token
->replace($default_text['value'], $token_types), $default_text['format']);
}
return $summary;
}
function getRangeFeedback($quiz, $score) {
foreach ($quiz
->get('result_options')
->referencedEntities() as $paragraph) {
$range = $paragraph
->get('quiz_feedback_range')
->get(0)
->getValue();
if ($score >= $range['from'] && $score <= $range['to']) {
return $paragraph;
}
}
}
}