public function QuizQuestionAnsweringForm::submitForm in Quiz 6.x
Same name and namespace in other branches
- 8.6 src/Form/QuizQuestionAnsweringForm.php \Drupal\quiz\Form\QuizQuestionAnsweringForm::submitForm()
- 8.5 src/Form/QuizQuestionAnsweringForm.php \Drupal\quiz\Form\QuizQuestionAnsweringForm::submitForm()
Submit handler for the question answering form.
There is no validation code here, but there may be feedback code for correct feedback.
Overrides FormInterface::submitForm
File
- src/
Form/ QuizQuestionAnsweringForm.php, line 198
Class
- QuizQuestionAnsweringForm
- The form used to deliver questions to users and capture their responses.
Namespace
Drupal\quiz\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('quiz.settings');
/* @var $quiz_session \Drupal\quiz\Services\QuizSessionInterface */
$quiz_session = \Drupal::service('quiz.session');
/* @var $quiz_result QuizResult */
$quiz_result = $form_state
->getBuildInfo()['args'][1];
$feedback_count = 0;
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$time_reached = $quiz_result
->isTimeReached();
$layout = $quiz_result
->getLayout();
if ($time_reached) {
// Too late.
// @todo move to quiz_question_answering_form_validate(), and then put all
// the "quiz end" logic in a sharable place. We just need to not fire the
// logic that saves all the users answers.
\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()])) {
// User submitted a response to this question.
$qqid = $qra
->get('question_id')
->getString();
$qra
->set('answer_timestamp', \Drupal::time()
->getRequestTime());
// Get the unscaled points awarded from the question response
// implementation and then apply the weighted ratio in this quiz. For
// example a MCQ question itself may be worth 4 points but worth 10
// points in this quiz. A score of 2 would mean 5 points being
// recorded.
$qra
->set('points_awarded', $qra
->score($form_state
->getValues()['question'][$qqid]) * $qra
->getWeightedRatio());
// Mark question as not skipped, in case it was skipped previously.
$qra
->set('is_skipped', FALSE);
// Mark as doubtful.
$qra
->set('is_doubtful', !empty($form_state
->getValues()['question'][$qqid]['is_doubtful']) ? 1 : 0);
// Mark correctness.
$qra
->set('is_correct', $qra
->isCorrect());
$qra
->save();
// Does this question type have feedback? We need to track it across pages.
$feedback_count += $qra
->getQuizQuestion()
->hasFeedback();
// Increment the question position.
$quiz_result
->setQuestion($qra
->get('number')
->getString() + 1);
}
}
}
// Wat do?
$next_number = $quiz_session
->getCurrentQuestion($quiz);
if ($time_reached || !isset($layout[$next_number])) {
// If this is the last question, finalize the quiz.
$this
->submitFinalize($form, $form_state);
}
else {
// No question feedback. Go to next question.
$form_state
->setRedirect('quiz.question.take', [
'quiz' => $quiz
->id(),
'question_number' => $next_number,
]);
}
$review_options = $quiz
->get('review_options')
->get(0);
if ($review_options && !empty($review_options
->getValue()['question']) && array_filter($review_options
->getValue()['question']) && $feedback_count) {
// This page contains questions that can provide feedback, and question
// feedback is enabled on the quiz.
$form_state
->setRedirect('quiz.question.feedback', [
'quiz' => $quiz
->id(),
'question_number' => $next_number - 1,
]);
// For ajax_quiz.
$form_state
->set('feedback', TRUE);
}
}