You are here

function phrase_captcha_captcha in CAPTCHA Pack 7

Same name and namespace in other branches
  1. 8 text_captcha/modules/phrase_captcha/phrase_captcha.module \phrase_captcha_captcha()
  2. 5 text_captcha/phrase_captcha/phrase_captcha.module \phrase_captcha_captcha()
  3. 6 text_captcha/phrase_captcha/phrase_captcha.module \phrase_captcha_captcha()

Implements hook_captcha().

File

text_captcha/phrase_captcha/phrase_captcha.module, line 43

Code

function phrase_captcha_captcha($op, $captcha_type = '') {
  switch ($op) {
    case 'list':
      return array(
        'Phrase CAPTCHA',
      );
    case 'generate':
      if ($captcha_type == 'Phrase CAPTCHA') {

        // Generate words
        $words = _phrase_captcha_generate_words(variable_get('phrase_captcha_word_quantity', 5));

        // Pick a random word selection challenge
        $word_challenges = _phrase_captcha_enabled_word_challenges();
        $key = array_rand($word_challenges);
        $function = $word_challenges[$key];
        list($phrase_words, $question, $solution) = call_user_func($function, $words);

        // Build options list
        $all_words = array_merge($words, _phrase_captcha_generate_words(variable_get('phrase_captcha_additional_word_quantity', 1)));
        shuffle($all_words);
        $options = array();
        foreach ($all_words as $word) {
          $options[$word] = $word;
        }

        // Store the answer and build the form elements
        $captcha = array();
        $captcha['solution'] = $solution;
        $captcha['form']['captcha_phrase'] = array(
          '#type' => 'markup',
          '#value' => '"' . implode(' ', $phrase_words) . '"',
          '#weight' => -2,
        );
        $captcha['form']['captcha_response'] = array(
          '#type' => 'radios',
          '#title' => $question,
          '#options' => $options,
          // Extra class needed for additional CSS'ing of the options
          '#attributes' => array(
            'class' => array(
              'text-captcha-word-list-radios',
            ),
          ),
          // The following entry '#DANGEROUS_SKIP_CHECK' is needed to prevent
          // that Drupal checks during validation phase if a submitted option
          // is in the list of possible options. (see includes/form.inc)
          // The options are randomly generated on each call and consequently
          // almost never the same during the generate phase and the validation
          // phase.
          '#DANGEROUS_SKIP_CHECK' => TRUE,
          //
          '#required' => TRUE,
        );

        // Add css to form
        $captcha['#attached']['css'] = array(
          drupal_get_path('module', 'phrase_captcha') . '/../text_captcha.css',
        );
        return $captcha;
      }
  }
}