You are here

function _math_captcha_build_captcha in CAPTCHA Pack 5

Same name and namespace in other branches
  1. 8 math_captcha/math_captcha.challenge.inc \_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.module
function for addition challenges
_math_captcha_multiplication_challenge in math_captcha/math_captcha.module
function for multiplication challenges
_math_captcha_subtraction_challenge in math_captcha/math_captcha.module
function for subtraction challenges

File

math_captcha/math_captcha.module, line 191

Code

function _math_captcha_build_captcha($x, $y, $operator, $result, $maxlength = 3) {
  $form_item = array();
  $form_item['form']['captcha_response'] = array(
    '#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)) {
    case 0:

      // question like "x + y = ?"
      $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;
    case 1:

      // question like "x + ? = z"
      $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;
    case 2:

      // question like "? + y = z"
      $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;
}