You are here

function quiz_questions_form in Quiz 8.4

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_questions_form()
  2. 5 quiz.module \quiz_questions_form()
  3. 6.6 quiz.admin.inc \quiz_questions_form()
  4. 6.2 quiz.admin.inc \quiz_questions_form()
  5. 6.3 quiz.admin.inc \quiz_questions_form()
  6. 6.4 quiz.admin.inc \quiz_questions_form()
  7. 6.5 quiz.admin.inc \quiz_questions_form()
  8. 7.6 quiz.admin.inc \quiz_questions_form()
  9. 7 quiz.admin.inc \quiz_questions_form()
  10. 7.4 quiz.admin.inc \quiz_questions_form()
  11. 7.5 quiz.admin.inc \quiz_questions_form()

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

Parameters

$form_state: The form state variable

$quiz: The quiz node.

Return value

HTML output to create page.

2 string references to 'quiz_questions_form'
QuizController::quizQuestions in lib/Drupal/quiz/Controller/QuizController.php
quiz_questions in ./quiz.admin.inc
Creates a form for quiz questions.

File

./quiz.admin.inc, line 519
Administrator interface for Quiz module.

Code

function quiz_questions_form($form, $form_state, $quiz) {
  if ($form_state['rebuild']) {

    // Save the active filters in $_SESSION
    $filters = $form_state['values']['browser']['table']['header']['filters'];
    _quiz_questions_store_filters($filters);
  }
  $types = _quiz_get_question_types();
  _quiz_add_fields_for_creating_questions($form, $types, $quiz);

  // Display questions in this quiz.
  $form['question_list'] = array(
    '#type' => 'fieldset',
    '#title' => t('Questions in this quiz'),
    '#theme' => 'question_selection_table',
    '#collapsible' => TRUE,
    '#attributes' => array(
      'id' => 'mq-fieldset',
    ),
    'question_status' => array(
      '#tree' => TRUE,
    ),
  );
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'quiz') . '/templates/quiz_question_browser.js',
  );

  // Add randomization settings if this quiz allows randomized questions
  _quiz_add_fields_for_random_quiz($form, $quiz);

  // Build up a list of questions
  $questions_to_add = array();

  // We use $form_state[post] to avoid validation failures when questions are added using AJAX
  if (isset($form_state['post']['weights'])) {
    $questions = _quiz_get_questions_from_form_state($form_state, $questions_to_add);
  }
  else {

    // We are coming in fresh and fetches the questions currently on the quiz from the database...
    $include_random = $quiz->randomization == 2;
    $questions = quiz_get_questions($quiz
      ->id(), $quiz
      ->getRevisionId(), TRUE, FALSE, FALSE, $include_random);
  }
  if (empty($questions)) {
    $form['question_list']['no_questions'] = array(
      '#markup' => '<div id = "no-questions">' . 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.') . '</div>',
    );
  }

  // We add the browser and allows the browser to give us information on what questions are displayed in the browser...
  $hidden_questions = array();
  $form['question_list']['browser'] = _quiz_question_browser_form($hidden_questions, $questions_to_add, $form_state, $quiz, $types);

  // We add the questions from the browser as hidden question rows in the question list. Doing this we can have
  // the question show up in the question list instantly when a question is chosen in the browser(using js).
  _quiz_add_hidden_questions($questions, $hidden_questions, $form_state, $quiz);

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

  // Show the number of questions in the table header.
  $always_count = 0;
  foreach ($form['question_list']['stayers'] as $stayer) {
    if ($stayer['#default_value'] === 1) {
      $always_count++;
    }
  }
  $form['question_list']['#title'] .= ' (' . $always_count . ')';

  // Give the user the option to create a new revision of the quiz
  _quiz_add_revision_checkbox($form, $quiz);

  // Timestamp is needed to avoid multiple users editing the same quiz at the same time.
  $form['timestamp'] = array(
    '#type' => 'hidden',
    '#default_value' => REQUEST_TIME,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#submit' => array(
      'quiz_questions_form_submit',
    ),
  );
  return $form;
}