You are here

function _math_captcha_build_captcha in CAPTCHA Pack 8

Same name and namespace in other branches
  1. 5 math_captcha/math_captcha.module \_math_captcha_build_captcha()
  2. 6 math_captcha/math_captcha.challenge.inc \_math_captcha_build_captcha()
  3. 7 math_captcha/math_captcha.challenge.inc \_math_captcha_build_captcha()

Helper function to build a math CAPTCHA form item.

3 calls to _math_captcha_build_captcha()
_math_captcha_addition_challenge in math_captcha/math_captcha.challenge.inc
Function for addition challenges.
_math_captcha_multiplication_challenge in math_captcha/math_captcha.challenge.inc
Function for multiplication challenges.
_math_captcha_subtraction_challenge in math_captcha/math_captcha.challenge.inc
Function for subtraction challenges.

File

math_captcha/math_captcha.challenge.inc, line 66
Provides challenge functions for MATH CAPTCHA administration.

Code

function _math_captcha_build_captcha($x, $y, $operator, $result, $maxlength = 3) {
  $form_item = [];
  $form_item['form']['captcha_response'] = [
    '#type' => 'textfield',
    '#title' => t('Math question'),
    '#required' => TRUE,
    '#size' => $maxlength + 2,
    '#maxlength' => $maxlength,
    '#description' => t('Solve this math question and enter the solution with digits. E.g. for "two plus four = ?" enter "6".'),
  ];
  switch (mt_rand(0, 2)) {

    // Question like "x + y = ?".
    case 0:
      $form_item['solution'] = "{$result}";
      $form_item['form']['captcha_response']['#field_prefix'] = _math_captcha_repr($x, TRUE) . ' ' . _math_captcha_repr_op($operator) . ' ' . _math_captcha_repr($y, TRUE) . ' ' . _math_captcha_repr_op('=');
      break;

    // Question like "x + ? = z".
    case 1:
      $form_item['solution'] = "{$y}";
      $form_item['form']['captcha_response']['#field_prefix'] = _math_captcha_repr($x, TRUE) . ' ' . _math_captcha_repr_op($operator) . ' ';
      $form_item['form']['captcha_response']['#field_suffix'] = ' ' . _math_captcha_repr_op('=') . ' ' . _math_captcha_repr($result);
      break;

    // Question like "? + y = z".
    case 2:
      $form_item['solution'] = "{$x}";
      $form_item['form']['captcha_response']['#field_suffix'] = ' ' . _math_captcha_repr_op($operator) . ' ' . _math_captcha_repr($y, TRUE) . ' ' . _math_captcha_repr_op('=') . ' ' . _math_captcha_repr($result);
      break;
  }
  return $form_item;
}