View source
<?php
namespace Drupal\quiz\Form;
use Drupal;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\quiz\Entity\QuizResult;
class QuizQuestionAnsweringForm extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state) : array {
$questions = $form_state
->getBuildInfo()['args'][0];
$quiz_result = QuizResult::load($form_state
->getBuildInfo()['args'][1]);
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$form['#quiz'] = array(
'nid' => $quiz->nid,
'vid' => $quiz->vid,
);
$form['#quiz_result'] = $quiz_result;
if (!is_array($questions)) {
if ($questions
->bundle() == 'page') {
foreach ($quiz_result
->getLayout() as $qra) {
if ($qra
->get('question_id')
->getString() == $questions
->id()) {
$questions = array(
$questions,
);
foreach ($quiz_result
->getLayout() as $qra2) {
if ($qra2->qqr_pid == $qra->qqr_id) {
$questions[] = $qra2
->getQuizQuestion();
}
}
break;
}
}
}
else {
$questions = array(
$questions
->id() => $questions,
);
}
}
$form['#attributes']['class'] = array(
'answering-form',
);
$form['#tree'] = TRUE;
foreach ($questions as $question) {
$class = Html::getClass('quiz-question-' . $question
->bundle());
$qra = $question
->getResponse($quiz_result);
$element = $question
->getAnsweringForm($form_state, $qra);
$build = \Drupal::entityTypeManager()
->getViewBuilder('quiz_question')
->view($question, 'question');
$form['question'][$question
->id()] = array(
'#attributes' => array(
'class' => array(
$class,
),
),
'#type' => 'container',
'header' => !$qra
->get('display_number')
->isEmpty() ? array(
'#markup' => "<h2>" . t("Question @question", array(
'@question' => $qra
->get('display_number')
->getString(),
)) . "</h2>",
) : NULL,
'question' => $build,
'answer' => $element,
);
$form['question'][$question
->id()]['answer']['#quiz_result_answer'] = $qra;
$blank_and_change = $qra
->get('is_skipped')
->getString() && $quiz
->get('allow_change_blank')
->getString();
if (!$quiz
->get('allow_change')
->getString() && !$qra
->get('answer_timestamp')
->isEmpty()) {
if ($blank_and_change) {
}
else {
$form['question'][$question
->id()]['#disabled'] = TRUE;
if (empty($form_state
->getUserInput())) {
\Drupal::messenger()
->addWarning(t('Changing answers is disabled.'));
}
}
}
if ($quiz
->get('mark_doubtful')
->getString() && $question
->isQuestion()) {
$form['question'][$question
->id()]['is_doubtful'] = array(
'#type' => 'checkbox',
'#title' => t('Doubtful?'),
'#default_value' => $qra
->get('is_doubtful')
->getString(),
);
}
}
$is_last = $quiz
->isLastQuestion();
$form['navigation']['#type'] = 'actions';
$form['navigation']['#theme'] = 'quiz_question_navigation_form';
$form['navigation']['submit_hidden'] = array(
'#weight' => -9999,
'#type' => 'submit',
'#value' => $is_last ? t('Finish') : t('Next'),
'#attributes' => array(
'style' => 'display: none',
),
);
if ($quiz
->get('backwards_navigation')
->getString() && $_SESSION['quiz'][$quiz
->id()]['current'] != 1) {
$form['navigation']['back'] = array(
'#weight' => 10,
'#type' => 'submit',
'#value' => t('Back'),
'#submit' => array(
'::submitBack',
),
'#limit_validation_errors' => array(),
);
if ($is_last) {
$form['navigation']['#last'] = TRUE;
$form['navigation']['last_text'] = array(
'#weight' => 0,
'#markup' => '<p><em>' . t('This is the last question. Press Finish to deliver your answers') . '</em></p>',
);
}
}
$form['navigation']['submit'] = array(
'#weight' => 30,
'#type' => 'submit',
'#value' => $is_last ? t('Finish') : t('Next'),
'#ajax' => [],
);
if ($is_last && $quiz
->get('backwards_navigation')
->getString() && !$quiz
->get('repeat_until_correct')
->getString()) {
$form['#attributes']['class'][] = 'quiz-answer-confirm';
$form['#attributes']['data-confirm-message'] = t("By proceeding you won't be able to go back and edit your answers.");
$form['#attached']['library'][] = 'quiz/confirm';
}
if ($quiz
->get('allow_skipping')
->getString()) {
$form['navigation']['skip'] = array(
'#weight' => 20,
'#type' => 'submit',
'#value' => $is_last ? t('Leave blank and finish') : t('Leave blank'),
'#access' => $question->type == 'quiz_directions' ? FALSE : TRUE,
'#submit' => array(
'::submitBlank',
),
'#limit_validation_errors' => array(),
);
}
return $form;
}
public function getFormId() {
return 'quiz_question_answering_form';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('quiz.settings');
$result_id = $form_state
->getBuildInfo()['args'][1];
$feedback_count = 0;
$quiz_result = QuizResult::load($result_id);
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$time_reached = $quiz
->get('time_limit')
->getString() > 0 && \Drupal::time()
->getRequestTime() > $quiz_result
->get('time_start')
->getString() + $quiz
->get('time_limit')
->getString() + $config
->get('time_limit_buffer', 5);
$layout = $quiz_result
->getLayout();
if ($time_reached) {
\Drupal::messenger()
->addError(t('The last answer was not submitted, as the time ran out.'));
}
else {
$submitted = $form_state
->getValue('question');
foreach ($layout as $qra) {
if (isset($submitted[$qra
->get('question_id')
->getString()])) {
$qqid = $qra
->get('question_id')
->getString();
$qra
->set('answer_timestamp', \Drupal::time()
->getRequestTime());
$qra->points_awarded = $qra
->score($form_state
->getValues()['question'][$qqid]) * $qra
->getWeightedRatio();
$qra->is_skipped = FALSE;
$qra->is_doubtful = !empty($form_state
->getValues()['question'][$qqid]['is_doubtful']) ? 1 : 0;
$qra
->save();
$feedback_count += $qra
->getQuizQuestion()
->hasFeedback();
$quiz_result
->setQuestion($qra
->get('number')
->getString() + 1);
}
}
}
$next_number = $_SESSION['quiz'][$quiz
->id()]['current'];
if ($time_reached || !isset($layout[$_SESSION['quiz'][$quiz
->id()]['current']])) {
$this
->submitFinalize($form, $form_state);
}
else {
$form_state
->setRedirect('quiz.question.take', [
'quiz' => $quiz
->id(),
'question_number' => $_SESSION['quiz'][$quiz
->id()]['current'],
]);
}
if ($quiz
->get('review_options')
->get(0) && !empty($quiz
->get('review_options')
->get(0)
->getValue()['question']) && array_filter($quiz
->get('review_options')
->get(0)
->getValue()['question']) && $feedback_count) {
$form_state
->setRedirect('quiz.question.feedback', [
'quiz' => $quiz
->id(),
'question_number' => $next_number - 1,
]);
}
}
public function submitBlank(array $form, FormStateInterface $form_state) {
$quiz_result = QuizResult::load($form_state
->getBuildInfo()['args'][1]);
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$questions = $quiz_result
->getLayout();
if (!empty($form_state
->getUserInput()['question'])) {
foreach (array_keys($form_state
->getUserInput()['question']) as $qqid) {
foreach ($quiz_result
->getLayout() as $idx => $qra) {
if ($qra
->get('question_id')
->getString() == $qqid) {
$qra
->set('is_skipped', TRUE);
$qra
->set('is_correct', FALSE);
$qra
->set('points_awarded', FALSE);
$qra
->set('answer_timestamp', \Drupal::time()
->getRequestTime());
$qra
->save();
}
}
$quiz_result
->setQuestion($_SESSION['quiz'][$quiz
->id()]['current'] + 1);
}
}
else {
$quiz_result
->setQuestion($quiz, $_SESSION['quiz'][$quiz
->id()]['current'] + 1);
}
$form_state
->setRedirect('quiz.question.take', [
'quiz' => $quiz
->id(),
'question_number' => $_SESSION['quiz'][$quiz
->id()]['current'],
]);
$layout = $quiz_result
->getLayout();
if (!isset($layout[$_SESSION['quiz'][$quiz
->id()]['current']])) {
$this
->submitFinalize($form, $form_state);
}
}
function submitFinalize(array $form, FormStateInterface $form_state) {
$quiz_result = QuizResult::load($form_state
->getBuildInfo()['args'][1]);
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$quiz_result
->finalize();
if (empty($quiz->review_options['question']) || !array_filter($quiz->review_options['question']) || empty($form_state['feedback'])) {
$form_state
->setRedirect('entity.quiz_result.canonical', [
'quiz' => $quiz
->id(),
'quiz_result' => $quiz_result
->id(),
]);
}
$_SESSION['quiz']['temp']['result_id'] = $quiz_result
->id();
unset($_SESSION['quiz'][$quiz
->id()]);
}
function submitBack(&$form, FormStateInterface $form_state) {
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($form['#quiz']['vid']
->getString());
$quiz_result = QuizResult::load($_SESSION['quiz'][$quiz
->id()]['result_id']);
$quiz_result
->setQuestion($_SESSION['quiz'][$quiz
->id()]['current'] - 1);
$form_state
->setRedirect('quiz.question.take', [
'quiz' => $quiz
->id(),
'question_number' => $_SESSION['quiz'][$quiz
->id()]['current'],
]);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('quiz.settings');
$quiz_result = QuizResult::load($form_state
->getBuildInfo()['args'][1]);
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$questions = $quiz_result
->getLayout();
$time_reached = !$quiz
->get('time_limit')
->isEmpty() && \Drupal::time()
->getRequestTime() > $quiz_result
->get('time_start')
->getString() + $quiz
->get('time_limit')
->getString() + $config
->get('quiz_time_limit_buffer', 5);
if ($time_reached) {
return;
}
}
}