You are here

quiz_multichoice.module in Quiz 6.x

File

question_types/quiz_multichoice/quiz_multichoice.module
View source
<?php

use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\quiz\Entity\QuizQuestionType;

/**
 * @file
 * Contains quiz_multichoice.module
 */

/**
 * Implements hook_help().
 */
function quiz_multichoice_help($path, $args) {
  switch ($path) {
    case 'help.page.quiz_multichoice':
      return t("\n      <p>This module provides a multiple choice question type for Quiz.</p>\n\n      <p>The module has three settings.\n      <em>Multiple answers</em> allows the quiz taker to select more than one alternative\n      (it also allows for the possibility that none of the alternatives are correct).\n      Alternatives are selected using checkboxes instead of radio buttons.\n      <em>Random order</em> displays the alternatives in random order when quiz is beeing taken.\n      <em>Simple scoring</em> gives max score if everything is correct. Zero points otherwise.</p>\n\n      <p>The scoring system in multichoice is a bit complex. With multiple answers each alternative adds a given number of points to\n      the total score if it is chosen, and another number of points is added if the alternative isn't chosen. Both <em>score if chosen</em> and\n      <em>score if not chosen</em> may be edited for each alternative by the question creator.\n      If multiple answers isn't allowed the score will be set to the <em>score if chosen</em> of the alternative that has been chosen.\n      The question is considered correct if the quiz taker gets the maximum amount of points possible for the question.</p>\n    ");
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add JS to the multichoice form to help correctness and point configuration.
 */
function quiz_multichoice_form_quiz_question_multichoice_form_alter(&$form, FormStateInterface $form_state) {
  $form['#attached']['library'] = [
    'quiz_multichoice/helper',
  ];
  $form['#attached']['drupalSettings']['quiz_multichoice']['scoring'] = Drupal::config('quiz_multichoice.settings')
    ->get('scoring');
  foreach (Element::children($form['alternatives']['widget']) as $key) {
    if (is_numeric($key)) {
      $form['alternatives']['widget'][$key]['subform']['multichoice_correct']['widget']['value']['#attributes']['data-multichoice-delta'] = $key;
      $form['alternatives']['widget'][$key]['subform']['multichoice_correct']['widget']['value']['#attributes']['class'][] = 'quiz-multichoice-correct-checkbox';
    }
  }
}

/**
 * Implements hook_form_alter().
 *
 * Add multiple choice defaults to the bundle form.
 */
function quiz_multichoice_form_quiz_question_type_edit_form_alter(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getFormObject() instanceof BundleEntityFormBase) {
    if ($form_state
      ->getFormObject()
      ->getEntity()
      ->id() == 'multichoice') {
      $config = Drupal::config('quiz_multichoice.settings');
      $form['scoring'] = [
        '#type' => 'radios',
        '#title' => t('Default scoring method'),
        '#description' => t('Choose the default scoring method for questions with multiple correct answers.'),
        '#options' => [
          0 => t('Give minus one point for incorrect answers'),
          1 => t("Give one point for each incorrect option that haven't been chosen"),
        ],
        '#default_value' => $config
          ->get('scoring'),
      ];
    }
  }
}

/**
 * Implements hook_entity_update().
 *
 * Set configuration.
 */
function quiz_multichoice_entity_update(EntityInterface $entity) {
  if (!$entity instanceof QuizQuestionType) {
    return;
  }
  if ($entity
    ->id() == 'multichoice') {
    $config = Drupal::configFactory()
      ->getEditable('quiz_multichoice.settings');
    $config
      ->set('scoring', $entity->scoring);
    $config
      ->save();
  }
}