You are here

public function QuizEntityForm::buildForm in Quiz 8.6

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

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Entity/QuizEntityForm.php, line 22

Class

QuizEntityForm

Namespace

Drupal\quiz\Entity

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $form['quiz'] = [
    '#weight' => 5,
    '#type' => 'vertical_tabs',
  ];
  $form['taking_options'] = array(
    '#type' => 'details',
    '#title' => t('Taking options'),
    '#group' => 'quiz',
  );
  $form['random'] = array(
    '#type' => 'details',
    '#title' => t('Randomization'),
    '#group' => 'quiz',
  );
  $form['availability_options'] = array(
    '#type' => 'details',
    '#title' => t('Availability options'),
    '#group' => 'quiz',
  );
  $form['pass_fail_options'] = array(
    '#type' => 'details',
    '#title' => t('Pass/fail options'),
    '#group' => 'quiz',
  );
  $form['question_feedback'] = array(
    '#type' => 'details',
    '#title' => t('Question feedback'),
    '#group' => 'quiz',
  );
  $form['quiz_feedback'] = array(
    '#type' => 'details',
    '#title' => t('Quiz feedback'),
    '#group' => 'quiz',
  );
  $form['allow_resume']['#group'] = 'taking_options';
  $form['allow_skipping']['#group'] = 'taking_options';
  $form['allow_jumping']['#group'] = 'taking_options';
  $form['allow_change']['#group'] = 'taking_options';
  $form['allow_change_blank']['#group'] = 'taking_options';
  $form['backwards_navigation']['#group'] = 'taking_options';
  $form['repeat_until_correct']['#group'] = 'taking_options';
  $form['build_on_last']['#group'] = 'taking_options';
  $form['mark_doubtful']['#group'] = 'taking_options';
  $form['show_passed']['#group'] = 'taking_options';
  $form['takes']['#group'] = 'availability_options';
  $form['time_limit']['#group'] = 'availability_options';
  $form['show_attempt_stats']['#group'] = 'taking_options';

  //$form['review_options']['#group'] = 'taking_options';
  $form['keep_results']['#group'] = 'taking_options';
  $form['build_on_last']['#group'] = 'taking_options';
  $form['randomization']['#group'] = 'random';
  $form['number_of_random_questions']['#group'] = 'random';
  $form['max_score_for_random']['#group'] = 'random';
  $form['quiz_terms']['#group'] = 'random';

  //$form['quiz_always']['#group'] = 'availability_options';
  $form['quiz_date']['#group'] = 'availability_options';
  $form['pass_rate']['#group'] = 'pass_fail_options';
  $form['summary_pass']['#group'] = 'pass_fail_options';
  $form['summary_default']['#group'] = 'pass_fail_options';
  $form['result_options']['#group'] = 'quiz_feedback';

  // Build the review options.
  $review_options = quiz_get_feedback_options();
  $form['question_feedback']['help']['#markup'] = t('Control what feedback appears and when. To display any per-question feedback, one of the "Question" review options must be enabled.');
  $form['question_feedback']['review_options']['#tree'] = TRUE;
  foreach (QuizFeedbackType::loadMultiple() as $key => $when) {
    $form['question_feedback']['review_options'][$key] = array(
      '#title' => $when
        ->label(),
      '#description' => $when
        ->get('description'),
      '#type' => 'checkboxes',
      '#options' => $review_options,
      '#weight' => 100,
      '#default_value' => $this
        ->getEntity()
        ->get('review_options')
        ->isEmpty() ? [] : $this
        ->getEntity()
        ->get('review_options')
        ->get(0)
        ->getValue()[$key] ?? [],
    );
  }
  if ($this->entity
    ->hasAttempts()) {
    $override = \Drupal::currentUser()
      ->hasPermission('override quiz revisioning');
    if (Drupal::config('quiz.settings')
      ->get('revisioning', FALSE)) {
      $form['revision']['#required'] = !$override;
    }
    else {
      $message = $override ? t('<strong>Warning:</strong> This quiz has attempts. You can edit this quiz, but it is not recommended.<br/>Attempts in progress and reporting will be affected.<br/>You should delete all attempts on this quiz before editing.') : t('You must delete all attempts on this quiz before editing.');

      // Revisioning is disabled.
      $form['revision_information']['#access'] = FALSE;
      $form['revision']['#access'] = FALSE;
      $form['actions']['warning'] = [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => $message,
      ];
      \Drupal::messenger()
        ->addWarning($message);
      $form['actions']['#disabled'] = TRUE;
    }
    $form['revision']['#description'] = '<strong>Warning:</strong> This quiz has attempts.<br/>In order to update this quiz you must create a new revision.<br/>This will affect reporting.<br/>This will only affect new attempts.';
  }
  return $form;
}