You are here

public function QuizQuestionsForm::buildForm in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 src/Form/QuizQuestionsForm.php \Drupal\quiz\Form\QuizQuestionsForm::buildForm()
  2. 8.5 src/Form/QuizQuestionsForm.php \Drupal\quiz\Form\QuizQuestionsForm::buildForm()

Handles "manage questions" tab.

Displays form which allows questions to be assigned to the given quiz.

This function is not used if the question assignment type "categorized random questions" is chosen.

Overrides FormInterface::buildForm

File

src/Form/QuizQuestionsForm.php, line 92

Class

QuizQuestionsForm
Form to manage questions in a quiz.

Namespace

Drupal\quiz\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $types = quiz_get_question_types();
  $quiz = $form_state
    ->getBuildInfo()['args'][0];
  $this
    ->_quiz_add_fields_for_creating_questions($form, $types, $quiz);
  $header = [
    'Question',
    'Type',
    'Max score',
    'Auto max score',
  ];
  if ($quiz
    ->get('randomization')
    ->getString() == 2) {
    $header[] = 'Required';
  }
  $header = array_merge($header, [
    'Revision',
    'Operations',
    'Weight',
    'Parent',
  ]);

  // Display questions in this quiz.
  $form['question_list'] = [
    '#type' => 'table',
    '#title' => t('Questions in this @quiz', [
      '@quiz' => QuizUtil::getQuizName(),
    ]),
    '#type' => 'table',
    '#header' => $header,
    '#empty' => t('There are currently no questions in this @quiz. Assign existing questions by using the question browser below. You can also use the links above to create new questions.', [
      '@quiz' => QuizUtil::getQuizName(),
    ]),
    '#tabledrag' => [
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'qqr-pid',
        'source' => 'qqr-id',
        'hidden' => TRUE,
        'limit' => 1,
      ],
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'table-sort-weight',
      ],
    ],
  ];

  // @todo deal with $include_random.
  $all_questions = $quiz
    ->getQuestions();
  uasort($all_questions, [
    'self',
    'sortQuestions',
  ]);
  $questions = [];
  foreach ($all_questions as $qqr_id => $question) {
    if (!$question
      ->get('qqr_pid')
      ->getString()) {

      // This is a parent question.
      $questions[$qqr_id] = $question;
      $questions += $this
        ->getSubQuestions($question, $all_questions);
    }
  }

  // We add the questions to the form array.
  $this
    ->_quiz_add_questions_to_form($form, $questions, $quiz, $types);

  // @todo Show the number of questions in the table header.
  $always_count = isset($form['question_list']['titles']) ? count($form['question_list']['titles']) : 0;

  //$form['question_list']['#title'] .= ' (' . $always_count . ')';

  // Timestamp is needed to avoid multiple users editing the same quiz at the
  // same time.
  $form['timestamp'] = [
    '#type' => 'hidden',
    '#default_value' => \Drupal::time()
      ->getRequestTime(),
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('Submit'),
  ];

  // Give the user the option to create a new revision of the quiz.
  $this
    ->_quiz_add_revision_checkbox($form, $quiz);
  return $form;
}