You are here

function QuizQuestionsForm::_quiz_add_questions_to_form in Quiz 8.5

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

Adds the questions in the $questions array to the form.

@todo Not bringing in revision data yet.

Parameters

array $form: FAPI form(array).

Drupal\Quiz\Entity\QuizQuestionRelationship[] $questions: The questions to be added to the question list(array).

Quiz $quiz: The quiz.

$question_types: array of all available question types.

1 call to QuizQuestionsForm::_quiz_add_questions_to_form()
QuizQuestionsForm::buildForm in src/Form/QuizQuestionsForm.php
Handles "manage questions" tab.

File

src/Form/QuizQuestionsForm.php, line 320

Class

QuizQuestionsForm
Form to manage questions in a quiz.

Namespace

Drupal\quiz\Form

Code

function _quiz_add_questions_to_form(&$form, &$questions, &$quiz, &$question_types) {
  foreach ($questions as $id => $question_relationship) {
    $question_vid = $question_relationship
      ->get('question_vid')
      ->getString();

    /* @var $quiz Quiz */
    $quiz_question = Drupal::entityTypeManager()
      ->getStorage('quiz_question')
      ->loadRevision($question_vid);
    $table =& $form['question_list'];
    $view_url = Url::fromRoute('entity.quiz_question.canonical', [
      'quiz_question' => $quiz_question
        ->id(),
    ], [
      'attributes' => [
        'class' => 'use-ajax',
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 800,
        ]),
      ],
      'query' => \Drupal::destination()
        ->getAsArray(),
    ]);
    $edit_url = Url::fromRoute('entity.quiz_question.edit_form', [
      'quiz_question' => $quiz_question
        ->id(),
    ], [
      'attributes' => [
        'class' => 'use-ajax',
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 800,
        ]),
      ],
      'query' => \Drupal::destination()
        ->getAsArray(),
    ]);
    $remove_url = Url::fromRoute('entity.quiz_question_relationship.delete_form', [
      'quiz_question_relationship' => $question_relationship
        ->id(),
    ], [
      'attributes' => [
        'class' => 'use-ajax',
        'data-dialog-type' => 'modal',
      ],
      'query' => \Drupal::destination()
        ->getAsArray(),
    ]);
    if ($quiz_question
      ->access('view')) {
      $question_titles = [
        '#markup' => Link::fromTextAndUrl($quiz_question
          ->get('title')
          ->getString(), $view_url)
          ->toString(),
      ];
    }
    else {
      $question_titles = [
        '#plain_text' => $quiz_question
          ->get('title')
          ->getString(),
      ];
    }
    $table[$id]['#attributes']['class'][] = 'draggable';
    if ($quiz_question
      ->bundle() != 'page') {
      $table[$id]['#attributes']['class'][] = 'tabledrag-leaf';
    }
    $table[$id]['title'] = $question_titles;
    if ($question_relationship
      ->get('qqr_pid')
      ->getString()) {
      $indentation = [
        '#theme' => 'indentation',
        '#size' => 1,
      ];
      $table[$id]['title']['#prefix'] = render($indentation);
    }
    $table[$id]['type'] = array(
      '#markup' => $quiz_question
        ->bundle(),
    );

    // Toggle the max score input based on the auto max score checkbox
    // Hide for ungraded questions (directions, pages, etc.)
    $table[$id]['max_score'] = array(
      '#type' => $quiz_question
        ->isGraded() ? 'textfield' : 'hidden',
      '#size' => 2,
      '#disabled' => (bool) $question_relationship
        ->get('auto_update_max_score')
        ->getString(),
      '#default_value' => $question_relationship
        ->get('max_score')
        ->getString(),
      '#states' => array(
        'disabled' => array(
          "#edit-question-list-{$id}-auto-update-max-score" => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $table[$id]['auto_update_max_score'] = array(
      '#type' => $quiz_question
        ->isGraded() ? 'checkbox' : 'hidden',
      '#default_value' => $question_relationship
        ->get('auto_update_max_score')
        ->getString() ? $question_relationship
        ->get('auto_update_max_score')
        ->getString() : 0,
    );

    // Add checkboxes to mark compulsory questions for randomized quizzes.
    if ($quiz
      ->get('randomization')
      ->getString() == 2) {
      $table[$id]['question_status'] = array(
        '#type' => 'checkbox',
        '#default_value' => $question_relationship
          ->get('question_status')
          ->getString(),
      );
    }
    $entity_manager = Drupal::entityTypeManager();
    $access_handler = $entity_manager
      ->getAccessControlHandler('quiz_question');

    // Add a checkbox to update to the latest revision of the question.
    $latest_quiz_question = Drupal::entityTypeManager()
      ->getStorage('quiz_question')
      ->load($quiz_question
      ->id());
    if ($question_relationship
      ->get('question_vid')->value == $latest_quiz_question
      ->getRevisionId()) {
      $update_cell = array(
        '#markup' => t('<em>Up to date</em>'),
      );
    }
    else {
      $revisions_url = Url::fromRoute('entity.quiz_question.edit_form', [
        'quiz_question' => $quiz_question
          ->id(),
      ]);
      $update_cell = array(
        '#type' => 'checkbox',
        '#return_value' => $latest_quiz_question
          ->getRevisionId(),
        '#title' => t('Update to latest'),
      );
    }
    $table[$id]['question_vid'] = $update_cell;
    $update_question = $access_handler
      ->access($quiz_question, 'update');
    $table[$id]['operations'] = array(
      '#type' => 'operations',
      '#links' => [
        [
          'title' => t('Edit'),
          'url' => $edit_url,
        ],
        [
          'title' => t('Remove'),
          'url' => $remove_url,
        ],
      ],
    );
    $table[$id]['#weight'] = (int) $question_relationship
      ->get('weight')
      ->getString();
    $table[$id]['weight'] = array(
      '#title_display' => 'invisible',
      '#title' => $this
        ->t('Weight for ID @id', [
        '@id' => $id,
      ]),
      '#type' => 'number',
      '#default_value' => (int) $question_relationship
        ->get('weight')
        ->getString(),
      '#attributes' => [
        'class' => [
          'table-sort-weight',
        ],
      ],
    );
    $table[$id]['parent']['qqr_id'] = array(
      '#title' => t('Relationship ID'),
      '#type' => 'hidden',
      '#default_value' => $question_relationship
        ->get('qqr_id')
        ->getString(),
      '#attributes' => [
        'class' => [
          'qqr-id',
        ],
      ],
      '#parents' => [
        'question_list',
        $id,
        'qqr_id',
      ],
    );
    $table[$id]['parent']['qqr_pid'] = array(
      '#title' => t('Parent ID'),
      '#title_display' => 'invisible',
      '#type' => 'number',
      '#size' => 3,
      '#min' => 0,
      '#default_value' => $question_relationship
        ->get('qqr_pid')
        ->getString(),
      '#attributes' => [
        'class' => [
          'qqr-pid',
        ],
      ],
      '#parents' => [
        'question_list',
        $id,
        'qqr_pid',
      ],
    );
  }
}