You are here

public function QuizResultEntityForm::buildForm in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 src/Entity/QuizResultEntityForm.php \Drupal\quiz\Entity\QuizResultEntityForm::buildForm()

Add the questions in this result to the edit form.

Overrides EntityForm::buildForm

File

src/Entity/QuizResultEntityForm.php, line 18

Class

QuizResultEntityForm

Namespace

Drupal\quiz\Entity

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /* @var $quiz_result QuizResult */
  $quiz_result = $this->entity;
  if ($quiz_result
    ->isNew()) {
    $quiz = $quiz_result
      ->getQuiz();
    if ($quiz_result
      ->findOldResult()) {
      $form['build_on_last'] = array(
        '#title' => t('Keep answers from last attempt?'),
        '#type' => 'radios',
        '#options' => array(
          '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', array(
      '@quiz' => _quiz_get_quiz_name(),
    ));
  }
  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;
}