QuizResultEntityForm.php in Quiz 6.x
File
src/Form/QuizResultEntityForm.php
View source
<?php
namespace Drupal\quiz\Form;
use Drupal;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizResult;
use Drupal\quiz\Util\QuizUtil;
use function _quiz_get_quiz_name;
class QuizResultEntityForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$quiz_result = $this->entity;
if ($quiz_result
->isNew()) {
$quiz = $quiz_result
->getQuiz();
if ($quiz_result
->findOldResult()) {
$form['build_on_last'] = [
'#title' => t('Keep answers from last attempt?'),
'#type' => 'radios',
'#options' => [
'fresh' => t('No answers'),
'correct' => t('Only correct answers'),
'all' => t('All answers'),
],
'#default_value' => $quiz
->get('build_on_last')
->getString(),
'#description' => t('You can choose to keep previous answers or start a new attempt.'),
'#access' => $quiz
->get('build_on_last')
->getString() != 'fresh',
];
}
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#value'] = t('Start @quiz', [
'@quiz' => QuizUtil::getQuizName(),
]);
}
else {
$form['question']['#tree'] = TRUE;
$render_controller = Drupal::entityTypeManager()
->getViewBuilder('quiz_result_answer');
foreach ($quiz_result
->getLayout() as $layoutIdx => $qra) {
$form['question'][$layoutIdx]['feedback'] = $render_controller
->view($qra);
$form['question'][$layoutIdx] += $qra
->getReportForm();
}
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#value'] = t('Save score');
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$quiz_result = $this->entity;
if ($quiz_result
->isNew()) {
$quiz_result->build_on_last = $form_state
->getValue('build_on_last');
}
else {
$layout = $this->entity
->getLayout();
foreach ($form_state
->getValue('question') as $layoutIdx => $question) {
$qra = $layout[$layoutIdx];
$qra
->set('points_awarded', $question['score']);
$qra
->set('answer_feedback', $question['answer_feedback']);
$qra
->setEvaluated();
$qra
->save();
}
$quiz_result
->finalize();
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($quiz_result
->get('vid')
->getString());
$results_got_deleted = $quiz_result
->maintainResults();
$add = '';
if ($quiz
->get('keep_results')
->getString() == Quiz::KEEP_BEST && $results_got_deleted) {
$add = t('Note that this @quiz is set to only keep each users best answer.', [
'@quiz' => QuizUtil::getQuizName(),
]);
}
\Drupal::messenger()
->addMessage(t('The scoring data you provided has been saved.') . $add);
}
parent::submitForm($form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$new = $this->entity
->isNew();
parent::save($form, $form_state);
if ($new) {
$quiz_session = \Drupal::service('quiz.session');
$quiz_session
->startQuiz($this->entity);
}
}
}