You are here

function phrase_captcha_captcha in CAPTCHA Pack 6

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. 7 text_captcha/phrase_captcha/phrase_captcha.module \phrase_captcha_captcha()

Implementation of hook_captcha

File

text_captcha/phrase_captcha/phrase_captcha.module, line 45

Code

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

        // generate words
        $words = _phrase_captcha_generate_words((int) 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((int) 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' => '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,
        );

        // additional text CAPTCHA CSS rules
        drupal_add_css(drupal_get_path('module', 'phrase_captcha') . '/../text_captcha.css');
        return $captcha;
      }
  }
}